//===========================================================================
//  @(#) $Name: arts++-1-1-a8 $
//  @(#) $Id: ArtsPortMatrixAggregator.hh,v 1.7 2000/10/02 17:05:18 dwm Exp $
//===========================================================================
//  Copyright Notice
//
//  By accessing this software, arts++, you are duly informed
//  of and agree to be bound by the conditions described below in this
//  notice:
//
//  This software product, arts++, is developed by Daniel W. McRobb, and
//  copyrighted(C) 1998 by the University of California, San Diego
//  (UCSD), with all rights reserved.  UCSD administers the CAIDA grant,
//  NCR-9711092, under which part of this code was developed.
//
//  There is no charge for arts++ software.  You can redistribute it
//  and/or modify it under the terms of the GNU Lesser General Public
//  License, Version 2.1, February 1999, which is incorporated by
//  reference herein.
//
//  arts++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF
//  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use
//  of it will not infringe on any third party's intellectual
//  property rights.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with arts++.  Copies can also be obtained from:
//
//    http://www.gnu.org/copyleft/lesser.html
//
//  or by writing to:
//
//  Free Software Foundation, Inc.
//  59 Temple Place, Suite 330
//  Boston, MA 02111-1307
//  USA
//
//  Or contact:
//
//    info@caida.org
//===========================================================================

#ifndef _ARTSPORTMATRIXAGGREGATOR_HH_
#define _ARTSPORTMATRIXAGGREGATOR_HH_

#include <map>
#include <vector>

#include "ArtsPortMatrix.hh"
#include "ArtsSelectedPortTable.hh"

//----------------------------------------------------------------------------
//     class ArtsPortMatrixKeyValue 
//----------------------------------------------------------------------------
//!  This class is used as a key in a map of counters used by the
//!  ArtsPortMatrixAggregator class.
//----------------------------------------------------------------------------
class ArtsPortMatrixKeyValue
{
public:
  //--------------------------------------------------------------------------
  //       bool operator < (const ArtsPortMatrixKeyValue keyValue) const 
  //..........................................................................
  //!  less-than operator.  This is used by the ArtsPortMatrixAggregator
  //!  class for comparison of keys in an STL map of counters.  It
  //!  determines ordering of elements in the map.
  //--------------------------------------------------------------------------
  bool operator < (const ArtsPortMatrixKeyValue keyValue) const;

  //--------------------------------------------------------------------------
  //       bool operator == (const ArtsPortMatrixKeyValue keyValue) const 
  //..........................................................................
  //!  Returns true if *this is equal to keyValue.
  //--------------------------------------------------------------------------
  bool operator == (const ArtsPortMatrixKeyValue keyValue) const;
  
  //--------------------------------------------------------------------------
  //!  The source port number.
  //--------------------------------------------------------------------------
  uint16_t  srcPort;
  
  //--------------------------------------------------------------------------
  //!  The destination port number.
  //--------------------------------------------------------------------------
  uint16_t  dstPort;
};

//---------------------------------------------------------------------------
//  class ArtsPortMatrixAggregator
//---------------------------------------------------------------------------
//!  This class is used to aggregate port matrix objects in the time
//!  domain.
//---------------------------------------------------------------------------
class ArtsPortMatrixAggregator
{
public:
  //--------------------------------------------------------------------------
  //!  Holds a packet and byte counter.
  //--------------------------------------------------------------------------
  typedef struct {
    uint64_t  Pkts;
    uint64_t  Bytes;
  } counter_t;

  //-------------------------------------------------------------------------
  //               ArtsPortMatrixAggregator(const Arts & arts)
  //.........................................................................
  //!  construct from an Arts object.
  //-------------------------------------------------------------------------
  ArtsPortMatrixAggregator(const Arts & arts);
  
  //-------------------------------------------------------------------------
  //                       void Add(const Arts & arts)
  //.........................................................................
  //!  Add the contents of an Arts object.  The input Arts object must be
  //!  a port matrix object.  The effect here is to add any new port matrix
  //!  entries and add to the counters for existing port matrix entries.
  //-------------------------------------------------------------------------
  void Add(const Arts & arts);
  
  //-------------------------------------------------------------------------
  //              ArtsPortMatrix *ConvertToArtsPortMatrix() const
  //.........................................................................
  //!  Return an ArtsPortMatrix object (dynamically allocated) containing
  //!  the aggregate data.  The caller is responsible for calling delete()
  //!  on the returned ArtsPortMatrix pointer.
  //-------------------------------------------------------------------------
  ArtsPortMatrix *ConvertToArtsPortMatrix() const;

  //-------------------------------------------------------------------------
  //       uint16_t PickPort(const ArtsPortMatrixKeyValue & portmKey,
  //                         const ArtsPortChooser & portChooser) const
  //.........................................................................
  //!  Given an ArtsPortMatrixKeyValue and an ArtsPortChooser, return the
  //!  chosen port number.  This returns the lower of the source and
  //!  destination port numbers in ArtsPortMatrixKeyValue that qualifies
  //!  the criteria in portChooser, or 0 if neither port meets the
  //!  portChooser criteria.
  //-------------------------------------------------------------------------
  uint16_t PickPort(const ArtsPortMatrixKeyValue & portmKey,
                    const ArtsPortChooser & portChooser) const;
  
  //-------------------------------------------------------------------------
  // ArtsSelectedPortTable *                         
  // ConvertToArtsSelectedPortTable(const ArtsPortChooser & portChooser) const
  //.........................................................................
  //!  Returns an ArtsSelectedPortTable object (dynamically allocated)
  //!  containing the aggregate data.  The returned object will be a
  //!  ArtsSelectedPortTable with data organized according to portChooser.
  //!  Port matrix entries which match portChooser criteria will get their
  //!  own table entries, all others will get lumped into port 0, and
  //!  we don't duplicate data.
  //!
  //!  The caller is responsible for calling delete() on the returned
  //!  ArtsSelectedPortTable object.
  //-------------------------------------------------------------------------
  ArtsSelectedPortTable *
  ConvertToArtsSelectedPortTable(const ArtsPortChooser & portChooser) const;

  //--------------------------------------------------------------------------
  //   ArtsSelectedPortTable * 
  //   ArtsPortMatrixAggregator:: 
  //   ConvertToArtsSelectedPortTable(int numTopPorts,
  //                                  bool byPkts = false) const 
  //..........................................................................
  //  
  //--------------------------------------------------------------------------
  ArtsSelectedPortTable *
  ArtsPortMatrixAggregator::
  ConvertToArtsSelectedPortTable(int numTopPorts,
                                 bool byPkts = false) const;
  
  //-------------------------------------------------------------------------
  //                   vector<pair<uint16_t,counter_t> > *                   
  //                   TopSourcesByBytes(uint16_t numSources)
  //.........................................................................
  //!  Returns a vector of ArtsPortMatrixEntry containing the top numSources
  //!  source port numbers and their corresponding pkt/byte counters.  Note
  //!  that instead of adding to our type space, we use the
  //!  ArtsPortMatrixEntry class.  In the returned vector, the Dst()
  //!  for each ArtsPortMatrixEntry will always be 0 since we're adding up
  //!  data to every destination port for each source port.
  //!
  //!  This member is what lets us easily pick out the top N source ports
  //!  in an aggregate object, usually so we can then go back over the
  //!  time-series data and create a stacked bar chart of bits/sec vs.
  //!  time for each of the top N source ports.
  //-------------------------------------------------------------------------
  vector<ArtsPortMatrixEntry> *TopSourcesByBytes(uint16_t numDests);
  
  //-------------------------------------------------------------------------
  //            vector<ArtsPortMatrixEntry> *                       
  //            TopDestinationsByBytes(uint16_t numSources)
  //.........................................................................
  //!  Returns a vector of ArtsPortMatrixEntry containing the top numDests
  //!  destination port numbers and their corresponding pkt/byte counters.
  //!  Note that instead of adding to our type space, we use the
  //!  ArtsPortMatrixEntry class.  In the returned vector, the Src()
  //!  for each ArtsPortMatrixEntry will always be 0 since we're adding up
  //!  traffic from every source port for each destination port.
  //!
  //!  This member is what lets us easily pick out the top N destination
  //!  ports in an aggregate object, usually so we can then go back over the
  //!  time-series data and create a stacked bar chart of bits/sec vs.
  //!  time for each of the top N destination ports.
  //-------------------------------------------------------------------------
  vector<ArtsPortMatrixEntry> *TopDestinationsByBytes(uint16_t numSources);

  //-------------------------------------------------------------------------
  //                       uint64_t TotalPkts() const
  //.........................................................................
  //!  Returns the sum of all packets in the ArtsPortMatrixAggregator
  //!  object.  This is the sum of all packets for all contained port
  //!  matrix entries.
  //-------------------------------------------------------------------------
  uint64_t TotalPkts() const;

  //-------------------------------------------------------------------------
  //                       uint64_t TotalBytes() const                      
  //.........................................................................
  //!  Returns the sum of all bytes in the ArtsPortMatrixAggregator
  //!  object.  This is the sum of all bytes for all contained port
  //!  matrix entries.
  //-------------------------------------------------------------------------
  uint64_t TotalBytes() const;
  
private:
  ArtsHeader                               _header;
  vector<ArtsAttribute>                    _attributes;
  map<ArtsPortMatrixKeyValue,counter_t,less<ArtsPortMatrixKeyValue> >  _portCounters;
  uint64_t                                 _totalPkts;
  uint64_t                                 _totalBytes;
};

#endif  // _ARTSPORTMATRIXAGGREGATOR_HH_
