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.InstantiationCommand;
37 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
38 import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstantiationProvider;
39 import org.onap.policy.clamp.controlloop.runtime.main.web.AbstractRestController;
40 import org.onap.policy.models.base.PfModelException;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.DeleteMapping;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PostMapping;
46 import org.springframework.web.bind.annotation.PutMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestHeader;
49 import org.springframework.web.bind.annotation.RequestParam;
50 import org.springframework.web.bind.annotation.RestController;
53 * Class to provide REST end points for creating, deleting, query and commanding a control loop definition.
56 @RequiredArgsConstructor
57 public class InstantiationController extends AbstractRestController {
59 private static final String TAGS = "Clamp Control Loop Instantiation API";
61 // The CL provider for instantiation requests
62 private final ControlLoopInstantiationProvider provider;
65 * Creates a control loop.
67 * @param requestId request ID used in ONAP logging
68 * @param controlLoops the control loops
70 * @throws PfModelException on errors creating a control loop
73 @PostMapping(value = "/instantiation",
74 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
75 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
77 value = "Commissions control loop definitions",
78 notes = "Commissions control loop definitions, returning the control loop IDs",
79 response = InstantiationResponse.class,
81 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
84 name = VERSION_MINOR_NAME,
85 description = VERSION_MINOR_DESCRIPTION,
86 response = String.class),
88 name = VERSION_PATCH_NAME,
89 description = VERSION_PATCH_DESCRIPTION,
90 response = String.class),
92 name = VERSION_LATEST_NAME,
93 description = VERSION_LATEST_DESCRIPTION,
94 response = String.class),
96 name = REQUEST_ID_NAME,
97 description = REQUEST_ID_HDR_DESCRIPTION,
98 response = UUID.class)
103 name = EXTENSION_NAME,
105 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
106 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
113 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
114 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
115 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
119 public ResponseEntity<InstantiationResponse> create(
121 name = REQUEST_ID_NAME,
122 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
123 @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ControlLoops controlLoops)
124 throws PfModelException {
126 return ResponseEntity.ok().body(provider.createControlLoops(controlLoops));
130 * Queries details of all control loops.
132 * @param requestId request ID used in ONAP logging
133 * @param name the name of the control loop to get, null for all control loops
134 * @param version the version of the control loop to get, null for all control loops
135 * @return the control loops
136 * @throws PfModelException on errors getting commissioning of control loop
139 @GetMapping(value = "/instantiation",
140 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
141 @ApiOperation(value = "Query details of the requested control loops",
142 notes = "Queries details of the requested control loops, returning all control loop details",
143 response = ControlLoops.class,
145 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
148 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
149 response = String.class),
150 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
151 response = String.class),
152 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
153 response = String.class),
154 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
155 response = UUID.class)},
159 name = EXTENSION_NAME,
161 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
162 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
169 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
170 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
171 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
175 public ResponseEntity<ControlLoops> query(
177 name = REQUEST_ID_NAME,
178 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
179 @ApiParam(value = "Control Loop definition name", required = false) @RequestParam(
181 required = false) String name,
182 @ApiParam(value = "Control Loop definition version", required = false) @RequestParam(
184 required = false) String version)
185 throws PfModelException {
187 return ResponseEntity.ok().body(provider.getControlLoops(name, version));
191 * Updates a control loop.
193 * @param requestId request ID used in ONAP logging
194 * @param controlLoops the control loops
196 * @throws PfModelException on errors updating of control loops
199 @PutMapping(value = "/instantiation",
200 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
201 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
203 value = "Updates control loop definitions",
204 notes = "Updates control loop definitions, returning the updated control loop definition IDs",
205 response = InstantiationResponse.class,
207 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
210 name = VERSION_MINOR_NAME,
211 description = VERSION_MINOR_DESCRIPTION,
212 response = String.class),
214 name = VERSION_PATCH_NAME,
215 description = VERSION_PATCH_DESCRIPTION,
216 response = String.class),
218 name = VERSION_LATEST_NAME,
219 description = VERSION_LATEST_DESCRIPTION,
220 response = String.class),
222 name = REQUEST_ID_NAME,
223 description = REQUEST_ID_HDR_DESCRIPTION,
224 response = UUID.class)
229 name = EXTENSION_NAME,
231 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
232 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
239 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
240 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
241 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
245 public ResponseEntity<InstantiationResponse> update(
247 name = REQUEST_ID_NAME,
248 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
249 @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ControlLoops controlLoops)
250 throws PfModelException {
252 return ResponseEntity.ok().body(provider.updateControlLoops(controlLoops));
256 * Deletes a control loop definition.
258 * @param requestId request ID used in ONAP logging
259 * @param name the name of the control loop to delete
260 * @param version the version of the control loop to delete
262 * @throws PfModelException on errors deleting of control loop
265 @DeleteMapping(value = "/instantiation",
266 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
267 @ApiOperation(value = "Delete a control loop",
268 notes = "Deletes a control loop, returning optional error details",
269 response = InstantiationResponse.class,
271 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
274 name = VERSION_MINOR_NAME,
275 description = VERSION_MINOR_DESCRIPTION,
276 response = String.class),
278 name = VERSION_PATCH_NAME,
279 description = VERSION_PATCH_DESCRIPTION,
280 response = String.class),
282 name = VERSION_LATEST_NAME,
283 description = VERSION_LATEST_DESCRIPTION,
284 response = String.class),
286 name = REQUEST_ID_NAME,
287 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)
309 public ResponseEntity<InstantiationResponse> delete(
311 name = REQUEST_ID_NAME,
312 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
313 @ApiParam(value = "Control Loop definition name", required = true) @RequestParam("name") String name,
314 @ApiParam(value = "Control Loop definition version") @RequestParam(
316 required = false) String version)
317 throws PfModelException {
319 return ResponseEntity.ok().body(provider.deleteControlLoop(name, version));
323 * Issues control loop commands to control loops.
325 * @param requestId request ID used in ONAP logging
326 * @param command the command to issue to control loops
327 * @return the control loop definitions
328 * @throws PfModelException on errors issuing a command
329 * @throws ControlLoopException on errors issuing a command
332 @PutMapping(value = "/instantiation/command",
333 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
334 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
335 @ApiOperation(value = "Issue a command to the requested control loops",
336 notes = "Issues a command to a control loop, ordering a state change on the control loop",
337 response = InstantiationResponse.class,
339 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
342 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
343 response = String.class),
344 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
345 response = String.class),
346 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
347 response = String.class),
348 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
349 response = UUID.class)},
353 name = EXTENSION_NAME,
355 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
356 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
363 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
364 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
365 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
369 public ResponseEntity<InstantiationResponse> issueControlLoopCommand(
371 name = REQUEST_ID_NAME,
372 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
374 value = "Entity Body of control loop command",
375 required = true) @RequestBody InstantiationCommand command)
376 throws ControlLoopException, PfModelException {
378 return ResponseEntity.accepted().body(provider.issueControlLoopCommand(command));
382 * Queries details of all control loops.
384 * @param requestId request ID used in ONAP logging
385 * @param name the name of the control loop to get, null for all control loops
386 * @param version the version of the control loop to get, null for all control loops
387 * @return the control loops
388 * @throws PfModelException on errors getting commissioning of control loop
391 @GetMapping(value = "/instantiationState",
392 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
393 @ApiOperation(value = "Query details of the requested control loops",
394 notes = "Queries details of the requested control loops, returning all control loop details",
395 response = ControlLoops.class,
397 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
400 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
401 response = String.class),
402 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
403 response = String.class),
404 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
405 response = String.class),
406 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
407 response = UUID.class)},
411 name = EXTENSION_NAME,
413 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
414 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
421 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
422 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
423 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
427 public ResponseEntity<ControlLoopOrderStateResponse> getInstantiationOrderState(
429 name = REQUEST_ID_NAME,
430 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
431 @ApiParam(value = "Control Loop definition name", required = false) @RequestParam(
433 required = false) String name,
434 @ApiParam(value = "Control Loop definition version", required = false) @RequestParam(
436 required = false) String version)
437 throws PfModelException {
439 return ResponseEntity.ok().body(provider.getInstantiationOrderState(name, version));