1 # Copyright (C) 2021 Wind River Systems, Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from flask import request
16 from flask_restx import Resource, reqparse
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
24 from o2common.helper import o2logging
25 logger = o2logging.get_logger(__name__)
28 def configure_api_route():
29 # Set global bus for resource
31 bus = MessageBus.get_instance()
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.' +
39 _in='query', default=1)
40 @api_monitoring_v1.param(
42 'Set any value for show all fields. This value will cover "fields" ' +
45 @api_monitoring_v1.param(
47 'Set fields to show, split by comman, "/" for parent and children.' +
48 ' Like "name,parent/children". This value will cover' +
51 @api_monitoring_v1.param(
53 'Set fields to exclude showing, split by comman, "/" for parent and ' +
54 'children. Like "name,parent/children". This value will cover ' +
57 @api_monitoring_v1.param(
59 'Exclude showing all default fields, Set "true" to enable.',
61 class AlarmListRouter(Resource):
63 model = AlarmDTO.alarm_event_record_get
65 @api_monitoring_v1.marshal_list_with(model)
67 parser = reqparse.RequestParser()
68 parser.add_argument(PAGE_PARAM, location='args')
69 args = parser.parse_args()
71 if args.nextpage_opaque_marker is not None:
72 kwargs['page'] = args.nextpage_opaque_marker
74 ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
75 return link_header(request.full_path, ret)
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(
83 'Set any value for show all fields. This value will cover "fields" ' +
86 @api_monitoring_v1.param(
88 'Set fields to show, split by comman, "/" for parent and children.' +
89 ' Like "name,parent/children". This value will cover' +
92 @api_monitoring_v1.param(
94 'Set fields to exclude showing, split by comman, "/" for parent and ' +
95 'children. Like "name,parent/children". This value will cover ' +
98 @api_monitoring_v1.param(
100 'Exclude showing all default fields, Set "true" to enable.',
102 class AlarmGetRouter(Resource):
104 model = AlarmDTO.alarm_event_record_get
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:
112 api_monitoring_v1.abort(
113 404, "Resource type {} doesn't exist".format(alarmEventRecordId))
116 # ---------- Alarm Subscriptions ---------- #
117 @api_monitoring_v1.route("/alarmSubscriptions")
118 class SubscriptionsListRouter(Resource):
120 model = SubscriptionDTO.subscription_get
121 expect = SubscriptionDTO.subscription
122 post_resp = SubscriptionDTO.subscription_post_resp
124 @api_monitoring_v1.doc('List alarm subscriptions')
125 @api_monitoring_v1.marshal_list_with(model)
126 @api_monitoring_v1.param(
128 'Page number of the results to fetch. Default: 1',
129 _in='query', default=1)
130 @api_monitoring_v1.param(
132 'Set any value for show all fields. This value will cover "fields" ' +
135 @api_monitoring_v1.param(
137 'Set fields to show, split by comman, "/" for parent and children.' +
138 ' Like "name,parent/children". This value will cover' +
139 ' "exculde_fields".',
141 @api_monitoring_v1.param(
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".',
147 @api_monitoring_v1.param(
149 'Exclude showing all default fields, Set "true" to enable.',
152 parser = reqparse.RequestParser()
153 parser.add_argument(PAGE_PARAM, location='args')
154 args = parser.parse_args()
156 if args.nextpage_opaque_marker is not None:
157 kwargs['page'] = args.nextpage_opaque_marker
159 ret = alarm_view.subscriptions(bus.uow, **kwargs)
160 return link_header(request.full_path, ret)
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)
166 data = api_monitoring_v1.payload
167 result = alarm_view.subscription_create(data, bus.uow)
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):
176 model = SubscriptionDTO.subscription_get
178 @api_monitoring_v1.doc('Get Alarm Subscription by ID')
179 @api_monitoring_v1.marshal_with(model)
180 @api_monitoring_v1.param(
182 'Set any value for show all fields. This value will cover "fields" ' +
185 @api_monitoring_v1.param(
187 'Set fields to show, split by comman, "/" for parent and children.' +
188 ' Like "name,parent/children". This value will cover' +
189 ' "exculde_fields".',
191 @api_monitoring_v1.param(
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".',
197 @api_monitoring_v1.param(
199 'Exclude showing all default fields, Set "true" to enable.',
201 def get(self, alarmSubscriptionID):
202 result = alarm_view.subscription_one(
203 alarmSubscriptionID, bus.uow)
204 if result is not None:
206 api_monitoring_v1.abort(404, "Subscription {} doesn't exist".format(
207 alarmSubscriptionID))
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)