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