blob: 6587d701b3f6239c38890c56710106266767798d [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
2 * Copyright 2015-present Open Networking Laboratory
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 Vachuska41fe1ec2015-12-03 23:17:02 -080016package org.onosproject.net.topology;
17
18import static org.onosproject.net.Link.State.ACTIVE;
19import 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 */
25public 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 Lidfba7392016-01-22 16:46:58 -080037 *
38 * @param indirectLinkCost indirect link cost
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080039 */
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