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.Participant;
42 import org.onap.policy.clamp.controlloop.models.messages.rest.SimpleResponse;
43 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
44 import org.onap.policy.clamp.controlloop.participant.simulator.main.rest.RestController;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Class to provide REST end points for participant simulator to query/update details of all participants.
51 public class SimulationParticipantController extends RestController {
52 private static final Logger LOGGER = LoggerFactory.getLogger(SimulationParticipantController.class);
55 * Queries details of all participants within the simulator.
57 * @param requestId request ID used in ONAP logging
58 * @param name the name of the participant to get, null to get all
59 * @param version the version of the participant to get, null to get all
60 * @return the participants
64 @Path("/participants/{name}/{version}")
65 @ApiOperation(value = "Query details of the requested simulated participants",
66 notes = "Queries details of the requested simulated participants, "
67 + "returning all participant details",
68 response = List.class,
70 "Clamp Control Loop Participant Simulator API"
72 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
75 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
76 response = String.class),
77 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
78 response = String.class),
79 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
80 response = String.class),
81 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
82 response = UUID.class)},
85 name = EXTENSION_NAME,
87 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
88 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
95 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
96 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
97 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
101 public Response participants(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
102 @ApiParam(value = "Participant name", required = true) @PathParam("name") String name,
103 @ApiParam(value = "Participant version", required = true) @PathParam("version") String version) {
106 List<Participant> response = getSimulationProvider().getParticipants(name, version);
107 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
110 } catch (ControlLoopException cle) {
111 LOGGER.warn("get of participants failed", cle);
112 SimpleResponse resp = new SimpleResponse();
113 resp.setErrorDetails(cle.getErrorResponse().getErrorMessage());
114 return addLoggingHeaders(
115 addVersionControlHeaders(Response.status(cle.getErrorResponse().getResponseCode())), requestId)
116 .entity(resp).build();
122 * Updates a participant in the simulator.
124 * @param requestId request ID used in ONAP logging
125 * @param body the body of a participant
130 @Path("/participants")
132 value = "Updates simulated participants",
133 notes = "Updates simulated participants, returning the updated control loop definition IDs",
134 response = TypedSimpleResponse.class,
136 "Clamp Control Loop Participant Simulator API"
138 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
141 name = VERSION_MINOR_NAME,
142 description = VERSION_MINOR_DESCRIPTION,
143 response = String.class),
145 name = VERSION_PATCH_NAME,
146 description = VERSION_PATCH_DESCRIPTION,
147 response = String.class),
149 name = VERSION_LATEST_NAME,
150 description = VERSION_LATEST_DESCRIPTION,
151 response = String.class),
153 name = REQUEST_ID_NAME,
154 description = REQUEST_ID_HDR_DESCRIPTION,
155 response = UUID.class)
159 name = EXTENSION_NAME,
161 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
162 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
169 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
170 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
171 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
175 public Response update(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
176 @ApiParam(value = "Body of a participant", required = true) Participant body) {
179 TypedSimpleResponse<Participant> response = getSimulationProvider().updateParticipant(body);
180 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
183 } catch (ControlLoopException cle) {
184 LOGGER.warn("update of participant failed", cle);
185 TypedSimpleResponse<Participant> resp = new TypedSimpleResponse<>();
186 resp.setErrorDetails(cle.getErrorResponse().getErrorMessage());
187 return addLoggingHeaders(
188 addVersionControlHeaders(Response.status(cle.getErrorResponse().getResponseCode())), requestId)
189 .entity(resp).build();