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 o2ims.views import alarm_view
21 from o2ims.views.api_ns import api_ims_monitoring as api_monitoring_v1
22 from o2ims.views.alarm_dto import AlarmDTO, SubscriptionDTO
24 from o2common.helper import o2logging
25 logger = o2logging.get_logger(__name__)
28 def configure_api_route():
29 # Set global bus for resource
31 bus = MessageBus.get_instance()
34 # ---------- API versions ---------- #
35 @api_monitoring_v1.route("/v1/api_versions")
36 class VersionRouter(Resource):
39 'uriPrefix': request.base_url.rsplit('/', 1)[0],
42 # 'isDeprecated': 'False',
43 # 'retirementDate': ''
48 # ---------- Alarm Event Record ---------- #
49 @api_monitoring_v1.route("/v1/alarms")
50 @api_monitoring_v1.param(PAGE_PARAM,
51 'Page number of the results to fetch.' +
53 _in='query', default=1)
54 @api_monitoring_v1.param(
56 'Set any value for show all fields. This value will cover "fields" ' +
59 @api_monitoring_v1.param(
61 'Set fields to show, split by comma, "/" for parent and children.' +
62 ' Like "name,parent/children". This value will cover' +
65 @api_monitoring_v1.param(
67 'Set fields to exclude showing, split by comma, "/" for parent and ' +
68 'children. Like "name,parent/children". This value will cover ' +
71 @api_monitoring_v1.param(
73 'Exclude showing all default fields, Set "true" to enable.',
75 @api_monitoring_v1.param(
77 'Filter of the query.',
79 class AlarmListRouter(Resource):
81 model = AlarmDTO.alarm_event_record_get
83 @api_monitoring_v1.marshal_list_with(model)
85 parser = reqparse.RequestParser()
86 parser.add_argument(PAGE_PARAM, location='args')
87 parser.add_argument('filter', location='args')
88 args = parser.parse_args()
90 if args.nextpage_opaque_marker is not None:
91 kwargs['page'] = args.nextpage_opaque_marker
92 kwargs['filter'] = args.filter if args.filter is not None else ''
94 ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
95 return link_header(request.full_path, ret)
98 @api_monitoring_v1.route("/v1/alarms/<alarmEventRecordId>")
99 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
100 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
101 @api_monitoring_v1.param(
103 'Set any value for show all fields. This value will cover "fields" ' +
106 @api_monitoring_v1.param(
108 'Set fields to show, split by comma, "/" for parent and children.' +
109 ' Like "name,parent/children". This value will cover' +
110 ' "exculde_fields".',
112 @api_monitoring_v1.param(
114 'Set fields to exclude showing, split by comma, "/" for parent and ' +
115 'children. Like "name,parent/children". This value will cover ' +
116 '"exclude_default".',
118 @api_monitoring_v1.param(
120 'Exclude showing all default fields, Set "true" to enable.',
122 class AlarmGetRouter(Resource):
124 model = AlarmDTO.alarm_event_record_get
126 @api_monitoring_v1.doc('Get resource type')
127 @api_monitoring_v1.marshal_with(model)
128 def get(self, alarmEventRecordId):
129 result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
130 if result is not None:
132 api_monitoring_v1.abort(
133 404, "Resource type {} doesn't exist".format(alarmEventRecordId))
136 # ---------- Alarm Subscriptions ---------- #
137 @api_monitoring_v1.route("/v1/alarmSubscriptions")
138 class SubscriptionsListRouter(Resource):
140 model = SubscriptionDTO.subscription_get
141 expect = SubscriptionDTO.subscription
142 post_resp = SubscriptionDTO.subscription_post_resp
144 @api_monitoring_v1.doc('List alarm subscriptions')
145 @api_monitoring_v1.marshal_list_with(model)
146 @api_monitoring_v1.param(
148 'Page number of the results to fetch. Default: 1',
149 _in='query', default=1)
150 @api_monitoring_v1.param(
152 'Set any value for show all fields. This value will cover "fields" ' +
155 @api_monitoring_v1.param(
157 'Set fields to show, split by comma, "/" for parent and children.' +
158 ' Like "name,parent/children". This value will cover' +
159 ' "exculde_fields".',
161 @api_monitoring_v1.param(
163 'Set fields to exclude showing, split by comma, "/" for parent and ' +
164 'children. Like "name,parent/children". This value will cover ' +
165 '"exclude_default".',
167 @api_monitoring_v1.param(
169 'Exclude showing all default fields, Set "true" to enable.',
171 @api_monitoring_v1.param(
173 'Filter of the query.',
176 parser = reqparse.RequestParser()
177 parser.add_argument(PAGE_PARAM, location='args')
178 parser.add_argument('filter', location='args')
179 args = parser.parse_args()
181 if args.nextpage_opaque_marker is not None:
182 kwargs['page'] = args.nextpage_opaque_marker
183 kwargs['filter'] = args.filter if args.filter is not None else ''
185 ret = alarm_view.subscriptions(bus.uow, **kwargs)
186 return link_header(request.full_path, ret)
188 @api_monitoring_v1.doc('Create a alarm subscription')
189 @api_monitoring_v1.expect(expect)
190 @api_monitoring_v1.marshal_with(post_resp, code=201)
192 data = api_monitoring_v1.payload
193 result = alarm_view.subscription_create(data, bus.uow)
197 @api_monitoring_v1.route("/v1/alarmSubscriptions/<alarmSubscriptionID>")
198 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
199 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
200 class SubscriptionGetDelRouter(Resource):
202 model = SubscriptionDTO.subscription_get
204 @api_monitoring_v1.doc('Get Alarm Subscription by ID')
205 @api_monitoring_v1.marshal_with(model)
206 @api_monitoring_v1.param(
208 'Set any value for show all fields. This value will cover "fields" ' +
211 @api_monitoring_v1.param(
213 'Set fields to show, split by comma, "/" for parent and children.' +
214 ' Like "name,parent/children". This value will cover' +
215 ' "exculde_fields".',
217 @api_monitoring_v1.param(
219 'Set fields to exclude showing, split by comma, "/" for parent and ' +
220 'children. Like "name,parent/children". This value will cover ' +
221 '"exclude_default".',
223 @api_monitoring_v1.param(
225 'Exclude showing all default fields, Set "true" to enable.',
227 def get(self, alarmSubscriptionID):
228 result = alarm_view.subscription_one(
229 alarmSubscriptionID, bus.uow)
230 if result is not None:
232 api_monitoring_v1.abort(404, "Subscription {} doesn't exist".format(
233 alarmSubscriptionID))
235 @api_monitoring_v1.doc('Delete subscription by ID')
236 @api_monitoring_v1.response(204, 'Subscription deleted')
237 def delete(self, alarmSubscriptionID):
238 result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)