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.List;
33 import java.util.UUID;
34 import javax.ws.rs.core.Response.Status;
35 import lombok.RequiredArgsConstructor;
36 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
37 import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse;
38 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
39 import org.onap.policy.clamp.controlloop.runtime.main.web.AbstractRestController;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
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.RequestBody;
49 import org.springframework.web.bind.annotation.RequestHeader;
50 import org.springframework.web.bind.annotation.RequestParam;
51 import org.springframework.web.bind.annotation.RestController;
54 * Class to provide REST end points for creating, deleting, querying commissioned control loops.
57 @RequiredArgsConstructor
58 public class CommissioningController extends AbstractRestController {
60 private static final String TAGS = "Clamp Control Loop Commissioning API";
62 private final CommissioningProvider provider;
65 * Creates a control loop definition.
67 * @param requestId request ID used in ONAP logging
68 * @param body the body of control loop following TOSCA definition
70 * @throws PfModelException on errors creating a control loop definition
73 @PostMapping(value = "/commission",
74 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
75 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
77 value = "Commissions control loop definitions",
78 notes = "Commissions control loop definitions, returning the commissioned control loop definition IDs",
79 response = CommissioningResponse.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<CommissioningResponse> 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 ToscaServiceTemplate body)
124 throws PfModelException {
126 return ResponseEntity.ok().body(provider.createControlLoopDefinitions(body));
130 * Deletes a control loop definition.
132 * @param requestId request ID used in ONAP logging
133 * @param name the name of the control loop definition to delete
134 * @param version the version of the control loop definition to delete
136 * @throws PfModelException on errors deleting a control loop definition
139 @DeleteMapping(value = "/commission",
140 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
141 @ApiOperation(value = "Delete a commissioned control loop",
142 notes = "Deletes a Commissioned Control Loop, returning optional error details",
143 response = CommissioningResponse.class,
145 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
148 name = VERSION_MINOR_NAME,
149 description = VERSION_MINOR_DESCRIPTION,
150 response = String.class),
152 name = VERSION_PATCH_NAME,
153 description = VERSION_PATCH_DESCRIPTION,
154 response = String.class),
156 name = VERSION_LATEST_NAME,
157 description = VERSION_LATEST_DESCRIPTION,
158 response = String.class),
160 name = REQUEST_ID_NAME,
161 description = REQUEST_ID_HDR_DESCRIPTION,
162 response = UUID.class)},
166 name = EXTENSION_NAME,
168 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
169 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
176 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
177 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
178 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
182 public ResponseEntity<CommissioningResponse> delete(
184 name = REQUEST_ID_NAME,
185 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
186 @ApiParam(value = "Control Loop definition name", required = true) @RequestParam(
187 value = "name") String name,
189 value = "Control Loop definition version",
190 required = true) @RequestParam("version") String version)
191 throws PfModelException {
193 return ResponseEntity.ok().body(provider.deleteControlLoopDefinition(name, version));
197 * Queries details of all or specific control loop definitions.
199 * @param requestId request ID used in ONAP logging
200 * @param name the name of the control loop definition to get, null for all definitions
201 * @param version the version of the control loop definition to get, null for all definitions
202 * @return the control loop definitions
203 * @throws PfModelException on errors getting details of all or specific control loop definitions
206 @GetMapping(value = "/commission",
207 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
208 @ApiOperation(value = "Query details of the requested commissioned control loop definitions",
209 notes = "Queries details of the requested commissioned control loop definitions, "
210 + "returning all control loop details",
211 response = ToscaNodeTemplate.class,
213 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
216 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
217 response = String.class),
218 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
219 response = String.class),
220 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
221 response = String.class),
222 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
223 response = UUID.class)},
227 name = EXTENSION_NAME,
229 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
230 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
237 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
238 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
239 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
243 public ResponseEntity<List<ToscaNodeTemplate>> query(
245 name = REQUEST_ID_NAME,
246 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
247 @ApiParam(value = "Control Loop definition name", required = false) @RequestParam(
249 required = false) String name,
250 @ApiParam(value = "Control Loop definition version", required = false) @RequestParam(
252 required = false) String version)
253 throws PfModelException {
255 return ResponseEntity.ok().body(provider.getControlLoopDefinitions(name, version));
259 * Retrieves the Tosca Service Template.
261 * @param requestId request ID used in ONAP logging
262 * @param name the name of the tosca service template to retrieve
263 * @param version the version of the tosca service template to get
264 * @return the specified tosca service template
265 * @throws PfModelException on errors getting the Tosca Service Template
268 @GetMapping(value = "/commission/toscaservicetemplate",
269 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
270 @ApiOperation(value = "Query details of the requested tosca service templates",
271 notes = "Queries details of the requested commissioned tosca service template, "
272 + "returning all tosca service template details",
273 response = ToscaServiceTemplate.class,
275 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
278 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
279 response = String.class),
280 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
281 response = String.class),
282 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
283 response = String.class),
284 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
285 response = UUID.class)},
289 name = EXTENSION_NAME,
291 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
292 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
299 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
300 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
301 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
305 public ResponseEntity<String> queryToscaServiceTemplate(
307 name = REQUEST_ID_NAME,
308 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
309 @ApiParam(value = "Tosca service template name", required = false) @RequestParam(
311 required = false) String name,
312 @ApiParam(value = "Tosca service template version", required = false) @RequestParam(
314 required = false) String version)
315 throws PfModelException {
317 return ResponseEntity.ok().body(provider.getToscaServiceTemplateReduced(name, version));
321 * Retrieves the Json Schema for the specified Tosca Service Template.
323 * @param requestId request ID used in ONAP logging
324 * @param section section of the tosca service template to get schema for
325 * @return the specified tosca service template or section Json Schema
326 * @throws PfModelException on errros getting the Json Schema for the specified Tosca Service Template
329 @GetMapping(value = "/commission/toscaServiceTemplateSchema",
330 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
331 @ApiOperation(value = "Query details of the requested tosca service template json schema",
332 notes = "Queries details of the requested commissioned tosca service template json schema, "
333 + "returning all tosca service template json schema details",
334 response = ToscaServiceTemplate.class,
336 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
339 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
340 response = String.class),
341 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
342 response = String.class),
343 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
344 response = String.class),
345 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
346 response = UUID.class)},
350 name = EXTENSION_NAME,
352 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
353 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
360 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
361 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
362 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
366 public ResponseEntity<String> queryToscaServiceTemplateJsonSchema(
368 name = REQUEST_ID_NAME,
369 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
370 @ApiParam(value = "Section of Template schema is desired for", required = false) @RequestParam(
373 defaultValue = "all") String section)
374 throws PfModelException {
376 return ResponseEntity.ok().body(provider.getToscaServiceTemplateSchema(section));
380 * Retrieves the Common or Instance Properties for the specified Tosca Service Template.
382 * @param requestId request ID used in ONAP logging
383 * @param common a flag, true to get common properties, false to get instance properties
384 * @param name the name of the tosca service template to retrieve
385 * @param version the version of the tosca service template to get
386 * @return the specified tosca service template or section Json Schema
387 * @throws PfModelException on errors getting the Common or Instance Properties
388 * @throws ControlLoopException on error getting the Common or Instance Properties
391 @GetMapping(value = "/commission/getCommonOrInstanceProperties",
392 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
393 @ApiOperation(value = "Query details of the requested tosca service template common or instance properties",
394 notes = "Queries details of the requested commissioned tosca service template json common"
395 + "or instance properties, returning all tosca service template common or instance property details",
396 response = ToscaServiceTemplate.class,
397 tags = {"Clamp Control Loop Commissioning API"},
398 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
401 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
402 response = String.class),
403 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
404 response = String.class),
405 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
406 response = String.class),
407 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
408 response = UUID.class)},
412 name = EXTENSION_NAME,
414 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
415 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
422 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
423 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
424 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
428 public ResponseEntity<Map<String, ToscaNodeTemplate>> queryToscaServiceCommonOrInstanceProperties(
430 name = REQUEST_ID_NAME,
431 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
432 @ApiParam(value = "Flag, true for common properties, false for instance", required = false) @RequestParam(
434 defaultValue = "false",
435 required = false) boolean common,
436 @ApiParam(value = "Tosca service template name", required = false) @RequestParam(
438 required = false) String name,
439 @ApiParam(value = "Tosca service template version", required = false) @RequestParam(
441 required = false) String version)
442 throws PfModelException {
444 return ResponseEntity.ok().body(provider.getNodeTemplatesWithCommonOrInstanceProperties(common, name, version));
448 * Queries the elements of a specific control loop.
450 * @param requestId request ID used in ONAP logging
451 * @param name the name of the control loop definition to get
452 * @param version the version of the control loop definition to get
453 * @return the control loop element definitions
454 * @throws PfModelException on errors getting the elements of a specific control loop
457 @GetMapping(value = "/commission/elements",
458 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
459 @ApiOperation(value = "Query details of the requested commissioned control loop element definitions",
460 notes = "Queries details of the requested commissioned control loop element definitions, "
461 + "returning all control loop elements' details",
462 response = ToscaNodeTemplate.class,
464 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
467 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
468 response = String.class),
469 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
470 response = String.class),
471 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
472 response = String.class),
473 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
474 response = UUID.class)},
478 name = EXTENSION_NAME,
480 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
481 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
488 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
489 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
490 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
494 public ResponseEntity<List<ToscaNodeTemplate>> queryElements(
496 name = REQUEST_ID_NAME,
497 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
498 @ApiParam(value = "Control Loop definition name", required = false) @RequestParam(
500 required = false) String name,
501 @ApiParam(value = "Control Loop definition version", required = false) @RequestParam(
503 required = false) String version)
504 throws PfModelException {
506 List<ToscaNodeTemplate> nodeTemplate = provider.getControlLoopDefinitions(name, version);
507 // Prevent ambiguous queries with multiple returns
508 if (nodeTemplate.size() > 1) {
509 throw new PfModelException(Status.NOT_ACCEPTABLE, "Multiple ControlLoops are not supported");
512 List<ToscaNodeTemplate> response = provider.getControlLoopElementDefinitions(nodeTemplate.get(0));
513 return ResponseEntity.ok().body(response);