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.models.messages.rest.commissioning.CommissioningResponse;
37 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
38 import org.onap.policy.clamp.controlloop.runtime.main.web.AbstractRestController;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.springframework.http.MediaType;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.bind.annotation.DeleteMapping;
45 import org.springframework.web.bind.annotation.GetMapping;
46 import org.springframework.web.bind.annotation.PostMapping;
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, querying commissioned control loops.
56 @RequiredArgsConstructor
57 public class CommissioningController extends AbstractRestController {
59 private static final String TAGS = "Clamp Control Loop Commissioning API";
61 private final CommissioningProvider provider;
64 * Creates a control loop definition.
66 * @param requestId request ID used in ONAP logging
67 * @param body the body of control loop following TOSCA definition
69 * @throws PfModelException on errors creating a control loop definition
72 @PostMapping(value = "/commission",
73 consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
74 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
76 value = "Commissions control loop definitions",
77 notes = "Commissions control loop definitions, returning the commissioned control loop definition IDs",
78 response = CommissioningResponse.class,
80 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
83 name = VERSION_MINOR_NAME,
84 description = VERSION_MINOR_DESCRIPTION,
85 response = String.class),
87 name = VERSION_PATCH_NAME,
88 description = VERSION_PATCH_DESCRIPTION,
89 response = String.class),
91 name = VERSION_LATEST_NAME,
92 description = VERSION_LATEST_DESCRIPTION,
93 response = String.class),
95 name = REQUEST_ID_NAME,
96 description = REQUEST_ID_HDR_DESCRIPTION,
97 response = UUID.class)
102 name = EXTENSION_NAME,
104 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
105 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
112 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
113 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
114 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
118 public ResponseEntity<CommissioningResponse> create(
120 name = REQUEST_ID_NAME,
121 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
122 @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ToscaServiceTemplate body)
123 throws PfModelException {
125 return ResponseEntity.ok().body(provider.createControlLoopDefinitions(body));
129 * Deletes a control loop definition.
131 * @param requestId request ID used in ONAP logging
132 * @param name the name of the control loop definition to delete
133 * @param version the version of the control loop definition to delete
135 * @throws PfModelException on errors deleting a control loop definition
138 @DeleteMapping(value = "/commission",
139 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
140 @ApiOperation(value = "Delete a commissioned control loop",
141 notes = "Deletes a Commissioned Control Loop, returning optional error details",
142 response = CommissioningResponse.class,
144 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
147 name = VERSION_MINOR_NAME,
148 description = VERSION_MINOR_DESCRIPTION,
149 response = String.class),
151 name = VERSION_PATCH_NAME,
152 description = VERSION_PATCH_DESCRIPTION,
153 response = String.class),
155 name = VERSION_LATEST_NAME,
156 description = VERSION_LATEST_DESCRIPTION,
157 response = String.class),
159 name = REQUEST_ID_NAME,
160 description = REQUEST_ID_HDR_DESCRIPTION,
161 response = UUID.class)},
165 name = EXTENSION_NAME,
167 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
168 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
175 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
176 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
177 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
181 public ResponseEntity<CommissioningResponse> delete(
183 name = REQUEST_ID_NAME,
184 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
185 @ApiParam(value = "Control Loop definition name", required = true) @RequestParam(
186 value = "name") String name,
188 value = "Control Loop definition version",
189 required = true) @RequestParam("version") String version)
190 throws PfModelException {
192 return ResponseEntity.ok().body(provider.deleteControlLoopDefinition(name, version));
196 * Queries details of all or specific control loop definitions.
198 * @param requestId request ID used in ONAP logging
199 * @param name the name of the control loop definition to get, null for all definitions
200 * @param version the version of the control loop definition to get, null for all definitions
201 * @return the control loop definitions
202 * @throws PfModelException on errors getting details of all or specific control loop definitions
205 @GetMapping(value = "/commission",
206 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
207 @ApiOperation(value = "Query details of the requested commissioned control loop definitions",
208 notes = "Queries details of the requested commissioned control loop definitions, "
209 + "returning all control loop details",
210 response = ToscaNodeTemplate.class,
212 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
215 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
216 response = String.class),
217 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
218 response = String.class),
219 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
220 response = String.class),
221 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
222 response = UUID.class)},
226 name = EXTENSION_NAME,
228 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
229 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
236 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
237 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
238 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
242 public ResponseEntity<List<ToscaNodeTemplate>> query(
244 name = REQUEST_ID_NAME,
245 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
246 @ApiParam(value = "Control Loop definition name", required = false) @RequestParam(
248 required = false) String name,
249 @ApiParam(value = "Control Loop definition version", required = false) @RequestParam(
251 required = false) String version)
252 throws PfModelException {
254 return ResponseEntity.ok().body(provider.getControlLoopDefinitions(name, version));
258 * Retrieves the Tosca Service Template.
260 * @param requestId request ID used in ONAP logging
261 * @param name the name of the tosca service template to retrieve
262 * @param version the version of the tosca service template to get
263 * @return the specified tosca service template
264 * @throws PfModelException on errors getting the Tosca Service Template
267 @GetMapping(value = "/commission/toscaservicetemplate",
268 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
269 @ApiOperation(value = "Query details of the requested tosca service templates",
270 notes = "Queries details of the requested commissioned tosca service template, "
271 + "returning all tosca service template details",
272 response = ToscaServiceTemplate.class,
274 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
277 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
278 response = String.class),
279 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
280 response = String.class),
281 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
282 response = String.class),
283 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
284 response = UUID.class)},
288 name = EXTENSION_NAME,
290 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
291 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
298 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
299 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
300 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
304 public ResponseEntity<String> queryToscaServiceTemplate(
306 name = REQUEST_ID_NAME,
307 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
308 @ApiParam(value = "Tosca service template name", required = false) @RequestParam(
310 required = false) String name,
311 @ApiParam(value = "Tosca service template version", required = false) @RequestParam(
313 required = false) String version)
314 throws PfModelException {
316 return ResponseEntity.ok().body(provider.getToscaServiceTemplateReduced(name, version));
320 * Retrieves the Json Schema for the specified Tosca Service Template.
322 * @param requestId request ID used in ONAP logging
323 * @param section section of the tosca service template to get schema for
324 * @return the specified tosca service template or section Json Schema
325 * @throws PfModelException on errros getting the Json Schema for the specified Tosca Service Template
328 @GetMapping(value = "/commission/toscaServiceTemplateSchema",
329 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
330 @ApiOperation(value = "Query details of the requested tosca service template json schema",
331 notes = "Queries details of the requested commissioned tosca service template json schema, "
332 + "returning all tosca service template json schema details",
333 response = ToscaServiceTemplate.class,
335 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
338 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
339 response = String.class),
340 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
341 response = String.class),
342 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
343 response = String.class),
344 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
345 response = UUID.class)},
349 name = EXTENSION_NAME,
351 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
352 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
359 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
360 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
361 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
365 public ResponseEntity<String> queryToscaServiceTemplateJsonSchema(
367 name = REQUEST_ID_NAME,
368 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
369 @ApiParam(value = "Section of Template schema is desired for", required = false) @RequestParam(
372 defaultValue = "all") String section)
373 throws PfModelException {
375 return ResponseEntity.ok().body(provider.getToscaServiceTemplateSchema(section));
379 * Retrieves the Common or Instance Properties for the specified Tosca Service Template.
381 * @param requestId request ID used in ONAP logging
382 * @param common a flag, true to get common properties, false to get instance properties
383 * @param name the name of the tosca service template to retrieve
384 * @param version the version of the tosca service template to get
385 * @return the specified tosca service template or section Json Schema
386 * @throws PfModelException on errors getting the Common or Instance Properties
389 @GetMapping(value = "/commission/getCommonOrInstanceProperties",
390 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
391 @ApiOperation(value = "Query details of the requested tosca service template common or instance properties",
392 notes = "Queries details of the requested commissioned tosca service template json common"
393 + "or instance properties, returning all tosca service template common or instance property details",
394 response = ToscaServiceTemplate.class,
395 tags = {"Clamp Control Loop Commissioning API"},
396 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
399 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
400 response = String.class),
401 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
402 response = String.class),
403 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
404 response = String.class),
405 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
406 response = UUID.class)},
410 name = EXTENSION_NAME,
412 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
413 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
420 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
421 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
422 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
426 public ResponseEntity<Map<String, ToscaNodeTemplate>> queryToscaServiceCommonOrInstanceProperties(
428 name = REQUEST_ID_NAME,
429 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
430 @ApiParam(value = "Flag, true for common properties, false for instance", required = false) @RequestParam(
432 defaultValue = "false",
433 required = false) boolean common,
434 @ApiParam(value = "Tosca service template name", required = false) @RequestParam(
436 required = false) String name,
437 @ApiParam(value = "Tosca service template version", required = false) @RequestParam(
439 required = false) String version)
440 throws PfModelException {
442 return ResponseEntity.ok().body(provider.getNodeTemplatesWithCommonOrInstanceProperties(common, name, version));
446 * Queries the elements of a specific control loop.
448 * @param requestId request ID used in ONAP logging
449 * @param name the name of the control loop definition to get
450 * @param version the version of the control loop definition to get
451 * @return the control loop element definitions
452 * @throws PfModelException on errors getting the elements of a specific control loop
455 @GetMapping(value = "/commission/elements",
456 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
457 @ApiOperation(value = "Query details of the requested commissioned control loop element definitions",
458 notes = "Queries details of the requested commissioned control loop element definitions, "
459 + "returning all control loop elements' details",
460 response = ToscaNodeTemplate.class,
462 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
465 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
466 response = String.class),
467 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
468 response = String.class),
469 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
470 response = String.class),
471 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
472 response = UUID.class)},
476 name = EXTENSION_NAME,
478 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
479 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
486 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
487 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
488 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
492 public ResponseEntity<List<ToscaNodeTemplate>> queryElements(
494 name = REQUEST_ID_NAME,
495 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
496 @ApiParam(value = "Control Loop definition name", required = false) @RequestParam(
498 required = false) String name,
499 @ApiParam(value = "Control Loop definition version", required = false) @RequestParam(
501 required = false) String version)
502 throws PfModelException {
504 List<ToscaNodeTemplate> nodeTemplate = provider.getControlLoopDefinitions(name, version);
505 // Prevent ambiguous queries with multiple returns
506 if (nodeTemplate.size() > 1) {
507 throw new PfModelException(Status.NOT_ACCEPTABLE, "Multiple ControlLoops are not supported");
510 List<ToscaNodeTemplate> response = provider.getControlLoopElementDefinitions(nodeTemplate.get(0));
511 return ResponseEntity.ok().body(response);