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