blob: 89eba303efea84cd97cf74890f4369f52f699a6b [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#include <list>
2#include <utility>
3#include <algorithm>
akmhoque05d5fcf2014-04-15 14:58:45 -05004#include "utility/nlsr_logger.hpp"
akmhoqueba094742014-02-28 11:47:21 -06005#include "nlsr_npt.hpp"
6#include "nlsr_npte.hpp"
7#include "nlsr.hpp"
8
akmhoque05d5fcf2014-04-15 14:58:45 -05009#define THIS_FILE "nlsr_npt.cpp"
10
akmhoqueba094742014-02-28 11:47:21 -060011namespace nlsr
12{
13
akmhoque5a44dd42014-03-12 18:11:32 -050014 using namespace std;
akmhoqueba094742014-02-28 11:47:21 -060015
akmhoque5a44dd42014-03-12 18:11:32 -050016 static bool
17 npteCompare(Npte& npte, string& name)
18 {
19 return npte.getNamePrefix()==name;
20 }
21
22
23
24 void
25 Npt::addNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
26 {
akmhoque05d5fcf2014-04-15 14:58:45 -050027 std::list<Npte >::iterator it = std::find_if( m_npteList.begin(),
28 m_npteList.end(), bind(&npteCompare, _1, name));
29 if ( it == m_npteList.end() )
akmhoqueba094742014-02-28 11:47:21 -060030 {
akmhoque5a44dd42014-03-12 18:11:32 -050031 Npte newEntry(name);
32 newEntry.addRoutingTableEntry(rte);
33 newEntry.generateNhlfromRteList();
akmhoque05d5fcf2014-04-15 14:58:45 -050034 newEntry.getNhl().sort();
35 m_npteList.push_back(newEntry);
36 if(rte.getNhl().getSize()> 0)
akmhoque5a44dd42014-03-12 18:11:32 -050037 {
akmhoque05d5fcf2014-04-15 14:58:45 -050038 pnlsr.getFib().update(pnlsr, name,newEntry.getNhl());
akmhoque5a44dd42014-03-12 18:11:32 -050039 }
akmhoqueba094742014-02-28 11:47:21 -060040 }
akmhoque5a44dd42014-03-12 18:11:32 -050041 else
akmhoqueba094742014-02-28 11:47:21 -060042 {
akmhoque05d5fcf2014-04-15 14:58:45 -050043 if ( rte.getNhl().getSize()> 0 )
akmhoque5a44dd42014-03-12 18:11:32 -050044 {
45 (*it).addRoutingTableEntry(rte);
46 (*it).generateNhlfromRteList();
akmhoque05d5fcf2014-04-15 14:58:45 -050047 (*it).getNhl().sort();
48 pnlsr.getFib().update(pnlsr, name,(*it).getNhl());
akmhoque5a44dd42014-03-12 18:11:32 -050049 }
50 else
51 {
52 (*it).resetRteListNextHop();
akmhoque05d5fcf2014-04-15 14:58:45 -050053 (*it).getNhl().reset();
54 pnlsr.getFib().remove(pnlsr,name);
akmhoque5a44dd42014-03-12 18:11:32 -050055 }
akmhoqueba094742014-02-28 11:47:21 -060056 }
akmhoque5a44dd42014-03-12 18:11:32 -050057 }
akmhoqueba094742014-02-28 11:47:21 -060058
akmhoque5a44dd42014-03-12 18:11:32 -050059 void
60 Npt::removeNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
61 {
akmhoque05d5fcf2014-04-15 14:58:45 -050062 std::list<Npte >::iterator it = std::find_if( m_npteList.begin(),
63 m_npteList.end(), bind(&npteCompare, _1, name));
64 if ( it != m_npteList.end() )
akmhoqueba094742014-02-28 11:47:21 -060065 {
akmhoque5a44dd42014-03-12 18:11:32 -050066 string destRouter=rte.getDestination();
67 (*it).removeRoutingTableEntry(rte);
68 if ( ((*it).getRteListSize() == 0 ) &&
69 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/1",1) ) &&
70 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/2",2) ) &&
71 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/3",3) ) )
72 {
akmhoque05d5fcf2014-04-15 14:58:45 -050073 m_npteList.erase(it);
74 pnlsr.getFib().remove(pnlsr,name);
akmhoque5a44dd42014-03-12 18:11:32 -050075 }
76 else
77 {
78 (*it).generateNhlfromRteList();
akmhoque05d5fcf2014-04-15 14:58:45 -050079 pnlsr.getFib().update(pnlsr, name,(*it).getNhl());
akmhoque5a44dd42014-03-12 18:11:32 -050080 }
akmhoqueba094742014-02-28 11:47:21 -060081 }
akmhoque5a44dd42014-03-12 18:11:32 -050082 }
akmhoqueba094742014-02-28 11:47:21 -060083
84
akmhoque5a44dd42014-03-12 18:11:32 -050085 void
86 Npt::addNpteByDestName(string name, string destRouter, Nlsr& pnlsr)
87 {
88 std::pair<RoutingTableEntry& , bool> rteCheck=
89 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
90 if(rteCheck.second)
akmhoqueba094742014-02-28 11:47:21 -060091 {
akmhoque5a44dd42014-03-12 18:11:32 -050092 addNpte(name,rteCheck.first,pnlsr);
93 }
94 else
95 {
96 RoutingTableEntry rte(destRouter);
97 addNpte(name, rte,pnlsr);
98 }
99 }
100
101 void
102 Npt::removeNpte(string name, string destRouter, Nlsr& pnlsr)
103 {
104 std::pair<RoutingTableEntry& , bool> rteCheck=
105 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
106 if(rteCheck.second)
107 {
108 removeNpte(name,rteCheck.first,pnlsr);
109 }
110 else
111 {
112 RoutingTableEntry rte(destRouter);
113 removeNpte(name, rte,pnlsr);
114 }
115 }
116
117 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500118 Npt::updateWithNewRoute(Nlsr& pnlsr)
akmhoque5a44dd42014-03-12 18:11:32 -0500119 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500120 for(std::list<Npte >::iterator it=m_npteList.begin(); it!=m_npteList.end(); ++it)
akmhoque5a44dd42014-03-12 18:11:32 -0500121 {
122 std::list<RoutingTableEntry> rteList=(*it).getRteList();
123 for(std::list<RoutingTableEntry >::iterator rteit=rteList.begin();
124 rteit !=rteList.end(); ++rteit)
125 {
akmhoqueba094742014-02-28 11:47:21 -0600126 std::pair<RoutingTableEntry& , bool> rteCheck=
akmhoque5a44dd42014-03-12 18:11:32 -0500127 pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
akmhoqueba094742014-02-28 11:47:21 -0600128 if(rteCheck.second)
129 {
akmhoque5a44dd42014-03-12 18:11:32 -0500130 addNpte((*it).getNamePrefix(),rteCheck.first,pnlsr);
akmhoqueba094742014-02-28 11:47:21 -0600131 }
132 else
133 {
akmhoque5a44dd42014-03-12 18:11:32 -0500134 RoutingTableEntry rte((*rteit).getDestination());
135 addNpte((*it).getNamePrefix(), rte,pnlsr);
akmhoqueba094742014-02-28 11:47:21 -0600136 }
akmhoque5a44dd42014-03-12 18:11:32 -0500137 }
akmhoqueba094742014-02-28 11:47:21 -0600138 }
akmhoque5a44dd42014-03-12 18:11:32 -0500139 }
akmhoqueba094742014-02-28 11:47:21 -0600140
akmhoque5a44dd42014-03-12 18:11:32 -0500141 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500142 Npt::print()
akmhoque5a44dd42014-03-12 18:11:32 -0500143 {
144 cout<<"----------------NPT----------------------"<<endl;
akmhoque05d5fcf2014-04-15 14:58:45 -0500145 for(std::list<Npte >::iterator it=m_npteList.begin(); it!=m_npteList.end(); ++it)
akmhoqueba094742014-02-28 11:47:21 -0600146 {
akmhoque5a44dd42014-03-12 18:11:32 -0500147 cout <<(*it)<<endl;
akmhoqueba094742014-02-28 11:47:21 -0600148 }
akmhoque5a44dd42014-03-12 18:11:32 -0500149 }
akmhoqueba094742014-02-28 11:47:21 -0600150
151}