Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [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 | |
| 17 | package org.onosproject.net.topology; |
| 18 | |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 19 | import org.onlab.graph.ScalarWeight; |
| 20 | import org.onlab.graph.Weight; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 21 | import org.onosproject.net.AnnotationKeys; |
| 22 | |
| 23 | /** |
| 24 | * Link weight for measuring link cost using the link metric annotation. |
| 25 | */ |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 26 | public class MetricLinkWeight implements LinkWeigher { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 27 | |
| 28 | @Override |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 29 | 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 Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 40 | String v = edge.link().annotations().value(AnnotationKeys.METRIC); |
| 41 | try { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 42 | return ScalarWeight.toWeight(v != null ? Double.parseDouble(v) : 1); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 43 | } catch (NumberFormatException e) { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 44 | return ScalarWeight.toWeight(1.0); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | } |
| 48 | |