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 | |
| 9 | using namespace std; |
| 10 | |
| 11 | static bool |
| 12 | npteCompare(Npte& npte, string& name){ |
| 13 | return npte.getNamePrefix()==name; |
| 14 | } |
| 15 | |
| 16 | // Following two methods will update FIB with response to change in NPT |
| 17 | |
| 18 | void |
| 19 | Npt::addNpte(string name, RoutingTableEntry& rte, nlsr& pnlsr) |
| 20 | { |
| 21 | std::list<Npte >::iterator it = std::find_if( npteList.begin(), |
| 22 | npteList.end(), bind(&npteCompare, _1, name)); |
| 23 | |
| 24 | if ( it == npteList.end() ) |
| 25 | { |
| 26 | Npte newEntry( name); |
| 27 | newEntry.addRoutingTableEntry(rte); |
| 28 | newEntry.generateNhlfromRteList(); |
| 29 | npteList.push_back(newEntry); |
| 30 | // update FIB here with nhl list newEntry.getNhl() |
| 31 | pnlsr.getFib().updateFib(name,newEntry.getNhl(), |
| 32 | pnlsr.getConfParameter().getMaxFacesPerPrefix()); |
| 33 | } |
| 34 | else |
| 35 | { |
| 36 | (*it).addRoutingTableEntry(rte); |
| 37 | (*it).generateNhlfromRteList(); |
| 38 | // update FIB here with nhl list from (*it).getNhl() |
| 39 | pnlsr.getFib().updateFib(name,(*it).getNhl() , |
| 40 | pnlsr.getConfParameter().getMaxFacesPerPrefix()); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | Npt::removeNpte(string name, RoutingTableEntry& rte, nlsr& pnlsr) |
| 46 | { |
| 47 | std::list<Npte >::iterator it = std::find_if( npteList.begin(), |
| 48 | npteList.end(), bind(&npteCompare, _1, name)); |
| 49 | if ( it != npteList.end() ) |
| 50 | { |
| 51 | string destRouter=rte.getDestination(); |
| 52 | |
| 53 | (*it).removeRoutingTableEntry(rte); |
| 54 | if ( ((*it).getRteListSize() == 0 ) && |
| 55 | (!pnlsr.getLsdb().doesLsaExist(destRouter+"/1",1) ) && |
| 56 | (!pnlsr.getLsdb().doesLsaExist(destRouter+"/2",2) ) && |
| 57 | (!pnlsr.getLsdb().doesLsaExist(destRouter+"/3",3) ) ) |
| 58 | { |
| 59 | npteList.erase(it); // remove entry from NPT |
| 60 | // remove FIB entry with this name |
| 61 | pnlsr.getFib().removeFromFib(name); |
| 62 | |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | (*it).generateNhlfromRteList(); |
| 67 | // update FIB entry with new NHL |
| 68 | pnlsr.getFib().updateFib(name,(*it).getNhl(), |
| 69 | pnlsr.getConfParameter().getMaxFacesPerPrefix()); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | void |
| 76 | Npt::addNpte(string name, string destRouter, nlsr& pnlsr) |
| 77 | { |
| 78 | std::pair<RoutingTableEntry& , bool> rteCheck= |
| 79 | pnlsr.getRoutingTable().findRoutingTableEntry(destRouter); |
| 80 | if(rteCheck.second) |
| 81 | { |
| 82 | addNpte(name,rteCheck.first,pnlsr); |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | RoutingTableEntry rte(destRouter); |
| 87 | addNpte(name, rte,pnlsr); |
| 88 | } |
| 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 | } |