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.runtime.main.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.UUID;
32 import lombok.RequiredArgsConstructor;
33 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
35 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
36 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
37 import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstantiationProvider;
38 import org.onap.policy.clamp.controlloop.runtime.main.web.AbstractRestController;
39 import org.onap.policy.models.base.PfModelException;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.DeleteMapping;
43 import org.springframework.web.bind.annotation.GetMapping;
44 import org.springframework.web.bind.annotation.PostMapping;
45 import org.springframework.web.bind.annotation.PutMapping;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RequestHeader;
48 import org.springframework.web.bind.annotation.RequestParam;
49 import org.springframework.web.bind.annotation.RestController;
52 * Class to provide REST end points for creating, deleting, query and commanding a control loop definition.
55 @RequiredArgsConstructor
56 public class InstantiationController extends AbstractRestController {
58 private static final String TAGS = "Clamp Control Loop Instantiation API";
60 // The CL provider for instantiation requests
61 private final ControlLoopInstantiationProvider provider;
64 * Creates a control loop.
66 * @param requestId request ID used in ONAP logging
67 * @param controlLoops the control loops
69 * @throws PfModelException on errors creating a control loop
72 @PostMapping(value = "/instantiation",
73 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
74 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
76 value = "Commissions control loop definitions",
77 notes = "Commissions control loop definitions, returning the control loop IDs",
78 response = InstantiationResponse.class,
80 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
83 name = VERSION_MINOR_NAME,
84 description = VERSION_MINOR_DESCRIPTION,
85 response = String.class),
87 name = VERSION_PATCH_NAME,
88 description = VERSION_PATCH_DESCRIPTION,
89 response = String.class),
91 name = VERSION_LATEST_NAME,
92 description = VERSION_LATEST_DESCRIPTION,
93 response = String.class),
95 name = REQUEST_ID_NAME,
96 description = REQUEST_ID_HDR_DESCRIPTION,
97 response = UUID.class)
102 name = EXTENSION_NAME,
104 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
105 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
112 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
113 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
114 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
118 public ResponseEntity<InstantiationResponse> create(
120 name = REQUEST_ID_NAME,
121 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
122 @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ControlLoops controlLoops)
123 throws PfModelException {
125 return ResponseEntity.ok().body(provider.createControlLoops(controlLoops));
129 * Queries details of all control loops.
131 * @param requestId request ID used in ONAP logging
132 * @param name the name of the control loop to get, null for all control loops
133 * @param version the version of the control loop to get, null for all control loops
134 * @return the control loops
135 * @throws PfModelException on errors getting commissioning of control loop
138 @GetMapping(value = "/instantiation",
139 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
140 @ApiOperation(value = "Query details of the requested control loops",
141 notes = "Queries details of the requested control loops, returning all control loop details",
142 response = ControlLoops.class,
144 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
147 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
148 response = String.class),
149 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
150 response = String.class),
151 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
152 response = String.class),
153 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
154 response = UUID.class)},
158 name = EXTENSION_NAME,
160 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
161 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
168 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
169 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
170 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
174 public ResponseEntity<ControlLoops> query(
176 name = REQUEST_ID_NAME,
177 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
178 @ApiParam(value = "Control Loop definition name", required = false) @RequestParam(
180 required = false) String name,
181 @ApiParam(value = "Control Loop definition version", required = false) @RequestParam(
183 required = false) String version)
184 throws PfModelException {
186 return ResponseEntity.ok().body(provider.getControlLoops(name, version));
190 * Updates a control loop.
192 * @param requestId request ID used in ONAP logging
193 * @param controlLoops the control loops
195 * @throws PfModelException on errors updating of control loops
198 @PutMapping(value = "/instantiation",
199 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
200 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
202 value = "Updates control loop definitions",
203 notes = "Updates control loop definitions, returning the updated control loop definition IDs",
204 response = InstantiationResponse.class,
206 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
209 name = VERSION_MINOR_NAME,
210 description = VERSION_MINOR_DESCRIPTION,
211 response = String.class),
213 name = VERSION_PATCH_NAME,
214 description = VERSION_PATCH_DESCRIPTION,
215 response = String.class),
217 name = VERSION_LATEST_NAME,
218 description = VERSION_LATEST_DESCRIPTION,
219 response = String.class),
221 name = REQUEST_ID_NAME,
222 description = REQUEST_ID_HDR_DESCRIPTION,
223 response = UUID.class)
228 name = EXTENSION_NAME,
230 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
231 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
238 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
239 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
240 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
244 public ResponseEntity<InstantiationResponse> update(
246 name = REQUEST_ID_NAME,
247 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
248 @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ControlLoops controlLoops)
249 throws PfModelException {
251 return ResponseEntity.ok().body(provider.updateControlLoops(controlLoops));
255 * Deletes a control loop definition.
257 * @param requestId request ID used in ONAP logging
258 * @param name the name of the control loop to delete
259 * @param version the version of the control loop to delete
261 * @throws PfModelException on errors deleting of control loop
264 @DeleteMapping(value = "/instantiation",
265 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
266 @ApiOperation(value = "Delete a control loop",
267 notes = "Deletes a control loop, returning optional error details",
268 response = InstantiationResponse.class,
270 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
273 name = VERSION_MINOR_NAME,
274 description = VERSION_MINOR_DESCRIPTION,
275 response = String.class),
277 name = VERSION_PATCH_NAME,
278 description = VERSION_PATCH_DESCRIPTION,
279 response = String.class),
281 name = VERSION_LATEST_NAME,
282 description = VERSION_LATEST_DESCRIPTION,
283 response = String.class),
285 name = REQUEST_ID_NAME,
286 description = REQUEST_ID_HDR_DESCRIPTION,
287 response = UUID.class)},
291 name = EXTENSION_NAME,
293 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
294 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
301 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
302 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
303 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
308 public ResponseEntity<InstantiationResponse> delete(
310 name = REQUEST_ID_NAME,
311 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
312 @ApiParam(value = "Control Loop definition name", required = true) @RequestParam("name") String name,
313 @ApiParam(value = "Control Loop definition version") @RequestParam(
315 required = false) String version)
316 throws PfModelException {
318 return ResponseEntity.ok().body(provider.deleteControlLoop(name, version));
322 * Issues control loop commands to control loops.
324 * @param requestId request ID used in ONAP logging
325 * @param command the command to issue to control loops
326 * @return the control loop definitions
327 * @throws PfModelException on errors issuing a command
328 * @throws ControlLoopException on errors issuing a command
331 @PutMapping(value = "/instantiation/command",
332 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
333 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
334 @ApiOperation(value = "Issue a command to the requested control loops",
335 notes = "Issues a command to a control loop, ordering a state change on the control loop",
336 response = InstantiationResponse.class,
338 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
341 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
342 response = String.class),
343 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
344 response = String.class),
345 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
346 response = String.class),
347 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
348 response = UUID.class)},
352 name = EXTENSION_NAME,
354 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
355 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
362 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
363 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
364 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
368 public ResponseEntity<InstantiationResponse> issueControlLoopCommand(
370 name = REQUEST_ID_NAME,
371 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
373 value = "Entity Body of control loop command",
374 required = true) @RequestBody InstantiationCommand command)
375 throws ControlLoopException, PfModelException {
377 return ResponseEntity.accepted().body(provider.issueControlLoopCommand(command));