d6ca6d0d1af1eb1a7498cc0b6fcdaec2e4d7e906
[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.participant.simulator.simulation.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.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;
47
48 /**
49  * Class to provide REST end points for participant simulator to query/update details of all participants.
50  */
51 public class SimulationParticipantController extends RestController {
52     private static final Logger LOGGER = LoggerFactory.getLogger(SimulationParticipantController.class);
53
54     /**
55      * Queries details of all participants within the simulator.
56      *
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
61      */
62     // @formatter:off
63     @GET
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,
69             tags = {
70                 "Clamp Control Loop Participant Simulator API"
71             },
72             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
73             responseHeaders = {
74                     @ResponseHeader(
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)},
83             extensions = {
84                     @Extension(
85                             name = EXTENSION_NAME,
86                             properties = {
87                                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
88                                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
89                             }
90                     )
91             }
92     )
93     @ApiResponses(
94             value = {
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)
98             }
99     )
100     // @formatter:on
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) {
104
105         try {
106             List<Participant> response = getSimulationProvider().getParticipants(name, version);
107             return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
108                     .build();
109
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();
117         }
118
119     }
120
121     /**
122      * Updates a participant in the simulator.
123      *
124      * @param requestId request ID used in ONAP logging
125      * @param body the body of a participant
126      * @return a response
127      */
128     // @formatter:off
129     @PUT
130     @Path("/participants")
131     @ApiOperation(
132             value = "Updates simulated participants",
133             notes = "Updates simulated participants, returning the updated control loop definition IDs",
134             response = TypedSimpleResponse.class,
135             tags = {
136                 "Clamp Control Loop Participant Simulator API"
137                 },
138             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
139             responseHeaders = {
140                     @ResponseHeader(
141                             name = VERSION_MINOR_NAME,
142                             description = VERSION_MINOR_DESCRIPTION,
143                             response = String.class),
144                     @ResponseHeader(
145                             name = VERSION_PATCH_NAME,
146                             description = VERSION_PATCH_DESCRIPTION,
147                             response = String.class),
148                     @ResponseHeader(
149                             name = VERSION_LATEST_NAME,
150                             description = VERSION_LATEST_DESCRIPTION,
151                             response = String.class),
152                     @ResponseHeader(
153                             name = REQUEST_ID_NAME,
154                             description = REQUEST_ID_HDR_DESCRIPTION,
155                             response = UUID.class)
156                 },
157             extensions = {
158                 @Extension(
159                     name = EXTENSION_NAME,
160                     properties = {
161                             @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
162                             @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
163                     }
164                 )
165             }
166         )
167     @ApiResponses(
168             value = {
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)
172             }
173         )
174     // @formatter:on
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) {
177
178         try {
179             TypedSimpleResponse<Participant> response = getSimulationProvider().updateParticipant(body);
180             return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId).entity(response)
181                     .build();
182
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();
190         }
191     }
192 }