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