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):
39 @api_monitoring_v1.doc('Get Monitoring API version')
42 'uriPrefix': request.base_url.rsplit('/', 1)[0],
45 # 'isDeprecated': 'False',
46 # 'retirementDate': ''
51 # ---------- Alarm Event Record ---------- #
52 @api_monitoring_v1.route("/v1/alarms")
53 @api_monitoring_v1.param(PAGE_PARAM,
54 'Page number of the results to fetch.' +
56 _in='query', default=1)
57 @api_monitoring_v1.param(
59 'Set any value for show all fields. This value will cover "fields" ' +
62 @api_monitoring_v1.param(
64 'Set fields to show, split by comma, "/" for parent and children.' +
65 ' Like "name,parent/children". This value will cover' +
68 @api_monitoring_v1.param(
70 'Set fields to exclude showing, split by comma, "/" for parent and ' +
71 'children. Like "name,parent/children". This value will cover ' +
74 @api_monitoring_v1.param(
76 'Exclude showing all default fields, Set "true" to enable.',
78 @api_monitoring_v1.param(
80 'Filter of the query.',
82 class AlarmListRouter(Resource):
84 model = AlarmDTO.alarm_event_record_get
86 @api_monitoring_v1.doc('Get Alarm Event Record List')
87 @api_monitoring_v1.marshal_list_with(model)
89 parser = reqparse.RequestParser()
90 parser.add_argument(PAGE_PARAM, location='args')
91 parser.add_argument('filter', location='args')
92 args = parser.parse_args()
94 if args.nextpage_opaque_marker is not None:
95 kwargs['page'] = args.nextpage_opaque_marker
96 kwargs['filter'] = args.filter if args.filter is not None else ''
98 ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
99 return link_header(request.full_path, ret)
102 @api_monitoring_v1.route("/v1/alarms/<alarmEventRecordId>")
103 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
104 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
105 @api_monitoring_v1.param(
107 'Set any value for show all fields. This value will cover "fields" ' +
110 @api_monitoring_v1.param(
112 'Set fields to show, split by comma, "/" for parent and children.' +
113 ' Like "name,parent/children". This value will cover' +
114 ' "exculde_fields".',
116 @api_monitoring_v1.param(
118 'Set fields to exclude showing, split by comma, "/" for parent and ' +
119 'children. Like "name,parent/children". This value will cover ' +
120 '"exclude_default".',
122 @api_monitoring_v1.param(
124 'Exclude showing all default fields, Set "true" to enable.',
126 class AlarmGetRouter(Resource):
128 model = AlarmDTO.alarm_event_record_get
130 @api_monitoring_v1.doc('Get Alarm Event Record Information')
131 @api_monitoring_v1.marshal_with(model)
132 def get(self, alarmEventRecordId):
133 result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
134 if result is not None:
136 raise NotFoundException(
137 "Alarm Event Record {} doesn't exist".format(alarmEventRecordId))
140 # ---------- Alarm Subscriptions ---------- #
141 @api_monitoring_v1.route("/v1/alarmSubscriptions")
142 class SubscriptionsListRouter(Resource):
144 model = SubscriptionDTO.subscription_get
145 expect = SubscriptionDTO.subscription_create
147 @api_monitoring_v1.doc('Get Alarm Subscription List')
148 @api_monitoring_v1.marshal_list_with(model)
149 @api_monitoring_v1.param(
151 'Page number of the results to fetch. Default: 1',
152 _in='query', default=1)
153 @api_monitoring_v1.param(
155 'Set any value for show all fields. This value will cover "fields" ' +
158 @api_monitoring_v1.param(
160 'Set fields to show, split by comma, "/" for parent and children.' +
161 ' Like "name,parent/children". This value will cover' +
162 ' "exculde_fields".',
164 @api_monitoring_v1.param(
166 'Set fields to exclude showing, split by comma, "/" for parent and ' +
167 'children. Like "name,parent/children". This value will cover ' +
168 '"exclude_default".',
170 @api_monitoring_v1.param(
172 'Exclude showing all default fields, Set "true" to enable.',
174 @api_monitoring_v1.param(
176 'Filter of the query.',
179 parser = reqparse.RequestParser()
180 parser.add_argument(PAGE_PARAM, location='args')
181 parser.add_argument('filter', location='args')
182 args = parser.parse_args()
184 if args.nextpage_opaque_marker is not None:
185 kwargs['page'] = args.nextpage_opaque_marker
186 kwargs['filter'] = args.filter if args.filter is not None else ''
188 ret = alarm_view.subscriptions(bus.uow, **kwargs)
189 return link_header(request.full_path, ret)
191 @api_monitoring_v1.doc('Create a Alarm Subscription')
192 @api_monitoring_v1.expect(expect)
193 @api_monitoring_v1.marshal_with(
195 mask='{alarmSubscriptionId,callback,consumerSubscriptionId,filter}')
197 data = api_monitoring_v1.payload
198 callback = data.get('callback', None)
200 raise BadRequestException('The callback parameter is required')
202 result = alarm_view.subscription_create(data, bus.uow)
206 @api_monitoring_v1.route("/v1/alarmSubscriptions/<alarmSubscriptionID>")
207 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
208 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
209 class SubscriptionGetDelRouter(Resource):
211 model = SubscriptionDTO.subscription_get
213 @api_monitoring_v1.doc('Get Alarm Subscription Information')
214 @api_monitoring_v1.marshal_with(model)
215 @api_monitoring_v1.param(
217 'Set any value for show all fields. This value will cover "fields" ' +
220 @api_monitoring_v1.param(
222 'Set fields to show, split by comma, "/" for parent and children.' +
223 ' Like "name,parent/children". This value will cover' +
224 ' "exculde_fields".',
226 @api_monitoring_v1.param(
228 'Set fields to exclude showing, split by comma, "/" for parent and ' +
229 'children. Like "name,parent/children". This value will cover ' +
230 '"exclude_default".',
232 @api_monitoring_v1.param(
234 'Exclude showing all default fields, Set "true" to enable.',
236 def get(self, alarmSubscriptionID):
237 result = alarm_view.subscription_one(
238 alarmSubscriptionID, bus.uow)
239 if result is not None:
241 raise NotFoundException(
242 "Subscription {} doesn't exist".format(alarmSubscriptionID))
244 @api_monitoring_v1.doc('Delete an Alarm Subscription')
245 @api_monitoring_v1.response(200, 'Subscription deleted')
246 def delete(self, alarmSubscriptionID):
247 result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)