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