2 # -------------------------------------------------------------------------
3 # Copyright (C) 2020 Wipro Limited.
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 # -------------------------------------------------------------------------
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
25 LOG = log.getLogger(__name__)
28 class Threshold(constraint.Constraint):
30 def __init__(self, _name, _type, _demand_list, _priority=0,
32 constraint.Constraint.__init__(
33 self, _name, _type, _demand_list, _priority)
34 self.properties_list = _properties.get('evaluate')
36 def solve(self, _decision_path, _candidate_list, _request):
38 conflict_list = list()
39 demand_name = _decision_path.current_demand.name
41 LOG.info(_LI("Solving constraint {} of type '{}' for demand - [{}]").format(
42 self.name, self.constraint_type, demand_name))
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'))
50 attribute_value = candidate.get(attribute)
51 if not operation(attribute_value, threshold):
52 conflict_list.append(candidate)
55 filtered_candidates = [c for c in _candidate_list if c not in conflict_list]
57 return filtered_candidates