#include <string>
#include <utility>
#include "lsdb.hpp"
#include "nlsr.hpp"
#include "utility/name-helper.hpp"

namespace nlsr {

using namespace std;

void
Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
{
  m_nlsr.getScheduler().cancelEvent(eid);
}

static bool
nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
{
  return nlsa1.getKey() == key;
}


bool
Lsdb::buildAndInstallOwnNameLsa()
{
  NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
                  "name",
                  m_nlsr.getSequencingManager().getNameLsaSeq() + 1,
                  m_nlsr.getConfParameter().getRouterDeadInterval(),
                  m_nlsr.getNamePrefixList());
  m_nlsr.getSequencingManager().increaseNameLsaSeq();
  return installNameLsa(nameLsa);
}

NameLsa*
Lsdb::findNameLsa(const ndn::Name& key)
{
  std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
                                                 m_nameLsdb.end(),
                                                 bind(nameLsaCompareByKey, _1, key));
  if (it != m_nameLsdb.end())
  {
    return &(*it);
  }
  return 0;
}

bool
Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
{
  NameLsa* nameLsaCheck = findNameLsa(key);
  if (nameLsaCheck != 0)
  {
    if (nameLsaCheck->getLsSeqNo() < seqNo)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  return true;
}

ndn::EventId
Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo, int expTime)
{
  return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
                                             ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
                                                       this, key, seqNo));
}

bool
Lsdb::installNameLsa(NameLsa& nlsa)
{
  int timeToExpire = m_lsaRefreshTime;
  NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
  if (chkNameLsa == 0)
  {
    addNameLsa(nlsa);
    nlsa.writeLog();
    printNameLsdb();
    if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
    {
      m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
                                           nlsa.getOrigRouter());
      std::list<ndn::Name> nameList = nlsa.getNpl().getNameList();
      for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
           it++)
      {
        if ((*it) != m_nlsr.getConfParameter().getRouterPrefix())
        {
          m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
        }
      }
    }
    if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
    {
      timeToExpire = nlsa.getLifeTime();
    }
    nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
                                                      nlsa.getLsSeqNo(),
                                                      timeToExpire));
  }
  else
  {
    if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo())
    {
      chkNameLsa->writeLog();
      chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
      chkNameLsa->setLifeTime(nlsa.getLifeTime());
      chkNameLsa->getNpl().sort();
      nlsa.getNpl().sort();
      std::list<ndn::Name> nameToAdd;
      std::set_difference(nlsa.getNpl().getNameList().begin(),
                          nlsa.getNpl().getNameList().end(),
                          chkNameLsa->getNpl().getNameList().begin(),
                          chkNameLsa->getNpl().getNameList().end(),
                          std::inserter(nameToAdd, nameToAdd.begin()));
      for (std::list<ndn::Name>::iterator it = nameToAdd.begin();
           it != nameToAdd.end();
           ++it)
      {
        chkNameLsa->addName((*it));
        if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
        {
          if ((*it) != m_nlsr.getConfParameter().getRouterPrefix())
          {
            m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
          }
        }
      }
      std::list<ndn::Name> nameToRemove;
      std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
                          chkNameLsa->getNpl().getNameList().end(),
                          nlsa.getNpl().getNameList().begin(),
                          nlsa.getNpl().getNameList().end(),
                          std::inserter(nameToRemove, nameToRemove.begin()));
      for (std::list<ndn::Name>::iterator it = nameToRemove.begin();
           it != nameToRemove.end(); ++it)
      {
        chkNameLsa->removeName((*it));
        if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
        {
          if ((*it) != m_nlsr.getConfParameter().getRouterPrefix())
          {
            m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
          }
        }
      }
      if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
      {
        timeToExpire = nlsa.getLifeTime();
      }
      cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
      chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
                                                               nlsa.getLsSeqNo(),
                                                               timeToExpire));
      chkNameLsa->writeLog();
    }
  }
  return true;
}

bool
Lsdb::addNameLsa(NameLsa& nlsa)
{
  std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
                                                 m_nameLsdb.end(),
                                                 bind(nameLsaCompareByKey, _1,
                                                      nlsa.getKey()));
  if (it == m_nameLsdb.end())
  {
    m_nameLsdb.push_back(nlsa);
    return true;
  }
  return false;
}

bool
Lsdb::removeNameLsa(const ndn::Name& key)
{
  std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
                                                 m_nameLsdb.end(),
                                                 bind(nameLsaCompareByKey, _1, key));
  if (it != m_nameLsdb.end())
  {
    (*it).writeLog();
    if ((*it).getOrigRouter() !=
        m_nlsr.getConfParameter().getRouterPrefix())
    {
      m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
                                              (*it).getOrigRouter());
      for (std::list<ndn::Name>::iterator nit = (*it).getNpl().getNameList().begin();
           nit != (*it).getNpl().getNameList().end(); ++nit)
      {
        if ((*nit) != m_nlsr.getConfParameter().getRouterPrefix())
        {
          m_nlsr.getNamePrefixTable().removeEntry((*nit), (*it).getOrigRouter());
        }
      }
    }
    m_nameLsdb.erase(it);
    return true;
  }
  return false;
}

bool
Lsdb::doesNameLsaExist(const ndn::Name& key)
{
  std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
                                                 m_nameLsdb.end(),
                                                 bind(nameLsaCompareByKey, _1, key));
  if (it == m_nameLsdb.end())
  {
    return false;
  }
  return true;
}

void
Lsdb::printNameLsdb()
{
  cout << "---------------Name LSDB-------------------" << endl;
  for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
       it != m_nameLsdb.end() ; it++)
  {
    cout << (*it) << endl;
  }
}

// Cor LSA and LSDB related Functions start here


static bool
corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
{
  return clsa.getKey() == key;
}

bool
Lsdb::buildAndInstallOwnCoordinateLsa()
{
  CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
                       "coordinate",
                       m_nlsr.getSequencingManager().getCorLsaSeq() + 1,
                       m_nlsr.getConfParameter().getRouterDeadInterval(),
                       m_nlsr.getConfParameter().getCorR(),
                       m_nlsr.getConfParameter().getCorTheta());
  m_nlsr.getSequencingManager().increaseCorLsaSeq();
  installCoordinateLsa(corLsa);
  return true;
}

CoordinateLsa*
Lsdb::findCoordinateLsa(const ndn::Name& key)
{
  std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
                                                       m_corLsdb.end(),
                                                       bind(corLsaCompareByKey, _1, key));
  if (it != m_corLsdb.end())
  {
    return &(*it);
  }
  return 0;
}

bool
Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
{
  CoordinateLsa* clsa = findCoordinateLsa(key);
  if (clsa != 0)
  {
    if (clsa->getLsSeqNo() < seqNo)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  return true;
}

ndn::EventId
Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
                                      int expTime)
{
  return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
                                             ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
                                                       this, key, seqNo));
}

bool
Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
{
  int timeToExpire = m_lsaRefreshTime;
  CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
  if (chkCorLsa == 0)
  {
    addCoordinateLsa(clsa);
    printCorLsdb(); //debugging purpose
    if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
    {
      m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
                                           clsa.getOrigRouter());
    }
    if (m_nlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
    {
      m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
    }
    if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
    {
      timeToExpire = clsa.getLifeTime();
    }
    scheduleCoordinateLsaExpiration(clsa.getKey(),
                                    clsa.getLsSeqNo(), timeToExpire);
  }
  else
  {
    if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo())
    {
      chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
      chkCorLsa->setLifeTime(clsa.getLifeTime());
      if (!chkCorLsa->isEqualContent(clsa))
      {
        chkCorLsa->setCorRadius(clsa.getCorRadius());
        chkCorLsa->setCorTheta(clsa.getCorTheta());
        if (m_nlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
        {
          m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
        }
      }
      if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
      {
        timeToExpire = clsa.getLifeTime();
      }
      cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
      chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
                                                                    clsa.getLsSeqNo(),
                                                                    timeToExpire));
    }
  }
  return true;
}

bool
Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
{
  std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
                                                       m_corLsdb.end(),
                                                       bind(corLsaCompareByKey, _1,
                                                            clsa.getKey()));
  if (it == m_corLsdb.end())
  {
    m_corLsdb.push_back(clsa);
    return true;
  }
  return false;
}

bool
Lsdb::removeCoordinateLsa(const ndn::Name& key)
{
  std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
                                                       m_corLsdb.end(),
                                                       bind(corLsaCompareByKey, _1, key));
  if (it != m_corLsdb.end())
  {
    if ((*it).getOrigRouter() !=
        m_nlsr.getConfParameter().getRouterPrefix())
    {
      m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
                                              (*it).getOrigRouter());
    }
    m_corLsdb.erase(it);
    return true;
  }
  return false;
}

bool
Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
{
  std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
                                                       m_corLsdb.end(),
                                                       bind(corLsaCompareByKey, _1, key));
  if (it == m_corLsdb.end())
  {
    return false;
  }
  return true;
}

void
Lsdb::printCorLsdb() //debugging
{
  cout << "---------------Cor LSDB-------------------" << endl;
  for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
       it != m_corLsdb.end() ; it++)
  {
    cout << (*it) << endl;
  }
}


// Adj LSA and LSDB related function starts here

static bool
adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
{
  return alsa.getKey() == key;
}


void
Lsdb::scheduledAdjLsaBuild()
{
  cout << "scheduledAdjLsaBuild Called" << endl;
  m_nlsr.setIsBuildAdjLsaSheduled(0);
  if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr))
  {
    int adjBuildCount = m_nlsr.getAdjBuildCount();
    if (adjBuildCount > 0)
    {
      if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0)
      {
        buildAndInstallOwnAdjLsa();
      }
      else
      {
        ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
        key.append("adjacency");
        removeAdjLsa(key);
        m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
      }
      m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
    }
  }
  else
  {
    m_nlsr.setIsBuildAdjLsaSheduled(1);
    int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
                         m_nlsr.getConfParameter().getInterestResendTime();
    m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
                                        ndn::bind(&Lsdb::scheduledAdjLsaBuild,
                                                  this));
  }
}


bool
Lsdb::addAdjLsa(AdjLsa& alsa)
{
  std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
                                                m_adjLsdb.end(),
                                                bind(adjLsaCompareByKey, _1,
                                                     alsa.getKey()));
  if (it == m_adjLsdb.end())
  {
    m_adjLsdb.push_back(alsa);
    return true;
  }
  return false;
}

AdjLsa*
Lsdb::findAdjLsa(const ndn::Name& key)
{
  std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
                                                m_adjLsdb.end(),
                                                bind(adjLsaCompareByKey, _1, key));
  if (it != m_adjLsdb.end())
  {
    return &(*it);
  }
  return 0;
}


bool
Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
{
  AdjLsa*  adjLsaCheck = findAdjLsa(key);
  if (adjLsaCheck != 0)
  {
    if (adjLsaCheck->getLsSeqNo() < seqNo)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  return true;
}


ndn::EventId
Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo, int expTime)
{
  return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
                                             ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
                                                       this, key, seqNo));
}

bool
Lsdb::installAdjLsa(AdjLsa& alsa)
{
  int timeToExpire = m_lsaRefreshTime;
  AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
  if (chkAdjLsa == 0)
  {
    addAdjLsa(alsa);
    alsa.addNptEntries(m_nlsr);
    m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
    if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
    {
      timeToExpire = alsa.getLifeTime();
    }
    scheduleAdjLsaExpiration(alsa.getKey(),
                             alsa.getLsSeqNo(), timeToExpire);
  }
  else
  {
    if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo())
    {
      chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
      chkAdjLsa->setLifeTime(alsa.getLifeTime());
      if (!chkAdjLsa->isEqualContent(alsa))
      {
        chkAdjLsa->getAdl().reset();
        chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
        m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
      }
      if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
      {
        timeToExpire = alsa.getLifeTime();
      }
      cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
      chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
                                                             alsa.getLsSeqNo(),
                                                             timeToExpire));
    }
  }
  return true;
}

bool
Lsdb::buildAndInstallOwnAdjLsa()
{
  AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
                "adjacency",
                m_nlsr.getSequencingManager().getAdjLsaSeq() + 1,
                m_nlsr.getConfParameter().getRouterDeadInterval(),
                m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
                m_nlsr.getAdjacencyList());
  m_nlsr.getSequencingManager().increaseAdjLsaSeq();
  // publish routing update
  ndn::Name lsaPrefix = m_nlsr.getConfParameter().getChronosyncLsaPrefix();
  lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
  m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
                                                    lsaPrefix);
  return installAdjLsa(adjLsa);
}

bool
Lsdb::removeAdjLsa(const ndn::Name& key)
{
  std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
                                                m_adjLsdb.end(),
                                                bind(adjLsaCompareByKey, _1, key));
  if (it != m_adjLsdb.end())
  {
    (*it).removeNptEntries(m_nlsr);
    m_adjLsdb.erase(it);
    return true;
  }
  return false;
}

bool
Lsdb::doesAdjLsaExist(const ndn::Name& key)
{
  std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
                                                m_adjLsdb.end(),
                                                bind(adjLsaCompareByKey, _1, key));
  if (it == m_adjLsdb.end())
  {
    return false;
  }
  return true;
}

std::list<AdjLsa>&
Lsdb::getAdjLsdb()
{
  return m_adjLsdb;
}

void
Lsdb::setLsaRefreshTime(int lrt)
{
  m_lsaRefreshTime = lrt;
}

void
Lsdb::setThisRouterPrefix(string trp)
{
  m_thisRouterPrefix = trp;
}

void
Lsdb::exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
{
  cout << "Lsdb::exprireOrRefreshNameLsa Called " << endl;
  cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
  NameLsa* chkNameLsa = findNameLsa(lsaKey);
  if (chkNameLsa != 0)
  {
    cout << " LSA Exists with seq no: " << chkNameLsa->getLsSeqNo() << endl;
    if (chkNameLsa->getLsSeqNo() == seqNo)
    {
      if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix)
      {
        chkNameLsa->writeLog();
        cout << "Own Name LSA, so refreshing name LSA" << endl;
        chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
        m_nlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
        chkNameLsa->writeLog();
        // schedule refreshing event again
        chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
                                                                 chkNameLsa->getLsSeqNo(),
                                                                 m_lsaRefreshTime));
        // publish routing update
        ndn::Name lsaPrefix = m_nlsr.getConfParameter().getChronosyncLsaPrefix();
        lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
        m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
                                                          lsaPrefix);
      }
      else
      {
        cout << "Other's Name LSA, so removing form LSDB" << endl;
        removeNameLsa(lsaKey);
      }
    }
  }
}

void
Lsdb::exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
{
  cout << "Lsdb::exprireOrRefreshAdjLsa Called " << endl;
  cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
  AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
  if (chkAdjLsa != 0)
  {
    cout << " LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo() << endl;
    if (chkAdjLsa->getLsSeqNo() == seqNo)
    {
      if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix)
      {
        cout << "Own Adj LSA, so refreshing Adj LSA" << endl;
        chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
        m_nlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
        // schedule refreshing event again
        chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
                                                               chkAdjLsa->getLsSeqNo(),
                                                               m_lsaRefreshTime));
        // publish routing update
        ndn::Name lsaPrefix = m_nlsr.getConfParameter().getChronosyncLsaPrefix();
        lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
        m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
                                                          lsaPrefix);
      }
      else
      {
        cout << "Other's Adj LSA, so removing form LSDB" << endl;
        removeAdjLsa(lsaKey);
      }
      // schedule Routing table calculaiton
      m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
    }
  }
}

void
Lsdb::exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
                                    uint64_t seqNo)
{
  cout << "Lsdb::exprireOrRefreshCorLsa Called " << endl;
  cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
  CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
  if (chkCorLsa != 0)
  {
    cout << " LSA Exists with seq no: " << chkCorLsa->getLsSeqNo() << endl;
    if (chkCorLsa->getLsSeqNo() == seqNo)
    {
      if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix)
      {
        cout << "Own Cor LSA, so refreshing Cor LSA" << endl;
        chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
        m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
        // schedule refreshing event again
        chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
                                        chkCorLsa->getKey(),
                                        chkCorLsa->getLsSeqNo(),
                                        m_lsaRefreshTime));
        // publish routing update
        ndn::Name lsaPrefix = m_nlsr.getConfParameter().getChronosyncLsaPrefix();
        lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
        m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
                                                          lsaPrefix);
      }
      else
      {
        cout << "Other's Cor LSA, so removing form LSDB" << endl;
        removeCoordinateLsa(lsaKey);
      }
      if (m_nlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
      {
        m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
      }
    }
  }
}


void
Lsdb::expressInterest(const ndn::Name& interestName, uint32_t interestLifeTime)
{
  std::cout << "Expressing Interest :" << interestName << std::endl;
  ndn::Interest interest(interestName);
  interest.setInterestLifetime(ndn::time::seconds(interestLifeTime));
  interest.setMustBeFresh(true);
  m_nlsr.getNlsrFace().expressInterest(interest,
                                       ndn::bind(&Lsdb::processContent,
                                                 this, _1, _2),
                                       ndn::bind(&Lsdb::processInterestTimedOut,
                                                 this, _1));
}

void
Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
{
  const ndn::Name& intName(interest.getName());
  std::cout << "Interest recevied for LSA: " << intName << std::endl;
  string chkString("LSA");
  int32_t lsaPosition = util::getNameComponentPosition(interest.getName(),
                                                       chkString);
  if (lsaPosition >= 0)
  {
    std::string interestedLsType;
    uint64_t interestedLsSeqNo;
    ndn::Name origRouter = intName.getSubName(lsaPosition + 1,
                                              interest.getName().size() - lsaPosition - 3);
    interestedLsType  = intName[-2].toEscapedString();
    interestedLsSeqNo = intName[-1].toNumber();
    std::cout << "Router Name: " << origRouter << std::endl;
    std::cout << "Ls Type    : " << interestedLsType << std::endl;
    std::cout << "Ls Seq     : " << interestedLsSeqNo << endl;
    std::cout << "Ls Type: " << interestedLsType << std::endl;
    if (interestedLsType == "name")
    {
      processInterestForNameLsa(interest,
                                origRouter.append(interestedLsType),
                                interestedLsSeqNo);
      return;
    }
    else if (interestedLsType == "adjacency")
    {
      processInterestForAdjacencyLsa(interest,
                                     origRouter.append(interestedLsType),
                                     interestedLsSeqNo);
      return;
    }
    else if (interestedLsType == "coordinate")
    {
      processInterestForCoordinateLsa(interest,
                                      origRouter.append(interestedLsType),
                                      interestedLsSeqNo);
      return;
    }
    else
    {
      cout << "Unrecognized LSA Type :(" << endl;
    }
  }
}

void
Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
                                const ndn::Name& lsaKey,
                                uint32_t interestedlsSeqNo)
{
  NameLsa*  nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
  if (nameLsa != 0)
  {
    if (nameLsa->getLsSeqNo() >= interestedlsSeqNo)
    {
      ndn::Data data(ndn::Name(interest.getName()).appendVersion());
      data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
      std::string content = nameLsa->getData();
      data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
                      content.size());
      m_keyChain.sign(data);
      std::cout << ">> D: " << data << std::endl;
      m_nlsr.getNlsrFace().put(data);
    }
  }
}

void
Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
                                     const ndn::Name& lsaKey,
                                     uint32_t interestedlsSeqNo)
{
  AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
  if (adjLsa != 0)
  {
    if (adjLsa->getLsSeqNo() >= interestedlsSeqNo)
    {
      ndn::Data data(ndn::Name(interest.getName()).appendVersion());
      data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
      std::string content = adjLsa->getData();
      data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
                      content.size());
      m_keyChain.sign(data);
      std::cout << ">> D: " << data << std::endl;
      m_nlsr.getNlsrFace().put(data);
    }
  }
}

void
Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
                                      const ndn::Name& lsaKey,
                                      uint32_t interestedlsSeqNo)
{
  CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
  if (corLsa != 0)
  {
    if (corLsa->getLsSeqNo() >= interestedlsSeqNo)
    {
      ndn::Data data(ndn::Name(interest.getName()).appendVersion());
      data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
      std::string content = corLsa->getData();
      data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
                      content.size());
      m_keyChain.sign(data);
      std::cout << ">> D: " << data << std::endl;
      m_nlsr.getNlsrFace().put(data);
    }
  }
}

void
Lsdb::processContent(const ndn::Interest& interest, const ndn::Data& data)
{
  const ndn::Name& dataName = data.getName();
  std::cout << "Data received for name: " << dataName << std::endl;
  string dataContent(reinterpret_cast<const char*>(data.getContent().value()));
  string chkString("LSA");
  int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
  if (lsaPosition >= 0)
  {
    std::string interestedLsType;
    uint64_t interestedLsSeqNo;
    ndn::Name origRouter = dataName.getSubName(lsaPosition + 1,
                                               dataName.size() - lsaPosition - 4);
    interestedLsType  = dataName[-3].toEscapedString();
    interestedLsSeqNo = dataName[-2].toNumber();
    std::cout << "Ls Type    : " << interestedLsType << std::endl;
    std::cout << "Ls Seq     : " << interestedLsSeqNo << std::endl;
    std::cout << "Ls Type: " << interestedLsType << std::endl;
    if (interestedLsType == "name")
    {
      processContentNameLsa(origRouter.append(interestedLsType),
                            interestedLsSeqNo, dataContent);
      return;
    }
    else if (interestedLsType == "adjacency")
    {
      processContentAdjacencyLsa(origRouter.append(interestedLsType),
                                 interestedLsSeqNo, dataContent);
      return;
    }
    else if (interestedLsType == "coordinate")
    {
      processContentCoordinateLsa(origRouter.append(interestedLsType),
                                  interestedLsSeqNo, dataContent);
      return;
    }
    else
    {
      cout << "Unrecognized LSA Type :(" << endl;
    }
  }
}

void
Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
                            uint32_t lsSeqNo, std::string& dataContent)
{
  if (isNameLsaNew(lsaKey, lsSeqNo))
  {
    NameLsa nameLsa;
    if (nameLsa.initializeFromContent(dataContent))
    {
      installNameLsa(nameLsa);
    }
    else
    {
      std::cout << "LSA data decoding error :(" << std::endl;
    }
  }
}

void
Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
                                 uint32_t lsSeqNo, std::string& dataContent)
{
  if (isAdjLsaNew(lsaKey, lsSeqNo))
  {
    AdjLsa adjLsa;
    if (adjLsa.initializeFromContent(dataContent))
    {
      installAdjLsa(adjLsa);
    }
    else
    {
      std::cout << "LSA data decoding error :(" << std::endl;
    }
  }
}

void
Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
                                  uint32_t lsSeqNo, std::string& dataContent)
{
  if (isCoordinateLsaNew(lsaKey, lsSeqNo))
  {
    CoordinateLsa corLsa;
    if (corLsa.initializeFromContent(dataContent))
    {
      installCoordinateLsa(corLsa);
    }
    else
    {
      std::cout << "LSA data decoding error :(" << std::endl;
    }
  }
}

void
Lsdb::processInterestTimedOut(const ndn::Interest& interest)
{
  const ndn::Name& interestName(interest.getName());
  cout << "Interest timed out for Name: " << interestName << endl;
}


void
Lsdb::printAdjLsdb()
{
  cout << "---------------Adj LSDB-------------------" << endl;
  for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
       it != m_adjLsdb.end() ; it++)
  {
    cout << (*it) << endl;
  }
}

//-----utility function -----
bool
Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType)
{
  if (lsType == "name")
  {
    return doesNameLsaExist(key);
  }
  else if (lsType == "adjacency")
  {
    return doesAdjLsaExist(key);
  }
  else if (lsType == "coordinate")
  {
    return doesCoordinateLsaExist(key);
  }
  return false;
}

}//namespace nlsr

