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