Modify threshold policy to support multiple constraints
[optf/has.git] / conductor / conductor / solver / optimizer / constraints / threshold.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (C) 2020 Wipro Limited.
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 from conductor.i18n import _LI
21 from conductor.solver.optimizer.constraints import constraint
22 from conductor.solver.utils.utils import OPERATIONS
23 from oslo_log import log
24
25 LOG = log.getLogger(__name__)
26
27
28 class Threshold(constraint.Constraint):
29
30     def __init__(self, _name, _type, _demand_list, _priority=0,
31                  _properties=None):
32         constraint.Constraint.__init__(
33             self, _name, _type, _demand_list, _priority)
34         self.properties_list = _properties.get('evaluate')
35
36     def solve(self, _decision_path, _candidate_list, _request):
37
38         conflict_list = list()
39         demand_name = _decision_path.current_demand.name
40
41         LOG.info(_LI("Solving constraint {} of type '{}' for demand - [{}]").format(
42             self.name, self.constraint_type, demand_name))
43
44         for candidate in _candidate_list:
45             for prop in self.properties_list:
46                 attribute = prop.get('attribute')
47                 threshold = prop.get('threshold')
48                 operation = OPERATIONS.get(prop.get('operator'))
49
50                 attribute_value = candidate.get(attribute)
51                 if not operation(attribute_value, threshold):
52                     conflict_list.append(candidate)
53                     continue
54
55         filtered_candidates = [c for c in _candidate_list if c not in conflict_list]
56
57         return filtered_candidates