6b32eeec652addce583240b3dcc91154b3e984fc
[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 import request
16 from flask_restx import Resource, reqparse
17
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
23
24 from o2common.helper import o2logging
25 logger = o2logging.get_logger(__name__)
26
27
28 def configure_api_route():
29     # Set global bus for resource
30     global bus
31     bus = MessageBus.get_instance()
32
33
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.' +
38                          ' Default: 1',
39                          _in='query', default=1)
40 class AlarmListRouter(Resource):
41
42     model = AlarmDTO.alarm_event_record_get
43
44     @api_monitoring_v1.marshal_list_with(model)
45     def get(self):
46         parser = reqparse.RequestParser()
47         parser.add_argument(PAGE_PARAM, location='args')
48         args = parser.parse_args()
49         kwargs = {}
50         if args.nextpage_opaque_marker is not None:
51             kwargs['page'] = args.nextpage_opaque_marker
52
53         ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
54         return link_header(request.full_path, ret)
55
56
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):
61
62     model = AlarmDTO.alarm_event_record_get
63
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:
69             return result
70         api_monitoring_v1.abort(
71             404, "Resource type {} doesn't exist".format(alarmEventRecordId))
72
73
74 # ----------  Alarm Subscriptions ---------- #
75 @api_monitoring_v1.route("/alarmSubscriptions")
76 class SubscriptionsListRouter(Resource):
77
78     model = SubscriptionDTO.subscription_get
79     expect = SubscriptionDTO.subscription
80     post_resp = SubscriptionDTO.subscription_post_resp
81
82     @api_monitoring_v1.doc('List alarm subscriptions')
83     @api_monitoring_v1.marshal_list_with(model)
84     def get(self):
85         parser = reqparse.RequestParser()
86         parser.add_argument(PAGE_PARAM, location='args')
87         args = parser.parse_args()
88         kwargs = {}
89         if args.nextpage_opaque_marker is not None:
90             kwargs['page'] = args.nextpage_opaque_marker
91
92         ret = alarm_view.subscriptions(bus.uow, **kwargs)
93         return link_header(request.full_path, ret)
94
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)
98     def post(self):
99         data = api_monitoring_v1.payload
100         result = alarm_view.subscription_create(data, bus.uow)
101         return result, 201
102
103
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):
108
109     model = SubscriptionDTO.subscription_get
110
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:
117             return result
118         api_monitoring_v1.abort(404, "Subscription {} doesn't exist".format(
119             alarmSubscriptionID))
120
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)
125         return result, 204