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