Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 1 | package org.onosproject.net.topology; |
| 2 | |
| 3 | import static org.onosproject.net.Link.State.ACTIVE; |
| 4 | import 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 | */ |
| 10 | public 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 Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 22 | * |
| 23 | * @param indirectLinkCost indirect link cost |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 24 | */ |
| 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 | |