blob: c557016b6d139af91f60333716415e857020b7ad [file] [log] [blame]
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -08001package org.onosproject.net.topology;
2
3import static org.onosproject.net.Link.State.ACTIVE;
4import static org.onosproject.net.Link.Type.INDIRECT;
5
6/**
7 * Link weight for measuring link cost as hop count with indirect links
8 * being as expensive as traversing the entire graph to assume the worst.
9 */
10public class HopCountLinkWeight implements LinkWeight {
11 private final int indirectLinkCost;
12
13 /**
14 * Creates a new hop-count weight.
15 */
16 public HopCountLinkWeight() {
17 this.indirectLinkCost = Short.MAX_VALUE;
18 }
19
20 /**
21 * Creates a new hop-count weight with the specified cost of indirect links.
22 */
23 public HopCountLinkWeight(int indirectLinkCost) {
24 this.indirectLinkCost = indirectLinkCost;
25 }
26
27 @Override
28 public double weight(TopologyEdge edge) {
29 // To force preference to use direct paths first, make indirect
30 // links as expensive as the linear vertex traversal.
31 return edge.link().state() ==
32 ACTIVE ? (edge.link().type() ==
33 INDIRECT ? indirectLinkCost : 1) : -1;
34 }
35}
36