blob: 2dfa311312aa318ba6c7d02531cc7ed5dfb63b15 [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#include <list>
2#include <utility>
3#include <algorithm>
4
5#include "nlsr_npt.hpp"
6#include "nlsr_npte.hpp"
7#include "nlsr.hpp"
8
9namespace nlsr
10{
11
akmhoque5a44dd42014-03-12 18:11:32 -050012 using namespace std;
akmhoqueba094742014-02-28 11:47:21 -060013
akmhoque5a44dd42014-03-12 18:11:32 -050014 static bool
15 npteCompare(Npte& npte, string& name)
16 {
17 return npte.getNamePrefix()==name;
18 }
19
20
21
22 void
23 Npt::addNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
24 {
25 std::list<Npte >::iterator it = std::find_if( npteList.begin(),
26 npteList.end(), bind(&npteCompare, _1, name));
27 if ( it == npteList.end() )
akmhoqueba094742014-02-28 11:47:21 -060028 {
akmhoque5a44dd42014-03-12 18:11:32 -050029 Npte newEntry(name);
30 newEntry.addRoutingTableEntry(rte);
31 newEntry.generateNhlfromRteList();
32 newEntry.getNhl().sortNhl();
33 npteList.push_back(newEntry);
34 if(rte.getNhl().getNhlSize()> 0)
35 {
36 pnlsr.getFib().updateFib(pnlsr, name,newEntry.getNhl());
37 }
akmhoqueba094742014-02-28 11:47:21 -060038 }
akmhoque5a44dd42014-03-12 18:11:32 -050039 else
akmhoqueba094742014-02-28 11:47:21 -060040 {
akmhoque5a44dd42014-03-12 18:11:32 -050041 if ( rte.getNhl().getNhlSize()> 0 )
42 {
43 (*it).addRoutingTableEntry(rte);
44 (*it).generateNhlfromRteList();
45 (*it).getNhl().sortNhl();
46 pnlsr.getFib().updateFib(pnlsr, name,(*it).getNhl());
47 }
48 else
49 {
50 (*it).resetRteListNextHop();
51 (*it).getNhl().resetNhl();
52 pnlsr.getFib().removeFromFib(pnlsr,name);
53 }
akmhoqueba094742014-02-28 11:47:21 -060054 }
akmhoque5a44dd42014-03-12 18:11:32 -050055 }
akmhoqueba094742014-02-28 11:47:21 -060056
akmhoque5a44dd42014-03-12 18:11:32 -050057 void
58 Npt::removeNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
59 {
60 std::list<Npte >::iterator it = std::find_if( npteList.begin(),
61 npteList.end(), bind(&npteCompare, _1, name));
62 if ( it != npteList.end() )
akmhoqueba094742014-02-28 11:47:21 -060063 {
akmhoque5a44dd42014-03-12 18:11:32 -050064 string destRouter=rte.getDestination();
65 (*it).removeRoutingTableEntry(rte);
66 if ( ((*it).getRteListSize() == 0 ) &&
67 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/1",1) ) &&
68 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/2",2) ) &&
69 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/3",3) ) )
70 {
71 npteList.erase(it);
72 pnlsr.getFib().removeFromFib(pnlsr,name);
73 }
74 else
75 {
76 (*it).generateNhlfromRteList();
77 pnlsr.getFib().updateFib(pnlsr, name,(*it).getNhl());
78 }
akmhoqueba094742014-02-28 11:47:21 -060079 }
akmhoque5a44dd42014-03-12 18:11:32 -050080 }
akmhoqueba094742014-02-28 11:47:21 -060081
82
akmhoque5a44dd42014-03-12 18:11:32 -050083 void
84 Npt::addNpteByDestName(string name, string destRouter, Nlsr& pnlsr)
85 {
86 std::pair<RoutingTableEntry& , bool> rteCheck=
87 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
88 if(rteCheck.second)
akmhoqueba094742014-02-28 11:47:21 -060089 {
akmhoque5a44dd42014-03-12 18:11:32 -050090 addNpte(name,rteCheck.first,pnlsr);
91 }
92 else
93 {
94 RoutingTableEntry rte(destRouter);
95 addNpte(name, rte,pnlsr);
96 }
97 }
98
99 void
100 Npt::removeNpte(string name, string destRouter, Nlsr& pnlsr)
101 {
102 std::pair<RoutingTableEntry& , bool> rteCheck=
103 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
104 if(rteCheck.second)
105 {
106 removeNpte(name,rteCheck.first,pnlsr);
107 }
108 else
109 {
110 RoutingTableEntry rte(destRouter);
111 removeNpte(name, rte,pnlsr);
112 }
113 }
114
115 void
116 Npt::updateNptWithNewRoute(Nlsr& pnlsr)
117 {
118 for(std::list<Npte >::iterator it=npteList.begin(); it!=npteList.end(); ++it)
119 {
120 std::list<RoutingTableEntry> rteList=(*it).getRteList();
121 for(std::list<RoutingTableEntry >::iterator rteit=rteList.begin();
122 rteit !=rteList.end(); ++rteit)
123 {
akmhoqueba094742014-02-28 11:47:21 -0600124 std::pair<RoutingTableEntry& , bool> rteCheck=
akmhoque5a44dd42014-03-12 18:11:32 -0500125 pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
akmhoqueba094742014-02-28 11:47:21 -0600126 if(rteCheck.second)
127 {
akmhoque5a44dd42014-03-12 18:11:32 -0500128 addNpte((*it).getNamePrefix(),rteCheck.first,pnlsr);
akmhoqueba094742014-02-28 11:47:21 -0600129 }
130 else
131 {
akmhoque5a44dd42014-03-12 18:11:32 -0500132 RoutingTableEntry rte((*rteit).getDestination());
133 addNpte((*it).getNamePrefix(), rte,pnlsr);
akmhoqueba094742014-02-28 11:47:21 -0600134 }
akmhoque5a44dd42014-03-12 18:11:32 -0500135 }
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 void
140 Npt::printNpt()
141 {
142 cout<<"----------------NPT----------------------"<<endl;
143 for(std::list<Npte >::iterator it=npteList.begin(); it!=npteList.end(); ++it)
akmhoqueba094742014-02-28 11:47:21 -0600144 {
akmhoque5a44dd42014-03-12 18:11:32 -0500145 cout <<(*it)<<endl;
akmhoqueba094742014-02-28 11:47:21 -0600146 }
akmhoque5a44dd42014-03-12 18:11:32 -0500147 }
akmhoqueba094742014-02-28 11:47:21 -0600148
149}