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
21 from o2ims.views import alarm_view
22 from o2ims.views.api_ns import api_ims_monitoring as api_monitoring_v1
23 from o2ims.views.alarm_dto import AlarmDTO, SubscriptionDTO
25 from o2common.helper import o2logging
26 logger = o2logging.get_logger(__name__)
29 def configure_api_route():
30 # Set global bus for resource
32 bus = MessageBus.get_instance()
35 # ---------- API versions ---------- #
36 @api_monitoring_v1.route("/v1/api_versions")
37 class VersionRouter(Resource):
40 'uriPrefix': request.base_url.rsplit('/', 1)[0],
43 # 'isDeprecated': 'False',
44 # 'retirementDate': ''
49 # ---------- Alarm Event Record ---------- #
50 @api_monitoring_v1.route("/v1/alarms")
51 @api_monitoring_v1.param(PAGE_PARAM,
52 'Page number of the results to fetch.' +
54 _in='query', default=1)
55 @api_monitoring_v1.param(
57 'Set any value for show all fields. This value will cover "fields" ' +
60 @api_monitoring_v1.param(
62 'Set fields to show, split by comma, "/" for parent and children.' +
63 ' Like "name,parent/children". This value will cover' +
66 @api_monitoring_v1.param(
68 'Set fields to exclude showing, split by comma, "/" for parent and ' +
69 'children. Like "name,parent/children". This value will cover ' +
72 @api_monitoring_v1.param(
74 'Exclude showing all default fields, Set "true" to enable.',
76 @api_monitoring_v1.param(
78 'Filter of the query.',
80 class AlarmListRouter(Resource):
82 model = AlarmDTO.alarm_event_record_get
84 @api_monitoring_v1.marshal_list_with(model)
86 parser = reqparse.RequestParser()
87 parser.add_argument(PAGE_PARAM, location='args')
88 parser.add_argument('filter', location='args')
89 args = parser.parse_args()
91 if args.nextpage_opaque_marker is not None:
92 kwargs['page'] = args.nextpage_opaque_marker
93 kwargs['filter'] = args.filter if args.filter is not None else ''
95 ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
96 return link_header(request.full_path, ret)
99 @api_monitoring_v1.route("/v1/alarms/<alarmEventRecordId>")
100 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
101 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
102 @api_monitoring_v1.param(
104 'Set any value for show all fields. This value will cover "fields" ' +
107 @api_monitoring_v1.param(
109 'Set fields to show, split by comma, "/" for parent and children.' +
110 ' Like "name,parent/children". This value will cover' +
111 ' "exculde_fields".',
113 @api_monitoring_v1.param(
115 'Set fields to exclude showing, split by comma, "/" for parent and ' +
116 'children. Like "name,parent/children". This value will cover ' +
117 '"exclude_default".',
119 @api_monitoring_v1.param(
121 'Exclude showing all default fields, Set "true" to enable.',
123 class AlarmGetRouter(Resource):
125 model = AlarmDTO.alarm_event_record_get
127 @api_monitoring_v1.doc('Get AlarmEventRecord')
128 @api_monitoring_v1.marshal_with(model)
129 def get(self, alarmEventRecordId):
130 result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
131 if result is not None:
133 raise NotFoundException(
134 "Alarm Event Record {} doesn't exist".format(alarmEventRecordId))
137 # ---------- Alarm Subscriptions ---------- #
138 @api_monitoring_v1.route("/v1/alarmSubscriptions")
139 class SubscriptionsListRouter(Resource):
141 model = SubscriptionDTO.subscription_get
142 expect = SubscriptionDTO.subscription
143 post_resp = SubscriptionDTO.subscription_post_resp
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(post_resp, code=201)
193 data = api_monitoring_v1.payload
194 result = alarm_view.subscription_create(data, bus.uow)
198 @api_monitoring_v1.route("/v1/alarmSubscriptions/<alarmSubscriptionID>")
199 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
200 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
201 class SubscriptionGetDelRouter(Resource):
203 model = SubscriptionDTO.subscription_get
205 @api_monitoring_v1.doc('Get Alarm Subscription by ID')
206 @api_monitoring_v1.marshal_with(model)
207 @api_monitoring_v1.param(
209 'Set any value for show all fields. This value will cover "fields" ' +
212 @api_monitoring_v1.param(
214 'Set fields to show, split by comma, "/" for parent and children.' +
215 ' Like "name,parent/children". This value will cover' +
216 ' "exculde_fields".',
218 @api_monitoring_v1.param(
220 'Set fields to exclude showing, split by comma, "/" for parent and ' +
221 'children. Like "name,parent/children". This value will cover ' +
222 '"exclude_default".',
224 @api_monitoring_v1.param(
226 'Exclude showing all default fields, Set "true" to enable.',
228 def get(self, alarmSubscriptionID):
229 result = alarm_view.subscription_one(
230 alarmSubscriptionID, bus.uow)
231 if result is not None:
233 raise NotFoundException(
234 "Subscription {} doesn't exist".format(alarmSubscriptionID))
236 @api_monitoring_v1.doc('Delete subscription by ID')
237 @api_monitoring_v1.response(200, 'Subscription deleted')
238 def delete(self, alarmSubscriptionID):
239 result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)