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.views import alarm_view
23 from o2ims.views.api_ns import api_ims_monitoring as api_monitoring_v1
24 from o2ims.views.alarm_dto import AlarmDTO, SubscriptionDTO, \
27 from o2common.helper import o2logging
28 logger = o2logging.get_logger(__name__)
31 def configure_api_route():
32 # Set global bus for resource
34 bus = MessageBus.get_instance()
37 # ---------- API versions ---------- #
38 @api_monitoring_v1.route("/v1/api_versions")
39 class VersionRouter(Resource):
40 model = MonitoringApiV1DTO.api_version_info_get
42 @api_monitoring_v1.doc('Get Monitoring API version')
43 @api_monitoring_v1.marshal_list_with(model)
46 'uriPrefix': request.base_url.rsplit('/', 1)[0],
49 # 'isDeprecated': 'False',
50 # 'retirementDate': ''
55 # ---------- Alarm Event Record ---------- #
56 @api_monitoring_v1.route("/v1/alarms")
57 @api_monitoring_v1.param(PAGE_PARAM,
58 'Page number of the results to fetch.' +
60 _in='query', default=1)
61 @api_monitoring_v1.param(
63 'Set any value for show all fields. This value will cover "fields" ' +
66 @api_monitoring_v1.param(
68 'Set fields to show, split by comma, "/" for parent and children.' +
69 ' Like "name,parent/children". This value will cover' +
72 @api_monitoring_v1.param(
74 'Set fields to exclude showing, split by comma, "/" for parent and ' +
75 'children. Like "name,parent/children". This value will cover ' +
78 @api_monitoring_v1.param(
80 'Exclude showing all default fields, Set "true" to enable.',
82 @api_monitoring_v1.param(
84 'Filter of the query.',
86 class AlarmListRouter(Resource):
88 model = AlarmDTO.alarm_event_record_get
90 @api_monitoring_v1.doc('Get Alarm Event Record List')
91 @api_monitoring_v1.marshal_list_with(model)
93 parser = reqparse.RequestParser()
94 parser.add_argument(PAGE_PARAM, location='args')
95 parser.add_argument('filter', location='args')
96 args = parser.parse_args()
98 if args.nextpage_opaque_marker is not None:
99 kwargs['page'] = args.nextpage_opaque_marker
100 kwargs['filter'] = args.filter if args.filter is not None else ''
102 ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
103 return link_header(request.full_path, ret)
106 @api_monitoring_v1.route("/v1/alarms/<alarmEventRecordId>")
107 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
108 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
109 @api_monitoring_v1.param(
111 'Set any value for show all fields. This value will cover "fields" ' +
114 @api_monitoring_v1.param(
116 'Set fields to show, split by comma, "/" for parent and children.' +
117 ' Like "name,parent/children". This value will cover' +
118 ' "exculde_fields".',
120 @api_monitoring_v1.param(
122 'Set fields to exclude showing, split by comma, "/" for parent and ' +
123 'children. Like "name,parent/children". This value will cover ' +
124 '"exclude_default".',
126 @api_monitoring_v1.param(
128 'Exclude showing all default fields, Set "true" to enable.',
130 class AlarmGetRouter(Resource):
132 model = AlarmDTO.alarm_event_record_get
134 @api_monitoring_v1.doc('Get Alarm Event Record Information')
135 @api_monitoring_v1.marshal_with(model)
136 def get(self, alarmEventRecordId):
137 result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
138 if result is not None:
140 raise NotFoundException(
141 "Alarm Event Record {} doesn't exist".format(alarmEventRecordId))
143 @api_monitoring_v1.doc('Patch Alarm Event Record Information')
144 @api_monitoring_v1.marshal_with(model)
145 def patch(self, alarmEventRecordId):
146 result = alarm_view.alarm_event_record_ack(alarmEventRecordId, bus.uow)
147 if result is not None:
149 raise NotFoundException(
150 "Alarm Event Record {} doesn't exist".format(alarmEventRecordId))
153 # ---------- Alarm Subscriptions ---------- #
154 @api_monitoring_v1.route("/v1/alarmSubscriptions")
155 class SubscriptionsListRouter(Resource):
157 model = SubscriptionDTO.subscription_get
158 expect = SubscriptionDTO.subscription_create
160 @api_monitoring_v1.doc('Get Alarm Subscription List')
161 @api_monitoring_v1.marshal_list_with(model)
162 @api_monitoring_v1.param(
164 'Page number of the results to fetch. Default: 1',
165 _in='query', default=1)
166 @api_monitoring_v1.param(
168 'Set any value for show all fields. This value will cover "fields" ' +
171 @api_monitoring_v1.param(
173 'Set fields to show, split by comma, "/" for parent and children.' +
174 ' Like "name,parent/children". This value will cover' +
175 ' "exculde_fields".',
177 @api_monitoring_v1.param(
179 'Set fields to exclude showing, split by comma, "/" for parent and ' +
180 'children. Like "name,parent/children". This value will cover ' +
181 '"exclude_default".',
183 @api_monitoring_v1.param(
185 'Exclude showing all default fields, Set "true" to enable.',
187 @api_monitoring_v1.param(
189 'Filter of the query.',
192 parser = reqparse.RequestParser()
193 parser.add_argument(PAGE_PARAM, location='args')
194 parser.add_argument('filter', location='args')
195 args = parser.parse_args()
197 if args.nextpage_opaque_marker is not None:
198 kwargs['page'] = args.nextpage_opaque_marker
199 kwargs['filter'] = args.filter if args.filter is not None else ''
201 ret = alarm_view.subscriptions(bus.uow, **kwargs)
202 return link_header(request.full_path, ret)
204 @api_monitoring_v1.doc('Create a Alarm Subscription')
205 @api_monitoring_v1.expect(expect)
206 @api_monitoring_v1.marshal_with(
208 mask='{alarmSubscriptionId,callback,consumerSubscriptionId,filter}')
210 data = api_monitoring_v1.payload
211 callback = data.get('callback', None)
213 raise BadRequestException('The callback parameter is required')
215 result = alarm_view.subscription_create(data, bus.uow)
219 @api_monitoring_v1.route("/v1/alarmSubscriptions/<alarmSubscriptionID>")
220 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
221 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
222 class SubscriptionGetDelRouter(Resource):
224 model = SubscriptionDTO.subscription_get
226 @api_monitoring_v1.doc('Get Alarm Subscription Information')
227 @api_monitoring_v1.marshal_with(model)
228 @api_monitoring_v1.param(
230 'Set any value for show all fields. This value will cover "fields" ' +
233 @api_monitoring_v1.param(
235 'Set fields to show, split by comma, "/" for parent and children.' +
236 ' Like "name,parent/children". This value will cover' +
237 ' "exculde_fields".',
239 @api_monitoring_v1.param(
241 'Set fields to exclude showing, split by comma, "/" for parent and ' +
242 'children. Like "name,parent/children". This value will cover ' +
243 '"exclude_default".',
245 @api_monitoring_v1.param(
247 'Exclude showing all default fields, Set "true" to enable.',
249 def get(self, alarmSubscriptionID):
250 result = alarm_view.subscription_one(
251 alarmSubscriptionID, bus.uow)
252 if result is not None:
254 raise NotFoundException(
255 "Subscription {} doesn't exist".format(alarmSubscriptionID))
257 @api_monitoring_v1.doc('Delete an Alarm Subscription')
258 @api_monitoring_v1.response(200, 'Subscription deleted')
259 def delete(self, alarmSubscriptionID):
260 result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)