1 # Copyright (C) 2022 Wind River Systems, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
20 import uuid as uuid_gen
22 from o2common.service import unit_of_work
23 from o2common.config import config, conf
24 from o2ims.domain import alarm_obj as alarm
26 from o2common.helper import o2logging
27 logger = o2logging.get_logger(__name__)
30 def load_alarm_dictionary_from_conf_file(conf_path: str):
32 logger.info("Converting alarm.yaml to dict: ")
34 if not os.path.isfile(conf_path):
35 logger.error("file %s doesn't exist. Ending execution" %
38 errno.ENOENT, os.strerror(errno.ENOENT), conf_path
42 with open(conf_path, 'r') as stream:
43 alarm_yaml = yaml.load(stream, Loader=yaml.FullLoader)
44 dictionaries = alarm_yaml.get('dictionary')
45 except Exception as exp:
47 raise RuntimeError(exp)
49 for dictionary in list(dictionaries.keys()):
50 # res_type = uow.resource_types.get_by_name(dictionary)
51 # logger.info('res_type: ' + res_type.resourceTypeName)
52 alarm_dict = alarm.AlarmDictionary(dictionary)
53 alarm_dict.entityType = dictionary
54 alarm_dict.alarmDictionaryVersion = \
55 dictionaries[dictionary]['version']
56 alarm_dict.alarmDefinition = \
57 dictionaries[dictionary]['alarmDefinition']
58 conf.alarm_dictionaries.add(alarm_dict)
62 output = json.dumps(dict, sort_keys=True, indent=4)
66 def load_alarm_definition(uow: unit_of_work.AbstractUnitOfWork):
67 logger.info("Converting events.yaml to dict: ")
68 EVENT_TYPES_FILE = config.get_events_yaml_filename()
70 if not os.path.isfile(EVENT_TYPES_FILE):
71 logger.error("file %s doesn't exist. Ending execution" %
74 errno.ENOENT, os.strerror(errno.ENOENT), EVENT_TYPES_FILE
78 with open(EVENT_TYPES_FILE, 'r') as stream:
79 event_types = yaml.load(stream, Loader=yaml.FullLoader)
80 except Exception as exp:
82 raise RuntimeError(exp)
84 for alarm_id in list(event_types.keys()):
85 if isinstance(alarm_id, float):
86 # force 3 digits after the decimal point,
87 # to include trailing zero's (ex.: 200.010)
88 formatted_alarm_id = "{:.3f}".format(alarm_id)
89 event_types[formatted_alarm_id] = event_types.pop(alarm_id)
91 event_types = collections.OrderedDict(sorted(event_types.items()))
94 uneditable_descriptions = {'100.114', '200.007',
95 '200.02', '200.021', '200.022', '800.002'}
97 # Parse events.yaml dict, and add any new alarm to definition table:
99 "Parsing events.yaml and adding any new alarm to definition table: ")
100 for event_type in event_types:
102 if event_types.get(event_type).get('Type') == "Alarm":
103 event_uuid = str(uuid_gen.uuid3(
104 uuid_gen.NAMESPACE_URL, str(event_type)))
106 string_event_type = str(event_type)
108 yaml_event_list.append(string_event_type)
110 if str(event_type) not in uneditable_descriptions:
111 event_description = (event_types.get(event_type)
114 event_description = event_types.get(
115 event_type).get('Description')
117 event_description = str(event_description)
118 event_description = (event_description[:250] + ' ...') \
119 if len(event_description) > 250 else event_description
120 prop_action = event_types.get(
121 event_type).get("Proposed_Repair_Action")
124 alarm_def = uow.alarm_definitions.get(event_uuid)
125 event_mgmt_affecting = str(event_types.get(event_type).get(
126 'Management_Affecting_Severity', 'warning'))
128 event_degrade_affecting = str(event_types.get(event_type).get(
129 'Degrade_Affecting_Severity', 'none'))
132 alarm_def.description = event_description
133 alarm_def.mgmt_affecting = event_mgmt_affecting
134 alarm_def.degrade_affecting = event_degrade_affecting
136 alarm_def = alarm.AlarmDefinition(
138 name=str(event_type),
139 last_change=alarm.AlarmLastChangeEnum.ADDED,
140 desc=event_description, prop_action=prop_action,
141 clearing_type=alarm.ClearingTypeEnum.MANUAL,
144 logger.info(str(event_type))
145 uow.alarm_definitions.add(alarm_def)
149 prob_cause = event_types.get(event_type).get("Probable_Cause")
150 prob_cause_uuid = str(uuid_gen.uuid3(
151 uuid_gen.NAMESPACE_URL, prob_cause))
154 probable_cause = uow.alarm_probable_causes.get(prob_cause_uuid)
155 if probable_cause is None:
156 pc = alarm.ProbableCause(
157 prob_cause_uuid, prob_cause, prob_cause)
158 uow.alarm_probable_causes.add(pc)