Skip to content

Commit dc59b10

Browse files
[Mellanox] Add validation for thermal policy (#78)
1 parent ed50e72 commit dc59b10

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

sonic_platform_base/sonic_thermal_control/thermal_json_object.py

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ def load_from_json(self, json_obj):
1717
"""
1818
pass
1919

20+
def __eq__(self, other):
21+
"""
22+
Compare input object with this object, return True if equal. Subclass should override this
23+
if necessary.
24+
:param other: Object to compare with.
25+
:return: True if equal else False
26+
"""
27+
return self.__class__ == other.__class__
28+
2029
@classmethod
2130
def register_concrete_type(cls, type_name, object_type):
2231
"""

sonic_platform_base/sonic_thermal_control/thermal_manager_base.py

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def _load_policy(cls, json_policy):
151151

152152
policy = ThermalPolicy()
153153
policy.load_from_json(json_policy)
154+
policy.validate_duplicate_policy(cls._policy_dict.values())
154155
cls._policy_dict[name] = policy
155156
else:
156157
raise Exception('{} not found in policy'.format(cls.JSON_FIELD_POLICY_NAME))

sonic_platform_base/sonic_thermal_control/thermal_policy.py

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .thermal_json_object import ThermalJsonObject
2+
from collections import OrderedDict
23

34

45
class ThermalPolicy(object):
@@ -15,10 +16,10 @@ def __init__(self):
1516
self.name = None
1617

1718
# Conditions load from JSON policy file
18-
self.conditions = []
19+
self.conditions = OrderedDict()
1920

2021
# Actions load from JSON policy file
21-
self.actions = []
22+
self.actions = OrderedDict()
2223

2324
def load_from_json(self, json_obj):
2425
"""
@@ -32,16 +33,23 @@ def load_from_json(self, json_obj):
3233
if self.JSON_FIELD_CONDITIONS in json_obj:
3334
for json_condition in json_obj[self.JSON_FIELD_CONDITIONS]:
3435
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))
3538
cond_obj = cond_type()
3639
cond_obj.load_from_json(json_condition)
37-
self.conditions.append(cond_obj)
40+
self.conditions[cond_type] = cond_obj
3841

3942
if self.JSON_FIELD_ACTIONS in json_obj:
4043
for json_action in json_obj[self.JSON_FIELD_ACTIONS]:
4144
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))
4247
action_obj = action_type()
4348
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')
4553
else:
4654
raise Exception('name field not found in policy')
4755

@@ -51,7 +59,7 @@ def is_match(self, thermal_info_dict):
5159
:param thermal_info_dict: A dictionary stores all thermal information.
5260
:return: True if all conditions matches else False.
5361
"""
54-
for condition in self.conditions:
62+
for condition in self.conditions.values():
5563
if not condition.is_match(thermal_info_dict):
5664
return False
5765

@@ -63,5 +71,19 @@ def do_action(self, thermal_info_dict):
6371
:param thermal_info_dict: A dictionary stores all thermal information.
6472
:return:
6573
"""
66-
for action in self.actions:
74+
for action in self.actions.values():
6775
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

Comments
 (0)