Modify threshold policy to support multiple constraints
[optf/has.git] / conductor / conductor / solver / utils / utils.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (c) 2015-2017 AT&T Intellectual Property
4 #
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
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 #
17 # -------------------------------------------------------------------------
18 #
19
20 import math
21 from oslo_log import log
22
23
24 LOG = log.getLogger(__name__)
25
26
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
32               }
33
34
35 def compute_air_distance(_src, _dst):
36     """Compute Air Distance
37
38     based on latitude and longitude
39     input: a pair of (lat, lon)s
40     output: air distance as km
41     """
42     distance = 0.0
43     latency_score = 0.0
44
45     if _src == _dst:
46         return distance
47
48     radius = 6371.0  # km
49
50
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))
58     distance = radius * c
59
60     return distance
61
62
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])
67
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))
75     return latency_score
76
77
78 def convert_km_to_miles(_km):
79     return _km * 0.621371
80
81
82 def convert_miles_to_km(_miles):
83     return _miles / 0.621371