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_restx import Resource
17 from o2common.service.messagebus import MessageBus
18 from o2ims.views import alarm_view
19 from o2ims.views.api_ns import api_monitoring_v1
20 from o2ims.views.alarm_dto import AlarmDTO, SubscriptionDTO
22 from o2common.helper import o2logging
23 logger = o2logging.get_logger(__name__)
26 def configure_api_route():
27 # Set global bus for resource
29 bus = MessageBus.get_instance()
32 # ---------- Alarm Event Record ---------- #
33 @api_monitoring_v1.route("/alarms")
34 class AlarmListRouter(Resource):
36 model = AlarmDTO.alarm_event_record_get
38 @api_monitoring_v1.marshal_list_with(model)
40 return alarm_view.alarm_event_records(bus.uow)
43 @api_monitoring_v1.route("/alarms/<alarmEventRecordId>")
44 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
45 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
46 class AlarmGetRouter(Resource):
48 model = AlarmDTO.alarm_event_record_get
50 @api_monitoring_v1.doc('Get resource type')
51 @api_monitoring_v1.marshal_with(model)
52 def get(self, alarmEventRecordId):
53 result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
54 if result is not None:
56 api_monitoring_v1.abort(
57 404, "Resource type {} doesn't exist".format(alarmEventRecordId))
60 # ---------- Alarm Subscriptions ---------- #
61 @api_monitoring_v1.route("/alarmSubscriptions")
62 class SubscriptionsListRouter(Resource):
64 model = SubscriptionDTO.subscription_get
65 expect = SubscriptionDTO.subscription
66 post_resp = SubscriptionDTO.subscription_post_resp
68 @api_monitoring_v1.doc('List alarm subscriptions')
69 @api_monitoring_v1.marshal_list_with(model)
71 return alarm_view.subscriptions(bus.uow)
73 @api_monitoring_v1.doc('Create a alarm subscription')
74 @api_monitoring_v1.expect(expect)
75 @api_monitoring_v1.marshal_with(post_resp, code=201)
77 data = api_monitoring_v1.payload
78 result = alarm_view.subscription_create(data, bus.uow)
82 @api_monitoring_v1.route("/alarmSubscriptions/<alarmSubscriptionID>")
83 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
84 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
85 class SubscriptionGetDelRouter(Resource):
87 model = SubscriptionDTO.subscription_get
89 @api_monitoring_v1.doc('Get Alarm Subscription by ID')
90 @api_monitoring_v1.marshal_with(model)
91 def get(self, alarmSubscriptionID):
92 result = alarm_view.subscription_one(
93 alarmSubscriptionID, bus.uow)
94 if result is not None:
96 api_monitoring_v1.abort(404, "Subscription {} doesn't exist".format(
99 @api_monitoring_v1.doc('Delete subscription by ID')
100 @api_monitoring_v1.response(204, 'Subscription deleted')
101 def delete(self, alarmSubscriptionID):
102 result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)