Fix install O2 on subcloud failed
[pti/o2.git] / o2ims / views / alarm_route.py
1 # Copyright (C) 2021-2024 Wind River Systems, Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 from flask import request
16 from flask_restx import Resource, reqparse
17
18 from o2common.service.messagebus import MessageBus
19 from o2common.views.pagination_route import link_header, PAGE_PARAM
20 from o2common.views.route_exception import NotFoundException, \
21     BadRequestException
22 from o2ims.domain.alarm_obj import PerceivedSeverityEnum
23 from o2ims.views import alarm_view
24 from o2ims.views.api_ns import api_ims_monitoring as api_monitoring_v1
25 from o2ims.views.alarm_dto import AlarmDTO, SubscriptionDTO, \
26     MonitoringApiV1DTO
27
28 from o2common.helper import o2logging
29 logger = o2logging.get_logger(__name__)
30
31
32 def configure_api_route():
33     # Set global bus for resource
34     global bus
35     bus = MessageBus.get_instance()
36
37
38 # ----------  API versions ---------- #
39 @api_monitoring_v1.route("/v1/api_versions")
40 class VersionRouter(Resource):
41     model = MonitoringApiV1DTO.api_version_info_get
42
43     @api_monitoring_v1.doc('Get Monitoring API version')
44     @api_monitoring_v1.marshal_list_with(model)
45     def get(self):
46         return {
47             'uriPrefix': request.base_url.rsplit('/', 1)[0],
48             'apiVersions': [{
49                 'version': '1.0.0',
50                 # 'isDeprecated': 'False',
51                 # 'retirementDate': ''
52             }]
53         }
54
55
56 # ----------  Alarm Event Record ---------- #
57 @api_monitoring_v1.route("/v1/alarms")
58 @api_monitoring_v1.param(PAGE_PARAM,
59                          'Page number of the results to fetch.' +
60                          ' Default: 1',
61                          _in='query', default=1)
62 @api_monitoring_v1.param(
63     'all_fields',
64     'Set any value for show all fields. This value will cover "fields" ' +
65     'and "all_fields".',
66     _in='query')
67 @api_monitoring_v1.param(
68     'fields',
69     'Set fields to show, split by comma, "/" for parent and children.' +
70     ' Like "name,parent/children". This value will cover' +
71     ' "exculde_fields".',
72     _in='query')
73 @api_monitoring_v1.param(
74     'exclude_fields',
75     'Set fields to exclude showing, split by comma, "/" for parent and ' +
76     'children. Like "name,parent/children". This value will cover ' +
77     '"exclude_default".',
78     _in='query')
79 @api_monitoring_v1.param(
80     'exclude_default',
81     'Exclude showing all default fields, Set "true" to enable.',
82     _in='query')
83 @api_monitoring_v1.param(
84     'filter',
85     'Filter of the query.',
86     _in='query')
87 class AlarmListRouter(Resource):
88
89     model = AlarmDTO.alarm_event_record_get
90
91     @api_monitoring_v1.doc('Get Alarm Event Record List')
92     @api_monitoring_v1.marshal_list_with(model)
93     def get(self):
94         parser = reqparse.RequestParser()
95         parser.add_argument(PAGE_PARAM, location='args')
96         parser.add_argument('filter', location='args')
97         args = parser.parse_args()
98         kwargs = {}
99         if args.nextpage_opaque_marker is not None:
100             kwargs['page'] = args.nextpage_opaque_marker
101         kwargs['filter'] = args.filter if args.filter is not None else ''
102
103         ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
104         return link_header(request.full_path, ret)
105
106
107 @api_monitoring_v1.route("/v1/alarms/<alarmEventRecordId>")
108 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
109 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
110 @api_monitoring_v1.param(
111     'all_fields',
112     'Set any value for show all fields. This value will cover "fields" ' +
113     'and "all_fields".',
114     _in='query')
115 @api_monitoring_v1.param(
116     'fields',
117     'Set fields to show, split by comma, "/" for parent and children.' +
118     ' Like "name,parent/children". This value will cover' +
119     ' "exculde_fields".',
120     _in='query')
121 @api_monitoring_v1.param(
122     'exclude_fields',
123     'Set fields to exclude showing, split by comma, "/" for parent and ' +
124     'children. Like "name,parent/children". This value will cover ' +
125     '"exclude_default".',
126     _in='query')
127 @api_monitoring_v1.param(
128     'exclude_default',
129     'Exclude showing all default fields, Set "true" to enable.',
130     _in='query')
131 class AlarmGetRouter(Resource):
132
133     model = AlarmDTO.alarm_event_record_get
134     patch = AlarmDTO.alarm_event_record_patch
135
136     @api_monitoring_v1.doc('Get Alarm Event Record Information')
137     @api_monitoring_v1.marshal_with(model)
138     def get(self, alarmEventRecordId):
139         result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
140         if result is not None:
141             return result
142         raise NotFoundException(
143             "Alarm Event Record {} doesn't exist".format(alarmEventRecordId))
144
145     @api_monitoring_v1.doc('Patch Alarm Event Record Information')
146     @api_monitoring_v1.expect(patch)
147     @api_monitoring_v1.marshal_with(patch)
148     def patch(self, alarmEventRecordId):
149         data = api_monitoring_v1.payload
150         ack_action = data.get('alarmAcknowledged', None)
151         clear_action = data.get('perceivedSeverity', None)
152
153         ack_is_none = ack_action is None
154         clear_is_none = clear_action is None
155         if (ack_is_none and clear_is_none) or (not ack_is_none and
156                                                not clear_is_none):
157             raise BadRequestException('Either "alarmAcknowledged" or '
158                                       '"perceivedSeverity" shall be included '
159                                       'in a request, but not both.')
160         if ack_action:
161             result = alarm_view.alarm_event_record_ack(alarmEventRecordId,
162                                                        bus.uow)
163             if result is not None:
164                 return result
165         elif clear_action:
166             if clear_action != PerceivedSeverityEnum.CLEARED:
167                 raise BadRequestException(
168                     'Only the value "5" for "CLEARED" is permitted of '
169                     '"perceivedSeverity".')
170
171             result = alarm_view.alarm_event_record_clear(alarmEventRecordId,
172                                                          bus.uow)
173             if result is not None:
174                 return result
175         raise NotFoundException(
176             "Alarm Event Record {} doesn't exist".format(alarmEventRecordId))
177
178
179 # ----------  Alarm Subscriptions ---------- #
180 @api_monitoring_v1.route("/v1/alarmSubscriptions")
181 class SubscriptionsListRouter(Resource):
182
183     model = SubscriptionDTO.subscription_get
184     expect = SubscriptionDTO.subscription_create
185
186     @api_monitoring_v1.doc('Get Alarm Subscription List')
187     @api_monitoring_v1.marshal_list_with(model)
188     @api_monitoring_v1.param(
189         PAGE_PARAM,
190         'Page number of the results to fetch. Default: 1',
191         _in='query', default=1)
192     @api_monitoring_v1.param(
193         'all_fields',
194         'Set any value for show all fields. This value will cover "fields" ' +
195         'and "all_fields".',
196         _in='query')
197     @api_monitoring_v1.param(
198         'fields',
199         'Set fields to show, split by comma, "/" for parent and children.' +
200         ' Like "name,parent/children". This value will cover' +
201         ' "exculde_fields".',
202         _in='query')
203     @api_monitoring_v1.param(
204         'exclude_fields',
205         'Set fields to exclude showing, split by comma, "/" for parent and ' +
206         'children. Like "name,parent/children". This value will cover ' +
207         '"exclude_default".',
208         _in='query')
209     @api_monitoring_v1.param(
210         'exclude_default',
211         'Exclude showing all default fields, Set "true" to enable.',
212         _in='query')
213     @api_monitoring_v1.param(
214         'filter',
215         'Filter of the query.',
216         _in='query')
217     def get(self):
218         parser = reqparse.RequestParser()
219         parser.add_argument(PAGE_PARAM, location='args')
220         parser.add_argument('filter', location='args')
221         args = parser.parse_args()
222         kwargs = {}
223         if args.nextpage_opaque_marker is not None:
224             kwargs['page'] = args.nextpage_opaque_marker
225         kwargs['filter'] = args.filter if args.filter is not None else ''
226
227         ret = alarm_view.subscriptions(bus.uow, **kwargs)
228         return link_header(request.full_path, ret)
229
230     @api_monitoring_v1.doc('Create a Alarm Subscription')
231     @api_monitoring_v1.expect(expect)
232     @api_monitoring_v1.marshal_with(
233         model, code=201,
234         mask='{alarmSubscriptionId,callback,consumerSubscriptionId,filter}')
235     def post(self):
236         data = api_monitoring_v1.payload
237         callback = data.get('callback', None)
238         if not callback:
239             raise BadRequestException('The callback parameter is required')
240
241         result = alarm_view.subscription_create(data, bus.uow)
242         return result, 201
243
244
245 @api_monitoring_v1.route("/v1/alarmSubscriptions/<alarmSubscriptionID>")
246 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
247 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
248 class SubscriptionGetDelRouter(Resource):
249
250     model = SubscriptionDTO.subscription_get
251
252     @api_monitoring_v1.doc('Get Alarm Subscription Information')
253     @api_monitoring_v1.marshal_with(model)
254     @api_monitoring_v1.param(
255         'all_fields',
256         'Set any value for show all fields. This value will cover "fields" ' +
257         'and "all_fields".',
258         _in='query')
259     @api_monitoring_v1.param(
260         'fields',
261         'Set fields to show, split by comma, "/" for parent and children.' +
262         ' Like "name,parent/children". This value will cover' +
263         ' "exculde_fields".',
264         _in='query')
265     @api_monitoring_v1.param(
266         'exclude_fields',
267         'Set fields to exclude showing, split by comma, "/" for parent and ' +
268         'children. Like "name,parent/children". This value will cover ' +
269         '"exclude_default".',
270         _in='query')
271     @api_monitoring_v1.param(
272         'exclude_default',
273         'Exclude showing all default fields, Set "true" to enable.',
274         _in='query')
275     def get(self, alarmSubscriptionID):
276         result = alarm_view.subscription_one(
277             alarmSubscriptionID, bus.uow)
278         if result is not None:
279             return result
280         raise NotFoundException(
281             "Subscription {} doesn't exist".format(alarmSubscriptionID))
282
283     @api_monitoring_v1.doc('Delete an Alarm Subscription')
284     @api_monitoring_v1.response(200, 'Subscription deleted')
285     def delete(self, alarmSubscriptionID):
286         result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)
287         return result, 200