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 io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiParam;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30 import io.swagger.annotations.Authorization;
31 import io.swagger.annotations.BasicAuthDefinition;
32 import io.swagger.annotations.Extension;
33 import io.swagger.annotations.ExtensionProperty;
34 import io.swagger.annotations.Info;
35 import io.swagger.annotations.ResponseHeader;
36 import io.swagger.annotations.SecurityDefinition;
37 import io.swagger.annotations.SwaggerDefinition;
38 import java.net.HttpURLConnection;
39 import java.util.List;
40 import java.util.UUID;
41 import lombok.RequiredArgsConstructor;
42 import org.onap.policy.api.main.exception.PolicyApiRuntimeException;
43 import org.onap.policy.api.main.service.ToscaServiceTemplateService;
44 import org.onap.policy.common.endpoints.event.comm.Topic;
45 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
46 import org.onap.policy.models.base.PfModelException;
47 import org.onap.policy.models.base.PfModelRuntimeException;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.web.bind.annotation.DeleteMapping;
54 import org.springframework.web.bind.annotation.GetMapping;
55 import org.springframework.web.bind.annotation.PathVariable;
56 import org.springframework.web.bind.annotation.PostMapping;
57 import org.springframework.web.bind.annotation.PutMapping;
58 import org.springframework.web.bind.annotation.RequestBody;
59 import org.springframework.web.bind.annotation.RequestHeader;
60 import org.springframework.web.bind.annotation.RequestMapping;
61 import org.springframework.web.bind.annotation.RestController;
64 * Class to provide REST API services for Tosca Node templates.
67 @RequestMapping(path = "/policy/api/v1", produces = { "application/json", "application/yaml" })
68 @Api(value = "Tosca Node template Design API")
71 description = "Tosca Node template Design API is publicly exposed for clients to Create/Read/Update/Delete"
72 + " node templates which can be recognized"
73 + " and executable by incorporated policy engines. It is an"
74 + " independent component running rest service that takes all node templates design API calls"
75 + " from clients and then assign them to different API working functions.",
76 version = "1.0.0", title = "Tosca Node template Design",
77 extensions = {@Extension(properties = {@ExtensionProperty(name = "planned-retirement-date", value = "tbd"),
78 @ExtensionProperty(name = "component", value = "Policy Framework")})}),
79 schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},
80 securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")}))
81 @RequiredArgsConstructor
82 public class NodeTemplateController extends CommonRestController {
84 private static final Logger LOGGER = LoggerFactory.getLogger(NodeTemplateController.class);
86 private final ToscaServiceTemplateService toscaServiceTemplateService;
89 * Creates one or more new tosca node templates in one call.
91 * @param body the body of the node templates in TOSCA definition
93 * @return the Response object containing the results of the API operation
95 @PostMapping("/nodetemplates")
96 @ApiOperation(value = "Create one or more new node templates",
97 notes = "Client should provide TOSCA body of the new node templates",
98 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
99 response = ToscaServiceTemplate.class,
101 @ResponseHeader(name = VERSION_MINOR_NAME,
102 description = VERSION_MINOR_DESCRIPTION,
103 response = String.class),
104 @ResponseHeader(name = VERSION_PATCH_NAME,
105 description = VERSION_PATCH_DESCRIPTION,
106 response = String.class),
107 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
108 response = String.class),
109 @ResponseHeader(name = REQUEST_ID_NAME,
110 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
112 @Extension(name = EXTENSION_NAME, properties = {
113 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
114 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
115 @ApiResponses(value = {
116 @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
117 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
118 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
119 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
120 @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
121 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
122 public ResponseEntity<ToscaServiceTemplate> createToscaNodeTemplates(
123 @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
124 @RequestBody @ApiParam(value = "Entity body of tosca node templates", required = true)
125 ToscaServiceTemplate body) {
127 if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
128 NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
132 ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.createToscaNodeTemplates(body);
133 return makeOkResponse(requestId, nodeTemplates);
134 } catch (PfModelException | PfModelRuntimeException pfme) {
135 final var msg = "POST /nodetemplates";
136 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
142 * Updates one or more node templates in one call.
144 * @param body the body of the node templates in TOSCA definition
146 * @return the Response object containing the results of the API operation
148 @PutMapping("/nodetemplates")
149 @ApiOperation(value = "Updates one or more new node templates",
150 notes = "Client should provide TOSCA body of the updated node templates",
151 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
152 response = ToscaServiceTemplate.class,
154 @ResponseHeader(name = VERSION_MINOR_NAME,
155 description = VERSION_MINOR_DESCRIPTION,
156 response = String.class),
157 @ResponseHeader(name = VERSION_PATCH_NAME,
158 description = VERSION_PATCH_DESCRIPTION,
159 response = String.class),
160 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
161 response = String.class),
162 @ResponseHeader(name = REQUEST_ID_NAME,
163 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
165 @Extension(name = EXTENSION_NAME, properties = {
166 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
167 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
168 @ApiResponses(value = {
169 @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
170 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
171 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
172 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
173 @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
174 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
175 public ResponseEntity<ToscaServiceTemplate> updateToscaNodeTemplates(
176 @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
177 @RequestBody @ApiParam(value = "Entity body of tosca node templates", required = true)
178 ToscaServiceTemplate body) {
180 if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
181 NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
185 ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.updateToscaNodeTemplates(body);
186 return makeOkResponse(requestId, nodeTemplates);
187 } catch (PfModelException | PfModelRuntimeException pfme) {
188 final var msg = "PUT /nodetemplates";
189 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
195 * Deletes a node template with specific name and version.
197 * @param name the name of node template
198 * @param version the version of node template
199 * @return the Response object containing the results of the API operation
201 @DeleteMapping("/nodetemplates/{name}/versions/{version}")
202 @ApiOperation(value = "Updates one or more new node templates",
203 notes = "Client should provide TOSCA body of the updated node templates",
204 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
205 response = ToscaServiceTemplate.class,
207 @ResponseHeader(name = VERSION_MINOR_NAME,
208 description = VERSION_MINOR_DESCRIPTION,
209 response = String.class),
210 @ResponseHeader(name = VERSION_PATCH_NAME,
211 description = VERSION_PATCH_DESCRIPTION,
212 response = String.class),
213 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
214 response = String.class),
215 @ResponseHeader(name = REQUEST_ID_NAME,
216 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
218 @Extension(name = EXTENSION_NAME, properties = {
219 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
220 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
221 @ApiResponses(value = {
222 @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
223 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
224 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
225 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
226 @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
227 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
228 public ResponseEntity<ToscaServiceTemplate> deleteToscaNodeTemplates(
229 @PathVariable("name") @ApiParam(value = "Name of the node template", required = true) String name,
230 @PathVariable("version") @ApiParam(value = "Version of the node template",
231 required = true) String version,
232 @RequestHeader(name = REQUEST_ID_NAME, required = false)
233 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
235 ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.deleteToscaNodeTemplate(name, version);
236 return makeOkResponse(requestId, nodeTemplates);
237 } catch (PfModelException | PfModelRuntimeException pfme) {
238 final var msg = String.format("DELETE /nodetemplates/%s/versions/%s", name, version);
239 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
245 * Retrieves the specified version of a node template.
247 * @param name the name of the node template
248 * @param version the version of the node template
250 * @return the Response object containing the results of the API operation
252 @GetMapping("/nodetemplates/{name}/versions/{version}")
253 @ApiOperation(value = "Retrieve one version of a tosca node template",
254 notes = "Returns a particular version of a node template",
255 response = ToscaNodeTemplate.class,
256 responseContainer = "List",
258 @ResponseHeader(name = VERSION_MINOR_NAME,
259 description = VERSION_MINOR_DESCRIPTION,
260 response = String.class),
261 @ResponseHeader(name = VERSION_PATCH_NAME,
262 description = VERSION_PATCH_DESCRIPTION,
263 response = String.class),
264 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
265 response = String.class),
266 @ResponseHeader(name = REQUEST_ID_NAME,
267 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
268 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplates", },
270 @Extension(name = EXTENSION_NAME, properties = {
271 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
272 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")
276 @ApiResponses(value = {
277 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
278 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
279 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
280 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)
282 public ResponseEntity<List<ToscaNodeTemplate>> getSpecificVersionOfNodeTemplate(
283 @PathVariable("name") @ApiParam(value = "Name of the node template", required = true) String name,
284 @PathVariable("version") @ApiParam(value = "Version of the node template",
285 required = true) String version,
286 @RequestHeader(name = REQUEST_ID_NAME, required = false)
287 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
289 List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(name, version);
290 return makeOkResponse(requestId, nodeTemplates);
291 } catch (PfModelException | PfModelRuntimeException pfme) {
292 var msg = String.format("GET /nodetemplates/%s/versions/%s", name, version);
293 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
299 * Retrieves all the node templates from the tosca service template.
301 * @return the Response object containing the results of the API operation
303 @GetMapping("/nodetemplates")
304 @ApiOperation(value = "Retrieve all the available tosca node templates",
305 notes = "Returns all the node templates from the service template",
306 response = ToscaNodeTemplate.class,
307 responseContainer = "List",
309 @ResponseHeader(name = VERSION_MINOR_NAME,
310 description = VERSION_MINOR_DESCRIPTION,
311 response = String.class),
312 @ResponseHeader(name = VERSION_PATCH_NAME,
313 description = VERSION_PATCH_DESCRIPTION,
314 response = String.class),
315 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
316 response = String.class),
317 @ResponseHeader(name = REQUEST_ID_NAME,
318 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
319 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplates", },
321 @Extension(name = EXTENSION_NAME, properties = {
322 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
323 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")
327 @ApiResponses(value = {
328 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
329 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
330 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
331 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)
333 public ResponseEntity<List<ToscaNodeTemplate>> getAllNodeTemplates(
334 @RequestHeader(name = REQUEST_ID_NAME, required = false)
335 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
337 List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(null, null);
338 return makeOkResponse(requestId, nodeTemplates);
339 } catch (PfModelException | PfModelRuntimeException pfme) {
340 var msg = "GET /nodetemplates";
341 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);