akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 1 | #include <list> |
| 2 | #include <utility> |
| 3 | #include <algorithm> |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 4 | #include "utility/nlsr_logger.hpp" |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 5 | #include "nlsr_npt.hpp" |
| 6 | #include "nlsr_npte.hpp" |
| 7 | #include "nlsr.hpp" |
| 8 | |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 9 | #define THIS_FILE "nlsr_npt.cpp" |
| 10 | |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 11 | namespace nlsr |
| 12 | { |
| 13 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 14 | using namespace std; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 15 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 16 | static bool |
| 17 | npteCompare(Npte& npte, string& name) |
| 18 | { |
| 19 | return npte.getNamePrefix()==name; |
| 20 | } |
| 21 | |
| 22 | |
| 23 | |
| 24 | void |
| 25 | Npt::addNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr) |
| 26 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 27 | std::list<Npte >::iterator it = std::find_if( m_npteList.begin(), |
| 28 | m_npteList.end(), bind(&npteCompare, _1, name)); |
| 29 | if ( it == m_npteList.end() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 30 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 31 | Npte newEntry(name); |
| 32 | newEntry.addRoutingTableEntry(rte); |
| 33 | newEntry.generateNhlfromRteList(); |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 34 | newEntry.getNhl().sort(); |
| 35 | m_npteList.push_back(newEntry); |
| 36 | if(rte.getNhl().getSize()> 0) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 37 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 38 | pnlsr.getFib().update(pnlsr, name,newEntry.getNhl()); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 39 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 40 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 41 | else |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 42 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 43 | if ( rte.getNhl().getSize()> 0 ) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 44 | { |
| 45 | (*it).addRoutingTableEntry(rte); |
| 46 | (*it).generateNhlfromRteList(); |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 47 | (*it).getNhl().sort(); |
| 48 | pnlsr.getFib().update(pnlsr, name,(*it).getNhl()); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 49 | } |
| 50 | else |
| 51 | { |
| 52 | (*it).resetRteListNextHop(); |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 53 | (*it).getNhl().reset(); |
| 54 | pnlsr.getFib().remove(pnlsr,name); |
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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 58 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 59 | void |
| 60 | Npt::removeNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr) |
| 61 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 62 | std::list<Npte >::iterator it = std::find_if( m_npteList.begin(), |
| 63 | m_npteList.end(), bind(&npteCompare, _1, name)); |
| 64 | if ( it != m_npteList.end() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 65 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 66 | string destRouter=rte.getDestination(); |
| 67 | (*it).removeRoutingTableEntry(rte); |
| 68 | if ( ((*it).getRteListSize() == 0 ) && |
| 69 | (!pnlsr.getLsdb().doesLsaExist(destRouter+"/1",1) ) && |
| 70 | (!pnlsr.getLsdb().doesLsaExist(destRouter+"/2",2) ) && |
| 71 | (!pnlsr.getLsdb().doesLsaExist(destRouter+"/3",3) ) ) |
| 72 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 73 | m_npteList.erase(it); |
| 74 | pnlsr.getFib().remove(pnlsr,name); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 75 | } |
| 76 | else |
| 77 | { |
| 78 | (*it).generateNhlfromRteList(); |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 79 | pnlsr.getFib().update(pnlsr, name,(*it).getNhl()); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 80 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 81 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 82 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 83 | |
| 84 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 85 | void |
| 86 | Npt::addNpteByDestName(string name, string destRouter, Nlsr& pnlsr) |
| 87 | { |
| 88 | std::pair<RoutingTableEntry& , bool> rteCheck= |
| 89 | pnlsr.getRoutingTable().findRoutingTableEntry(destRouter); |
| 90 | if(rteCheck.second) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 91 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 92 | addNpte(name,rteCheck.first,pnlsr); |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | RoutingTableEntry rte(destRouter); |
| 97 | addNpte(name, rte,pnlsr); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | Npt::removeNpte(string name, string destRouter, Nlsr& pnlsr) |
| 103 | { |
| 104 | std::pair<RoutingTableEntry& , bool> rteCheck= |
| 105 | pnlsr.getRoutingTable().findRoutingTableEntry(destRouter); |
| 106 | if(rteCheck.second) |
| 107 | { |
| 108 | removeNpte(name,rteCheck.first,pnlsr); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | RoutingTableEntry rte(destRouter); |
| 113 | removeNpte(name, rte,pnlsr); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 118 | Npt::updateWithNewRoute(Nlsr& pnlsr) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 119 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 120 | for(std::list<Npte >::iterator it=m_npteList.begin(); it!=m_npteList.end(); ++it) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 121 | { |
| 122 | std::list<RoutingTableEntry> rteList=(*it).getRteList(); |
| 123 | for(std::list<RoutingTableEntry >::iterator rteit=rteList.begin(); |
| 124 | rteit !=rteList.end(); ++rteit) |
| 125 | { |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 126 | std::pair<RoutingTableEntry& , bool> rteCheck= |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 127 | pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination()); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 128 | if(rteCheck.second) |
| 129 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 130 | addNpte((*it).getNamePrefix(),rteCheck.first,pnlsr); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 131 | } |
| 132 | else |
| 133 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 134 | RoutingTableEntry rte((*rteit).getDestination()); |
| 135 | addNpte((*it).getNamePrefix(), rte,pnlsr); |
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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 140 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 141 | void |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 142 | Npt::print() |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 143 | { |
| 144 | cout<<"----------------NPT----------------------"<<endl; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 145 | for(std::list<Npte >::iterator it=m_npteList.begin(); it!=m_npteList.end(); ++it) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 146 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 147 | cout <<(*it)<<endl; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 148 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 149 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 150 | |
| 151 | } |