blob: e87f660026f9baf23c8ae886daf76ebfe6322236 [file] [log] [blame]
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -08003 *
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
17package org.onosproject.net.topology;
18
Ray Milkey7483e1b2018-02-07 15:43:01 -080019import org.onlab.graph.ScalarWeight;
20import org.onlab.graph.Weight;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080021import org.onosproject.net.AnnotationKeys;
22
23/**
24 * Link weight for measuring link cost using the link metric annotation.
25 */
Ray Milkey7483e1b2018-02-07 15:43:01 -080026public class MetricLinkWeight implements LinkWeigher {
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080027
28 @Override
Ray Milkey7483e1b2018-02-07 15:43:01 -080029 public Weight getInitialWeight() {
30 return ScalarWeight.toWeight(0.0);
31 }
32
33 @Override
34 public Weight getNonViableWeight() {
35 return ScalarWeight.NON_VIABLE_WEIGHT;
36 }
37
38 @Override
39 public Weight weight(TopologyEdge edge) {
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080040 String v = edge.link().annotations().value(AnnotationKeys.METRIC);
41 try {
Ray Milkey7483e1b2018-02-07 15:43:01 -080042 return ScalarWeight.toWeight(v != null ? Double.parseDouble(v) : 1);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080043 } catch (NumberFormatException e) {
Ray Milkey7483e1b2018-02-07 15:43:01 -080044 return ScalarWeight.toWeight(1.0);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080045 }
46 }
47}
48