blob: 9d22b847ce828ff3c9740991eee1e9af5324331b [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.onlab.util.GeoLocation;
22import org.onosproject.net.AnnotationKeys;
23import org.onosproject.net.Annotations;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.device.DeviceService;
27
28import 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 Milkey7483e1b2018-02-07 15:43:01 -080034public class GeoDistanceLinkWeight implements LinkWeigher {
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080035
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 Milkey7483e1b2018-02-07 15:43:01 -080050 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 Vachuska41fe1ec2015-12-03 23:17:02 -080061 GeoLocation src = getLocation(edge.link().src().deviceId());
62 GeoLocation dst = getLocation(edge.link().dst().deviceId());
Ray Milkey7483e1b2018-02-07 15:43:01 -080063 return ScalarWeight.toWeight(src != null && dst != null ? src.kilometersTo(dst) : MAX_KM);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080064 }
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