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