blob: e59479456aaf87d36135a32203f3a1c84eff83cd [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include <iostream>
2#include <cmath>
3#include "nlsr_lsdb.hpp"
4#include "nlsr_rtc.hpp"
5#include "nlsr_map.hpp"
6#include "nlsr_lsa.hpp"
7#include "nlsr_nexthop.hpp"
8#include "nlsr.hpp"
9
akmhoque1fd8c1e2014-02-19 19:41:49 -060010namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -060011{
akmhoque298385a2014-02-13 14:13:09 -060012
akmhoque1fd8c1e2014-02-19 19:41:49 -060013 using namespace std;
akmhoque298385a2014-02-13 14:13:09 -060014
akmhoque1fd8c1e2014-02-19 19:41:49 -060015 void
16 RoutingTableCalculator::allocateAdjMatrix()
17 {
18 adjMatrix = new double*[numOfRouter];
19 for(int i = 0; i < numOfRouter; ++i)
20 {
21 adjMatrix[i] = new double[numOfRouter];
22 }
23 }
akmhoque298385a2014-02-13 14:13:09 -060024
akmhoque1fd8c1e2014-02-19 19:41:49 -060025 void
26 RoutingTableCalculator::initMatrix()
27 {
28 for(int i=0; i<numOfRouter; i++)
29 {
30 for(int j=0; j<numOfRouter; j++)
31 adjMatrix[i][j]=0;
32 }
33 }
akmhoque298385a2014-02-13 14:13:09 -060034
akmhoque1fd8c1e2014-02-19 19:41:49 -060035 void
36 RoutingTableCalculator::makeAdjMatrix(Nlsr& pnlsr, Map pMap)
37 {
38 std::list<AdjLsa> adjLsdb=pnlsr.getLsdb().getAdjLsdb();
39 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
40 it!= adjLsdb.end() ; it++)
41 {
42 string linkStartRouter=(*it).getOrigRouter();
43 int row=pMap.getMappingNoByRouterName(linkStartRouter);
44 std::list<Adjacent> adl=(*it).getAdl().getAdjList();
45 for( std::list<Adjacent>::iterator itAdl=adl.begin();
46 itAdl!= adl.end() ; itAdl++)
47 {
48 string linkEndRouter=(*itAdl).getAdjacentName();
49 int col=pMap.getMappingNoByRouterName(linkEndRouter);
50 double cost=(*itAdl).getLinkCost();
51 if ( (row >= 0 && row<numOfRouter) && (col >= 0 && col<numOfRouter) )
52 {
53 adjMatrix[row][col]=cost;
54 }
55 }
akmhoque298385a2014-02-13 14:13:09 -060056
akmhoque1fd8c1e2014-02-19 19:41:49 -060057 }
58 }
akmhoque298385a2014-02-13 14:13:09 -060059
akmhoque1fd8c1e2014-02-19 19:41:49 -060060 void
61 RoutingTableCalculator::printAdjMatrix()
62 {
63 for(int i=0; i<numOfRouter; i++)
64 {
65 for(int j=0; j<numOfRouter; j++)
66 printf("%f ",adjMatrix[i][j]);
67 printf("\n");
68 }
69 }
akmhoque298385a2014-02-13 14:13:09 -060070
akmhoque1fd8c1e2014-02-19 19:41:49 -060071 void
72 RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost)
73 {
74 for ( int i = 0; i < numOfRouter; i++ )
75 {
76 if ( i == link )
77 {
78 adjMatrix[source][i]=linkCost;
79 }
80 else
81 {
82 adjMatrix[source][i]=0;
83 }
84 }
85 }
86
87 int
88 RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter)
89 {
90 int noLink=0;
91 for(int i=0; i<numOfRouter; i++)
92 {
93 if ( adjMatrix[sRouter][i] > 0 )
94 {
95 noLink++;
96 }
97 }
98 return noLink;
99 }
100
101 void
102 RoutingTableCalculator::getLinksFromAdjMatrix(int *links,
103 double *linkCosts, int source)
104 {
105 int j=0;
106 for (int i=0; i <numOfRouter; i++)
107 {
108 if ( adjMatrix[source][i] > 0 )
109 {
110 links[j]=i;
111 linkCosts[j]=adjMatrix[source][i];
112 j++;
113 }
114 }
115 }
116
117 void
118 RoutingTableCalculator::freeAdjMatrix()
119 {
120 for(int i = 0; i < numOfRouter; ++i)
121 {
122 delete [] adjMatrix[i];
123 }
124 delete [] adjMatrix;
125 }
akmhoque298385a2014-02-13 14:13:09 -0600126
127
akmhoque1fd8c1e2014-02-19 19:41:49 -0600128 void
129 RoutingTableCalculator::allocateLinks()
130 {
131 links=new int[vNoLink];
132 }
akmhoque298385a2014-02-13 14:13:09 -0600133
akmhoque1fd8c1e2014-02-19 19:41:49 -0600134 void RoutingTableCalculator::allocateLinkCosts()
135 {
136 linkCosts=new double[vNoLink];
137 }
akmhoque298385a2014-02-13 14:13:09 -0600138
akmhoqueb1710aa2014-02-19 17:13:36 -0600139
akmhoque1fd8c1e2014-02-19 19:41:49 -0600140 void
141 RoutingTableCalculator::freeLinks()
142 {
143 delete [] links;
144 }
145 void
146 RoutingTableCalculator::freeLinksCosts()
147 {
148 delete [] linkCosts;
149 }
akmhoque298385a2014-02-13 14:13:09 -0600150
akmhoque1fd8c1e2014-02-19 19:41:49 -0600151 void
152 LinkStateRoutingTableCalculator::calculatePath(Map& pMap,
153 RoutingTable& rt, Nlsr& pnlsr)
154 {
155 cout<<"LinkStateRoutingTableCalculator::calculatePath Called"<<endl;
156 allocateAdjMatrix();
157 initMatrix();
158 makeAdjMatrix(pnlsr,pMap);
159 cout<<pMap;
160 printAdjMatrix();
161 string routerName=pnlsr.getConfParameter().getRouterPrefix();
162 int sourceRouter=pMap.getMappingNoByRouterName(routerName);
163 int noLink=getNumOfLinkfromAdjMatrix(sourceRouter);
164 allocateParent();
165 allocateDistance();
akmhoque298385a2014-02-13 14:13:09 -0600166
akmhoque1fd8c1e2014-02-19 19:41:49 -0600167 if ( pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1 )
168 {
169 // Single Path
170 doDijkstraPathCalculation(sourceRouter);
171 // print all ls path -- debugging purpose
172 printAllLsPath(sourceRouter);
173 // update routing table
174 addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
175 }
176 else
177 {
178 // Multi Path
179 setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter));
180 allocateLinks();
181 allocateLinkCosts();
akmhoque298385a2014-02-13 14:13:09 -0600182
akmhoque1fd8c1e2014-02-19 19:41:49 -0600183 getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
184 for (int i=0 ; i < vNoLink; i++)
185 {
akmhoque298385a2014-02-13 14:13:09 -0600186
akmhoque1fd8c1e2014-02-19 19:41:49 -0600187 adjustAdMatrix(sourceRouter,links[i], linkCosts[i]);
188 printAdjMatrix();
189 doDijkstraPathCalculation(sourceRouter);
190 // print all ls path -- debugging purpose
191 printAllLsPath(sourceRouter);
192 //update routing table
193 addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
194 }
akmhoque298385a2014-02-13 14:13:09 -0600195
akmhoque1fd8c1e2014-02-19 19:41:49 -0600196 freeLinks();
197 freeLinksCosts();
198 }
akmhoque298385a2014-02-13 14:13:09 -0600199
akmhoque1fd8c1e2014-02-19 19:41:49 -0600200 freeParent();
201 freeDistance();
202 freeAdjMatrix();
203 }
akmhoque298385a2014-02-13 14:13:09 -0600204
akmhoque1fd8c1e2014-02-19 19:41:49 -0600205 void
206 LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter)
207 {
208 int i;
209 int v,u;
210 int *Q=new int[numOfRouter];
211 int head=0;
212 /* Initiate the Parent */
213 for (i = 0 ; i < numOfRouter; i++)
214 {
215 parent[i]=EMPTY_PARENT;
216 distance[i]=INF_DISTANCE;
217 Q[i]=i;
218 }
akmhoque298385a2014-02-13 14:13:09 -0600219
akmhoque1fd8c1e2014-02-19 19:41:49 -0600220 if ( sourceRouter != NO_MAPPING_NUM )
221 {
222 distance[sourceRouter]=0;
223 sortQueueByDistance(Q,distance,head,numOfRouter);
akmhoque298385a2014-02-13 14:13:09 -0600224
akmhoque1fd8c1e2014-02-19 19:41:49 -0600225 while (head < numOfRouter )
226 {
227 u=Q[head];
228 if(distance[u] == INF_DISTANCE)
229 {
230 break;
231 }
akmhoque298385a2014-02-13 14:13:09 -0600232
akmhoque1fd8c1e2014-02-19 19:41:49 -0600233 for(v=0 ; v <numOfRouter; v++)
234 {
235 if( adjMatrix[u][v] > 0 )
236 {
237 if ( isNotExplored(Q,v,head+1,numOfRouter) )
238 {
239 if( distance[u] + adjMatrix[u][v] < distance[v])
240 {
241 distance[v]=distance[u] + adjMatrix[u][v] ;
242 parent[v]=u;
243 }
akmhoque298385a2014-02-13 14:13:09 -0600244
akmhoque1fd8c1e2014-02-19 19:41:49 -0600245 }
akmhoque298385a2014-02-13 14:13:09 -0600246
akmhoque1fd8c1e2014-02-19 19:41:49 -0600247 }
akmhoque298385a2014-02-13 14:13:09 -0600248
akmhoque1fd8c1e2014-02-19 19:41:49 -0600249 }
akmhoque298385a2014-02-13 14:13:09 -0600250
akmhoque1fd8c1e2014-02-19 19:41:49 -0600251 head++;
252 sortQueueByDistance(Q,distance,head,numOfRouter);
253 }
254 }
255 delete [] Q;
256 }
akmhoque298385a2014-02-13 14:13:09 -0600257
akmhoque1fd8c1e2014-02-19 19:41:49 -0600258 void
259 LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(Nlsr& pnlsr,
260 RoutingTable& rt, Map& pMap, int sourceRouter)
261 {
262 cout<<"LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called";
263 cout<<endl;
264 for(int i=0; i < numOfRouter ; i++)
265 {
266 if ( i!= sourceRouter )
267 {
268 int nextHopRouter=getLsNextHop(i,sourceRouter);
269 double routeCost=distance[i];
270 string nextHopRouterName=pMap.getRouterNameByMappingNo(nextHopRouter);
271 int nxtHopFace=
272 pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
273 cout<<"Dest Router: "<<pMap.getRouterNameByMappingNo(i)<<endl;
274 cout<<"Next hop Router: "<<nextHopRouterName<<endl;
275 cout<<"Next hop Face: "<<nxtHopFace<<endl;
276 cout<<"Route Cost: "<<routeCost<<endl;
277 cout<<endl;
278 // Add next hop to routing table
279 NextHop nh(nxtHopFace,routeCost);
280 rt.addNextHop(pMap.getRouterNameByMappingNo(i),nh);
akmhoque298385a2014-02-13 14:13:09 -0600281
akmhoque1fd8c1e2014-02-19 19:41:49 -0600282 }
283 }
284 }
akmhoque298385a2014-02-13 14:13:09 -0600285
akmhoque1fd8c1e2014-02-19 19:41:49 -0600286 int
287 LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source)
288 {
289 int nextHop;
290 while ( parent[dest] != EMPTY_PARENT )
291 {
292 nextHop=dest;
293 dest=parent[dest];
akmhoque298385a2014-02-13 14:13:09 -0600294
akmhoque1fd8c1e2014-02-19 19:41:49 -0600295 }
akmhoque298385a2014-02-13 14:13:09 -0600296
akmhoque1fd8c1e2014-02-19 19:41:49 -0600297 if ( dest != source )
298 {
299 nextHop=NO_NEXT_HOP;
300 }
akmhoque298385a2014-02-13 14:13:09 -0600301
akmhoque1fd8c1e2014-02-19 19:41:49 -0600302 return nextHop;
303 }
akmhoque298385a2014-02-13 14:13:09 -0600304
akmhoque1fd8c1e2014-02-19 19:41:49 -0600305 void
306 LinkStateRoutingTableCalculator::printAllLsPath(int sourceRouter)
307 {
308 cout<<"LinkStateRoutingTableCalculator::printAllLsPath Called"<<endl;
309 cout<<"Source Router: "<<sourceRouter<<endl;
310 for(int i=0; i < numOfRouter ; i++)
311 {
312 if ( i!= sourceRouter )
313 {
314 printLsPath(i);
315 cout<<endl;
316 }
317 }
318 }
akmhoque298385a2014-02-13 14:13:09 -0600319
akmhoque1fd8c1e2014-02-19 19:41:49 -0600320 void
321 LinkStateRoutingTableCalculator::printLsPath(int destRouter)
322 {
323 if (parent[destRouter] != EMPTY_PARENT )
324 {
325 printLsPath(parent[destRouter]);
326 }
akmhoque298385a2014-02-13 14:13:09 -0600327
akmhoque1fd8c1e2014-02-19 19:41:49 -0600328 cout<<" "<<destRouter;
329 }
akmhoque298385a2014-02-13 14:13:09 -0600330
akmhoque1fd8c1e2014-02-19 19:41:49 -0600331 void
332 LinkStateRoutingTableCalculator::sortQueueByDistance(int *Q,
333 double *dist,int start,int element)
334 {
335 for ( int i=start ; i < element ; i ++)
336 {
337 for( int j=i+1; j<element; j ++)
338 {
339 if (dist[Q[j]] < dist[Q[i]])
340 {
341 int tempU=Q[j];
342 Q[j]=Q[i];
343 Q[i]=tempU;
344 }
345 }
346 }
akmhoque298385a2014-02-13 14:13:09 -0600347
akmhoque1fd8c1e2014-02-19 19:41:49 -0600348 }
akmhoque298385a2014-02-13 14:13:09 -0600349
akmhoque1fd8c1e2014-02-19 19:41:49 -0600350 int
351 LinkStateRoutingTableCalculator::isNotExplored(int *Q,
352 int u,int start, int element)
353 {
354 int ret=0;
355 for(int i=start; i< element; i++)
356 {
357 if ( Q[i] == u )
358 {
359 ret=1;
360 break;
361 }
362 }
363 return ret;
364 }
akmhoque298385a2014-02-13 14:13:09 -0600365
akmhoque1fd8c1e2014-02-19 19:41:49 -0600366 void
367 LinkStateRoutingTableCalculator::allocateParent()
368 {
369 parent=new int[numOfRouter];
370 }
371
372 void
373 LinkStateRoutingTableCalculator::allocateDistance()
374 {
375 distance= new double[numOfRouter];
376 }
377
378 void
379 LinkStateRoutingTableCalculator::freeParent()
380 {
381 delete [] parent;
382 }
383
384 void LinkStateRoutingTableCalculator::freeDistance()
385 {
386 delete [] distance;
387 }
akmhoque298385a2014-02-13 14:13:09 -0600388
389
390
akmhoque1fd8c1e2014-02-19 19:41:49 -0600391 void
392 HypRoutingTableCalculator::calculatePath(Map& pMap,
393 RoutingTable& rt, Nlsr& pnlsr)
394 {
395 makeAdjMatrix(pnlsr,pMap);
396 string routerName=pnlsr.getConfParameter().getRouterPrefix();
397 int sourceRouter=pMap.getMappingNoByRouterName(routerName);
398 int noLink=getNumOfLinkfromAdjMatrix(sourceRouter);
399 setNoLink(noLink);
400 allocateLinks();
401 allocateLinkCosts();
akmhoque298385a2014-02-13 14:13:09 -0600402
akmhoque1fd8c1e2014-02-19 19:41:49 -0600403 getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
akmhoque298385a2014-02-13 14:13:09 -0600404
akmhoque1fd8c1e2014-02-19 19:41:49 -0600405 for(int i=0 ; i < numOfRouter ; ++i)
406 {
407 int k=0;
408 if ( i != sourceRouter)
409 {
410 allocateLinkFaces();
411 allocateDistanceToNeighbor();
412 allocateDistFromNbrToDest();
akmhoque298385a2014-02-13 14:13:09 -0600413
akmhoque1fd8c1e2014-02-19 19:41:49 -0600414 for(int j=0; j<vNoLink; j++)
415 {
416 string nextHopRouterName=pMap.getRouterNameByMappingNo(links[j]);
417 int nextHopFace=
418 pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
419 double distToNbr=getHyperbolicDistance(pnlsr,pMap,
420 sourceRouter,links[j]);
421 double distToDestFromNbr=getHyperbolicDistance(pnlsr,
422 pMap,links[j],i);
423 if ( distToDestFromNbr >= 0 )
424 {
425 linkFaces[k] = nextHopFace;
426 distanceToNeighbor[k] = distToNbr;
427 distFromNbrToDest[k] = distToDestFromNbr;
428 k++;
429 }
430 }
akmhoque298385a2014-02-13 14:13:09 -0600431
akmhoque1fd8c1e2014-02-19 19:41:49 -0600432 addHypNextHopsToRoutingTable(pnlsr,pMap,rt,k,i);
akmhoque298385a2014-02-13 14:13:09 -0600433
akmhoque1fd8c1e2014-02-19 19:41:49 -0600434 freeLinkFaces();
435 freeDistanceToNeighbor();
436 freeDistFromNbrToDest();
437 }
438 }
akmhoque298385a2014-02-13 14:13:09 -0600439
akmhoque1fd8c1e2014-02-19 19:41:49 -0600440 freeLinks();
441 freeLinksCosts();
442 freeAdjMatrix();
443 }
akmhoque298385a2014-02-13 14:13:09 -0600444
akmhoque1fd8c1e2014-02-19 19:41:49 -0600445 void
446 HypRoutingTableCalculator::addHypNextHopsToRoutingTable(Nlsr& pnlsr,Map& pMap,
447 RoutingTable& rt, int noFaces, int dest)
448 {
449 for(int i=0 ; i < noFaces ; ++i)
450 {
451 string destRouter=pMap.getRouterNameByMappingNo(dest);
452 NextHop nh(linkFaces[i],distFromNbrToDest[i]);
453 rt.addNextHop(destRouter,nh);
454 if( isDryRun == 1 )
455 {
456 rt.addNextHopToDryTable(destRouter,nh);
457 }
458 }
akmhoque298385a2014-02-13 14:13:09 -0600459
akmhoque1fd8c1e2014-02-19 19:41:49 -0600460 }
akmhoque298385a2014-02-13 14:13:09 -0600461
akmhoque1fd8c1e2014-02-19 19:41:49 -0600462 double
463 HypRoutingTableCalculator::getHyperbolicDistance(Nlsr& pnlsr,
464 Map& pMap, int src, int dest)
465 {
466 double distance=0.0;
akmhoque298385a2014-02-13 14:13:09 -0600467
akmhoque1fd8c1e2014-02-19 19:41:49 -0600468 string srcRouterKey=pMap.getRouterNameByMappingNo(src)+"/3";
469 string destRouterKey=pMap.getRouterNameByMappingNo(dest)+"/3";
akmhoque298385a2014-02-13 14:13:09 -0600470
akmhoque1fd8c1e2014-02-19 19:41:49 -0600471 double srcRadius=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorRadius();
472 double srcTheta=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorTheta();
473
474 double destRadius=(pnlsr.getLsdb().getCorLsa(
475 destRouterKey).first).getCorRadius();
476 double destTheta=(pnlsr.getLsdb().getCorLsa(destRouterKey).first).getCorTheta();
akmhoque298385a2014-02-13 14:13:09 -0600477
478
akmhoque1fd8c1e2014-02-19 19:41:49 -0600479 double diffTheta = fabs (srcTheta - destTheta);
akmhoque298385a2014-02-13 14:13:09 -0600480
akmhoque1fd8c1e2014-02-19 19:41:49 -0600481 if (diffTheta > MATH_PI)
482 {
483 diffTheta = 2 * MATH_PI - diffTheta;
484 }
akmhoque298385a2014-02-13 14:13:09 -0600485
akmhoque1fd8c1e2014-02-19 19:41:49 -0600486 if ( srcRadius != -1 && destRadius != -1 )
487 {
488 if (diffTheta == 0)
489 distance = fabs (srcRadius - destRadius);
490 else
491 distance = acosh((cosh(srcRadius)*cosh(destRadius))-
492 (sinh(srcRadius)*sinh(destRadius)*cos(diffTheta)));
493 }
494 else
495 {
496 distance = -1;
497 }
akmhoque298385a2014-02-13 14:13:09 -0600498
akmhoque1fd8c1e2014-02-19 19:41:49 -0600499 return distance;
500 }
akmhoque298385a2014-02-13 14:13:09 -0600501
akmhoque1fd8c1e2014-02-19 19:41:49 -0600502 void
503 HypRoutingTableCalculator::allocateLinkFaces()
504 {
505 linkFaces=new int[vNoLink];
506 }
akmhoque298385a2014-02-13 14:13:09 -0600507
akmhoque1fd8c1e2014-02-19 19:41:49 -0600508 void
509 HypRoutingTableCalculator::allocateDistanceToNeighbor()
510 {
511 distanceToNeighbor=new double[vNoLink];
512 }
akmhoque298385a2014-02-13 14:13:09 -0600513
akmhoque1fd8c1e2014-02-19 19:41:49 -0600514 void
515 HypRoutingTableCalculator::allocateDistFromNbrToDest()
516 {
517 distFromNbrToDest=new double[vNoLink];
518 }
akmhoque298385a2014-02-13 14:13:09 -0600519
akmhoque1fd8c1e2014-02-19 19:41:49 -0600520 void
521 HypRoutingTableCalculator::freeLinkFaces()
522 {
523 delete [] linkFaces;
524 }
akmhoque298385a2014-02-13 14:13:09 -0600525
akmhoque1fd8c1e2014-02-19 19:41:49 -0600526 void
527 HypRoutingTableCalculator::freeDistanceToNeighbor()
528 {
529 delete [] distanceToNeighbor;
530 }
akmhoque298385a2014-02-13 14:13:09 -0600531
akmhoque1fd8c1e2014-02-19 19:41:49 -0600532 void
533 HypRoutingTableCalculator::freeDistFromNbrToDest()
534 {
535 delete [] distFromNbrToDest;
536 }
akmhoqueb1710aa2014-02-19 17:13:36 -0600537
538}//namespace nlsr