Update error handling; update selector; change delete response code to 200
[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 o2common.views.route_exception import NotFoundException
21 from o2ims.views import alarm_view
22 from o2ims.views.api_ns import api_ims_monitoring as api_monitoring_v1
23 from o2ims.views.alarm_dto import AlarmDTO, SubscriptionDTO
24
25 from o2common.helper import o2logging
26 logger = o2logging.get_logger(__name__)
27
28
29 def configure_api_route():
30     # Set global bus for resource
31     global bus
32     bus = MessageBus.get_instance()
33
34
35 # ----------  API versions ---------- #
36 @api_monitoring_v1.route("/v1/api_versions")
37 class VersionRouter(Resource):
38     def get(self):
39         return {
40             'uriPrefix': request.base_url.rsplit('/', 1)[0],
41             'apiVersions': [{
42                 'version': '1.0.0',
43                 # 'isDeprecated': 'False',
44                 # 'retirementDate': ''
45             }]
46         }
47
48
49 # ----------  Alarm Event Record ---------- #
50 @api_monitoring_v1.route("/v1/alarms")
51 @api_monitoring_v1.param(PAGE_PARAM,
52                          'Page number of the results to fetch.' +
53                          ' Default: 1',
54                          _in='query', default=1)
55 @api_monitoring_v1.param(
56     'all_fields',
57     'Set any value for show all fields. This value will cover "fields" ' +
58     'and "all_fields".',
59     _in='query')
60 @api_monitoring_v1.param(
61     'fields',
62     'Set fields to show, split by comma, "/" for parent and children.' +
63     ' Like "name,parent/children". This value will cover' +
64     ' "exculde_fields".',
65     _in='query')
66 @api_monitoring_v1.param(
67     'exclude_fields',
68     'Set fields to exclude showing, split by comma, "/" for parent and ' +
69     'children. Like "name,parent/children". This value will cover ' +
70     '"exclude_default".',
71     _in='query')
72 @api_monitoring_v1.param(
73     'exclude_default',
74     'Exclude showing all default fields, Set "true" to enable.',
75     _in='query')
76 @api_monitoring_v1.param(
77     'filter',
78     'Filter of the query.',
79     _in='query')
80 class AlarmListRouter(Resource):
81
82     model = AlarmDTO.alarm_event_record_get
83
84     @api_monitoring_v1.marshal_list_with(model)
85     def get(self):
86         parser = reqparse.RequestParser()
87         parser.add_argument(PAGE_PARAM, location='args')
88         parser.add_argument('filter', location='args')
89         args = parser.parse_args()
90         kwargs = {}
91         if args.nextpage_opaque_marker is not None:
92             kwargs['page'] = args.nextpage_opaque_marker
93         kwargs['filter'] = args.filter if args.filter is not None else ''
94
95         ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
96         return link_header(request.full_path, ret)
97
98
99 @api_monitoring_v1.route("/v1/alarms/<alarmEventRecordId>")
100 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
101 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
102 @api_monitoring_v1.param(
103     'all_fields',
104     'Set any value for show all fields. This value will cover "fields" ' +
105     'and "all_fields".',
106     _in='query')
107 @api_monitoring_v1.param(
108     'fields',
109     'Set fields to show, split by comma, "/" for parent and children.' +
110     ' Like "name,parent/children". This value will cover' +
111     ' "exculde_fields".',
112     _in='query')
113 @api_monitoring_v1.param(
114     'exclude_fields',
115     'Set fields to exclude showing, split by comma, "/" for parent and ' +
116     'children. Like "name,parent/children". This value will cover ' +
117     '"exclude_default".',
118     _in='query')
119 @api_monitoring_v1.param(
120     'exclude_default',
121     'Exclude showing all default fields, Set "true" to enable.',
122     _in='query')
123 class AlarmGetRouter(Resource):
124
125     model = AlarmDTO.alarm_event_record_get
126
127     @api_monitoring_v1.doc('Get AlarmEventRecord')
128     @api_monitoring_v1.marshal_with(model)
129     def get(self, alarmEventRecordId):
130         result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
131         if result is not None:
132             return result
133         raise NotFoundException(
134             "Alarm Event Record {} doesn't exist".format(alarmEventRecordId))
135
136
137 # ----------  Alarm Subscriptions ---------- #
138 @api_monitoring_v1.route("/v1/alarmSubscriptions")
139 class SubscriptionsListRouter(Resource):
140
141     model = SubscriptionDTO.subscription_get
142     expect = SubscriptionDTO.subscription
143     post_resp = SubscriptionDTO.subscription_post_resp
144
145     @api_monitoring_v1.doc('List alarm subscriptions')
146     @api_monitoring_v1.marshal_list_with(model)
147     @api_monitoring_v1.param(
148         PAGE_PARAM,
149         'Page number of the results to fetch. Default: 1',
150         _in='query', default=1)
151     @api_monitoring_v1.param(
152         'all_fields',
153         'Set any value for show all fields. This value will cover "fields" ' +
154         'and "all_fields".',
155         _in='query')
156     @api_monitoring_v1.param(
157         'fields',
158         'Set fields to show, split by comma, "/" for parent and children.' +
159         ' Like "name,parent/children". This value will cover' +
160         ' "exculde_fields".',
161         _in='query')
162     @api_monitoring_v1.param(
163         'exclude_fields',
164         'Set fields to exclude showing, split by comma, "/" for parent and ' +
165         'children. Like "name,parent/children". This value will cover ' +
166         '"exclude_default".',
167         _in='query')
168     @api_monitoring_v1.param(
169         'exclude_default',
170         'Exclude showing all default fields, Set "true" to enable.',
171         _in='query')
172     @api_monitoring_v1.param(
173         'filter',
174         'Filter of the query.',
175         _in='query')
176     def get(self):
177         parser = reqparse.RequestParser()
178         parser.add_argument(PAGE_PARAM, location='args')
179         parser.add_argument('filter', location='args')
180         args = parser.parse_args()
181         kwargs = {}
182         if args.nextpage_opaque_marker is not None:
183             kwargs['page'] = args.nextpage_opaque_marker
184         kwargs['filter'] = args.filter if args.filter is not None else ''
185
186         ret = alarm_view.subscriptions(bus.uow, **kwargs)
187         return link_header(request.full_path, ret)
188
189     @api_monitoring_v1.doc('Create a alarm subscription')
190     @api_monitoring_v1.expect(expect)
191     @api_monitoring_v1.marshal_with(post_resp, code=201)
192     def post(self):
193         data = api_monitoring_v1.payload
194         result = alarm_view.subscription_create(data, bus.uow)
195         return result, 201
196
197
198 @api_monitoring_v1.route("/v1/alarmSubscriptions/<alarmSubscriptionID>")
199 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
200 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
201 class SubscriptionGetDelRouter(Resource):
202
203     model = SubscriptionDTO.subscription_get
204
205     @api_monitoring_v1.doc('Get Alarm Subscription by ID')
206     @api_monitoring_v1.marshal_with(model)
207     @api_monitoring_v1.param(
208         'all_fields',
209         'Set any value for show all fields. This value will cover "fields" ' +
210         'and "all_fields".',
211         _in='query')
212     @api_monitoring_v1.param(
213         'fields',
214         'Set fields to show, split by comma, "/" for parent and children.' +
215         ' Like "name,parent/children". This value will cover' +
216         ' "exculde_fields".',
217         _in='query')
218     @api_monitoring_v1.param(
219         'exclude_fields',
220         'Set fields to exclude showing, split by comma, "/" for parent and ' +
221         'children. Like "name,parent/children". This value will cover ' +
222         '"exclude_default".',
223         _in='query')
224     @api_monitoring_v1.param(
225         'exclude_default',
226         'Exclude showing all default fields, Set "true" to enable.',
227         _in='query')
228     def get(self, alarmSubscriptionID):
229         result = alarm_view.subscription_one(
230             alarmSubscriptionID, bus.uow)
231         if result is not None:
232             return result
233         raise NotFoundException(
234             "Subscription {} doesn't exist".format(alarmSubscriptionID))
235
236     @api_monitoring_v1.doc('Delete subscription by ID')
237     @api_monitoring_v1.response(200, 'Subscription deleted')
238     def delete(self, alarmSubscriptionID):
239         result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)
240         return result, 200