-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopologyInstance.java
More file actions
1532 lines (1329 loc) · 61.2 KB
/
Copy pathTopologyInstance.java
File metadata and controls
1532 lines (1329 loc) · 61.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**::
* Copyright 2013, Big Switch Networks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
**/
package net.floodlightcontroller.topology;
import com.google.common.collect.ImmutableSet;
import net.floodlightcontroller.core.IOFSwitch;
import net.floodlightcontroller.core.types.NodePortTuple;
import net.floodlightcontroller.linkdiscovery.Link;
import net.floodlightcontroller.routing.BroadcastTree;
import net.floodlightcontroller.routing.Path;
import net.floodlightcontroller.routing.PathId;
import net.floodlightcontroller.statistics.SwitchPortBandwidth;
import net.floodlightcontroller.util.ClusterDFS;
import org.projectfloodlight.openflow.protocol.OFPortDesc;
import org.projectfloodlight.openflow.types.DatapathId;
import org.projectfloodlight.openflow.types.OFPort;
import org.projectfloodlight.openflow.types.U64;
import org.python.google.common.collect.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ThreadLocalRandom;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
/**
* A representation of a network topology. Used internally by
* {@link TopologyManager}
*/
public class TopologyInstance {
public static final short LT_SH_LINK = 1;
public static final short LT_BD_LINK = 2;
public static final short LT_TUNNEL = 3;
public static final int MAX_LINK_WEIGHT = 10000;
public static final int MAX_PATH_WEIGHT = Integer.MAX_VALUE - MAX_LINK_WEIGHT - 1;
public static final int PATH_CACHE_SIZE = 1000;
private static final Logger log = LoggerFactory.getLogger(TopologyInstance.class);
/* Global: general switch, port, link */
private Set<DatapathId> switches;
private Map<DatapathId, Set<OFPort>> portsWithLinks; /* only ports with links */
private Map<DatapathId, Set<OFPort>> portsPerSwitch; /* every port on the switch */
private Set<NodePortTuple> portsTunnel; /* all tunnel ports in topology */
private Set<NodePortTuple> portsBroadcastAll; /* all broadcast ports in topology */
private Map<DatapathId, Set<OFPort>> portsBroadcastPerSwitch; /* broadcast ports mapped per DPID */
private Set<NodePortTuple> portsWithMoreThanTwoLinks; /* a.k.a. "broadcast domain" non-P2P ports */
private Map<NodePortTuple, Set<Link>> links; /* every link in entire topology */
private Map<NodePortTuple, Set<Link>> linksNonBcastNonTunnel; /* only non-broadcast and non-tunnel links */
private Map<NodePortTuple, Set<Link>> linksExternal; /* BDDP links b/t clusters */
private Set<Link> linksNonExternalInterCluster;
private Map<Link,Integer> MalinkCost;
private boolean WP;
/* Blocked */
private Set<NodePortTuple> portsBlocked;
private Set<Link> linksBlocked;
/* Per-cluster */
private Set<Cluster> clusters;
private Map<DatapathId, Set<NodePortTuple>> clusterPorts; /* ports in the cluster ID */
private Map<DatapathId, Cluster> clusterFromSwitch; /* cluster for each switch */
/* Per-archipelago */
private Set<Archipelago> archipelagos; /* connected clusters */
private Map<Cluster, Archipelago> archipelagoFromCluster;
private Map<DatapathId, Set<NodePortTuple>> portsBroadcastPerArchipelago; /* broadcast ports in each archipelago ID */
private Map<PathId, List<Path>> pathcache; /* contains computed paths ordered best to worst */
protected TopologyInstance(Map<DatapathId, Set<OFPort>> portsWithLinks,
Set<NodePortTuple> portsBlocked,
Map<NodePortTuple, Set<Link>> linksNonBcastNonTunnel,
Set<NodePortTuple> portsWithMoreThanTwoLinks,
Set<NodePortTuple> portsTunnel,
Map<NodePortTuple, Set<Link>> links,
Map<DatapathId, Set<OFPort>> portsPerSwitch,
Map<NodePortTuple, Set<Link>> linksExternal) {
MalinkCost = new HashMap<Link, Integer>();
this.switches = new HashSet<DatapathId>(portsWithLinks.keySet());
this.portsWithLinks = new HashMap<DatapathId, Set<OFPort>>();
for (DatapathId sw : portsWithLinks.keySet()) {
this.portsWithLinks.put(sw, new HashSet<OFPort>(portsWithLinks.get(sw)));
}
this.portsPerSwitch = new HashMap<DatapathId, Set<OFPort>>();
for (DatapathId sw : portsPerSwitch.keySet()) {
this.portsPerSwitch.put(sw, new HashSet<OFPort>(portsPerSwitch.get(sw)));
}
this.portsBlocked = new HashSet<NodePortTuple>(portsBlocked);
this.linksNonBcastNonTunnel = new HashMap<NodePortTuple, Set<Link>>();
for (NodePortTuple npt : linksNonBcastNonTunnel.keySet()) {
this.linksNonBcastNonTunnel.put(npt, new HashSet<Link>(linksNonBcastNonTunnel.get(npt)));
}
this.links = new HashMap<NodePortTuple, Set<Link>>();
for (NodePortTuple npt : links.keySet()) {
this.links.put(npt, new HashSet<Link>(links.get(npt)));
}
this.linksExternal = new HashMap<NodePortTuple, Set<Link>>();
for (NodePortTuple npt : linksExternal.keySet()) {
this.linksExternal.put(npt, new HashSet<Link>(linksExternal.get(npt)));
}
this.linksNonExternalInterCluster = new HashSet<Link>();
this.archipelagos = new HashSet<Archipelago>();
this.portsWithMoreThanTwoLinks = new HashSet<NodePortTuple>(portsWithMoreThanTwoLinks);
this.portsTunnel = new HashSet<NodePortTuple>(portsTunnel);
this.linksBlocked = new HashSet<Link>();
this.clusters = new HashSet<Cluster>();
this.clusterFromSwitch = new HashMap<DatapathId, Cluster>();
this.portsBroadcastAll= new HashSet<NodePortTuple>();
this.portsBroadcastPerSwitch = new HashMap<DatapathId,Set<OFPort>>();
this.pathcache = new HashMap<PathId, List<Path>>();
this.portsBroadcastPerArchipelago = new HashMap<DatapathId, Set<NodePortTuple>>();
this.archipelagoFromCluster = new HashMap<Cluster, Archipelago>();
this.WP = false;
}
protected void compute() {
/*
* Step 1: Compute clusters ignoring ports with > 2 links and
* blocked links.
*/
identifyClusters();
/*
* Step 2: Associate non-blocked links within clusters to the cluster
* in which they reside. The remaining links are inter-cluster links.
*/
identifyIntraClusterLinks();
/*
* Step 3: Compute the archipelagos. (Def: group of conneccted clusters)
* Each archipelago will have its own broadcast tree, chosen by running
* dijkstra's algorithm from the archipelago ID switch (lowest switch
* DPID). We need a broadcast tree per archipelago since each
* archipelago is by definition isolated from all other archipelagos.
*/
identifyArchipelagos();
/*
* Step 4: Use Yens algorithm to permute through each node combination
* within each archipelago and compute multiple paths. The shortest
* path located (i.e. first run of dijkstra's algorithm) will be used
* as the broadcast tree for the archipelago.
*/
computeOrderedPaths();
/*
* Step 5: Determine the broadcast ports for each archipelago. These are
* the ports that reside on the broadcast tree computed and saved when
* performing path-finding. These are saved into multiple data structures
* to aid in quick lookup per archipelago, per-switch, and topology-global.
*/
computeBroadcastPortsPerArchipelago();
/*
* Step 6: Optionally, print topology to log for added verbosity or when debugging.
*/
printTopology();
}
/*
* Checks if OF port is edge port
*/
public boolean isEdge(DatapathId sw, OFPort portId) {
NodePortTuple np = new NodePortTuple(sw, portId);
if (links.get(np) == null || links.get(np).isEmpty()) {
return true;
}
else {
return false;
}
}
/*
* Returns broadcast ports for the given DatapathId
*/
public Set<OFPort> swBroadcastPorts(DatapathId sw) {
if (!portsBroadcastPerSwitch.containsKey(sw) || portsBroadcastPerSwitch.get(sw) == null) {
log.debug("Could not locate broadcast ports for switch {}", sw);
return Collections.emptySet();
} else {
if (log.isDebugEnabled()) {
log.debug("Found broadcast ports {} for switch {}", portsBroadcastPerSwitch.get(sw), sw);
}
return portsBroadcastPerSwitch.get(sw);
}
}
private void printTopology() {
log.debug("-----------------Topology-----------------------");
log.debug("All Links: {}", links);
log.debug("Tunnel Ports: {}", portsTunnel);
log.debug("Clusters: {}", clusters);
log.debug("Broadcast Ports Per Node (!!): {}", portsBroadcastPerSwitch);
log.debug("3+ Link Ports: {}", portsWithMoreThanTwoLinks);
log.debug("Archipelagos: {}", archipelagos);
log.debug("-----------------------------------------------");
}
private void identifyIntraClusterLinks() {
for (DatapathId s : switches) {
if (portsWithLinks.get(s) == null) continue;
for (OFPort p : portsWithLinks.get(s)) {
NodePortTuple np = new NodePortTuple(s, p);
if (linksNonBcastNonTunnel.get(np) == null) continue;
if (isBroadcastPort(np)) continue;
for (Link l : linksNonBcastNonTunnel.get(np)) {
if (isBlockedLink(l)) continue;
if (isBroadcastLink(l)) continue;
Cluster c1 = clusterFromSwitch.get(l.getSrc());
Cluster c2 = clusterFromSwitch.get(l.getDst());
if (c1 == c2) {
c1.addLink(l); /* link is within cluster */
} else {
linksNonExternalInterCluster.add(l);
}
}
}
}
}
/**
* @author Srinivasan Ramasubramanian
*
* This function divides the network into clusters. Every cluster is
* a strongly connected component. The network may contain unidirectional
* links. The function calls dfsTraverse for performing depth first
* search and cluster formation.
*
* The computation of strongly connected components is based on
* Tarjan's algorithm. For more details, please see the Wikipedia
* link below.
*
* http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
*/
private void identifyClusters() {
Map<DatapathId, ClusterDFS> dfsList = new HashMap<DatapathId, ClusterDFS>();
if (switches == null) return;
for (DatapathId key : switches) {
ClusterDFS cdfs = new ClusterDFS();
dfsList.put(key, cdfs);
}
Set<DatapathId> currSet = new HashSet<DatapathId>();
for (DatapathId sw : switches) {
ClusterDFS cdfs = dfsList.get(sw);
if (cdfs == null) {
log.error("No DFS object for switch {} found.", sw);
} else if (!cdfs.isVisited()) {
dfsTraverse(0, 1, sw, dfsList, currSet);
}
}
}
/**
* @author Srinivasan Ramasubramanian
*
* This algorithm computes the depth first search (DFS) traversal of the
* switches in the network, computes the lowpoint, and creates clusters
* (of strongly connected components).
*
* The computation of strongly connected components is based on
* Tarjan's algorithm. For more details, please see the Wikipedia
* link below.
*
* http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
*
* The initialization of lowpoint and the check condition for when a
* cluster should be formed is modified as we do not remove switches that
* are already part of a cluster.
*
* A return value of -1 indicates that dfsTraverse failed somewhere in the middle
* of computation. This could happen when a switch is removed during the cluster
* computation procedure.
*
* @param parentIndex: DFS index of the parent node
* @param currIndex: DFS index to be assigned to a newly visited node
* @param currSw: ID of the current switch
* @param dfsList: HashMap of DFS data structure for each switch
* @param currSet: Set of nodes in the current cluster in formation
* @return long: DSF index to be used when a new node is visited
*/
private long dfsTraverse (long parentIndex, long currIndex, DatapathId currSw,
Map<DatapathId, ClusterDFS> dfsList, Set<DatapathId> currSet) {
//Get the DFS object corresponding to the current switch
ClusterDFS currDFS = dfsList.get(currSw);
Set<DatapathId> nodesInMyCluster = new HashSet<DatapathId>();
Set<DatapathId> myCurrSet = new HashSet<DatapathId>();
//Assign the DFS object with right values.
currDFS.setVisited(true);
currDFS.setDfsIndex(currIndex);
currDFS.setParentDFSIndex(parentIndex);
currIndex++;
// Traverse the graph through every outgoing link.
if (portsWithLinks.get(currSw) != null){
for (OFPort p : portsWithLinks.get(currSw)) {
Set<Link> lset = linksNonBcastNonTunnel.get(new NodePortTuple(currSw, p));
if (lset == null) continue;
for (Link l : lset) {
DatapathId dstSw = l.getDst();
// ignore incoming links.
if (dstSw.equals(currSw)) continue;
// ignore if the destination is already added to
// another cluster
if (clusterFromSwitch.get(dstSw) != null) continue;
// ignore the link if it is blocked.
if (isBlockedLink(l)) continue;
// ignore this link if it is in broadcast domain
if (isBroadcastLink(l)) continue;
// Get the DFS object corresponding to the dstSw
ClusterDFS dstDFS = dfsList.get(dstSw);
if (dstDFS.getDfsIndex() < currDFS.getDfsIndex()) {
// could be a potential lowpoint
if (dstDFS.getDfsIndex() < currDFS.getLowpoint()) {
currDFS.setLowpoint(dstDFS.getDfsIndex());
}
} else if (!dstDFS.isVisited()) {
// make a DFS visit
currIndex = dfsTraverse(
currDFS.getDfsIndex(),
currIndex, dstSw,
dfsList, myCurrSet);
if (currIndex < 0) return -1;
// update lowpoint after the visit
if (dstDFS.getLowpoint() < currDFS.getLowpoint()) {
currDFS.setLowpoint(dstDFS.getLowpoint());
}
nodesInMyCluster.addAll(myCurrSet);
myCurrSet.clear();
}
// else, it is a node already visited with a higher
// dfs index, just ignore.
}
}
}
nodesInMyCluster.add(currSw);
currSet.addAll(nodesInMyCluster);
// Cluster computation.
// If the node's lowpoint is greater than its parent's DFS index,
// we need to form a new cluster with all the switches in the
// currSet.
if (currDFS.getLowpoint() > currDFS.getParentDFSIndex()) {
// The cluster thus far forms a strongly connected component.
// create a new switch cluster and the switches in the current
// set to the switch cluster.
Cluster sc = new Cluster();
for (DatapathId sw : currSet) {
sc.add(sw);
clusterFromSwitch.put(sw, sc);
}
// delete all the nodes in the current set.
currSet.clear();
// add the newly formed switch clusters to the cluster set.
clusters.add(sc);
}
return currIndex;
}
public Set<NodePortTuple> getBlockedPorts() {
return this.portsBlocked;
}
public Set<Link> getBlockedLinks() {
return this.linksBlocked;
}
/** Returns true if a link has either one of its switch ports
* blocked.
* @param l
* @return
*/
public boolean isBlockedLink(Link l) {
NodePortTuple n1 = new NodePortTuple(l.getSrc(), l.getSrcPort());
NodePortTuple n2 = new NodePortTuple(l.getDst(), l.getDstPort());
return (isBlockedPort(n1) || isBlockedPort(n2));
}
public boolean isBlockedPort(NodePortTuple npt) {
return portsBlocked.contains(npt);
}
public boolean isTunnelPort(NodePortTuple npt) {
return portsTunnel.contains(npt);
}
public boolean isTunnelLink(Link l) {
NodePortTuple n1 = new NodePortTuple(l.getSrc(), l.getSrcPort());
NodePortTuple n2 = new NodePortTuple(l.getDst(), l.getDstPort());
return (isTunnelPort(n1) || isTunnelPort(n2));
}
public boolean isBroadcastLink(Link l) {
NodePortTuple n1 = new NodePortTuple(l.getSrc(), l.getSrcPort());
NodePortTuple n2 = new NodePortTuple(l.getDst(), l.getDstPort());
return (isBroadcastPort(n1) || isBroadcastPort(n2));
}
public boolean isBroadcastPort(NodePortTuple npt) {
return portsBroadcastAll.contains(npt);
}
private class NodeDist implements Comparable<NodeDist> {
private final DatapathId node;
public DatapathId getNode() {
return node;
}
private final int dist;
public int getDist() {
return dist;
}
public NodeDist(DatapathId node, int dist) {
this.node = node;
this.dist = dist;
}
@Override
public int compareTo(NodeDist o) {
if (o.dist == this.dist) {
return (int)(this.node.getLong() - o.node.getLong());
}
return this.dist - o.dist;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
NodeDist other = (NodeDist) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (node == null) {
if (other.node != null)
return false;
} else if (!node.equals(other.node))
return false;
return true;
}
@Override
public int hashCode() {
assert false : "hashCode not designed";
return 42;
}
private TopologyInstance getOuterType() {
return TopologyInstance.this;
}
}
protected void identifyArchipelagos() {
// Iterate through each external link and create/merge archipelagos based on the
// islands that each link is connected to
Cluster srcCluster = null;
Cluster dstCluster = null;
Archipelago srcArchipelago = null;
Archipelago dstArchipelago = null;
Set<Link> links = new HashSet<Link>();
for (Set<Link> linkset : linksExternal.values()) {
links.addAll(linkset);
}
links.addAll(linksNonExternalInterCluster);
/* Base case of 1:1 mapping b/t clusters and archipelagos */
if (links.isEmpty()) {
if (!clusters.isEmpty()) {
for (Cluster c: clusters) {
Archipelago a = new Archipelago().add(c);
archipelagos.add(a);
archipelagoFromCluster.put(c, a);
}
}
} else { /* Only for two or more adjacent clusters that form archipelagos */
for (Link l : links) {
for (Cluster c : clusters) {
if (c.getNodes().contains(l.getSrc())) srcCluster = c;
if (c.getNodes().contains(l.getDst())) dstCluster = c;
}
for (Archipelago a : archipelagos) {
// Is source cluster a part of an existing archipelago?
if (a.isMember(srcCluster)) srcArchipelago = a;
// Is destination cluster a part of an existing archipelago?
if (a.isMember(dstCluster)) dstArchipelago = a;
}
// Are they both found in an archipelago? If so, then merge the two.
if (srcArchipelago != null && dstArchipelago != null && !srcArchipelago.equals(dstArchipelago)) {
archipelagos.remove(srcArchipelago);
srcArchipelago.merge(dstArchipelago);
archipelagos.add(srcArchipelago);
archipelagos.remove(dstArchipelago);
archipelagoFromCluster.put(dstCluster, srcArchipelago);
}
// If neither were found in an existing, then form a new archipelago.
else if (srcArchipelago == null && dstArchipelago == null) {
Archipelago a = new Archipelago().add(srcCluster).add(dstCluster);
archipelagos.add(a);
archipelagoFromCluster.put(srcCluster, a);
archipelagoFromCluster.put(dstCluster, a);
}
// If only one is found in an existing, then add the one not found to the existing.
else if (srcArchipelago != null && dstArchipelago == null) {
archipelagos.remove(srcArchipelago);
srcArchipelago.add(dstCluster);
archipelagos.add(srcArchipelago);
archipelagoFromCluster.put(dstCluster, srcArchipelago);
}
else if (srcArchipelago == null && dstArchipelago != null) {
archipelagos.remove(dstArchipelago);
dstArchipelago.add(srcCluster);
archipelagos.add(dstArchipelago);
archipelagoFromCluster.put(srcCluster, dstArchipelago);
}
srcCluster = null;
dstCluster = null;
srcArchipelago = null;
dstArchipelago = null;
}
}
}
/*
* Dijkstra that calculates destination rooted trees over the entire topology.
*/
private BroadcastTree dijkstra(Map<DatapathId, Set<Link>> links, DatapathId root,
Map<Link, Integer> linkCost,
boolean isDstRooted) {
HashMap<DatapathId, Link> nexthoplinks = new HashMap<DatapathId, Link>();
HashMap<DatapathId, Integer> cost = new HashMap<DatapathId, Integer>();
int w;
for (DatapathId node : links.keySet()) {
nexthoplinks.put(node, null);
cost.put(node, MAX_PATH_WEIGHT);
//log.debug("Added max cost to {}", node);
}
HashMap<DatapathId, Boolean> seen = new HashMap<DatapathId, Boolean>();
PriorityQueue<NodeDist> nodeq = new PriorityQueue<NodeDist>();
nodeq.add(new NodeDist(root, 0));
cost.put(root, 0);
//log.debug("{}", links);
while (nodeq.peek() != null) {
NodeDist n = nodeq.poll();
DatapathId cnode = n.getNode();
int cdist = n.getDist();
if (cdist >= MAX_PATH_WEIGHT) break;
if (seen.containsKey(cnode)) continue;
seen.put(cnode, true);
//log.debug("cnode {} and links {}", cnode, links.get(cnode));
if (links.get(cnode) == null) continue;
for (Link link : links.get(cnode)) {
DatapathId neighbor;
if (isDstRooted == true) {
neighbor = link.getSrc();
} else {
neighbor = link.getDst();
}
// links directed toward cnode will result in this condition
if (neighbor.equals(cnode)) continue;
if (seen.containsKey(neighbor)) continue;
if (linkCost == null || linkCost.get(link) == null) {
w = 1;
} else {
w = linkCost.get(link);
}
int ndist = cdist + w; // the weight of the link, always 1 in current version of floodlight.
log.debug("Neighbor: {}", neighbor);
log.debug("Cost: {}", cost);
log.debug("Neighbor cost: {}", cost.get(neighbor));
if (ndist < cost.get(neighbor)) {
cost.put(neighbor, ndist);
nexthoplinks.put(neighbor, link);
NodeDist ndTemp = new NodeDist(neighbor, ndist);
// Remove an object that's already in there.
// Note that the comparison is based on only the node id,
// and not node id and distance.
nodeq.remove(ndTemp);
// add the current object to the queue.
nodeq.add(ndTemp);
}
}
}
BroadcastTree ret = new BroadcastTree(nexthoplinks, cost);
return ret;
}
/*
* Creates a map of links and the cost associated with each link
*/
public Map<Link,Integer> initLinkCostMap(Map<Link, Integer> linkCost) {
// int tunnel_weight = portsWithLinks.size() + 1;
//
// switch (TopologyManager.getPathMetricInternal()){
// case HOPCOUNT_AVOID_TUNNELS:
// log.debug("Using hop count with tunnel bias for metrics");
// for (NodePortTuple npt : portsTunnel) {
// if (links.get(npt) == null) {
// continue;
// }
// for (Link link : links.get(npt)) {
// if (link == null) {
// continue;
// }
// linkCost.put(link, tunnel_weight);
// }
// }
// return linkCost;
//
// case HOPCOUNT:
// log.debug("Using hop count w/o tunnel bias for metrics");
// for (NodePortTuple npt : links.keySet()) {
// if (links.get(npt) == null) {
// continue;
// }
// for (Link link : links.get(npt)) {
// if (link == null) {
// continue;
// }
// linkCost.put(link,1);
// }
// }
// return linkCost;
//
// case LATENCY:
// log.debug("Using latency for path metrics");
// for (NodePortTuple npt : links.keySet()) {
// if (links.get(npt) == null) {
// continue;
// }
// for (Link link : links.get(npt)) {
// if (link == null) {
// continue;
// }
// if ((int)link.getLatency().getValue() < 0 ||
// (int)link.getLatency().getValue() > MAX_LINK_WEIGHT) {
// linkCost.put(link, MAX_LINK_WEIGHT);
// } else {
// linkCost.put(link,(int)link.getLatency().getValue());
// }
// }
// }
// return linkCost;
//
// case LINK_SPEED:
// TopologyManager.statisticsService.collectStatistics(true);
// log.debug("Using link speed for path metrics");
// for (NodePortTuple npt : links.keySet()) {
// if (links.get(npt) == null) {
// continue;
// }
// long rawLinkSpeed = 0;
// IOFSwitch s = TopologyManager.switchService.getSwitch(npt.getNodeId());
// if (s != null) {
// OFPortDesc p = s.getPort(npt.getPortId());
// if (p != null) {
// rawLinkSpeed = p.getCurrSpeed();
// }
// }
// for (Link link : links.get(npt)) {
// if (link == null) {
// continue;
// }
//
// if ((rawLinkSpeed / 10^6) / 8 > 1) {
// int linkSpeedMBps = (int)(rawLinkSpeed / 10^6) / 8;
// linkCost.put(link, (1/linkSpeedMBps)*1000);
// } else {
// linkCost.put(link, MAX_LINK_WEIGHT);
// }
// }
// }
// return linkCost;
//
// case UTILIZATION:
// TopologyManager.statisticsService.collectStatistics(true);
// log.debug("Using utilization for path metrics");
// for (NodePortTuple npt : links.keySet()) {
// if (links.get(npt) == null) continue;
// SwitchPortBandwidth spb = TopologyManager.statisticsService
// .getBandwidthConsumption(npt.getNodeId(), npt.getPortId());
// long bpsTx = 0;
// if (spb != null) {
// bpsTx = spb.getBitsPerSecondTx().getValue();
// }
// for (Link link : links.get(npt)) {
// if (link == null) {
// continue;
// }
//
// if ((bpsTx / 10^6) / 8 > 1) {
// int cost = (int) (bpsTx / 10^6) / 8;
// linkCost.put(link, cost);
// } else {
// linkCost.put(link, MAX_LINK_WEIGHT);
// }
// }
// }
// return linkCost;
//
//
// default:
// log.debug("Invalid Selection: Using Default Hop Count with Tunnel Bias for Metrics");
// for (NodePortTuple npt : portsTunnel) {
// if (links.get(npt) == null) continue;
// for (Link link : links.get(npt)) {
// if (link == null) continue;
// linkCost.put(link, tunnel_weight);
// }
// }
// return linkCost;
// }
for (NodePortTuple npt : links.keySet()) {
if (links.get(npt) == null) {
continue;
}
for (Link link : links.get(npt)) {
if (link == null ) {
continue;
}
if (linkCost.get(link)==null) {
int weight = ThreadLocalRandom.current().nextInt(1, 11);
linkCost.put(link, weight);
Link l = new Link();
l.setSrc(link.getDst());
l.setDst(link.getSrc());
l.setDstPort(link.getSrcPort());
l.setSrcPort(link.getDstPort());
l.setLatency(link.getLatency());
linkCost.put(l, weight);
}
}
}
return linkCost;
}
/*
* Calculates and stores n possible paths using Yen's algorithm,
* looping through every switch. These lists of routes are stored
* in the pathcache.
*/
private void computeOrderedPaths() {
List<Path> paths;
PathId pathId;
pathcache.clear();
for (Archipelago a : archipelagos) { /* for each archipelago */
Set<DatapathId> srcSws = a.getSwitches();
Set<DatapathId> dstSws = a.getSwitches();
log.debug("SRC {}", srcSws);
log.debug("DST {}", dstSws);
for (DatapathId src : srcSws) { /* permute all member switches */
for (DatapathId dst : dstSws) {
log.debug("Calling Yens {} {}", src, dst);
paths = yens(src, dst, TopologyManager.getMaxPathsToComputeInternal(),
a, a);
pathId = new PathId(src, dst);
pathcache.put(pathId, paths);
log.debug("Adding paths {}", paths);
}
}
}
}
private Path buildPath(PathId id, BroadcastTree tree) {
NodePortTuple npt;
DatapathId srcId = id.getSrc();
DatapathId dstId = id.getDst();
//set of NodePortTuples on the route
LinkedList<NodePortTuple> sPorts = new LinkedList<NodePortTuple>();
if (tree == null) return new Path(id, ImmutableList.of()); /* empty route */
Map<DatapathId, Link> nexthoplinks = tree.getLinks();
if (!switches.contains(srcId) || !switches.contains(dstId)) {
// This is a switch that is not connected to any other switch
// hence there was no update for links (and hence it is not
// in the network)
log.debug("buildpath: Standalone switch: {}", srcId);
// The only possible non-null path for this case is
// if srcId equals dstId --- and that too is an 'empty' path []
} else if ((nexthoplinks != null) && (nexthoplinks.get(srcId) != null)) {
while (!srcId.equals(dstId)) {
Link l = nexthoplinks.get(srcId);
npt = new NodePortTuple(l.getSrc(), l.getSrcPort());
sPorts.addLast(npt);
npt = new NodePortTuple(l.getDst(), l.getDstPort());
sPorts.addLast(npt);
srcId = nexthoplinks.get(srcId).getDst();
}
}
// else, no path exists, and path equals null
Path result = null;
if (sPorts != null && !sPorts.isEmpty()) {
result = new Path(id, sPorts);
}
log.trace("buildpath: {}", result);
return result == null ? new Path(id, ImmutableList.of()) : result;
}
/*
* Getter Functions
*/
public boolean pathExists(DatapathId srcId, DatapathId dstId) {
Archipelago srcA = getArchipelago(srcId);
Archipelago dstA = getArchipelago(dstId);
if (srcA == null || dstA == null || !srcA.equals(dstA)) {
return false;
}
BroadcastTree bt = srcA.getBroadcastTree();
if (bt == null) {
return false;
}
Link link = bt.getLinks().get(srcId);
if (link == null) {
return false;
}
return true;
}
private Map<DatapathId, Set<Link>> buildLinkDpidMap(Set<DatapathId> switches, Map<DatapathId,
Set<OFPort>> portsWithLinks, Map<NodePortTuple, Set<Link>> links) {
Map<DatapathId, Set<Link>> linkDpidMap = new HashMap<DatapathId, Set<Link>>();
for (DatapathId s : switches) {
if (portsWithLinks.get(s) == null) continue;
for (OFPort p : portsWithLinks.get(s)) {
NodePortTuple np = new NodePortTuple(s, p);
if (links.get(np) == null) continue;
for (Link l : links.get(np)) {
if (switches.contains(l.getSrc()) && switches.contains(l.getDst())) {
if (linkDpidMap.containsKey(s)) {
linkDpidMap.get(s).add(l);
} else {
linkDpidMap.put(s, new HashSet<Link>(Arrays.asList(l)));
}
}
}
}
}
return linkDpidMap;
}
/**
*
* This function returns K number of routes between a source and destination IF THEY EXIST IN THE ROUTECACHE.
* If the user requests more routes than available, only the routes already stored in memory will be returned.
* This value can be adjusted in floodlightdefault.properties.
*
*
* @param src: DatapathId of the route source.
* @param dst: DatapathId of the route destination.
* @param k: The number of routes that you want. Must be positive integer.
* @return ArrayList of Routes or null if bad parameters
*/
public List<Path> getPathsFast(DatapathId src, DatapathId dst, int k) {
PathId routeId = new PathId(src, dst);
List<Path> routes = pathcache.get(routeId);
if (routes == null || k < 1) {
return ImmutableList.of();
}
if (k >= TopologyManager.getMaxPathsToComputeInternal() || k >= routes.size()) {
return routes;
} else {
return routes.subList(0, k);
}
}
/**
*
* This function returns K number of paths between a source and destination. It will attempt to retrieve
* these paths from the pathcache. If the user requests more paths than are stored, Yen's algorithm will be
* run using the K value passed in.
*
*
* @param src: DatapathId of the path source.
* @param dst: DatapathId of the path destination.
* @param k: The number of path that you want. Must be positive integer.
* @return list of paths or empty
*/
public List<Path> getPathsSlow(DatapathId src, DatapathId dst, int k) {
PathId pathId = new PathId(src, dst);
List<Path> paths = pathcache.get(pathId);
if (paths == null || k < 1) return ImmutableList.of();
if (k >= TopologyManager.getMaxPathsToComputeInternal() || k >= paths.size()) {
return yens(src, dst, k, getArchipelago(src), getArchipelago(dst)); /* heavy computation */
}
else {
return new ArrayList<Path>(paths.subList(0, k));
}
}
/*
* In this, the parameter 'd' can be a switchId, or clusterId or archipelagoId
* as each of them represents a switch in the archipelago it belongs to.
*/
private Archipelago getArchipelago(DatapathId d) {
Cluster c = clusterFromSwitch.get(d);
if (c != null) {
return archipelagoFromCluster.get(c);
}
return null;
}
public void setPathCosts(Path p) {
U64 cost = U64.ZERO;
// Set number of hops. Assuming the list of NPTs is always even.
p.setHopCount(p.getPath().size()/2);
for (int i = 0; i <= p.getPath().size() - 2; i = i + 2) {
DatapathId src = p.getPath().get(i).getNodeId();
DatapathId dst = p.getPath().get(i + 1).getNodeId();