Yuta HIGUCHI | a684b6e | 2017-05-18 22:29:22 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | a684b6e | 2017-05-18 22:29:22 -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 | */ |
| 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 | import org.onlab.graph.ScalarWeight; |
| 22 | import org.onlab.graph.Weight; |
| 23 | |
| 24 | /** |
| 25 | * Link weight for measuring link cost as hop count with indirect links |
| 26 | * being as expensive as traversing the entire graph to assume the worst. |
| 27 | */ |
| 28 | public class HopCountLinkWeigher implements LinkWeigher { |
| 29 | |
| 30 | public static final LinkWeigher DEFAULT_HOP_COUNT_WEIGHER = new HopCountLinkWeigher(); |
| 31 | |
| 32 | private static final ScalarWeight ZERO = new ScalarWeight(0.0); |
| 33 | private static final ScalarWeight ONE = new ScalarWeight(1.0); |
| 34 | private static final ScalarWeight DEFAULT_INDIRECT = new ScalarWeight(Short.MAX_VALUE); |
| 35 | |
| 36 | private final ScalarWeight indirectLinkCost; |
| 37 | |
| 38 | /** |
| 39 | * Creates a new hop-count weight. |
| 40 | */ |
| 41 | public HopCountLinkWeigher() { |
| 42 | this.indirectLinkCost = DEFAULT_INDIRECT; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Creates a new hop-count weight with the specified cost of indirect links. |
| 47 | * |
| 48 | * @param indirectLinkCost indirect link cost |
| 49 | */ |
| 50 | public HopCountLinkWeigher(double indirectLinkCost) { |
| 51 | this.indirectLinkCost = new ScalarWeight(indirectLinkCost); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public Weight weight(TopologyEdge edge) { |
| 56 | if (edge.link().state() == ACTIVE) { |
| 57 | return edge.link().type() == INDIRECT ? indirectLinkCost : ONE; |
| 58 | } else { |
| 59 | return getNonViableWeight(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Weight getInitialWeight() { |
| 65 | return ZERO; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public Weight getNonViableWeight() { |
| 70 | return ScalarWeight.NON_VIABLE_WEIGHT; |
| 71 | } |
| 72 | |
| 73 | } |