1 # Copyright (C) 2021 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))
144 # ---------- Alarm Subscriptions ---------- #
145 @api_monitoring_v1.route("/v1/alarmSubscriptions")
146 class SubscriptionsListRouter(Resource):
148 model = SubscriptionDTO.subscription_get
149 expect = SubscriptionDTO.subscription_create
151 @api_monitoring_v1.doc('Get Alarm Subscription List')
152 @api_monitoring_v1.marshal_list_with(model)
153 @api_monitoring_v1.param(
155 'Page number of the results to fetch. Default: 1',
156 _in='query', default=1)
157 @api_monitoring_v1.param(
159 'Set any value for show all fields. This value will cover "fields" ' +
162 @api_monitoring_v1.param(
164 'Set fields to show, split by comma, "/" for parent and children.' +
165 ' Like "name,parent/children". This value will cover' +
166 ' "exculde_fields".',
168 @api_monitoring_v1.param(
170 'Set fields to exclude showing, split by comma, "/" for parent and ' +
171 'children. Like "name,parent/children". This value will cover ' +
172 '"exclude_default".',
174 @api_monitoring_v1.param(
176 'Exclude showing all default fields, Set "true" to enable.',
178 @api_monitoring_v1.param(
180 'Filter of the query.',
183 parser = reqparse.RequestParser()
184 parser.add_argument(PAGE_PARAM, location='args')
185 parser.add_argument('filter', location='args')
186 args = parser.parse_args()
188 if args.nextpage_opaque_marker is not None:
189 kwargs['page'] = args.nextpage_opaque_marker
190 kwargs['filter'] = args.filter if args.filter is not None else ''
192 ret = alarm_view.subscriptions(bus.uow, **kwargs)
193 return link_header(request.full_path, ret)
195 @api_monitoring_v1.doc('Create a Alarm Subscription')
196 @api_monitoring_v1.expect(expect)
197 @api_monitoring_v1.marshal_with(
199 mask='{alarmSubscriptionId,callback,consumerSubscriptionId,filter}')
201 data = api_monitoring_v1.payload
202 callback = data.get('callback', None)
204 raise BadRequestException('The callback parameter is required')
206 result = alarm_view.subscription_create(data, bus.uow)
210 @api_monitoring_v1.route("/v1/alarmSubscriptions/<alarmSubscriptionID>")
211 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
212 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
213 class SubscriptionGetDelRouter(Resource):
215 model = SubscriptionDTO.subscription_get
217 @api_monitoring_v1.doc('Get Alarm Subscription Information')
218 @api_monitoring_v1.marshal_with(model)
219 @api_monitoring_v1.param(
221 'Set any value for show all fields. This value will cover "fields" ' +
224 @api_monitoring_v1.param(
226 'Set fields to show, split by comma, "/" for parent and children.' +
227 ' Like "name,parent/children". This value will cover' +
228 ' "exculde_fields".',
230 @api_monitoring_v1.param(
232 'Set fields to exclude showing, split by comma, "/" for parent and ' +
233 'children. Like "name,parent/children". This value will cover ' +
234 '"exclude_default".',
236 @api_monitoring_v1.param(
238 'Exclude showing all default fields, Set "true" to enable.',
240 def get(self, alarmSubscriptionID):
241 result = alarm_view.subscription_one(
242 alarmSubscriptionID, bus.uow)
243 if result is not None:
245 raise NotFoundException(
246 "Subscription {} doesn't exist".format(alarmSubscriptionID))
248 @api_monitoring_v1.doc('Delete an Alarm Subscription')
249 @api_monitoring_v1.response(200, 'Subscription deleted')
250 def delete(self, alarmSubscriptionID):
251 result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)