ecad0e59e16758c169855cf897a9f6b3b7a7cbd6
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / NodeTemplateController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.rest;
24
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.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.RestController;
39
40 /**
41  * Class to provide REST API services for Tosca Node templates.
42  */
43 @RestController
44 @RequiredArgsConstructor
45 public class NodeTemplateController extends CommonRestController implements ToscaNodeTemplateDesignApi {
46
47     private final ToscaServiceTemplateService toscaServiceTemplateService;
48
49     /**
50      * Creates one or more new tosca node templates in one call.
51      *
52      * @param body the body of the node templates in TOSCA definition
53      *
54      * @return the Response object containing the results of the API operation
55      */
56     @Override
57     public ResponseEntity<ToscaServiceTemplate> createToscaNodeTemplates(ToscaServiceTemplate body, UUID requestId) {
58
59         if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
60             NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
61                 toJson(body));
62         }
63         try {
64             ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.createToscaNodeTemplates(body);
65             return makeOkResponse(requestId, nodeTemplates);
66         } catch (PfModelException | PfModelRuntimeException pfme) {
67             final var msg = "POST /nodetemplates";
68             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
69         }
70     }
71
72     /**
73      * Updates one or more node templates in one call.
74      *
75      * @param body the body of the node templates in TOSCA definition
76      *
77      * @return the Response object containing the results of the API operation
78      */
79     @Override
80     public ResponseEntity<ToscaServiceTemplate> updateToscaNodeTemplates(ToscaServiceTemplate body, UUID requestId) {
81
82         if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
83             NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
84                 toJson(body));
85         }
86         try {
87             ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.updateToscaNodeTemplates(body);
88             return makeOkResponse(requestId, nodeTemplates);
89         } catch (PfModelException | PfModelRuntimeException pfme) {
90             final var msg = "PUT /nodetemplates";
91             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
92         }
93     }
94
95     /**
96      * Deletes a node template with specific name and version.
97      *
98      * @param name the name of node template
99      * @param version the version of node template
100      * @return the Response object containing the results of the API operation
101      */
102     @Override
103     public ResponseEntity<ToscaServiceTemplate> deleteToscaNodeTemplates(String name, String version, UUID requestId) {
104         try {
105             ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.deleteToscaNodeTemplate(name, version);
106             return makeOkResponse(requestId, nodeTemplates);
107         } catch (PfModelException | PfModelRuntimeException pfme) {
108             final var msg = String.format("DELETE /nodetemplates/%s/versions/%s", name, version);
109             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
110         }
111     }
112
113     /**
114      * Retrieves the specified version of a node template.
115      *
116      * @param name the name of the node template
117      * @param version the version of the node template
118      *
119      * @return the Response object containing the results of the API operation
120      */
121     @Override
122     public ResponseEntity<List<ToscaNodeTemplate>> getSpecificVersionOfNodeTemplate(String name, String version,
123         UUID requestId) {
124         try {
125             List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(name, version);
126             return makeOkResponse(requestId, nodeTemplates);
127         } catch (PfModelException | PfModelRuntimeException pfme) {
128             var msg = String.format("GET /nodetemplates/%s/versions/%s", name, version);
129             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
130         }
131     }
132
133     /**
134      * Retrieves all the node templates from the tosca service template.
135      *
136      * @return the Response object containing the results of the API operation
137      */
138     @Override
139     public ResponseEntity<List<ToscaNodeTemplate>> getAllNodeTemplates(UUID requestId) {
140         try {
141             List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(null, null);
142             return makeOkResponse(requestId, nodeTemplates);
143         } catch (PfModelException | PfModelRuntimeException pfme) {
144             var msg = "GET /nodetemplates";
145             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
146         }
147     }
148 }