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_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 # ---------- Alarm Event Record ---------- #
35 @api_monitoring_v1.route("/alarms")
36 @api_monitoring_v1.param(PAGE_PARAM,
37 'Page number of the results to fetch.' +
39 _in='query', default=1)
40 class AlarmListRouter(Resource):
42 model = AlarmDTO.alarm_event_record_get
44 @api_monitoring_v1.marshal_list_with(model)
46 parser = reqparse.RequestParser()
47 parser.add_argument(PAGE_PARAM, location='args')
48 args = parser.parse_args()
50 if args.nextpage_opaque_marker is not None:
51 kwargs['page'] = args.nextpage_opaque_marker
53 ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
54 return link_header(request.full_path, ret)
57 @api_monitoring_v1.route("/alarms/<alarmEventRecordId>")
58 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
59 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
60 class AlarmGetRouter(Resource):
62 model = AlarmDTO.alarm_event_record_get
64 @api_monitoring_v1.doc('Get resource type')
65 @api_monitoring_v1.marshal_with(model)
66 def get(self, alarmEventRecordId):
67 result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
68 if result is not None:
70 api_monitoring_v1.abort(
71 404, "Resource type {} doesn't exist".format(alarmEventRecordId))
74 # ---------- Alarm Subscriptions ---------- #
75 @api_monitoring_v1.route("/alarmSubscriptions")
76 class SubscriptionsListRouter(Resource):
78 model = SubscriptionDTO.subscription_get
79 expect = SubscriptionDTO.subscription
80 post_resp = SubscriptionDTO.subscription_post_resp
82 @api_monitoring_v1.doc('List alarm subscriptions')
83 @api_monitoring_v1.marshal_list_with(model)
85 parser = reqparse.RequestParser()
86 parser.add_argument(PAGE_PARAM, location='args')
87 args = parser.parse_args()
89 if args.nextpage_opaque_marker is not None:
90 kwargs['page'] = args.nextpage_opaque_marker
92 ret = alarm_view.subscriptions(bus.uow, **kwargs)
93 return link_header(request.full_path, ret)
95 @api_monitoring_v1.doc('Create a alarm subscription')
96 @api_monitoring_v1.expect(expect)
97 @api_monitoring_v1.marshal_with(post_resp, code=201)
99 data = api_monitoring_v1.payload
100 result = alarm_view.subscription_create(data, bus.uow)
104 @api_monitoring_v1.route("/alarmSubscriptions/<alarmSubscriptionID>")
105 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
106 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
107 class SubscriptionGetDelRouter(Resource):
109 model = SubscriptionDTO.subscription_get
111 @api_monitoring_v1.doc('Get Alarm Subscription by ID')
112 @api_monitoring_v1.marshal_with(model)
113 def get(self, alarmSubscriptionID):
114 result = alarm_view.subscription_one(
115 alarmSubscriptionID, bus.uow)
116 if result is not None:
118 api_monitoring_v1.abort(404, "Subscription {} doesn't exist".format(
119 alarmSubscriptionID))
121 @api_monitoring_v1.doc('Delete subscription by ID')
122 @api_monitoring_v1.response(204, 'Subscription deleted')
123 def delete(self, alarmSubscriptionID):
124 result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)