blob: 089808ab47d25a2c600812a3e79b089157754c48 [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
akmhoqueb1710aa2014-02-19 17:13:36 -060010namespace nlsr {
11
akmhoque298385a2014-02-13 14:13:09 -060012using namespace std;
13
14void
15RoutingTableCalculator::allocateAdjMatrix()
16{
17 adjMatrix = new double*[numOfRouter];
18 for(int i = 0; i < numOfRouter; ++i)
19 {
20 adjMatrix[i] = new double[numOfRouter];
21 }
22}
23
24void
25RoutingTableCalculator::initMatrix()
26{
27 for(int i=0;i<numOfRouter;i++)
28 {
29 for(int j=0;j<numOfRouter;j++)
30 adjMatrix[i][j]=0;
31 }
32}
33
34void
akmhoque1a481092014-02-19 16:34:22 -060035RoutingTableCalculator::makeAdjMatrix(Nlsr& pnlsr, Map pMap)
akmhoque298385a2014-02-13 14:13:09 -060036{
37 std::list<AdjLsa> adjLsdb=pnlsr.getLsdb().getAdjLsdb();
38 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
39 it!= adjLsdb.end() ; it++)
40 {
41 string linkStartRouter=(*it).getOrigRouter();
42 int row=pMap.getMappingNoByRouterName(linkStartRouter);
43 std::list<Adjacent> adl=(*it).getAdl().getAdjList();
44 for( std::list<Adjacent>::iterator itAdl=adl.begin();
45 itAdl!= adl.end() ; itAdl++)
46 {
47 string linkEndRouter=(*itAdl).getAdjacentName();
48 int col=pMap.getMappingNoByRouterName(linkEndRouter);
49 double cost=(*itAdl).getLinkCost();
50 if ( (row >= 0 && row<numOfRouter) && (col >= 0 && col<numOfRouter) )
51 {
52 adjMatrix[row][col]=cost;
53 }
54 }
55
56 }
57}
58
59void
60RoutingTableCalculator::printAdjMatrix()
61{
62 for(int i=0;i<numOfRouter;i++)
63 {
64 for(int j=0;j<numOfRouter;j++)
65 printf("%f ",adjMatrix[i][j]);
66 printf("\n");
67 }
68}
69
70void
71RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost)
72{
73 for ( int i = 0; i < numOfRouter; i++ ){
74 if ( i == link ){
75 adjMatrix[source][i]=linkCost;
76 }
77 else{
78 adjMatrix[source][i]=0;
79 }
80 }
81}
82
83int
84RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter)
85{
86 int noLink=0;
87 for(int i=0;i<numOfRouter;i++)
88 {
89 if ( adjMatrix[sRouter][i] > 0 )
90 {
91 noLink++;
92 }
93 }
94 return noLink;
95}
96
97void
98RoutingTableCalculator::getLinksFromAdjMatrix(int *links,
99 double *linkCosts, int source)
100{
101 int j=0;
102 for (int i=0; i <numOfRouter; i++)
103 {
104 if ( adjMatrix[source][i] > 0 )
105 {
106 links[j]=i;
107 linkCosts[j]=adjMatrix[source][i];
108 j++;
109 }
110 }
111}
112
113void
114RoutingTableCalculator::freeAdjMatrix()
115{
116 for(int i = 0; i < numOfRouter; ++i)
117 {
118 delete [] adjMatrix[i];
119 }
120 delete [] adjMatrix;
121}
122
123
124void
125RoutingTableCalculator::allocateLinks()
126{
127 links=new int[vNoLink];
128}
129
130void RoutingTableCalculator::allocateLinkCosts()
131{
132 linkCosts=new double[vNoLink];
133}
134
akmhoqueb1710aa2014-02-19 17:13:36 -0600135
akmhoque298385a2014-02-13 14:13:09 -0600136void
137RoutingTableCalculator::freeLinks()
138{
139 delete [] links;
140}
141void
142RoutingTableCalculator::freeLinksCosts()
143{
144 delete [] linkCosts;
145}
146
147void
148LinkStateRoutingTableCalculator::calculatePath(Map& pMap,
akmhoque1a481092014-02-19 16:34:22 -0600149 RoutingTable& rt, Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -0600150{
151 cout<<"LinkStateRoutingTableCalculator::calculatePath Called"<<endl;
152 allocateAdjMatrix();
153 initMatrix();
154 makeAdjMatrix(pnlsr,pMap);
155 cout<<pMap;
156 printAdjMatrix();
157 string routerName=pnlsr.getConfParameter().getRouterPrefix();
158 int sourceRouter=pMap.getMappingNoByRouterName(routerName);
159 int noLink=getNumOfLinkfromAdjMatrix(sourceRouter);
160 allocateParent();
161 allocateDistance();
162
163 if ( pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1 )
164 {
165 // Single Path
166 doDijkstraPathCalculation(sourceRouter);
167 // print all ls path -- debugging purpose
168 printAllLsPath(sourceRouter);
169 // update routing table
170 addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
171 }
172 else
173 {
174 // Multi Path
175 setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter));
176 allocateLinks();
177 allocateLinkCosts();
178
179 getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
180 for (int i=0 ; i < vNoLink; i++)
181 {
182
183 adjustAdMatrix(sourceRouter,links[i], linkCosts[i]);
184 printAdjMatrix();
185 doDijkstraPathCalculation(sourceRouter);
186 // print all ls path -- debugging purpose
187 printAllLsPath(sourceRouter);
188 //update routing table
189 addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
190 }
191
192 freeLinks();
193 freeLinksCosts();
194 }
195
196 freeParent();
197 freeDistance();
198 freeAdjMatrix();
199}
200
201void
202LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter)
203{
204 int i;
205 int v,u;
206 int *Q=new int[numOfRouter];
207 int head=0;
208 /* Initiate the Parent */
209 for (i = 0 ; i < numOfRouter; i++){
210 parent[i]=EMPTY_PARENT;
211 distance[i]=INF_DISTANCE;
212 Q[i]=i;
213 }
214
215 if ( sourceRouter != NO_MAPPING_NUM ){
216 distance[sourceRouter]=0;
217 sortQueueByDistance(Q,distance,head,numOfRouter);
218
219 while (head < numOfRouter )
220 {
221 u=Q[head];
222 if(distance[u] == INF_DISTANCE)
223 {
224 break;
225 }
226
227 for(v=0 ; v <numOfRouter; v++)
228 {
229 if( adjMatrix[u][v] > 0 )
230 {
231 if ( isNotExplored(Q,v,head+1,numOfRouter) )
232 {
233 if( distance[u] + adjMatrix[u][v] < distance[v])
234 {
235 distance[v]=distance[u] + adjMatrix[u][v] ;
236 parent[v]=u;
237 }
238
239 }
240
241 }
242
243 }
244
245 head++;
246 sortQueueByDistance(Q,distance,head,numOfRouter);
247 }
248 }
249 delete [] Q;
250}
251
252void
akmhoque1a481092014-02-19 16:34:22 -0600253LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(Nlsr& pnlsr,
akmhoque298385a2014-02-13 14:13:09 -0600254 RoutingTable& rt, Map& pMap, int sourceRouter)
255{
256 cout<<"LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called";
257 cout<<endl;
258 for(int i=0; i < numOfRouter ; i++)
259 {
260 if ( i!= sourceRouter )
261 {
262 int nextHopRouter=getLsNextHop(i,sourceRouter);
263 double routeCost=distance[i];
264 string nextHopRouterName=pMap.getRouterNameByMappingNo(nextHopRouter);
265 int nxtHopFace=
266 pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
267 cout<<"Dest Router: "<<pMap.getRouterNameByMappingNo(i)<<endl;
268 cout<<"Next hop Router: "<<nextHopRouterName<<endl;
269 cout<<"Next hop Face: "<<nxtHopFace<<endl;
270 cout<<"Route Cost: "<<routeCost<<endl;
271 cout<<endl;
272 // Add next hop to routing table
273 NextHop nh(nxtHopFace,routeCost);
274 rt.addNextHop(pMap.getRouterNameByMappingNo(i),nh);
275
276 }
277 }
278}
279
280int
281LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source)
282{
283 int nextHop;
284 while ( parent[dest] != EMPTY_PARENT )
285 {
286 nextHop=dest;
287 dest=parent[dest];
288
289 }
290
291 if ( dest != source )
292 {
293 nextHop=NO_NEXT_HOP;
294 }
295
296 return nextHop;
297}
298
299void
300LinkStateRoutingTableCalculator::printAllLsPath(int sourceRouter)
301{
302 cout<<"LinkStateRoutingTableCalculator::printAllLsPath Called"<<endl;
303 cout<<"Source Router: "<<sourceRouter<<endl;
304 for(int i=0; i < numOfRouter ; i++)
305 {
306 if ( i!= sourceRouter )
307 {
308 printLsPath(i);
309 cout<<endl;
310 }
311 }
312}
313
314void
315LinkStateRoutingTableCalculator::printLsPath(int destRouter)
316{
317 if (parent[destRouter] != EMPTY_PARENT )
318 {
319 printLsPath(parent[destRouter]);
320 }
321
322 cout<<" "<<destRouter;
323}
324
325void
326LinkStateRoutingTableCalculator::sortQueueByDistance(int *Q,
327 double *dist,int start,int element)
328{
329 for ( int i=start ; i < element ; i ++)
330 {
331 for( int j=i+1; j<element; j ++)
332 {
333 if (dist[Q[j]] < dist[Q[i]])
334 {
335 int tempU=Q[j];
336 Q[j]=Q[i];
337 Q[i]=tempU;
338 }
339 }
340 }
341
342}
343
344int
345LinkStateRoutingTableCalculator::isNotExplored(int *Q,
346 int u,int start, int element)
347{
348 int ret=0;
349 for(int i=start; i< element; i++)
350 {
351 if ( Q[i] == u )
352 {
353 ret=1;
354 break;
355 }
356 }
357 return ret;
358}
359
360void
361LinkStateRoutingTableCalculator::allocateParent()
362{
363 parent=new int[numOfRouter];
364}
365
366void
367LinkStateRoutingTableCalculator::allocateDistance()
368{
369 distance= new double[numOfRouter];
370}
371
372void
373LinkStateRoutingTableCalculator::freeParent()
374{
375 delete [] parent;
376}
377
378void LinkStateRoutingTableCalculator::freeDistance()
379{
380 delete [] distance;
381}
382
383
384
385void
386HypRoutingTableCalculator::calculatePath(Map& pMap,
akmhoque1a481092014-02-19 16:34:22 -0600387 RoutingTable& rt, Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -0600388{
389 makeAdjMatrix(pnlsr,pMap);
390 string routerName=pnlsr.getConfParameter().getRouterPrefix();
391 int sourceRouter=pMap.getMappingNoByRouterName(routerName);
392 int noLink=getNumOfLinkfromAdjMatrix(sourceRouter);
393 setNoLink(noLink);
394 allocateLinks();
395 allocateLinkCosts();
396
397 getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
398
399 for(int i=0 ; i < numOfRouter ; ++i)
400 {
401 int k=0;
402 if ( i != sourceRouter)
403 {
404 allocateLinkFaces();
405 allocateDistanceToNeighbor();
406 allocateDistFromNbrToDest();
407
408 for(int j=0; j<vNoLink; j++)
409 {
410 string nextHopRouterName=pMap.getRouterNameByMappingNo(links[j]);
411 int nextHopFace=
412 pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
413 double distToNbr=getHyperbolicDistance(pnlsr,pMap,
414 sourceRouter,links[j]);
415 double distToDestFromNbr=getHyperbolicDistance(pnlsr,
416 pMap,links[j],i);
417 if ( distToDestFromNbr >= 0 )
418 {
419 linkFaces[k] = nextHopFace;
420 distanceToNeighbor[k] = distToNbr;
421 distFromNbrToDest[k] = distToDestFromNbr;
422 k++;
423 }
424 }
425
426 addHypNextHopsToRoutingTable(pnlsr,pMap,rt,k,i);
427
428 freeLinkFaces();
429 freeDistanceToNeighbor();
430 freeDistFromNbrToDest();
431 }
432 }
433
434 freeLinks();
435 freeLinksCosts();
436 freeAdjMatrix();
437}
438
439void
akmhoque1a481092014-02-19 16:34:22 -0600440HypRoutingTableCalculator::addHypNextHopsToRoutingTable(Nlsr& pnlsr,Map& pMap,
akmhoque298385a2014-02-13 14:13:09 -0600441 RoutingTable& rt, int noFaces, int dest)
442{
443 for(int i=0 ; i < noFaces ; ++i)
444 {
445 string destRouter=pMap.getRouterNameByMappingNo(dest);
446 NextHop nh(linkFaces[i],distFromNbrToDest[i]);
447 rt.addNextHop(destRouter,nh);
448 if( isDryRun == 1 )
449 {
450 rt.addNextHopToDryTable(destRouter,nh);
451 }
452 }
453
454}
455
456double
akmhoque1a481092014-02-19 16:34:22 -0600457HypRoutingTableCalculator::getHyperbolicDistance(Nlsr& pnlsr,
akmhoque298385a2014-02-13 14:13:09 -0600458 Map& pMap, int src, int dest)
459{
460 double distance=0.0;
461
462 string srcRouterKey=pMap.getRouterNameByMappingNo(src)+"/3";
463 string destRouterKey=pMap.getRouterNameByMappingNo(dest)+"/3";
464
465 double srcRadius=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorRadius();
466 double srcTheta=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorTheta();
467
468 double destRadius=(pnlsr.getLsdb().getCorLsa(destRouterKey).first).getCorRadius();
469 double destTheta=(pnlsr.getLsdb().getCorLsa(destRouterKey).first).getCorTheta();
470
471
472 double diffTheta = fabs (srcTheta - destTheta);
473
474 if (diffTheta > MATH_PI)
475 {
476 diffTheta = 2 * MATH_PI - diffTheta;
477 }
478
479 if ( srcRadius != -1 && destRadius != -1 )
480 {
481 if (diffTheta == 0)
482 distance = fabs (srcRadius - destRadius);
483 else
484 distance = acosh((cosh(srcRadius)*cosh(destRadius))-
485 (sinh(srcRadius)*sinh(destRadius)*cos(diffTheta)));
486 }else
487 {
488 distance = -1;
489 }
490
491 return distance;
492}
493
494void
495HypRoutingTableCalculator::allocateLinkFaces()
496{
497 linkFaces=new int[vNoLink];
498}
499
500void
501HypRoutingTableCalculator::allocateDistanceToNeighbor()
502{
503 distanceToNeighbor=new double[vNoLink];
504}
505
506void
507HypRoutingTableCalculator::allocateDistFromNbrToDest()
508{
509 distFromNbrToDest=new double[vNoLink];
510}
511
512void
513HypRoutingTableCalculator::freeLinkFaces()
514{
515 delete [] linkFaces;
516}
517
518void
519HypRoutingTableCalculator::freeDistanceToNeighbor()
520{
521 delete [] distanceToNeighbor;
522}
523
524void
525HypRoutingTableCalculator::freeDistFromNbrToDest()
526{
527 delete [] distFromNbrToDest;
528}
akmhoqueb1710aa2014-02-19 17:13:36 -0600529
530}//namespace nlsr