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.onlab.util.GeoLocation; |
| 22 | import org.onosproject.net.AnnotationKeys; |
| 23 | import org.onosproject.net.Annotations; |
| 24 | import org.onosproject.net.Device; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | |
| 28 | import static java.lang.Double.MAX_VALUE; |
| 29 | |
| 30 | /** |
| 31 | * Link weight for measuring link cost using the geo distance between link |
| 32 | * vertices as determined by the element longitude/latitude annotation. |
| 33 | */ |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 34 | public class GeoDistanceLinkWeight implements LinkWeigher { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 35 | |
| 36 | private static final double MAX_KM = 40_075 / 2.0; |
| 37 | |
| 38 | private final DeviceService deviceService; |
| 39 | |
| 40 | /** |
| 41 | * Creates a new link-weight with access to the specified device service. |
| 42 | * |
| 43 | * @param deviceService device service reference |
| 44 | */ |
| 45 | public GeoDistanceLinkWeight(DeviceService deviceService) { |
| 46 | this.deviceService = deviceService; |
| 47 | } |
| 48 | |
| 49 | @Override |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 50 | public Weight getInitialWeight() { |
| 51 | return ScalarWeight.toWeight(0.0); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public Weight getNonViableWeight() { |
| 56 | return ScalarWeight.NON_VIABLE_WEIGHT; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Weight weight(TopologyEdge edge) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 61 | GeoLocation src = getLocation(edge.link().src().deviceId()); |
| 62 | GeoLocation dst = getLocation(edge.link().dst().deviceId()); |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 63 | return ScalarWeight.toWeight(src != null && dst != null ? src.kilometersTo(dst) : MAX_KM); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | private GeoLocation getLocation(DeviceId deviceId) { |
| 67 | Device d = deviceService.getDevice(deviceId); |
| 68 | Annotations a = d != null ? d.annotations() : null; |
| 69 | double latitude = getDouble(a, AnnotationKeys.LATITUDE); |
| 70 | double longitude = getDouble(a, AnnotationKeys.LONGITUDE); |
| 71 | return latitude == MAX_VALUE || longitude == MAX_VALUE ? null : |
| 72 | new GeoLocation(latitude, longitude); |
| 73 | } |
| 74 | |
| 75 | private double getDouble(Annotations a, String key) { |
| 76 | String value = a != null ? a.value(key) : null; |
| 77 | try { |
| 78 | return value != null ? Double.parseDouble(value) : MAX_VALUE; |
| 79 | } catch (NumberFormatException e) { |
| 80 | return MAX_VALUE; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |