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
26 from o2common.helper import o2logging
27 logger = o2logging.get_logger(__name__)
30 def configure_api_route():
31 # Set global bus for resource
33 bus = MessageBus.get_instance()
36 # ---------- API versions ---------- #
37 @api_monitoring_v1.route("/v1/api_versions")
38 class VersionRouter(Resource):
41 'uriPrefix': request.base_url.rsplit('/', 1)[0],
44 # 'isDeprecated': 'False',
45 # 'retirementDate': ''
50 # ---------- Alarm Event Record ---------- #
51 @api_monitoring_v1.route("/v1/alarms")
52 @api_monitoring_v1.param(PAGE_PARAM,
53 'Page number of the results to fetch.' +
55 _in='query', default=1)
56 @api_monitoring_v1.param(
58 'Set any value for show all fields. This value will cover "fields" ' +
61 @api_monitoring_v1.param(
63 'Set fields to show, split by comma, "/" for parent and children.' +
64 ' Like "name,parent/children". This value will cover' +
67 @api_monitoring_v1.param(
69 'Set fields to exclude showing, split by comma, "/" for parent and ' +
70 'children. Like "name,parent/children". This value will cover ' +
73 @api_monitoring_v1.param(
75 'Exclude showing all default fields, Set "true" to enable.',
77 @api_monitoring_v1.param(
79 'Filter of the query.',
81 class AlarmListRouter(Resource):
83 model = AlarmDTO.alarm_event_record_get
85 @api_monitoring_v1.marshal_list_with(model)
87 parser = reqparse.RequestParser()
88 parser.add_argument(PAGE_PARAM, location='args')
89 parser.add_argument('filter', location='args')
90 args = parser.parse_args()
92 if args.nextpage_opaque_marker is not None:
93 kwargs['page'] = args.nextpage_opaque_marker
94 kwargs['filter'] = args.filter if args.filter is not None else ''
96 ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
97 return link_header(request.full_path, ret)
100 @api_monitoring_v1.route("/v1/alarms/<alarmEventRecordId>")
101 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
102 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
103 @api_monitoring_v1.param(
105 'Set any value for show all fields. This value will cover "fields" ' +
108 @api_monitoring_v1.param(
110 'Set fields to show, split by comma, "/" for parent and children.' +
111 ' Like "name,parent/children". This value will cover' +
112 ' "exculde_fields".',
114 @api_monitoring_v1.param(
116 'Set fields to exclude showing, split by comma, "/" for parent and ' +
117 'children. Like "name,parent/children". This value will cover ' +
118 '"exclude_default".',
120 @api_monitoring_v1.param(
122 'Exclude showing all default fields, Set "true" to enable.',
124 class AlarmGetRouter(Resource):
126 model = AlarmDTO.alarm_event_record_get
128 @api_monitoring_v1.doc('Get AlarmEventRecord')
129 @api_monitoring_v1.marshal_with(model)
130 def get(self, alarmEventRecordId):
131 result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
132 if result is not None:
134 raise NotFoundException(
135 "Alarm Event Record {} doesn't exist".format(alarmEventRecordId))
138 # ---------- Alarm Subscriptions ---------- #
139 @api_monitoring_v1.route("/v1/alarmSubscriptions")
140 class SubscriptionsListRouter(Resource):
142 model = SubscriptionDTO.subscription_get
143 expect = SubscriptionDTO.subscription_create
145 @api_monitoring_v1.doc('List alarm subscriptions')
146 @api_monitoring_v1.marshal_list_with(model)
147 @api_monitoring_v1.param(
149 'Page number of the results to fetch. Default: 1',
150 _in='query', default=1)
151 @api_monitoring_v1.param(
153 'Set any value for show all fields. This value will cover "fields" ' +
156 @api_monitoring_v1.param(
158 'Set fields to show, split by comma, "/" for parent and children.' +
159 ' Like "name,parent/children". This value will cover' +
160 ' "exculde_fields".',
162 @api_monitoring_v1.param(
164 'Set fields to exclude showing, split by comma, "/" for parent and ' +
165 'children. Like "name,parent/children". This value will cover ' +
166 '"exclude_default".',
168 @api_monitoring_v1.param(
170 'Exclude showing all default fields, Set "true" to enable.',
172 @api_monitoring_v1.param(
174 'Filter of the query.',
177 parser = reqparse.RequestParser()
178 parser.add_argument(PAGE_PARAM, location='args')
179 parser.add_argument('filter', location='args')
180 args = parser.parse_args()
182 if args.nextpage_opaque_marker is not None:
183 kwargs['page'] = args.nextpage_opaque_marker
184 kwargs['filter'] = args.filter if args.filter is not None else ''
186 ret = alarm_view.subscriptions(bus.uow, **kwargs)
187 return link_header(request.full_path, ret)
189 @api_monitoring_v1.doc('Create a alarm subscription')
190 @api_monitoring_v1.expect(expect)
191 @api_monitoring_v1.marshal_with(
193 mask='{alarmSubscriptionId,callback,consumerSubscriptionId,filter}')
195 data = api_monitoring_v1.payload
196 callback = data.get('callback', None)
198 raise BadRequestException('The callback parameter is required')
200 result = alarm_view.subscription_create(data, bus.uow)
204 @api_monitoring_v1.route("/v1/alarmSubscriptions/<alarmSubscriptionID>")
205 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
206 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
207 class SubscriptionGetDelRouter(Resource):
209 model = SubscriptionDTO.subscription_get
211 @api_monitoring_v1.doc('Get Alarm Subscription by ID')
212 @api_monitoring_v1.marshal_with(model)
213 @api_monitoring_v1.param(
215 'Set any value for show all fields. This value will cover "fields" ' +
218 @api_monitoring_v1.param(
220 'Set fields to show, split by comma, "/" for parent and children.' +
221 ' Like "name,parent/children". This value will cover' +
222 ' "exculde_fields".',
224 @api_monitoring_v1.param(
226 'Set fields to exclude showing, split by comma, "/" for parent and ' +
227 'children. Like "name,parent/children". This value will cover ' +
228 '"exclude_default".',
230 @api_monitoring_v1.param(
232 'Exclude showing all default fields, Set "true" to enable.',
234 def get(self, alarmSubscriptionID):
235 result = alarm_view.subscription_one(
236 alarmSubscriptionID, bus.uow)
237 if result is not None:
239 raise NotFoundException(
240 "Subscription {} doesn't exist".format(alarmSubscriptionID))
242 @api_monitoring_v1.doc('Delete subscription by ID')
243 @api_monitoring_v1.response(200, 'Subscription deleted')
244 def delete(self, alarmSubscriptionID):
245 result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)