2 # -------------------------------------------------------------------------
3 # Copyright (c) 2015-2017 AT&T Intellectual Property
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # -------------------------------------------------------------------------
21 from oslo_log import log
24 LOG = log.getLogger(__name__)
27 OPERATIONS = {'gte': lambda x, y: x >= y,
28 'lte': lambda x, y: x <= y,
29 'gt': lambda x, y: x > y,
30 'lt': lambda x, y: x < y,
31 'eq': lambda x, y: x == y
35 def compute_air_distance(_src, _dst):
36 """Compute Air Distance
38 based on latitude and longitude
39 input: a pair of (lat, lon)s
40 output: air distance as km
51 dlat = math.radians(_dst[0] - _src[0])
52 dlon = math.radians(_dst[1] - _src[1])
53 a = math.sin(dlat / 2.0) * math.sin(dlat / 2.0) + \
54 math.cos(math.radians(_src[0])) * \
55 math.cos(math.radians(_dst[0])) * \
56 math.sin(dlon / 2.0) * math.sin(dlon / 2.0)
57 c = 2.0 * math.atan2(math.sqrt(a), math.sqrt(1.0 - a))
63 def compute_latency_score(_src,_dst, _region_group):
64 """Compute the Network latency score between src and dst"""
65 earth_half_circumference = 20000
66 region_group_weight = _region_group.get(_dst[2])
68 if region_group_weight == 0 or region_group_weight is None :
69 LOG.debug("Computing the latency score based on distance between : ")
70 latency_score = compute_air_distance(_src,_dst)
71 elif _region_group > 0 :
72 LOG.debug("Computing the latency score ")
73 latency_score = compute_air_distance(_src, _dst) + region_group_weight * earth_half_circumference
74 LOG.debug("Finished Computing the latency score: "+str(latency_score))
78 def convert_km_to_miles(_km):
82 def convert_miles_to_km(_miles):
83 return _miles / 0.621371