blob: 30d2467113dd92d24f8c55b5146d098c90adf531 [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.
Jian Lidfba7392016-01-22 16:46:58 -080022 *
23 * @param indirectLinkCost indirect link cost
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080024 */
25 public HopCountLinkWeight(int indirectLinkCost) {
26 this.indirectLinkCost = indirectLinkCost;
27 }
28
29 @Override
30 public double weight(TopologyEdge edge) {
31 // To force preference to use direct paths first, make indirect
32 // links as expensive as the linear vertex traversal.
33 return edge.link().state() ==
34 ACTIVE ? (edge.link().type() ==
35 INDIRECT ? indirectLinkCost : 1) : -1;
36 }
37}
38