blob: 9c4d4cb5b08d046e174e1285c8da8accba123915 [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
12 using namespace std;
13
14 static bool
15 npteCompare(Npte& npte, string& name)
16 {
17 return npte.getNamePrefix()==name;
18 }
19
20// Following two methods will update FIB with response to change in NPT
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() )
28 {
29 Npte newEntry( name);
30 newEntry.addRoutingTableEntry(rte);
31 newEntry.generateNhlfromRteList();
32 npteList.push_back(newEntry);
33 // update FIB here with nhl list newEntry.getNhl()
34 pnlsr.getFib().updateFib(pnlsr, name,newEntry.getNhl(),
35 pnlsr.getConfParameter().getMaxFacesPerPrefix());
36 }
37 else
38 {
39 (*it).addRoutingTableEntry(rte);
40 (*it).generateNhlfromRteList();
41 // update FIB here with nhl list from (*it).getNhl()
42 pnlsr.getFib().updateFib(pnlsr, name,(*it).getNhl() ,
43 pnlsr.getConfParameter().getMaxFacesPerPrefix());
44 }
45 }
46
47 void
48 Npt::removeNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
49 {
50 std::list<Npte >::iterator it = std::find_if( npteList.begin(),
51 npteList.end(), bind(&npteCompare, _1, name));
52 if ( it != npteList.end() )
53 {
54 string destRouter=rte.getDestination();
55 (*it).removeRoutingTableEntry(rte);
56 if ( ((*it).getRteListSize() == 0 ) &&
57 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/1",1) ) &&
58 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/2",2) ) &&
59 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/3",3) ) )
60 {
61 npteList.erase(it); // remove entry from NPT
62 // remove FIB entry with this name
63 pnlsr.getFib().removeFromFib(pnlsr,name);
64 }
65 else
66 {
67 (*it).generateNhlfromRteList();
68 // update FIB entry with new NHL
69 pnlsr.getFib().updateFib(pnlsr, name,(*it).getNhl(),
70 pnlsr.getConfParameter().getMaxFacesPerPrefix());
71 }
72 }
73 }
74
75
76 void
77 Npt::addNpte(string name, string destRouter, Nlsr& pnlsr)
78 {
79 std::pair<RoutingTableEntry& , bool> rteCheck=
80 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
81 if(rteCheck.second)
82 {
83 addNpte(name,rteCheck.first,pnlsr);
84 }
85 else
86 {
87 RoutingTableEntry rte(destRouter);
88 addNpte(name, rte,pnlsr);
89 }
90 }
91
92 void
93 Npt::removeNpte(string name, string destRouter, Nlsr& pnlsr)
94 {
95 std::pair<RoutingTableEntry& , bool> rteCheck=
96 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
97 if(rteCheck.second)
98 {
99 removeNpte(name,rteCheck.first,pnlsr);
100 }
101 else
102 {
103 RoutingTableEntry rte(destRouter);
104 removeNpte(name, rte,pnlsr);
105 }
106 }
107
108 void
109 Npt::updateNptWithNewRoute(Nlsr& pnlsr)
110 {
111 for(std::list<Npte >::iterator it=npteList.begin(); it!=npteList.end(); ++it)
112 {
113 std::list<RoutingTableEntry> rteList=(*it).getRteList();
114 for(std::list<RoutingTableEntry >::iterator rteit=rteList.begin();
115 rteit !=rteList.end(); ++rteit)
116 {
117 std::pair<RoutingTableEntry& , bool> rteCheck=
118 pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
119 if(rteCheck.second)
120 {
121 addNpte((*it).getNamePrefix(),rteCheck.first,pnlsr);
122 }
123 else
124 {
125 RoutingTableEntry rte((*rteit).getDestination());
126 addNpte((*it).getNamePrefix(), rte,pnlsr);
127 }
128 }
129 }
130 }
131
132 void
133 Npt::printNpt()
134 {
135 cout<<"----------------NPT----------------------"<<endl;
136 for(std::list<Npte >::iterator it=npteList.begin(); it!=npteList.end(); ++it)
137 {
138 cout <<(*it)<<endl;
139 }
140 }
141
142}