2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022 Nordix Foundation.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.rest;
25 import java.util.List;
26 import java.util.UUID;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.policy.api.main.exception.PolicyApiRuntimeException;
29 import org.onap.policy.api.main.rest.genapi.ToscaNodeTemplateDesignApi;
30 import org.onap.policy.api.main.service.ToscaServiceTemplateService;
31 import org.onap.policy.common.endpoints.event.comm.Topic;
32 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
37 import org.springframework.context.annotation.Profile;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.bind.annotation.RestController;
42 * Class to provide REST API services for Tosca Node templates.
45 @RequiredArgsConstructor
47 public class NodeTemplateController extends CommonRestController implements ToscaNodeTemplateDesignApi {
49 private final ToscaServiceTemplateService toscaServiceTemplateService;
52 * Creates one or more new tosca node templates in one call.
54 * @param body the body of the node templates in TOSCA definition
56 * @return the Response object containing the results of the API operation
59 public ResponseEntity<ToscaServiceTemplate> createToscaNodeTemplates(ToscaServiceTemplate body, UUID requestId) {
61 if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
62 NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
66 ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.createToscaNodeTemplates(body);
67 return makeOkResponse(requestId, nodeTemplates);
68 } catch (PfModelException | PfModelRuntimeException pfme) {
69 final var msg = "POST /nodetemplates";
70 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
75 * Updates one or more node templates in one call.
77 * @param body the body of the node templates in TOSCA definition
79 * @return the Response object containing the results of the API operation
82 public ResponseEntity<ToscaServiceTemplate> updateToscaNodeTemplates(ToscaServiceTemplate body, UUID requestId) {
84 if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
85 NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
89 ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.updateToscaNodeTemplates(body);
90 return makeOkResponse(requestId, nodeTemplates);
91 } catch (PfModelException | PfModelRuntimeException pfme) {
92 final var msg = "PUT /nodetemplates";
93 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
98 * Deletes a node template with specific name and version.
100 * @param name the name of node template
101 * @param version the version of node template
102 * @return the Response object containing the results of the API operation
105 public ResponseEntity<ToscaServiceTemplate> deleteToscaNodeTemplates(String name, String version, UUID requestId) {
107 ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.deleteToscaNodeTemplate(name, version);
108 return makeOkResponse(requestId, nodeTemplates);
109 } catch (PfModelException | PfModelRuntimeException pfme) {
110 final var msg = String.format("DELETE /nodetemplates/%s/versions/%s", name, version);
111 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
116 * Retrieves the specified version of a node template.
118 * @param name the name of the node template
119 * @param version the version of the node template
121 * @return the Response object containing the results of the API operation
124 public ResponseEntity<List<ToscaNodeTemplate>> getSpecificVersionOfNodeTemplate(String name, String version,
127 List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(name, version);
128 return makeOkResponse(requestId, nodeTemplates);
129 } catch (PfModelException | PfModelRuntimeException pfme) {
130 var msg = String.format("GET /nodetemplates/%s/versions/%s", name, version);
131 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
136 * Retrieves all the node templates from the tosca service template.
138 * @return the Response object containing the results of the API operation
141 public ResponseEntity<List<ToscaNodeTemplate>> getAllNodeTemplates(UUID requestId) {
143 List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(null, null);
144 return makeOkResponse(requestId, nodeTemplates);
145 } catch (PfModelException | PfModelRuntimeException pfme) {
146 var msg = "GET /nodetemplates";
147 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);