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