2e19ffe3a7c3c1e5b678d152cd043c63c147061d
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.monitoring.rest;
22
23 import io.swagger.annotations.ApiOperation;
24 import io.swagger.annotations.ApiParam;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27 import io.swagger.annotations.Authorization;
28 import io.swagger.annotations.Extension;
29 import io.swagger.annotations.ExtensionProperty;
30 import io.swagger.annotations.ResponseHeader;
31 import java.time.Instant;
32 import java.util.UUID;
33 import javax.ws.rs.DefaultValue;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.QueryParam;
38 import javax.ws.rs.core.Response;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
41 import org.onap.policy.clamp.controlloop.runtime.main.rest.RestController;
42 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringHandler;
43 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
44 import org.onap.policy.models.base.PfModelException;
45 import org.onap.policy.models.base.PfModelRuntimeException;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * This class handles REST endpoints for CL Statistics monitoring.
51  */
52 public class MonitoringQueryController extends RestController {
53
54     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringQueryController.class);
55     private final MonitoringProvider provider;
56
57     /**
58      * Create Monitoring Controller.
59      */
60     public MonitoringQueryController() {
61         this.provider = MonitoringHandler.getInstance().getMonitoringProvider();
62     }
63
64
65     /**
66      * Queries details of control loop participants statistics.
67      *
68      * @param requestId request ID used in ONAP logging
69      * @param name the name of the participant to get, null for all participants statistics
70      * @param recordCount the record count to be fetched
71      * @return the participant statistics
72      */
73     // @formatter:off
74     @GET
75     @Path("/monitoring/participant")
76     @ApiOperation(value = "Query details of the requested participant stats",
77         notes = "Queries details of the requested participant stats, returning all participant stats",
78         response = ParticipantStatisticsList.class,
79         tags = {
80             "Clamp control loop Monitoring API"
81         },
82         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
83         responseHeaders = {
84             @ResponseHeader(
85                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
86                 response = String.class),
87             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
88                 response = String.class),
89             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
90                 response = String.class),
91             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
92                 response = UUID.class)},
93         extensions = {
94             @Extension(
95                 name = EXTENSION_NAME,
96                 properties = {
97                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
98                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
99                 }
100             )
101         }
102     )
103     @ApiResponses(
104         value = {
105             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
106             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
107             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
108         }
109     )
110     // @formatter:on
111     public Response queryParticipantStatistics(@HeaderParam(REQUEST_ID_NAME)
112                                                @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
113                                                @ApiParam(value = "Control Loop participant name", required = true)
114                                                @QueryParam("name") final String name,
115                                                @ApiParam(value = "Control Loop participant version", required = true)
116                                                @QueryParam("version") final String version,
117                                                @ApiParam(value = "Record count", required = false) @DefaultValue("0")
118                                                @QueryParam("recordCount") final int recordCount,
119                                                @ApiParam(value = "start time", required = false)
120                                                @QueryParam("startTime") final String startTime,
121                                                @ApiParam(value = "end time", required = false)
122                                                @QueryParam("endTime") final String endTime) {
123
124         try {
125             Instant startTimestamp = null;
126             Instant endTimestamp = null;
127
128             if (startTime != null) {
129                 startTimestamp = Instant.parse(startTime);
130             }
131             if (endTime != null) {
132                 endTimestamp = Instant.parse(endTime);
133             }
134             ParticipantStatisticsList response = provider.fetchFilteredParticipantStatistics(name, version, recordCount,
135                 startTimestamp, endTimestamp);
136             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
137                 .entity(response)
138                 .build();
139
140         } catch (PfModelRuntimeException e) {
141             LOGGER.warn("Monitoring of participants statistics failed", e);
142             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
143                 requestId).build();
144         }
145
146     }
147
148     /**
149      * Queries details of all participant statistics per control loop.
150      *
151      * @param requestId request ID used in ONAP logging
152      * @param name the name of the control loop
153      * @param version version of the control loop
154      * @return the control loop element statistics
155      */
156     // @formatter:off
157     @GET
158     @Path("/monitoring/participants/controlloop")
159     @ApiOperation(value = "Query details of all the participant stats in a control loop",
160         notes = "Queries details of the participant stats, returning all participant stats",
161         response = ClElementStatisticsList.class,
162         tags = {
163             "Clamp control loop Monitoring API"
164         },
165         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
166         responseHeaders = {
167             @ResponseHeader(
168                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
169                 response = String.class),
170             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
171                 response = String.class),
172             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
173                 response = String.class),
174             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
175                 response = UUID.class)},
176         extensions = {
177             @Extension(
178                 name = EXTENSION_NAME,
179                 properties = {
180                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
181                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
182                 }
183             )
184         })
185     @ApiResponses(
186         value = {
187             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
188             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
189             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
190         }
191     )
192     // @formatter:on
193     public Response queryParticipantStatisticsPerControlLoop(@HeaderParam(REQUEST_ID_NAME)
194                                                              @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
195                                                              @ApiParam(value = "Control Loop name", required = true)
196                                                              @QueryParam("name") final String name,
197                                                              @ApiParam(value = "Control Loop version", required = true)
198                                                              @QueryParam("version") final String version) {
199
200         try {
201             ParticipantStatisticsList response = provider.fetchParticipantStatsPerControlLoop(name, version);
202             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
203                 .entity(response)
204                 .build();
205
206         } catch (PfModelRuntimeException | PfModelException e) {
207             LOGGER.warn("Monitoring of Cl participant statistics failed", e);
208             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
209                 requestId).build();
210         }
211
212     }
213
214
215
216     /**
217      * Queries details of all control loop element statistics per control loop.
218      *
219      * @param requestId request ID used in ONAP logging
220      * @param name the name of the control loop
221      * @param version version of the control loop
222      * @return the control loop element statistics
223      */
224     // @formatter:off
225     @GET
226     @Path("/monitoring/clelements/controlloop")
227     @ApiOperation(value = "Query details of the requested cl element stats in a control loop",
228         notes = "Queries details of the requested cl element stats, returning all clElement stats",
229         response = ClElementStatisticsList.class,
230         tags = {
231             "Clamp control loop Monitoring API"
232         },
233         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
234         responseHeaders = {
235             @ResponseHeader(
236                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
237                 response = String.class),
238             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
239                 response = String.class),
240             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
241                 response = String.class),
242             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
243                 response = UUID.class)},
244         extensions = {
245             @Extension(
246                 name = EXTENSION_NAME,
247                 properties = {
248                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
249                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
250                 }
251             )
252         })
253     @ApiResponses(
254         value = {
255             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
256             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
257             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
258         }
259     )
260     // @formatter:on
261     public Response queryElementStatisticsPerControlLoop(@HeaderParam(REQUEST_ID_NAME)
262                                                          @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
263                                                          @ApiParam(value = "Control Loop name", required = true)
264                                                          @QueryParam("name") final String name,
265                                                          @ApiParam(value = "Control Loop version", required = true)
266                                                          @QueryParam("version") final String version) {
267
268         try {
269             ClElementStatisticsList response = provider.fetchClElementStatsPerControlLoop(name, version);
270             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
271                 .entity(response)
272                 .build();
273
274         } catch (PfModelRuntimeException | PfModelException e) {
275             LOGGER.warn("Monitoring of Cl Element statistics failed", e);
276             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
277                 requestId).build();
278         }
279
280     }
281
282
283
284
285     /**
286      * Queries details of all control loop element statistics per control loop.
287      *
288      * @param requestId request ID used in ONAP logging
289      * @param name the name of the control loop
290      * @param version version of the control loop
291      * @param id Id of the control loop element
292      * @param recordCount the record count to be fetched
293      * @return the control loop element statistics
294      */
295     // @formatter:off
296     @GET
297     @Path("/monitoring/clelement")
298     @ApiOperation(value = "Query details of the requested cl element stats",
299         notes = "Queries details of the requested cl element stats, returning all clElement stats",
300         response = ClElementStatisticsList.class,
301         tags = {
302             "Clamp control loop Monitoring API"
303         },
304         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
305         responseHeaders = {
306             @ResponseHeader(
307                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
308                 response = String.class),
309             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
310                 response = String.class),
311             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
312                 response = String.class),
313             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
314                 response = UUID.class)},
315         extensions = {
316             @Extension(
317                 name = EXTENSION_NAME,
318                 properties = {
319                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
320                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
321                 }
322             )
323         })
324     @ApiResponses(
325         value = {
326             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
327             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
328             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
329         }
330     )
331     // @formatter:on
332     public Response queryElementStatistics(@HeaderParam(REQUEST_ID_NAME)
333                                            @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
334                                            @ApiParam(value = "Participant name", required = true)
335                                            @QueryParam("name") final String name,
336                                            @ApiParam(value = "Participant version", required = true)
337                                            @QueryParam("version") final String version,
338                                            @ApiParam(value = "Record count", required = false)
339                                            @DefaultValue("0") @QueryParam("recordCount") final int recordCount,
340                                            @ApiParam(value = "Control Loop element id", required = false)
341                                            @QueryParam("id") final String id,
342                                            @ApiParam(value = "start time", required = false)
343                                            @QueryParam("startTime") final String startTime,
344                                            @ApiParam(value = "end time", required = false)
345                                            @QueryParam("endTime") final String endTime) {
346
347         try {
348             Instant startTimestamp = null;
349             Instant endTimestamp = null;
350
351             if (startTime != null) {
352                 startTimestamp = Instant.parse(startTime);
353             }
354             if (endTime != null) {
355                 endTimestamp = Instant.parse(endTime);
356             }
357             ClElementStatisticsList response = provider.fetchFilteredClElementStatistics(name, version, id,
358                 startTimestamp, endTimestamp, recordCount);
359             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
360                 .entity(response)
361                 .build();
362
363         } catch (PfModelRuntimeException | PfModelException e) {
364             LOGGER.warn("Monitoring of Cl Element statistics failed", e);
365             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
366                 requestId).build();
367         }
368
369     }
370
371 }