2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.runtime.main.rest;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiParam;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28 import io.swagger.annotations.Authorization;
29 import io.swagger.annotations.Extension;
30 import io.swagger.annotations.ExtensionProperty;
31 import io.swagger.annotations.ResponseHeader;
32 import java.util.UUID;
33 import lombok.RequiredArgsConstructor;
34 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
36 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopOrderStateResponse;
37 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstancePropertiesResponse;
38 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
39 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
40 import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstantiationProvider;
41 import org.onap.policy.clamp.controlloop.runtime.main.web.AbstractRestController;
42 import org.onap.policy.models.base.PfModelException;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.springframework.http.MediaType;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.bind.annotation.DeleteMapping;
47 import org.springframework.web.bind.annotation.GetMapping;
48 import org.springframework.web.bind.annotation.PostMapping;
49 import org.springframework.web.bind.annotation.PutMapping;
50 import org.springframework.web.bind.annotation.RequestBody;
51 import org.springframework.web.bind.annotation.RequestHeader;
52 import org.springframework.web.bind.annotation.RequestParam;
53 import org.springframework.web.bind.annotation.RestController;
56 * Class to provide REST end points for creating, deleting, query and commanding a control loop definition.
59 @RequiredArgsConstructor
60 public class InstantiationController extends AbstractRestController {
62 private static final String TAGS = "Clamp Control Loop Instantiation API";
64 // The CL provider for instantiation requests
65 private final ControlLoopInstantiationProvider provider;
68 * Creates a control loop.
70 * @param requestId request ID used in ONAP logging
71 * @param controlLoops the control loops
73 * @throws PfModelException on errors creating a control loop
76 @PostMapping(value = "/instantiation",
77 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
78 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
80 value = "Commissions control loop definitions",
81 notes = "Commissions control loop definitions, returning the control loop IDs",
82 response = InstantiationResponse.class,
84 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
87 name = VERSION_MINOR_NAME,
88 description = VERSION_MINOR_DESCRIPTION,
89 response = String.class),
91 name = VERSION_PATCH_NAME,
92 description = VERSION_PATCH_DESCRIPTION,
93 response = String.class),
95 name = VERSION_LATEST_NAME,
96 description = VERSION_LATEST_DESCRIPTION,
97 response = String.class),
99 name = REQUEST_ID_NAME,
100 description = REQUEST_ID_HDR_DESCRIPTION,
101 response = UUID.class)
106 name = EXTENSION_NAME,
108 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
109 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
116 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
117 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
118 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
122 public ResponseEntity<InstantiationResponse> create(
124 name = REQUEST_ID_NAME,
125 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
126 @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ControlLoops controlLoops)
127 throws PfModelException {
129 return ResponseEntity.ok().body(provider.createControlLoops(controlLoops));
133 * Saves instance properties.
135 * @param requestId request ID used in ONAP logging
136 * @param body the body of control loop following TOSCA definition
140 @PostMapping(value = "/instanceProperties",
141 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
142 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
144 value = "Saves instance properties",
145 notes = "Saves instance properties, returning the saved instances properties and it's version",
146 response = InstancePropertiesResponse.class,
148 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
151 name = VERSION_MINOR_NAME,
152 description = VERSION_MINOR_DESCRIPTION,
153 response = String.class),
155 name = VERSION_PATCH_NAME,
156 description = VERSION_PATCH_DESCRIPTION,
157 response = String.class),
159 name = VERSION_LATEST_NAME,
160 description = VERSION_LATEST_DESCRIPTION,
161 response = String.class),
163 name = REQUEST_ID_NAME,
164 description = REQUEST_ID_HDR_DESCRIPTION,
165 response = UUID.class)
170 name = EXTENSION_NAME,
172 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
173 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
180 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
181 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
182 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
186 public ResponseEntity<InstancePropertiesResponse> createInstanceProperties(
188 name = REQUEST_ID_NAME,
189 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
190 @ApiParam(value = "Body of instance properties", required = true) @RequestBody ToscaServiceTemplate body)
191 throws PfModelException {
193 return ResponseEntity.ok().body(provider.createInstanceProperties(body));
197 * Deletes a control loop definition and instance properties.
199 * @param requestId request ID used in ONAP logging
200 * @param name the name of the control loop to delete
201 * @param version the version of the control loop to delete
203 * @throws PfModelException on errors deleting of control loop and instance properties
206 @DeleteMapping(value = "/instanceProperties",
207 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
208 @ApiOperation(value = "Delete a control loop and instance properties",
209 notes = "Deletes a control loop and instance properties, returning optional error details",
210 response = InstantiationResponse.class,
212 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
215 name = VERSION_MINOR_NAME,
216 description = VERSION_MINOR_DESCRIPTION,
217 response = String.class),
219 name = VERSION_PATCH_NAME,
220 description = VERSION_PATCH_DESCRIPTION,
221 response = String.class),
223 name = VERSION_LATEST_NAME,
224 description = VERSION_LATEST_DESCRIPTION,
225 response = String.class),
227 name = REQUEST_ID_NAME,
228 description = REQUEST_ID_HDR_DESCRIPTION,
229 response = UUID.class)},
233 name = EXTENSION_NAME,
235 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
236 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
243 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
244 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
245 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
250 public ResponseEntity<InstantiationResponse> deleteInstanceProperties(
252 name = REQUEST_ID_NAME,
253 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
254 @ApiParam(value = "Control Loop definition name", required = true) @RequestParam("name") String name,
255 @ApiParam(value = "Control Loop definition version") @RequestParam(
257 required = true) String version) throws PfModelException {
259 return ResponseEntity.ok().body(provider.deleteInstanceProperties(name, version));
263 * Queries details of all control loops.
265 * @param requestId request ID used in ONAP logging
266 * @param name the name of the control loop to get, null for all control loops
267 * @param version the version of the control loop to get, null for all control loops
268 * @return the control loops
269 * @throws PfModelException on errors getting commissioning of control loop
272 @GetMapping(value = "/instantiation",
273 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
274 @ApiOperation(value = "Query details of the requested control loops",
275 notes = "Queries details of the requested control loops, returning all control loop details",
276 response = ControlLoops.class,
278 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
281 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
282 response = String.class),
283 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
284 response = String.class),
285 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
286 response = String.class),
287 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
288 response = UUID.class)},
292 name = EXTENSION_NAME,
294 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
295 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
302 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
303 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
304 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
308 public ResponseEntity<ControlLoops> query(
310 name = REQUEST_ID_NAME,
311 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
312 @ApiParam(value = "Control Loop definition name", required = false) @RequestParam(
314 required = false) String name,
315 @ApiParam(value = "Control Loop definition version", required = false) @RequestParam(
317 required = false) String version)
318 throws PfModelException {
320 return ResponseEntity.ok().body(provider.getControlLoops(name, version));
324 * Updates a control loop.
326 * @param requestId request ID used in ONAP logging
327 * @param controlLoops the control loops
329 * @throws PfModelException on errors updating of control loops
332 @PutMapping(value = "/instantiation",
333 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
334 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
336 value = "Updates control loop definitions",
337 notes = "Updates control loop definitions, returning the updated control loop definition IDs",
338 response = InstantiationResponse.class,
340 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
343 name = VERSION_MINOR_NAME,
344 description = VERSION_MINOR_DESCRIPTION,
345 response = String.class),
347 name = VERSION_PATCH_NAME,
348 description = VERSION_PATCH_DESCRIPTION,
349 response = String.class),
351 name = VERSION_LATEST_NAME,
352 description = VERSION_LATEST_DESCRIPTION,
353 response = String.class),
355 name = REQUEST_ID_NAME,
356 description = REQUEST_ID_HDR_DESCRIPTION,
357 response = UUID.class)
362 name = EXTENSION_NAME,
364 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
365 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
372 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
373 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
374 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
378 public ResponseEntity<InstantiationResponse> update(
380 name = REQUEST_ID_NAME,
381 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
382 @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ControlLoops controlLoops)
383 throws PfModelException {
385 return ResponseEntity.ok().body(provider.updateControlLoops(controlLoops));
389 * Deletes a control loop definition.
391 * @param requestId request ID used in ONAP logging
392 * @param name the name of the control loop to delete
393 * @param version the version of the control loop to delete
395 * @throws PfModelException on errors deleting of control loop
398 @DeleteMapping(value = "/instantiation",
399 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
400 @ApiOperation(value = "Delete a control loop",
401 notes = "Deletes a control loop, returning optional error details",
402 response = InstantiationResponse.class,
404 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
407 name = VERSION_MINOR_NAME,
408 description = VERSION_MINOR_DESCRIPTION,
409 response = String.class),
411 name = VERSION_PATCH_NAME,
412 description = VERSION_PATCH_DESCRIPTION,
413 response = String.class),
415 name = VERSION_LATEST_NAME,
416 description = VERSION_LATEST_DESCRIPTION,
417 response = String.class),
419 name = REQUEST_ID_NAME,
420 description = REQUEST_ID_HDR_DESCRIPTION,
421 response = UUID.class)},
425 name = EXTENSION_NAME,
427 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
428 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
435 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
436 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
437 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
442 public ResponseEntity<InstantiationResponse> delete(
444 name = REQUEST_ID_NAME,
445 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
446 @ApiParam(value = "Control Loop definition name", required = true) @RequestParam("name") String name,
447 @ApiParam(value = "Control Loop definition version") @RequestParam(
449 required = false) String version)
450 throws PfModelException {
452 return ResponseEntity.ok().body(provider.deleteControlLoop(name, version));
456 * Issues control loop commands to control loops.
458 * @param requestId request ID used in ONAP logging
459 * @param command the command to issue to control loops
460 * @return the control loop definitions
461 * @throws PfModelException on errors issuing a command
462 * @throws ControlLoopException on errors issuing a command
465 @PutMapping(value = "/instantiation/command",
466 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
467 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
468 @ApiOperation(value = "Issue a command to the requested control loops",
469 notes = "Issues a command to a control loop, ordering a state change on the control loop",
470 response = InstantiationResponse.class,
472 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
475 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
476 response = String.class),
477 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
478 response = String.class),
479 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
480 response = String.class),
481 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
482 response = UUID.class)},
486 name = EXTENSION_NAME,
488 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
489 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
496 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
497 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
498 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
502 public ResponseEntity<InstantiationResponse> issueControlLoopCommand(
504 name = REQUEST_ID_NAME,
505 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
507 value = "Entity Body of control loop command",
508 required = true) @RequestBody InstantiationCommand command)
509 throws ControlLoopException, PfModelException {
511 return ResponseEntity.accepted().body(provider.issueControlLoopCommand(command));
515 * Queries details of all control loops.
517 * @param requestId request ID used in ONAP logging
518 * @param name the name of the control loop to get, null for all control loops
519 * @param version the version of the control loop to get, null for all control loops
520 * @return the control loops
521 * @throws PfModelException on errors getting commissioning of control loop
524 @GetMapping(value = "/instantiationState",
525 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
526 @ApiOperation(value = "Query details of the requested control loops",
527 notes = "Queries details of the requested control loops, returning all control loop details",
528 response = ControlLoops.class,
530 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
533 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
534 response = String.class),
535 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
536 response = String.class),
537 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
538 response = String.class),
539 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
540 response = UUID.class)},
544 name = EXTENSION_NAME,
546 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
547 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
554 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
555 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
556 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
560 public ResponseEntity<ControlLoopOrderStateResponse> getInstantiationOrderState(
562 name = REQUEST_ID_NAME,
563 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
564 @ApiParam(value = "Control Loop name", required = false) @RequestParam(
566 required = false) String name,
567 @ApiParam(value = "Control Loop version", required = false) @RequestParam(
569 required = false) String version)
570 throws PfModelException {
572 return ResponseEntity.ok().body(provider.getInstantiationOrderState(name, version));