91ca8f86e3445bf1ff882acf9f57c6c07c8261ec
[pti/o2.git] / o2ims / views / alarm_route.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 from flask_restx import Resource
16
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
21
22 from o2common.helper import o2logging
23 logger = o2logging.get_logger(__name__)
24
25
26 def configure_api_route():
27     # Set global bus for resource
28     global bus
29     bus = MessageBus.get_instance()
30
31
32 # ----------  Alarm Event Record ---------- #
33 @api_monitoring_v1.route("/alarms")
34 class AlarmListRouter(Resource):
35
36     model = AlarmDTO.alarm_event_record_get
37
38     @api_monitoring_v1.marshal_list_with(model)
39     def get(self):
40         return alarm_view.alarm_event_records(bus.uow)
41
42
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):
47
48     model = AlarmDTO.alarm_event_record_get
49
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:
55             return result
56         api_monitoring_v1.abort(
57             404, "Resource type {} doesn't exist".format(alarmEventRecordId))
58
59
60 # ----------  Alarm Subscriptions ---------- #
61 @api_monitoring_v1.route("/alarmSubscriptions")
62 class SubscriptionsListRouter(Resource):
63
64     model = SubscriptionDTO.subscription_get
65     expect = SubscriptionDTO.subscription
66     post_resp = SubscriptionDTO.subscription_post_resp
67
68     @api_monitoring_v1.doc('List alarm subscriptions')
69     @api_monitoring_v1.marshal_list_with(model)
70     def get(self):
71         return alarm_view.subscriptions(bus.uow)
72
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)
76     def post(self):
77         data = api_monitoring_v1.payload
78         result = alarm_view.subscription_create(data, bus.uow)
79         return result, 201
80
81
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):
86
87     model = SubscriptionDTO.subscription_get
88
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:
95             return result
96         api_monitoring_v1.abort(404, "Subscription {} doesn't exist".format(
97             alarmSubscriptionID))
98
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)
103         return result, 204