Merge "Added all common modules in conductor directory"
[optf/has.git] / conductor / conductor / solver / utils / constraint_engine_interface.py
1 #!/usr/bin/env python
2 #
3 # -------------------------------------------------------------------------
4 #   Copyright (c) 2015-2017 AT&T Intellectual Property
5 #
6 #   Licensed under the Apache License, Version 2.0 (the "License");
7 #   you may not use this file except in compliance with the License.
8 #   You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #   Unless required by applicable law or agreed to in writing, software
13 #   distributed under the License is distributed on an "AS IS" BASIS,
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #   See the License for the specific language governing permissions and
16 #   limitations under the License.
17 #
18 # -------------------------------------------------------------------------
19 #
20
21
22 """Constraint/Engine Interface
23
24 Utility library that defines the interface between
25 the constraints and the conductor data engine.
26
27 """
28
29 from oslo_log import log
30
31 LOG = log.getLogger(__name__)
32
33
34 class ConstraintEngineInterface(object):
35     def __init__(self, client):
36         self.client = client
37
38     def get_candidate_location(self, candidate):
39         # Try calling a method (remember, "calls" are synchronous)
40         # FIXME(jdandrea): Doing this because Music calls are expensive.
41         lat = candidate.get('latitude')
42         lon = candidate.get('longitude')
43         if lat and lon:
44             response = (float(lat), float(lon))
45         else:
46             ctxt = {}
47             args = {"candidate": candidate}
48             response = self.client.call(ctxt=ctxt,
49                                         method="get_candidate_location",
50                                         args=args)
51             LOG.debug("get_candidate_location response: {}".format(response))
52         return response
53
54     def get_candidate_zone(self, candidate, _category=None):
55         # FIXME(jdandrea): Doing this because Music calls are expensive.
56         if _category == 'region':
57             response = candidate['location_id']
58         elif _category == 'complex':
59             response = candidate['complex_name']
60         else:
61             ctxt = {}
62             args = {"candidate": candidate, "category": _category}
63             response = self.client.call(ctxt=ctxt,
64                                         method="get_candidate_zone",
65                                         args=args)
66             LOG.debug("get_candidate_zone response: {}".format(response))
67         return response
68
69     def get_candidates_from_service(self, constraint_name,
70                                     constraint_type, candidate_list,
71                                     controller, inventory_type,
72                                     request, cost, demand_name):
73         ctxt = {}
74         args = {"constraint_name": constraint_name,
75                 "constraint_type": constraint_type,
76                 "candidate_list": candidate_list,
77                 "controller": controller,
78                 "inventory_type": inventory_type,
79                 "request": request,
80                 "cost": cost,
81                 "demand_name": demand_name}
82         response = self.client.call(ctxt=ctxt,
83                                     method="get_candidates_from_service",
84                                     args=args)
85         LOG.debug("get_candidates_from_service response: {}".format(response))
86         # response is a list of (candidate, cost) tuples
87         return response
88
89     def get_inventory_group_candidates(self, candidate_list,
90                                        demand_name, resolved_candidate):
91         # return a list of the "pair" candidates for the given candidate
92         ctxt = {}
93         args = {"candidate_list": candidate_list,
94                 "demand_name": demand_name,
95                 "resolved_candidate": resolved_candidate}
96         response = self.client.call(ctxt=ctxt,
97                                     method="get_inventory_group_candidates",
98                                     args=args)
99         LOG.debug("get_inventory_group_candidates \
100                    response: {}".format(response))
101         return response
102
103     def get_candidates_by_attributes(self, demand_name,
104                                      candidate_list, properties):
105         ctxt = {}
106         args = {"candidate_list": candidate_list,
107                 "properties": properties,
108                 "demand_name": demand_name}
109         response = self.client.call(ctxt=ctxt,
110                                     method="get_candidates_by_attributes",
111                                     args=args)
112         LOG.debug("get_candidates_by_attribute response: {}".format(response))
113         # response is a list of (candidate, cost) tuples
114         return response