akmhoque | ba09474 | 2014-02-28 11:47:21 -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 | |
| 9 | namespace nlsr |
| 10 | { |
| 11 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 12 | using namespace std; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 13 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 14 | 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() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 28 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 29 | 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 38 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 39 | else |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 40 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 41 | 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 54 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 55 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 56 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 57 | 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() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 63 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 64 | 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 79 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 80 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 81 | |
| 82 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 83 | 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) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 89 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 90 | 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 | { |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 124 | std::pair<RoutingTableEntry& , bool> rteCheck= |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 125 | pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination()); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 126 | if(rteCheck.second) |
| 127 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 128 | addNpte((*it).getNamePrefix(),rteCheck.first,pnlsr); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 129 | } |
| 130 | else |
| 131 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 132 | RoutingTableEntry rte((*rteit).getDestination()); |
| 133 | addNpte((*it).getNamePrefix(), rte,pnlsr); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 134 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 135 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 136 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 137 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 138 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 139 | void |
| 140 | Npt::printNpt() |
| 141 | { |
| 142 | cout<<"----------------NPT----------------------"<<endl; |
| 143 | for(std::list<Npte >::iterator it=npteList.begin(); it!=npteList.end(); ++it) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 144 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 145 | cout <<(*it)<<endl; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 146 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 147 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 148 | |
| 149 | } |