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

using namespace std;

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

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

  vector<ArtsAttribute>::const_iterator    inAttribute;

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

  counter_t  portCounter;
  vector<ArtsPortTableEntry>::const_iterator  inPortEntry;


  for (inPortEntry = arts.PortTableData()->PortEntries().begin();
       inPortEntry != arts.PortTableData()->PortEntries().end();
       inPortEntry++) {
    portCounter.inPkts = inPortEntry->InPkts();
    portCounter.inBytes = inPortEntry->InBytes();
    portCounter.outPkts = inPortEntry->OutPkts();
    portCounter.outBytes = inPortEntry->OutBytes();
    this->_portCounters[inPortEntry->PortNumber()] = portCounter;
  }
}

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

  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  portCounter;
  vector<ArtsPortTableEntry>::const_iterator         inPortEntry;
  map<uint16_t,counter_t,less<uint16_t> >::iterator  myPortEntry;
  
  for (inPortEntry = arts.PortTableData()->PortEntries().begin();
       inPortEntry != arts.PortTableData()->PortEntries().end();
       inPortEntry++) {
    myPortEntry = this->_portCounters.find(inPortEntry->PortNumber());
    if (myPortEntry == this->_portCounters.end()) {
      portCounter.inPkts = inPortEntry->InPkts();
      portCounter.inBytes = inPortEntry->InBytes();
      portCounter.outPkts = inPortEntry->OutPkts();
      portCounter.outBytes = inPortEntry->OutBytes();
      this->_portCounters[inPortEntry->PortNumber()] = portCounter;
    }
    else {
      (*myPortEntry).second.inPkts += inPortEntry->InPkts();
      (*myPortEntry).second.inBytes += inPortEntry->InBytes();
      (*myPortEntry).second.outPkts += inPortEntry->OutPkts();
      (*myPortEntry).second.outBytes += inPortEntry->OutBytes();
    }
  }
  return;
}

ArtsPortTable * ArtsPortTableAggregator::ConvertToArtsPortTable() const
{
  ArtsPortTableEntry  portEntry;
  map<uint16_t,counter_t,less<uint16_t> >::const_iterator  portCounter;
  ArtsPortTable  *artsPortTable = new ArtsPortTable();

  artsPortTable->Header() = this->_header;
  vector<ArtsAttribute>::const_iterator   inAttribute;
  
  for (inAttribute = this->_attributes.begin();
       inAttribute != this->_attributes.end(); inAttribute++) {
    artsPortTable->Attributes().push_back(*inAttribute);
  }
  
  for (portCounter = this->_portCounters.begin();
       portCounter != this->_portCounters.end(); portCounter++) {
    portEntry.PortNumber((*portCounter).first);
    portEntry.InPkts((*portCounter).second.inPkts);
    portEntry.InBytes((*portCounter).second.inBytes);
    portEntry.OutPkts((*portCounter).second.outPkts);
    portEntry.OutBytes((*portCounter).second.outBytes);
    artsPortTable->PortEntries().push_back(portEntry);
  }
  return(artsPortTable);
}
