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