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;
33 import java.util.UUID;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
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.ControlLoopElement;
43 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
44 import org.onap.policy.clamp.controlloop.models.messages.rest.SimpleResponse;
45 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
46 import org.onap.policy.clamp.controlloop.participant.simulator.main.rest.RestController;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * Class to provide REST end points for participant simulator to query/update details of controlLoopElements.
53 public class SimulationElementController extends RestController {
54 private static final Logger LOGGER = LoggerFactory.getLogger(SimulationElementController.class);
57 * Queries details of all control loop element within the simulator.
59 * @param requestId request ID used in ONAP logging
60 * @param name the name of the Control Loop element to get, null to get all
61 * @param version the version of the Control Loop element to get, null to get all
62 * @return the control loop elements
66 @Path("/elements/{name}/{version}")
67 @ApiOperation(value = "Query details of the requested simulated control loop elements",
68 notes = "Queries details of the requested simulated control loop elements, "
69 + "returning all control loop element details",
70 response = ControlLoops.class,
72 "Clamp Control Loop Participant Simulator API"
74 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
77 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
78 response = String.class),
79 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
80 response = String.class),
81 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
82 response = String.class),
83 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
84 response = UUID.class)},
87 name = EXTENSION_NAME,
89 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
90 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
97 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
98 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
99 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
103 public Response elements(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
104 @ApiParam(value = "Control loop element name", required = true) @PathParam("name") String name,
105 @ApiParam(value = "Control loop element version", required = true) @PathParam("version") String version) {
108 Map<UUID, ControlLoopElement> response = getSimulationProvider().getControlLoopElements(name, version);
109 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
112 } catch (ControlLoopException cle) {
113 LOGGER.warn("get of control loop elements failed", cle);
114 SimpleResponse resp = new SimpleResponse();
115 resp.setErrorDetails(cle.getErrorResponse().getErrorMessage());
116 return addLoggingHeaders(
117 addVersionControlHeaders(Response.status(cle.getErrorResponse().getResponseCode())), requestId)
118 .entity(resp).build();
124 * Updates a control loop element in the simulator.
126 * @param requestId request ID used in ONAP logging
127 * @param body the body of a control loop element
134 value = "Updates simulated control loop elements",
135 notes = "Updates simulated control loop elements, returning the updated control loop definition IDs",
136 response = TypedSimpleResponse.class,
138 "Clamp Control Loop Participant Simulator API"
140 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
143 name = VERSION_MINOR_NAME,
144 description = VERSION_MINOR_DESCRIPTION,
145 response = String.class),
147 name = VERSION_PATCH_NAME,
148 description = VERSION_PATCH_DESCRIPTION,
149 response = String.class),
151 name = VERSION_LATEST_NAME,
152 description = VERSION_LATEST_DESCRIPTION,
153 response = String.class),
155 name = REQUEST_ID_NAME,
156 description = REQUEST_ID_HDR_DESCRIPTION,
157 response = UUID.class)
161 name = EXTENSION_NAME,
163 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
164 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
171 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
172 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
173 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
177 public Response update(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
178 @ApiParam(value = "Body of a control loop element", required = true) ControlLoopElement body) {
181 TypedSimpleResponse<ControlLoopElement> response =
182 getSimulationProvider().updateControlLoopElement(body);
183 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
186 } catch (ControlLoopException cle) {
187 LOGGER.warn("update of control loop element failed", cle);
188 TypedSimpleResponse<ControlLoopElement> resp = new TypedSimpleResponse<>();
189 resp.setErrorDetails(cle.getErrorResponse().getErrorMessage());
190 return addLoggingHeaders(
191 addVersionControlHeaders(Response.status(cle.getErrorResponse().getResponseCode())), requestId)
192 .entity(resp).build();