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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.instantiation.rest;
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.util.UUID;
32 import javax.ws.rs.DELETE;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.HeaderParam;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.QueryParam;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.Response.Status;
41 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
42 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
43 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
44 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
45 import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstantiationProvider;
46 import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationHandler;
47 import org.onap.policy.clamp.controlloop.runtime.main.rest.RestController;
48 import org.onap.policy.models.base.PfModelException;
49 import org.onap.policy.models.base.PfModelRuntimeException;
50 import org.onap.policy.models.errors.concepts.ErrorResponseInfo;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * Class to provide REST end points for creating, deleting, query and commanding a control loop definition.
57 public class InstantiationController extends RestController {
59 private static final Logger LOGGER = LoggerFactory.getLogger(InstantiationController.class);
61 // The CL provider for instantiation requests
62 private final ControlLoopInstantiationProvider provider;
65 * create Instantiation Controller.
67 public InstantiationController() {
68 this.provider = InstantiationHandler.getInstance().getControlLoopInstantiationProvider();
72 * Creates a control loop.
74 * @param requestId request ID used in ONAP logging
75 * @param controlLoops the control loops
80 @Path("/instantiation")
82 value = "Commissions control loop definitions",
83 notes = "Commissions control loop definitions, returning the control loop IDs",
84 response = InstantiationResponse.class,
85 tags = {"Control Loop Instantiation API"},
86 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
89 name = VERSION_MINOR_NAME,
90 description = VERSION_MINOR_DESCRIPTION,
91 response = String.class),
93 name = VERSION_PATCH_NAME,
94 description = VERSION_PATCH_DESCRIPTION,
95 response = String.class),
97 name = VERSION_LATEST_NAME,
98 description = VERSION_LATEST_DESCRIPTION,
99 response = String.class),
101 name = REQUEST_ID_NAME,
102 description = REQUEST_ID_HDR_DESCRIPTION,
103 response = UUID.class)
108 name = EXTENSION_NAME,
110 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
111 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
118 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
119 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
120 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
124 public Response create(
125 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
126 @ApiParam(value = "Entity Body of Control Loop", required = true) ControlLoops controlLoops) {
129 InstantiationResponse response = provider.createControlLoops(controlLoops);
130 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
133 } catch (PfModelRuntimeException | PfModelException e) {
134 LOGGER.warn("creation of control loop failed", e);
135 return createInstantiationErrorResponse(e, requestId);
140 * Queries details of all control loops.
142 * @param requestId request ID used in ONAP logging
143 * @param name the name of the control loop to get, null for all control loops
144 * @param version the version of the control loop to get, null for all control loops
145 * @return the control loops
149 @Path("/instantiation")
150 @ApiOperation(value = "Query details of the requested control loops",
151 notes = "Queries details of the requested control loops, returning all control loop details",
152 response = ControlLoops.class,
154 "Clamp control loop Instantiation API"
156 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
159 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
160 response = String.class),
161 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
162 response = String.class),
163 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
164 response = String.class),
165 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
166 response = UUID.class)},
170 name = EXTENSION_NAME,
172 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
173 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
180 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
181 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
182 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
186 public Response query(
187 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
188 @ApiParam(value = "Control Loop definition name", required = true) @QueryParam("name") String name,
189 @ApiParam(value = "Control Loop definition version",
190 required = true) @QueryParam("version") String version) {
193 var response = provider.getControlLoops(name, version);
194 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
197 } catch (PfModelRuntimeException | PfModelException e) {
198 LOGGER.warn("commisssioning of control loop failed", e);
199 return createInstantiationErrorResponse(e, requestId);
205 * Updates a control loop.
207 * @param requestId request ID used in ONAP logging
208 * @param controlLoops the control loops
213 @Path("/instantiation")
215 value = "Updates control loop definitions",
216 notes = "Updates control loop definitions, returning the updated control loop definition IDs",
217 response = InstantiationResponse.class,
219 "Control Loop Instantiation API"
221 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
224 name = VERSION_MINOR_NAME,
225 description = VERSION_MINOR_DESCRIPTION,
226 response = String.class),
228 name = VERSION_PATCH_NAME,
229 description = VERSION_PATCH_DESCRIPTION,
230 response = String.class),
232 name = VERSION_LATEST_NAME,
233 description = VERSION_LATEST_DESCRIPTION,
234 response = String.class),
236 name = REQUEST_ID_NAME,
237 description = REQUEST_ID_HDR_DESCRIPTION,
238 response = UUID.class)
243 name = EXTENSION_NAME,
245 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
246 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
253 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
254 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
255 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
259 public Response update(
260 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
261 @ApiParam(value = "Entity Body of Control Loop", required = true) ControlLoops controlLoops) {
264 InstantiationResponse response = provider.updateControlLoops(controlLoops);
265 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
268 } catch (PfModelRuntimeException | PfModelException e) {
269 LOGGER.warn("update of control loops failed", e);
270 return createInstantiationErrorResponse(e, requestId);
275 * Deletes a control loop definition.
277 * @param requestId request ID used in ONAP logging
278 * @param name the name of the control loop to delete
279 * @param version the version of the control loop to delete
284 @Path("/instantiation")
285 @ApiOperation(value = "Delete a control loop",
286 notes = "Deletes a control loop, returning optional error details",
287 response = InstantiationResponse.class,
289 "Clamp Control Loop Instantiation API"
291 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
294 name = VERSION_MINOR_NAME,
295 description = VERSION_MINOR_DESCRIPTION,
296 response = String.class),
298 name = VERSION_PATCH_NAME,
299 description = VERSION_PATCH_DESCRIPTION,
300 response = String.class),
302 name = VERSION_LATEST_NAME,
303 description = VERSION_LATEST_DESCRIPTION,
304 response = String.class),
306 name = REQUEST_ID_NAME,
307 description = REQUEST_ID_HDR_DESCRIPTION,
308 response = UUID.class)},
312 name = EXTENSION_NAME,
314 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
315 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
322 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
323 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
324 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
329 public Response delete(
330 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
331 @ApiParam(value = "Control Loop definition name", required = true) @QueryParam("name") String name,
332 @ApiParam(value = "Control Loop definition version", required = true)
333 @QueryParam("version") String version) {
336 InstantiationResponse response = provider.deleteControlLoop(name, version);
337 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
340 } catch (PfModelRuntimeException | PfModelException e) {
341 LOGGER.warn("delete of control loop failed", e);
342 return createInstantiationErrorResponse(e, requestId);
347 * Issues control loop commands to control loops.
349 * @param requestId request ID used in ONAP logging
350 * @param command the command to issue to control loops
351 * @return the control loop definitions
355 @Path("/instantiation/command")
356 @ApiOperation(value = "Issue a command to the requested control loops",
357 notes = "Issues a command to a control loop, ordering a state change on the control loop",
358 response = InstantiationResponse.class,
360 "Clamp Control Loop Instantiation API"
362 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
365 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
366 response = String.class),
367 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
368 response = String.class),
369 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
370 response = String.class),
371 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
372 response = UUID.class)},
376 name = EXTENSION_NAME,
378 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
379 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
386 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
387 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
388 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
392 public Response issueControlLoopCommand(
393 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
394 @ApiParam(value = "Entity Body of control loop command", required = true) InstantiationCommand command) {
397 InstantiationResponse response = provider.issueControlLoopCommand(command);
398 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.ACCEPTED)), requestId)
399 .entity(response).build();
401 } catch (PfModelRuntimeException | PfModelException | ControlLoopException e) {
402 LOGGER.warn("creation of control loop failed", e);
403 return createInstantiationErrorResponse(e, requestId);
408 * create a Instantiation Response from an exception.
410 * @param requestId request ID used in ONAP logging
411 * @return the Instantiation Response
413 private Response createInstantiationErrorResponse(ErrorResponseInfo e, UUID requestId) {
414 var resp = new InstantiationResponse();
415 resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
416 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
417 requestId).entity(resp).build();