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