Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 16 | package org.onosproject.net.topology; |
| 17 | |
| 18 | import static org.onosproject.net.Link.State.ACTIVE; |
| 19 | import static org.onosproject.net.Link.Type.INDIRECT; |
| 20 | |
| 21 | /** |
| 22 | * Link weight for measuring link cost as hop count with indirect links |
| 23 | * being as expensive as traversing the entire graph to assume the worst. |
| 24 | */ |
| 25 | public class HopCountLinkWeight implements LinkWeight { |
| 26 | private final int indirectLinkCost; |
| 27 | |
| 28 | /** |
| 29 | * Creates a new hop-count weight. |
| 30 | */ |
| 31 | public HopCountLinkWeight() { |
| 32 | this.indirectLinkCost = Short.MAX_VALUE; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * 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] | 37 | * |
| 38 | * @param indirectLinkCost indirect link cost |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 39 | */ |
| 40 | public HopCountLinkWeight(int indirectLinkCost) { |
| 41 | this.indirectLinkCost = indirectLinkCost; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public double weight(TopologyEdge edge) { |
| 46 | // To force preference to use direct paths first, make indirect |
| 47 | // links as expensive as the linear vertex traversal. |
| 48 | return edge.link().state() == |
| 49 | ACTIVE ? (edge.link().type() == |
| 50 | INDIRECT ? indirectLinkCost : 1) : -1; |
| 51 | } |
| 52 | } |
| 53 | |