//===========================================================================
//  @(#) $Name: arts++-1-1-a12 $
//  @(#) $Id: ArtsNetMatrixAggregator.cc,v 1.2 2004/04/21 23:51:33 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
//===========================================================================

#include <string>

#include "ArtsNetMatrixAggregator.hh"

using namespace std;

static const std::string rcsid = "@(#) $Name: arts++-1-1-a12 $ $Id: ArtsNetMatrixAggregator.cc,v 1.2 2004/04/21 23:51:33 kkeys Exp $";

//-------------------------------------------------------------------------
//         bool ArtsNetMatrixKeyValue::                      
//         operator < (const ArtsNetMatrixKeyValue keyValue) const 
//.........................................................................
//  
//-------------------------------------------------------------------------
bool ArtsNetMatrixKeyValue::
operator < (const ArtsNetMatrixKeyValue keyValue) const
{
  if (this->srcNet < keyValue.srcNet)
    return(true);
  if (this->srcNet > keyValue.srcNet)
    return(false);
  
  if (this->srcMaskLen < keyValue.srcMaskLen)
    return(true);
  if (this->srcMaskLen > keyValue.srcMaskLen)
    return(false);

  if (this->dstNet < keyValue.dstNet)
    return(true);
  if (this->dstNet > keyValue.dstNet)
    return(false);

  if (this->dstMaskLen < keyValue.dstMaskLen)
    return(true);

  return(false);
}

//---------------------------------------------------------------------------
//        bool ArtsNetMatrixKeyValue::
//        operator == (const ArtsNetMatrixKeyValue keyValue) const
//...........................................................................
//
//---------------------------------------------------------------------------
bool ArtsNetMatrixKeyValue::
operator == (const ArtsNetMatrixKeyValue keyValue) const
{
  if (this->srcNet == keyValue.srcNet &&
      this->srcMaskLen == keyValue.srcMaskLen &&
      this->dstNet == keyValue.dstNet &&
      this->dstMaskLen == keyValue.dstMaskLen)
    return(true);
  return(false);
}

//-------------------------------------------------------------------------
//   ArtsNetMatrixAggregator::ArtsNetMatrixAggregator(const Arts & arts)   
//.........................................................................
//  
//-------------------------------------------------------------------------
ArtsNetMatrixAggregator::ArtsNetMatrixAggregator(const Arts & arts)
{
  assert(arts.Header().Identifier() == artsC_OBJECT_NET);
  
  this->_header = arts.Header();

  vector<ArtsAttribute>::const_iterator    inAttribute;

  for (inAttribute = arts.Attributes().begin();
       inAttribute != arts.Attributes().end(); ++inAttribute) {
    this->_attributes.push_back(*inAttribute);
  }

  counter_t               netMatrixCounter;
  ArtsNetMatrixKeyValue   key;
  vector<ArtsNetMatrixEntry>::const_iterator  inNetMatrixEntry;
  
  for (inNetMatrixEntry = arts.NetMatrixData()->NetEntries().begin();
       inNetMatrixEntry != arts.NetMatrixData()->NetEntries().end();
       ++inNetMatrixEntry) {
    key.srcNet = inNetMatrixEntry->Src();
    key.srcMaskLen = inNetMatrixEntry->SrcMaskLen();
    key.dstNet = inNetMatrixEntry->Dst();
    key.dstMaskLen = inNetMatrixEntry->DstMaskLen();
    
    netMatrixCounter.Pkts = inNetMatrixEntry->Pkts();
    netMatrixCounter.Bytes = inNetMatrixEntry->Bytes();
    this->_netCounters[key] = netMatrixCounter;
  }
}

//----------------------------------------------------------------------------
//           ArtsNetMatrixAggregator::~ArtsNetMatrixAggregator()           
//............................................................................
//  
//----------------------------------------------------------------------------
ArtsNetMatrixAggregator::~ArtsNetMatrixAggregator()
{
  this->_attributes.erase(this->_attributes.begin(),this->_attributes.end());
  this->_netCounters.erase(this->_netCounters.begin(),
                           this->_netCounters.end());
}

//-------------------------------------------------------------------------
//           void ArtsNetMatrixAggregator::Add(const Arts & arts)          
//.........................................................................
//  
//-------------------------------------------------------------------------
void ArtsNetMatrixAggregator::Add(const Arts & arts)
{
  assert(arts.Header().Identifier() == artsC_OBJECT_NET);

  vector<ArtsAttribute>::iterator          myHostAttribute;

  for (myHostAttribute = this->_attributes.begin();
         myHostAttribute != this->_attributes.end();
         ++myHostAttribute) {
    if (myHostAttribute->Identifier() == artsC_ATTR_HOST)
      break;
  }
  
  vector<ArtsAttribute>::iterator          myPeriodAttribute;
  vector<ArtsAttribute>::const_iterator    inPeriodAttribute;
  
  for (myPeriodAttribute = this->_attributes.begin();
         myPeriodAttribute != this->_attributes.end();
         ++myPeriodAttribute) {
    if (myPeriodAttribute->Identifier() == artsC_ATTR_PERIOD)
      break;
  }
  
  inPeriodAttribute = arts.FindPeriodAttribute();

  const uint32_t *myPeriod = myPeriodAttribute->Period();
  const uint32_t *inPeriod = inPeriodAttribute->Period();
  
  if (inPeriod[0] < myPeriod[0]) {
    myPeriodAttribute->Period(inPeriod[0],myPeriod[1]);
  }
  if (inPeriod[1] > myPeriod[1]) {
    myPeriodAttribute->Period(myPeriod[0],inPeriod[1]);
  }
    
  counter_t               netCounter;
  ArtsNetMatrixKeyValue   key;
  vector<ArtsNetMatrixEntry>::const_iterator        inNetEntry;
  map<ArtsNetMatrixKeyValue,counter_t,less<ArtsNetMatrixKeyValue> >::iterator   myNetEntry;

  for (inNetEntry = arts.NetMatrixData()->NetEntries().begin();
       inNetEntry != arts.NetMatrixData()->NetEntries().end();
       ++inNetEntry) {
    key.srcNet = inNetEntry->Src();
    key.srcMaskLen = inNetEntry->SrcMaskLen();
    key.dstNet = inNetEntry->Dst();
    key.dstMaskLen = inNetEntry->DstMaskLen();
    
    myNetEntry = this->_netCounters.find(key);
    if (myNetEntry != this->_netCounters.end()) {
      (*myNetEntry).second.Pkts += inNetEntry->Pkts();
      (*myNetEntry).second.Bytes += inNetEntry->Bytes();
    }
    else {
      netCounter.Pkts = inNetEntry->Pkts();
      netCounter.Bytes = inNetEntry->Bytes();
      this->_netCounters[key] = netCounter;
    }
  }
  return;
}

//-------------------------------------------------------------------------
//  ArtsNetMatrix *ArtsNetMatrixAggregator::ConvertToArtsNetMatrix() const 
//.........................................................................
//  
//-------------------------------------------------------------------------
ArtsNetMatrix *ArtsNetMatrixAggregator::ConvertToArtsNetMatrix() const
{
  ArtsNetMatrixEntry  netEntry;
  map<ArtsNetMatrixKeyValue,counter_t,less<ArtsNetMatrixKeyValue> >::const_iterator  netCounter;

  ArtsNetMatrix *artsNetMatrix = new ArtsNetMatrix();

  artsNetMatrix->Header() = this->_header;
  vector<ArtsAttribute>::const_iterator   inAttribute;

  for (inAttribute = this->_attributes.begin();
       inAttribute != this->_attributes.end(); ++inAttribute) {
    artsNetMatrix->Attributes().push_back(*inAttribute);
  }

  uint64_t  totalPkts = 0;
  uint64_t  totalBytes = 0;
  
  for (netCounter = this->_netCounters.begin();
       netCounter != this->_netCounters.end(); ++netCounter) {
    netEntry.Src((*netCounter).first.srcNet);
    netEntry.SrcMaskLen((*netCounter).first.srcMaskLen);
    netEntry.Dst((*netCounter).first.dstNet);
    netEntry.DstMaskLen((*netCounter).first.dstMaskLen);
    netEntry.Pkts((*netCounter).second.Pkts);
    netEntry.Bytes((*netCounter).second.Bytes);
    artsNetMatrix->NetEntries().push_back(netEntry);
    totalPkts += netEntry.Pkts();
    totalBytes += netEntry.Bytes();
  }
  artsNetMatrix->TotalPkts(totalPkts);
  artsNetMatrix->TotalBytes(totalBytes);
  
  return(artsNetMatrix);
}
