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.
15 # client talking to Stx standalone
17 from typing import List # Optional, Set
20 from cgtsclient.client import get_client as get_stx_client
21 from cgtsclient.exc import EndpointException
22 from dcmanagerclient.api.client import client as get_dc_client
23 from fmclient.client import get_client as get_fm_client
24 from fmclient.common.exceptions import HTTPNotFound
26 from o2app.adapter import unit_of_work
27 from o2common.config import config
28 from o2common.service.client.base_client import BaseClient
29 from o2ims.domain import alarm_obj as alarmModel
31 from o2common.helper import o2logging
32 logger = o2logging.get_logger(__name__)
35 CGTSCLIENT_ENDPOINT_ERROR_MSG = \
36 'Must provide Keystone credentials or user-defined endpoint and token'
39 class StxAlarmClient(BaseClient):
40 def __init__(self, uow: unit_of_work.AbstractUnitOfWork, driver=None):
42 self.driver = driver if driver else StxFaultClientImp()
45 def _get(self, id) -> alarmModel.FaultGenericModel:
46 return self.driver.getAlarmInfo(id)
48 def _list(self, **filters) -> List[alarmModel.FaultGenericModel]:
49 newmodels = self.driver.getAlarmList(**filters)
53 rs = uow.session.execute(
55 SELECT "alarmEventRecordId"
56 FROM "alarmEventRecord"
57 WHERE "perceivedSeverity" != :perceived_severity_enum
59 dict(perceived_severity_enum=alarmModel.PerceivedSeverityEnum.
64 # logger.debug('Exist alarm: ' + id)
65 exist_alarms[id] = False
70 if exist_alarms[m.id]:
72 exist_alarms[m.id] = True
74 logger.debug('alarm new: ' + m.id)
77 for alarm in exist_alarms:
78 logger.debug('exist alarm: ' + alarm)
79 if exist_alarms[alarm]:
80 # exist alarm is active
83 event = self._get(alarm)
85 logger.debug('alarm {} not in this resource pool {}'
86 .format(alarm, self._pool_id))
92 def _set_stx_client(self):
93 self.driver.setFaultClient(self._pool_id)
96 class StxEventClient(BaseClient):
97 def __init__(self, driver=None):
99 self.driver = driver if driver else StxFaultClientImp()
101 def _get(self, id) -> alarmModel.FaultGenericModel:
102 return self.driver.getEventInfo(id)
104 def _list(self, **filters) -> List[alarmModel.FaultGenericModel]:
105 return self.driver.getEventList(**filters)
107 def _set_stx_client(self):
108 self.driver.setFaultClient(self._pool_id)
111 # internal driver which implement client call to Stx Fault Management instance
112 class StxFaultClientImp(object):
113 def __init__(self, fm_client=None, stx_client=None, dc_client=None):
115 self.fmclient = fm_client if fm_client else self.getFmClient()
116 self.stxclient = stx_client if stx_client else self.getStxClient()
117 self.dcclient = dc_client if dc_client else self.getDcmanagerClient()
119 def getStxClient(self):
120 os_client_args = config.get_stx_access_info()
121 config_client = get_stx_client(**os_client_args)
124 def getDcmanagerClient(self):
125 os_client_args = config.get_dc_access_info()
126 config_client = get_dc_client(**os_client_args)
129 def getFmClient(self):
130 os_client_args = config.get_fm_access_info()
131 config_client = get_fm_client(1, **os_client_args)
134 def getSubcloudList(self):
135 self.dcclient = self.getDcmanagerClient()
136 subs = self.dcclient.subcloud_manager.list_subclouds()
137 known_subs = [sub for sub in subs if sub.sync_status != 'unknown']
140 def getSubcloudFaultClient(self, subcloud_id):
141 subcloud = self.dcclient.subcloud_manager.\
142 subcloud_additional_details(subcloud_id)
143 logger.debug('subcloud name: %s, oam_floating_ip: %s' %
144 (subcloud[0].name, subcloud[0].oam_floating_ip))
147 os_client_args = config.get_stx_access_info(
148 region_name=subcloud[0].name,
149 subcloud_hostname=subcloud[0].oam_floating_ip)
150 stx_client = get_stx_client(**os_client_args)
151 except EndpointException as e:
152 msg = e.format_message()
153 if CGTSCLIENT_ENDPOINT_ERROR_MSG in msg:
155 os_client_args = config.get_stx_access_info(
156 region_name=subcloud[0].name, sub_is_https=sub_is_https,
157 subcloud_hostname=subcloud[0].oam_floating_ip)
158 stx_client = get_stx_client(**os_client_args)
160 raise ValueError('Stx endpoint exception: %s' % msg)
162 raise ValueError('cgtsclient get subcloud client failed')
164 os_client_args = config.get_fm_access_info(
165 sub_is_https=sub_is_https,
166 subcloud_hostname=subcloud[0].oam_floating_ip)
167 fm_client = get_fm_client(1, **os_client_args)
169 return stx_client, fm_client
171 def setFaultClient(self, resource_pool_id):
172 systems = self.stxclient.isystem.list()
173 if resource_pool_id == systems[0].uuid:
174 logger.debug('Fault Client not change: %s' % resource_pool_id)
175 self.fmclient = self.getFmClient()
178 subclouds = self.getSubcloudList()
179 for subcloud in subclouds:
180 substxclient, subfaultclient = self.getSubcloudFaultClient(
181 subcloud.subcloud_id)
182 systems = substxclient.isystem.list()
183 if resource_pool_id == systems[0].uuid:
184 self.fmclient = subfaultclient
186 def getAlarmList(self, **filters) -> List[alarmModel.FaultGenericModel]:
187 alarms = self.fmclient.alarm.list(expand=True)
190 logger.debug('alarm 1:' + str(alarms[0].to_dict()))
191 # [print('alarm:' + str(alarm.to_dict())) for alarm in alarms if alarm]
192 return [alarmModel.FaultGenericModel(
193 alarmModel.EventTypeEnum.ALARM, self._alarmconverter(alarm))
194 for alarm in alarms if alarm]
196 def getAlarmInfo(self, id) -> alarmModel.FaultGenericModel:
198 alarm = self.fmclient.alarm.get(id)
199 logger.debug('get alarm id ' + id + ':' + str(alarm.to_dict()))
201 event = self.fmclient.event_log.get(id)
202 return alarmModel.FaultGenericModel(
203 alarmModel.EventTypeEnum.ALARM, self._eventconverter(event,
205 return alarmModel.FaultGenericModel(
206 alarmModel.EventTypeEnum.ALARM, self._alarmconverter(alarm))
208 def getEventList(self, **filters) -> List[alarmModel.FaultGenericModel]:
209 events = self.fmclient.event_log.list(alarms=True, expand=True)
210 logger.debug('event 1:' + str(events[0].to_dict()))
211 # [print('alarm:' + str(event.to_dict())) for event in events if event]
212 return [alarmModel.FaultGenericModel(
213 alarmModel.EventTypeEnum.EVENT, self._eventconverter(event))
214 for event in events if event]
216 def getEventInfo(self, id) -> alarmModel.FaultGenericModel:
217 event = self.fmclient.event_log.get(id)
218 logger.debug('get event id ' + id + ':' + str(event.to_dict()))
219 return alarmModel.FaultGenericModel(
220 alarmModel.EventTypeEnum.EVENT, self._eventconverter(event))
223 def _alarmconverter(alarm):
224 # setattr(alarm, 'alarm_def_id', uuid.uuid3(
225 # uuid.NAMESPACE_URL, alarm.alarm_id))
226 setattr(alarm, 'state', alarm.alarm_state)
228 setattr(alarm, 'alarm_def_id', str(uuid.uuid3(
229 uuid.NAMESPACE_URL, alarm.alarm_id)))
230 setattr(alarm, 'probable_cause_id', str(uuid.uuid3(
231 uuid.NAMESPACE_URL, alarm.probable_cause)))
235 def _eventconverter(event, clear=False):
236 setattr(event, 'alarm_id', event.event_log_id)
237 setattr(event, 'alarm_type', event.event_log_type)
239 logger.debug('alarm is clear')
240 event.state = 'clear'
241 setattr(event, 'alarm_def_id', str(uuid.uuid3(
242 uuid.NAMESPACE_URL, event.alarm_id)))
243 setattr(event, 'probable_cause_id', str(uuid.uuid3(
244 uuid.NAMESPACE_URL, event.probable_cause)))
248 def _alarmeventhasher(event, state=''):
249 # The event model and the alarm model have different parameter name
250 # of the state. alarm model is alarm_state, event model is state.
251 status = event.alarm_state if state == '' else state
252 return str(hash((event.uuid, event.timestamp, status)))