1
1
from .thermal_json_object import ThermalJsonObject
2
+ from collections import OrderedDict
2
3
3
4
4
5
class ThermalPolicy (object ):
@@ -15,10 +16,10 @@ def __init__(self):
15
16
self .name = None
16
17
17
18
# Conditions load from JSON policy file
18
- self .conditions = []
19
+ self .conditions = OrderedDict ()
19
20
20
21
# Actions load from JSON policy file
21
- self .actions = []
22
+ self .actions = OrderedDict ()
22
23
23
24
def load_from_json (self , json_obj ):
24
25
"""
@@ -32,16 +33,23 @@ def load_from_json(self, json_obj):
32
33
if self .JSON_FIELD_CONDITIONS in json_obj :
33
34
for json_condition in json_obj [self .JSON_FIELD_CONDITIONS ]:
34
35
cond_type = ThermalJsonObject .get_type (json_condition )
36
+ if cond_type in self .conditions :
37
+ raise Exception ('Duplicate thermal condition type detected in policy [{}]!' .format (self .name ))
35
38
cond_obj = cond_type ()
36
39
cond_obj .load_from_json (json_condition )
37
- self .conditions . append ( cond_obj )
40
+ self .conditions [ cond_type ] = cond_obj
38
41
39
42
if self .JSON_FIELD_ACTIONS in json_obj :
40
43
for json_action in json_obj [self .JSON_FIELD_ACTIONS ]:
41
44
action_type = ThermalJsonObject .get_type (json_action )
45
+ if action_type in self .actions :
46
+ raise Exception ('Duplicate thermal action type detected in policy [{}]!' .format (self .name ))
42
47
action_obj = action_type ()
43
48
action_obj .load_from_json (json_action )
44
- self .actions .append (action_obj )
49
+ self .actions [action_type ] = action_obj
50
+
51
+ if not len (self .conditions ) or not len (self .actions ):
52
+ raise Exception ('A policy requires at least 1 action and 1 condition' )
45
53
else :
46
54
raise Exception ('name field not found in policy' )
47
55
@@ -51,7 +59,7 @@ def is_match(self, thermal_info_dict):
51
59
:param thermal_info_dict: A dictionary stores all thermal information.
52
60
:return: True if all conditions matches else False.
53
61
"""
54
- for condition in self .conditions :
62
+ for condition in self .conditions . values () :
55
63
if not condition .is_match (thermal_info_dict ):
56
64
return False
57
65
@@ -63,5 +71,19 @@ def do_action(self, thermal_info_dict):
63
71
:param thermal_info_dict: A dictionary stores all thermal information.
64
72
:return:
65
73
"""
66
- for action in self .actions :
74
+ for action in self .actions . values () :
67
75
action .execute (thermal_info_dict )
76
+
77
+ def validate_duplicate_policy (self , policies ):
78
+ """
79
+ Detect this policy with existing policies, if a policy with same conditions exists, raise Exception.
80
+ :param policies: existing policies.
81
+ :return:
82
+ """
83
+ for policy in policies :
84
+ if len (policy .conditions ) != len (self .conditions ):
85
+ continue
86
+
87
+ for cond_type , value in policy .conditions .items ():
88
+ if cond_type in self .conditions and policy .conditions [cond_type ] == self .conditions [cond_type ]:
89
+ raise Exception ('Policy [{}] and policy [{}] have duplicate conditions' .format (policy .name , self .name ))
0 commit comments