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