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.ControlLoopOrderStateResponse;
36 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstancePropertiesResponse;
37 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
38 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
39 import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstantiationProvider;
40 import org.onap.policy.clamp.controlloop.runtime.main.web.AbstractRestController;
41 import org.onap.policy.models.base.PfModelException;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.springframework.http.MediaType;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.web.bind.annotation.DeleteMapping;
46 import org.springframework.web.bind.annotation.GetMapping;
47 import org.springframework.web.bind.annotation.PostMapping;
48 import org.springframework.web.bind.annotation.PutMapping;
49 import org.springframework.web.bind.annotation.RequestBody;
50 import org.springframework.web.bind.annotation.RequestHeader;
51 import org.springframework.web.bind.annotation.RequestParam;
52 import org.springframework.web.bind.annotation.RestController;
55 * Class to provide REST end points for creating, deleting, query and commanding a control loop definition.
58 @RequiredArgsConstructor
59 public class InstantiationController extends AbstractRestController {
61 private static final String TAGS = "Clamp Control Loop Instantiation API";
63 // The CL provider for instantiation requests
64 private final ControlLoopInstantiationProvider provider;
67 * Creates a control loop.
69 * @param requestId request ID used in ONAP logging
70 * @param controlLoops the control loops
72 * @throws PfModelException on errors creating a control loop
75 @PostMapping(value = "/instantiation",
76 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
77 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
79 value = "Commissions control loop definitions",
80 notes = "Commissions control loop definitions, returning the control loop IDs",
81 response = InstantiationResponse.class,
83 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
86 name = VERSION_MINOR_NAME,
87 description = VERSION_MINOR_DESCRIPTION,
88 response = String.class),
90 name = VERSION_PATCH_NAME,
91 description = VERSION_PATCH_DESCRIPTION,
92 response = String.class),
94 name = VERSION_LATEST_NAME,
95 description = VERSION_LATEST_DESCRIPTION,
96 response = String.class),
98 name = REQUEST_ID_NAME,
99 description = REQUEST_ID_HDR_DESCRIPTION,
100 response = UUID.class)
105 name = EXTENSION_NAME,
107 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
108 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
115 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
116 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
117 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
121 public ResponseEntity<InstantiationResponse> create(
123 name = REQUEST_ID_NAME,
124 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
125 @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ControlLoops controlLoops)
126 throws PfModelException {
128 return ResponseEntity.ok().body(provider.createControlLoops(controlLoops));
132 * Saves instance properties.
134 * @param requestId request ID used in ONAP logging
135 * @param body the body of control loop following TOSCA definition
139 @PostMapping(value = "/instanceProperties",
140 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
141 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
143 value = "Saves instance properties",
144 notes = "Saves instance properties, returning the saved instances properties and it's version",
145 response = InstancePropertiesResponse.class,
147 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
150 name = VERSION_MINOR_NAME,
151 description = VERSION_MINOR_DESCRIPTION,
152 response = String.class),
154 name = VERSION_PATCH_NAME,
155 description = VERSION_PATCH_DESCRIPTION,
156 response = String.class),
158 name = VERSION_LATEST_NAME,
159 description = VERSION_LATEST_DESCRIPTION,
160 response = String.class),
162 name = REQUEST_ID_NAME,
163 description = REQUEST_ID_HDR_DESCRIPTION,
164 response = UUID.class)
169 name = EXTENSION_NAME,
171 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
172 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
179 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
180 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
181 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
185 public ResponseEntity<InstancePropertiesResponse> createInstanceProperties(
187 name = REQUEST_ID_NAME,
188 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
189 @ApiParam(value = "Body of instance properties", required = true) @RequestBody ToscaServiceTemplate body)
190 throws PfModelException {
192 return ResponseEntity.ok().body(provider.saveInstanceProperties(body));
196 * Queries details of all control loops.
198 * @param requestId request ID used in ONAP logging
199 * @param name the name of the control loop to get, null for all control loops
200 * @param version the version of the control loop to get, null for all control loops
201 * @return the control loops
202 * @throws PfModelException on errors getting commissioning of control loop
205 @GetMapping(value = "/instantiation",
206 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
207 @ApiOperation(value = "Query details of the requested control loops",
208 notes = "Queries details of the requested control loops, returning all control loop details",
209 response = ControlLoops.class,
211 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
214 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
215 response = String.class),
216 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
217 response = String.class),
218 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
219 response = String.class),
220 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
221 response = UUID.class)},
225 name = EXTENSION_NAME,
227 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
228 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
235 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
236 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
237 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
241 public ResponseEntity<ControlLoops> query(
243 name = REQUEST_ID_NAME,
244 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
245 @ApiParam(value = "Control Loop definition name", required = false) @RequestParam(
247 required = false) String name,
248 @ApiParam(value = "Control Loop definition version", required = false) @RequestParam(
250 required = false) String version)
251 throws PfModelException {
253 return ResponseEntity.ok().body(provider.getControlLoops(name, version));
257 * Updates a control loop.
259 * @param requestId request ID used in ONAP logging
260 * @param controlLoops the control loops
262 * @throws PfModelException on errors updating of control loops
265 @PutMapping(value = "/instantiation",
266 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
267 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
269 value = "Updates control loop definitions",
270 notes = "Updates control loop definitions, returning the updated control loop definition IDs",
271 response = InstantiationResponse.class,
273 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
276 name = VERSION_MINOR_NAME,
277 description = VERSION_MINOR_DESCRIPTION,
278 response = String.class),
280 name = VERSION_PATCH_NAME,
281 description = VERSION_PATCH_DESCRIPTION,
282 response = String.class),
284 name = VERSION_LATEST_NAME,
285 description = VERSION_LATEST_DESCRIPTION,
286 response = String.class),
288 name = REQUEST_ID_NAME,
289 description = REQUEST_ID_HDR_DESCRIPTION,
290 response = UUID.class)
295 name = EXTENSION_NAME,
297 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
298 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
305 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
306 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
307 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
311 public ResponseEntity<InstantiationResponse> update(
313 name = REQUEST_ID_NAME,
314 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
315 @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ControlLoops controlLoops)
316 throws PfModelException {
318 return ResponseEntity.ok().body(provider.updateControlLoops(controlLoops));
322 * Deletes a control loop definition.
324 * @param requestId request ID used in ONAP logging
325 * @param name the name of the control loop to delete
326 * @param version the version of the control loop to delete
328 * @throws PfModelException on errors deleting of control loop
331 @DeleteMapping(value = "/instantiation",
332 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
333 @ApiOperation(value = "Delete a control loop",
334 notes = "Deletes a control loop, returning optional error details",
335 response = InstantiationResponse.class,
337 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
340 name = VERSION_MINOR_NAME,
341 description = VERSION_MINOR_DESCRIPTION,
342 response = String.class),
344 name = VERSION_PATCH_NAME,
345 description = VERSION_PATCH_DESCRIPTION,
346 response = String.class),
348 name = VERSION_LATEST_NAME,
349 description = VERSION_LATEST_DESCRIPTION,
350 response = String.class),
352 name = REQUEST_ID_NAME,
353 description = REQUEST_ID_HDR_DESCRIPTION,
354 response = UUID.class)},
358 name = EXTENSION_NAME,
360 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
361 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
368 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
369 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
370 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
375 public ResponseEntity<InstantiationResponse> delete(
377 name = REQUEST_ID_NAME,
378 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
379 @ApiParam(value = "Control Loop definition name", required = true) @RequestParam("name") String name,
380 @ApiParam(value = "Control Loop definition version") @RequestParam(
382 required = false) String version)
383 throws PfModelException {
385 return ResponseEntity.ok().body(provider.deleteControlLoop(name, version));
389 * Issues control loop commands to control loops.
391 * @param requestId request ID used in ONAP logging
392 * @param command the command to issue to control loops
393 * @return the control loop definitions
394 * @throws PfModelException on errors issuing a command
395 * @throws ControlLoopException on errors issuing a command
398 @PutMapping(value = "/instantiation/command",
399 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
400 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
401 @ApiOperation(value = "Issue a command to the requested control loops",
402 notes = "Issues a command to a control loop, ordering a state change on the control loop",
403 response = InstantiationResponse.class,
405 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
408 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
409 response = String.class),
410 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
411 response = String.class),
412 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
413 response = String.class),
414 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
415 response = UUID.class)},
419 name = EXTENSION_NAME,
421 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
422 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
429 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
430 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
431 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
435 public ResponseEntity<InstantiationResponse> issueControlLoopCommand(
437 name = REQUEST_ID_NAME,
438 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
440 value = "Entity Body of control loop command",
441 required = true) @RequestBody InstantiationCommand command)
442 throws ControlLoopException, PfModelException {
444 return ResponseEntity.accepted().body(provider.issueControlLoopCommand(command));
448 * Queries details of all control loops.
450 * @param requestId request ID used in ONAP logging
451 * @param name the name of the control loop to get, null for all control loops
452 * @param version the version of the control loop to get, null for all control loops
453 * @return the control loops
454 * @throws PfModelException on errors getting commissioning of control loop
457 @GetMapping(value = "/instantiationState",
458 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
459 @ApiOperation(value = "Query details of the requested control loops",
460 notes = "Queries details of the requested control loops, returning all control loop details",
461 response = ControlLoops.class,
463 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
466 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
467 response = String.class),
468 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
469 response = String.class),
470 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
471 response = String.class),
472 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
473 response = UUID.class)},
477 name = EXTENSION_NAME,
479 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
480 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
487 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
488 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
489 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
493 public ResponseEntity<ControlLoopOrderStateResponse> getInstantiationOrderState(
495 name = REQUEST_ID_NAME,
496 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
497 @ApiParam(value = "Control Loop definition name", required = false) @RequestParam(
499 required = false) String name,
500 @ApiParam(value = "Control Loop definition version", required = false) @RequestParam(
502 required = false) String version)
503 throws PfModelException {
505 return ResponseEntity.ok().body(provider.getInstantiationOrderState(name, version));