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.participant.simulator.simulation.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.List;
32 import java.util.UUID;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.HeaderParam;
35 import javax.ws.rs.PUT;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.Response.Status;
40 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
42 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
43 import org.onap.policy.clamp.controlloop.models.messages.rest.SimpleResponse;
44 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
45 import org.onap.policy.clamp.controlloop.participant.simulator.main.rest.RestController;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * Class to provide REST end points for participant simulator to query/update details of controlLoopElements.
52 public class SimulationElementController extends RestController {
53 private static final Logger LOGGER = LoggerFactory.getLogger(SimulationElementController.class);
56 * Queries details of all control loop element within the simulator.
58 * @param requestId request ID used in ONAP logging
59 * @param name the name of the Control Loop element to get, null to get all
60 * @param version the version of the Control Loop element to get, null to get all
61 * @return the control loop elements
65 @Path("/elements/{name}/{version}")
66 @ApiOperation(value = "Query details of the requested simulated control loop elements",
67 notes = "Queries details of the requested simulated control loop elements, "
68 + "returning all control loop element details",
69 response = ControlLoops.class,
71 "Clamp Control Loop Participant Simulator API"
73 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
76 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
77 response = String.class),
78 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
79 response = String.class),
80 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
81 response = String.class),
82 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
83 response = UUID.class)},
86 name = EXTENSION_NAME,
88 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
89 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
96 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
97 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
98 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
102 public Response elements(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
103 @ApiParam(value = "Control loop element name", required = true) @PathParam("name") String name,
104 @ApiParam(value = "Control loop element version", required = true) @PathParam("version") String version) {
107 List<ControlLoopElement> response = getSimulationProvider().getControlLoopElements(name, version);
108 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
111 } catch (ControlLoopException cle) {
112 LOGGER.warn("get of control loop elements failed", cle);
113 SimpleResponse resp = new SimpleResponse();
114 resp.setErrorDetails(cle.getErrorResponse().getErrorMessage());
115 return addLoggingHeaders(
116 addVersionControlHeaders(Response.status(cle.getErrorResponse().getResponseCode())), requestId)
117 .entity(resp).build();
123 * Updates a control loop element in the simulator.
125 * @param requestId request ID used in ONAP logging
126 * @param body the body of a control loop element
133 value = "Updates simulated control loop elements",
134 notes = "Updates simulated control loop elements, returning the updated control loop definition IDs",
135 response = TypedSimpleResponse.class,
137 "Clamp Control Loop Participant Simulator API"
139 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
142 name = VERSION_MINOR_NAME,
143 description = VERSION_MINOR_DESCRIPTION,
144 response = String.class),
146 name = VERSION_PATCH_NAME,
147 description = VERSION_PATCH_DESCRIPTION,
148 response = String.class),
150 name = VERSION_LATEST_NAME,
151 description = VERSION_LATEST_DESCRIPTION,
152 response = String.class),
154 name = REQUEST_ID_NAME,
155 description = REQUEST_ID_HDR_DESCRIPTION,
156 response = UUID.class)
160 name = EXTENSION_NAME,
162 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
163 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
170 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
171 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
172 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
176 public Response update(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
177 @ApiParam(value = "Body of a control loop element", required = true) ControlLoopElement body) {
180 TypedSimpleResponse<ControlLoopElement> response =
181 getSimulationProvider().updateControlLoopElement(body);
182 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
185 } catch (ControlLoopException cle) {
186 LOGGER.warn("update of control loop element failed", cle);
187 TypedSimpleResponse<ControlLoopElement> resp = new TypedSimpleResponse<>();
188 resp.setErrorDetails(cle.getErrorResponse().getErrorMessage());
189 return addLoggingHeaders(
190 addVersionControlHeaders(Response.status(cle.getErrorResponse().getResponseCode())), requestId)
191 .entity(resp).build();