1 # Copyright (C) 2021-2024 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 from flask import request
16 from flask_restx import Resource, reqparse
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, \
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, \
28 from o2common.helper import o2logging
29 logger = o2logging.get_logger(__name__)
32 def configure_api_route():
33 # Set global bus for resource
35 bus = MessageBus.get_instance()
38 # ---------- API versions ---------- #
39 @api_monitoring_v1.route("/v1/api_versions")
40 class VersionRouter(Resource):
41 model = MonitoringApiV1DTO.api_version_info_get
43 @api_monitoring_v1.doc('Get Monitoring API version')
44 @api_monitoring_v1.marshal_list_with(model)
47 'uriPrefix': request.base_url.rsplit('/', 1)[0],
50 # 'isDeprecated': 'False',
51 # 'retirementDate': ''
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.' +
61 _in='query', default=1)
62 @api_monitoring_v1.param(
64 'Set any value for show all fields. This value will cover "fields" ' +
67 @api_monitoring_v1.param(
69 'Set fields to show, split by comma, "/" for parent and children.' +
70 ' Like "name,parent/children". This value will cover' +
73 @api_monitoring_v1.param(
75 'Set fields to exclude showing, split by comma, "/" for parent and ' +
76 'children. Like "name,parent/children". This value will cover ' +
79 @api_monitoring_v1.param(
81 'Exclude showing all default fields, Set "true" to enable.',
83 @api_monitoring_v1.param(
85 'Filter of the query.',
87 class AlarmListRouter(Resource):
89 model = AlarmDTO.alarm_event_record_get
91 @api_monitoring_v1.doc('Get Alarm Event Record List')
92 @api_monitoring_v1.marshal_list_with(model)
94 parser = reqparse.RequestParser()
95 parser.add_argument(PAGE_PARAM, location='args')
96 parser.add_argument('filter', location='args')
97 args = parser.parse_args()
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 ''
103 ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
104 return link_header(request.full_path, ret)
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(
112 'Set any value for show all fields. This value will cover "fields" ' +
115 @api_monitoring_v1.param(
117 'Set fields to show, split by comma, "/" for parent and children.' +
118 ' Like "name,parent/children". This value will cover' +
119 ' "exculde_fields".',
121 @api_monitoring_v1.param(
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".',
127 @api_monitoring_v1.param(
129 'Exclude showing all default fields, Set "true" to enable.',
131 class AlarmGetRouter(Resource):
133 model = AlarmDTO.alarm_event_record_get
134 patch = AlarmDTO.alarm_event_record_patch
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:
142 raise NotFoundException(
143 "Alarm Event Record {} doesn't exist".format(alarmEventRecordId))
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)
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
157 raise BadRequestException('Either "alarmAcknowledged" or '
158 '"perceivedSeverity" shall be included '
159 'in a request, but not both.')
161 result = alarm_view.alarm_event_record_ack(alarmEventRecordId,
163 if result is not None:
166 if clear_action != PerceivedSeverityEnum.CLEARED:
167 raise BadRequestException(
168 'Only the value "5" for "CLEARED" is permitted of '
169 '"perceivedSeverity".')
171 result = alarm_view.alarm_event_record_clear(alarmEventRecordId,
173 if result is not None:
175 raise NotFoundException(
176 "Alarm Event Record {} doesn't exist".format(alarmEventRecordId))
179 # ---------- Alarm Subscriptions ---------- #
180 @api_monitoring_v1.route("/v1/alarmSubscriptions")
181 class SubscriptionsListRouter(Resource):
183 model = SubscriptionDTO.subscription_get
184 expect = SubscriptionDTO.subscription_create
186 @api_monitoring_v1.doc('Get Alarm Subscription List')
187 @api_monitoring_v1.marshal_list_with(model)
188 @api_monitoring_v1.param(
190 'Page number of the results to fetch. Default: 1',
191 _in='query', default=1)
192 @api_monitoring_v1.param(
194 'Set any value for show all fields. This value will cover "fields" ' +
197 @api_monitoring_v1.param(
199 'Set fields to show, split by comma, "/" for parent and children.' +
200 ' Like "name,parent/children". This value will cover' +
201 ' "exculde_fields".',
203 @api_monitoring_v1.param(
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".',
209 @api_monitoring_v1.param(
211 'Exclude showing all default fields, Set "true" to enable.',
213 @api_monitoring_v1.param(
215 'Filter of the query.',
218 parser = reqparse.RequestParser()
219 parser.add_argument(PAGE_PARAM, location='args')
220 parser.add_argument('filter', location='args')
221 args = parser.parse_args()
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 ''
227 ret = alarm_view.subscriptions(bus.uow, **kwargs)
228 return link_header(request.full_path, ret)
230 @api_monitoring_v1.doc('Create a Alarm Subscription')
231 @api_monitoring_v1.expect(expect)
232 @api_monitoring_v1.marshal_with(
234 mask='{alarmSubscriptionId,callback,consumerSubscriptionId,filter}')
236 data = api_monitoring_v1.payload
237 callback = data.get('callback', None)
239 raise BadRequestException('The callback parameter is required')
241 result = alarm_view.subscription_create(data, bus.uow)
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):
250 model = SubscriptionDTO.subscription_get
252 @api_monitoring_v1.doc('Get Alarm Subscription Information')
253 @api_monitoring_v1.marshal_with(model)
254 @api_monitoring_v1.param(
256 'Set any value for show all fields. This value will cover "fields" ' +
259 @api_monitoring_v1.param(
261 'Set fields to show, split by comma, "/" for parent and children.' +
262 ' Like "name,parent/children". This value will cover' +
263 ' "exculde_fields".',
265 @api_monitoring_v1.param(
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".',
271 @api_monitoring_v1.param(
273 'Exclude showing all default fields, Set "true" to enable.',
275 def get(self, alarmSubscriptionID):
276 result = alarm_view.subscription_one(
277 alarmSubscriptionID, bus.uow)
278 if result is not None:
280 raise NotFoundException(
281 "Subscription {} doesn't exist".format(alarmSubscriptionID))
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)