//===========================================================================
//  @(#) $Name: arts++-1-1-a12 $
//  @(#) $Id: ArtsInterfaceMatrixAggregator.hh,v 1.2 2004/04/21 23:51:26 kkeys 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 _ARTSINTERFACEMATRIXAGGREGATOR_HH_
#define _ARTSINTERFACEMATRIXAGGREGATOR_HH_

#include <map>
#include <vector>

#include "ArtsInterfaceMatrix.hh"

//----------------------------------------------------------------------------
//     class ArtsInterfaceMatrixKeyValue 
//----------------------------------------------------------------------------
//!  This class is used as a key in a map of counters in the
//!  ArtsInterfaceMatrixAggregator class.  It contains a source interface
//!  index (src) and a desitnation interface index (dst).
//----------------------------------------------------------------------------
class ArtsInterfaceMatrixKeyValue
{
public:
  //--------------------------------------------------------------------------
  //    bool operator < (const ArtsInterfaceMatrixKeyValue keyValue) const 
  //..........................................................................
  //!  Less-than operator.  Determines ordering of the map entries in
  //!  the ArtsInterfaceMatrixAggregator class.
  //--------------------------------------------------------------------------
  bool operator < (const ArtsInterfaceMatrixKeyValue keyValue) const;
  
  //--------------------------------------------------------------------------
  //   bool operator == (const ArtsInterfaceMatrixKeyValue keyValue) const 
  //..........................................................................
  //!  == operator.  Used for exact matching of map entries in the
  //!  ArtsInterfaceMatrixAggregator class.
  //--------------------------------------------------------------------------
  bool operator == (const ArtsInterfaceMatrixKeyValue keyValue) const;

  //--------------------------------------------------------------------------
  //!  Hods the source interface index (ifIndex).
  //--------------------------------------------------------------------------
  uint16_t  src;
  
  //--------------------------------------------------------------------------
  //!  Holds the destination interface index (ifIndex).
  //--------------------------------------------------------------------------
  uint16_t  dst;
};

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

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

  //-------------------------------------------------------------------------
  //                   std::vector<pair<uint16_t,counter_t> > *                   
  //                   TopSourcesByBytes(uint16_t numSources)
  //.........................................................................
  //!  Returns a vector of ArtsInterfaceMatrixEntry containing the top
  //!  numSources source interface numbers and their corresponding
  //!  pkt/byte counters.  Note that instead of adding to our type space,
  //!  we use the ArtsInterfaceMatrixEntry class.  In the returned
  //!  vector, the Dst() for each ArtsInterfaceMatrixEntry will always be
  //!  0 since we're adding up data to every destination interface for
  //!  each source interface.
  //!
  //!  This member is what lets us easily pick out the top N source
  //!  interfaces 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 interfaces.
  //-------------------------------------------------------------------------
  std::vector<ArtsInterfaceMatrixEntry> *TopSourcesByBytes(uint16_t numDests);
  
  //-------------------------------------------------------------------------
  //            std::vector<ArtsInterfaceMatrixEntry> *                       
  //            TopDestinationsByBytes(uint16_t numDests)
  //.........................................................................
  //!  Returns a vector of ArtsInterfaceMatrixEntry containing the top numDests
  //!  destination interface numbers and their corresponding pkt/byte counters.
  //!  Note that instead of adding to our type space, we use the
  //!  ArtsInterfaceMatrixEntry class.  In the returned vector, the Src()
  //!  for each ArtsInterfaceMatrixEntry will always be 0 since we're adding up
  //!  traffic from every source interface for each destination interface.
  //!
  //!  This member is what lets us easily pick out the top N destination
  //!  interfaces 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 interfaces.
  //-------------------------------------------------------------------------
  std::vector<ArtsInterfaceMatrixEntry> *TopDestinationsByBytes(uint16_t numDests);

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

  //-------------------------------------------------------------------------
  //                       uint64_t TotalBytes() const                      
  //.........................................................................
  //!  Returns the sum of all bytes in the ArtsInterfaceMatrixAggregator
  //!  object.  This is the sum of all bytes for all contained interface
  //!  matrix entries.
  //-------------------------------------------------------------------------
  uint64_t TotalBytes() const;

  //-------------------------------------------------------------------------
  //            const std::vector<ArtsAttribute> & Attributes() const;            
  //.........................................................................
  //!  Returns a const reference to the vector of ArtsAttribute objects
  //!  contained in the aggregate.
  //-------------------------------------------------------------------------
  const std::vector<ArtsAttribute> & Attributes() const;
  
private:
  ArtsHeader                               _header;
  std::vector<ArtsAttribute>                    _attributes;
 std::map<ArtsInterfaceMatrixKeyValue,counter_t,std::less<ArtsInterfaceMatrixKeyValue> >  _interfaceCounters;
  uint64_t                                 _totalPkts;
  uint64_t                                 _totalBytes;
};

#endif  // _ARTSINTERFACEMATRIXAGGREGATOR_HH_
