Fix synchronization issues in policy-api
[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.context.annotation.Profile;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.bind.annotation.RestController;
40
41 /**
42  * Class to provide REST API services for Tosca Node templates.
43  */
44 @RestController
45 @RequiredArgsConstructor
46 @Profile("default")
47 public class NodeTemplateController extends CommonRestController implements ToscaNodeTemplateDesignApi {
48
49     private final ToscaServiceTemplateService toscaServiceTemplateService;
50
51     /**
52      * Creates one or more new tosca node templates in one call.
53      *
54      * @param body the body of the node templates in TOSCA definition
55      *
56      * @return the Response object containing the results of the API operation
57      */
58     @Override
59     public ResponseEntity<ToscaServiceTemplate> createToscaNodeTemplates(ToscaServiceTemplate body, UUID requestId) {
60
61         if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
62             NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
63                 toJson(body));
64         }
65         try {
66             mutex.acquire();
67             ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.createToscaNodeTemplates(body);
68             return makeOkResponse(requestId, nodeTemplates);
69         } catch (PfModelException | PfModelRuntimeException pfme) {
70             final var msg = "POST /nodetemplates";
71             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
72         } catch (InterruptedException e) {
73             Thread.currentThread().interrupt();
74             throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);
75         } finally {
76             mutex.release();
77         }
78     }
79
80     /**
81      * Updates one or more node templates in one call.
82      *
83      * @param body the body of the node templates in TOSCA definition
84      *
85      * @return the Response object containing the results of the API operation
86      */
87     @Override
88     public ResponseEntity<ToscaServiceTemplate> updateToscaNodeTemplates(ToscaServiceTemplate body, UUID requestId) {
89
90         if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
91             NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
92                 toJson(body));
93         }
94         try {
95             mutex.acquire();
96             ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.updateToscaNodeTemplates(body);
97             return makeOkResponse(requestId, nodeTemplates);
98         } catch (PfModelException | PfModelRuntimeException pfme) {
99             final var msg = "PUT /nodetemplates";
100             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
101         } catch (InterruptedException e) {
102             Thread.currentThread().interrupt();
103             throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);
104         } finally {
105             mutex.release();
106         }
107     }
108
109     /**
110      * Deletes a node template with specific name and version.
111      *
112      * @param name the name of node template
113      * @param version the version of node template
114      * @return the Response object containing the results of the API operation
115      */
116     @Override
117     public ResponseEntity<ToscaServiceTemplate> deleteToscaNodeTemplates(String name, String version, UUID requestId) {
118         try {
119             mutex.acquire();
120             ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.deleteToscaNodeTemplate(name, version);
121             return makeOkResponse(requestId, nodeTemplates);
122         } catch (PfModelException | PfModelRuntimeException pfme) {
123             final var msg = String.format("DELETE /nodetemplates/%s/versions/%s", name, version);
124             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
125         } catch (InterruptedException e) {
126             Thread.currentThread().interrupt();
127             throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);
128         } finally {
129             mutex.release();
130         }
131     }
132
133     /**
134      * Retrieves the specified version of a node template.
135      *
136      * @param name the name of the node template
137      * @param version the version of the node template
138      *
139      * @return the Response object containing the results of the API operation
140      */
141     @Override
142     public ResponseEntity<List<ToscaNodeTemplate>> getSpecificVersionOfNodeTemplate(String name, String version,
143         UUID requestId) {
144         try {
145             List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(name, version);
146             return makeOkResponse(requestId, nodeTemplates);
147         } catch (PfModelException | PfModelRuntimeException pfme) {
148             var msg = String.format("GET /nodetemplates/%s/versions/%s", name, version);
149             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
150         }
151     }
152
153     /**
154      * Retrieves all the node templates from the tosca service template.
155      *
156      * @return the Response object containing the results of the API operation
157      */
158     @Override
159     public ResponseEntity<List<ToscaNodeTemplate>> getAllNodeTemplates(UUID requestId) {
160         try {
161             List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(null, null);
162             return makeOkResponse(requestId, nodeTemplates);
163         } catch (PfModelException | PfModelRuntimeException pfme) {
164             var msg = "GET /nodetemplates";
165             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
166         }
167     }
168 }