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) {
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));