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 org.onap.policy.api.main.exception.PolicyApiRuntimeException;
42 import org.onap.policy.api.main.rest.provider.NodeTemplateProvider;
43 import org.onap.policy.common.endpoints.event.comm.Topic;
44 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
45 import org.onap.policy.models.base.PfModelException;
46 import org.onap.policy.models.base.PfModelRuntimeException;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.web.bind.annotation.DeleteMapping;
54 import org.springframework.web.bind.annotation.ExceptionHandler;
55 import org.springframework.web.bind.annotation.GetMapping;
56 import org.springframework.web.bind.annotation.PathVariable;
57 import org.springframework.web.bind.annotation.PostMapping;
58 import org.springframework.web.bind.annotation.PutMapping;
59 import org.springframework.web.bind.annotation.RequestBody;
60 import org.springframework.web.bind.annotation.RequestHeader;
61 import org.springframework.web.bind.annotation.RequestMapping;
62 import org.springframework.web.bind.annotation.RestController;
65 * Class to provide REST API services for Tosca Node templates.
68 @RequestMapping(path = "/policy/api/v1", produces = { "application/json", "application/yaml" })
69 @Api(value = "Tosca Node template Design API")
72 description = "Tosca Node template Design API is publicly exposed for clients to Create/Read/Update/Delete"
73 + " node templates which can be recognized"
74 + " and executable by incorporated policy engines. It is an"
75 + " independent component running rest service that takes all node templates design API calls"
76 + " from clients and then assign them to different API working functions.",
77 version = "1.0.0", title = "Tosca Node template Design",
78 extensions = {@Extension(properties = {@ExtensionProperty(name = "planned-retirement-date", value = "tbd"),
79 @ExtensionProperty(name = "component", value = "Policy Framework")})}),
80 schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},
81 securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")}))
82 public class NodeTemplateController extends CommonRestController {
84 private static final Logger LOGGER = LoggerFactory.getLogger(NodeTemplateController.class);
87 private NodeTemplateProvider provider;
90 * Creates one or more new tosca node templates in one call.
92 * @param body the body of the node templates in TOSCA definition
94 * @return the Response object containing the results of the API operation
96 @PostMapping("/nodetemplates")
97 @ApiOperation(value = "Create one or more new node templates",
98 notes = "Client should provide TOSCA body of the new node templates",
99 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
100 response = ToscaServiceTemplate.class,
102 @ResponseHeader(name = VERSION_MINOR_NAME,
103 description = VERSION_MINOR_DESCRIPTION,
104 response = String.class),
105 @ResponseHeader(name = VERSION_PATCH_NAME,
106 description = VERSION_PATCH_DESCRIPTION,
107 response = String.class),
108 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
109 response = String.class),
110 @ResponseHeader(name = REQUEST_ID_NAME,
111 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
113 @Extension(name = EXTENSION_NAME, properties = {
114 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
115 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
116 @ApiResponses(value = {
117 @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
118 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
119 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
120 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
121 @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
122 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
123 public ResponseEntity<ToscaServiceTemplate> createToscaNodeTemplates(
124 @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
125 @RequestBody @ApiParam(value = "Entity body of tosca node templates", required = true)
126 ToscaServiceTemplate body) {
128 if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
129 NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
133 ToscaServiceTemplate nodeTemplates = provider.createNodeTemplates(body);
134 return makeOkResponse(requestId, nodeTemplates);
135 } catch (PfModelException | PfModelRuntimeException pfme) {
136 final var msg = "POST /nodetemplates";
137 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
143 * Updates one or more node templates in one call.
145 * @param body the body of the node templates in TOSCA definition
147 * @return the Response object containing the results of the API operation
149 @PutMapping("/nodetemplates")
150 @ApiOperation(value = "Updates one or more new node templates",
151 notes = "Client should provide TOSCA body of the updated node templates",
152 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
153 response = ToscaServiceTemplate.class,
155 @ResponseHeader(name = VERSION_MINOR_NAME,
156 description = VERSION_MINOR_DESCRIPTION,
157 response = String.class),
158 @ResponseHeader(name = VERSION_PATCH_NAME,
159 description = VERSION_PATCH_DESCRIPTION,
160 response = String.class),
161 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
162 response = String.class),
163 @ResponseHeader(name = REQUEST_ID_NAME,
164 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
166 @Extension(name = EXTENSION_NAME, properties = {
167 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
168 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
169 @ApiResponses(value = {
170 @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
171 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
172 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
173 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
174 @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
175 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
176 public ResponseEntity<ToscaServiceTemplate> updateToscaNodeTemplates(
177 @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
178 @RequestBody @ApiParam(value = "Entity body of tosca node templates", required = true)
179 ToscaServiceTemplate body) {
181 if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
182 NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
186 ToscaServiceTemplate nodeTemplates = provider.updateToscaNodeTemplates(body);
187 return makeOkResponse(requestId, nodeTemplates);
188 } catch (PfModelException | PfModelRuntimeException pfme) {
189 final var msg = "PUT /nodetemplates";
190 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
196 * Deletes a node template with specific name and version.
198 * @param name the name of node template
199 * @param version the version of node template
200 * @return the Response object containing the results of the API operation
202 @DeleteMapping("/nodetemplates/{name}/versions/{version}")
203 @ApiOperation(value = "Updates one or more new node templates",
204 notes = "Client should provide TOSCA body of the updated node templates",
205 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
206 response = ToscaServiceTemplate.class,
208 @ResponseHeader(name = VERSION_MINOR_NAME,
209 description = VERSION_MINOR_DESCRIPTION,
210 response = String.class),
211 @ResponseHeader(name = VERSION_PATCH_NAME,
212 description = VERSION_PATCH_DESCRIPTION,
213 response = String.class),
214 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
215 response = String.class),
216 @ResponseHeader(name = REQUEST_ID_NAME,
217 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
219 @Extension(name = EXTENSION_NAME, properties = {
220 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
221 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
222 @ApiResponses(value = {
223 @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
224 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
225 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
226 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
227 @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
228 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
229 public ResponseEntity<ToscaServiceTemplate> deleteToscaNodeTemplates(
230 @PathVariable("name") @ApiParam(value = "Name of the node template", required = true) String name,
231 @PathVariable("version") @ApiParam(value = "Version of the node template",
232 required = true) String version,
233 @RequestHeader(name = REQUEST_ID_NAME, required = false)
234 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
236 ToscaServiceTemplate nodeTemplates = provider.deleteToscaNodeTemplate(name, version);
237 return makeOkResponse(requestId, nodeTemplates);
238 } catch (PfModelException | PfModelRuntimeException pfme) {
239 final var msg = String.format("DELETE /nodetemplates/%s/versions/%s", name, version);
240 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
246 * Retrieves the specified version of a node template.
248 * @param name the name of the node template
249 * @param version the version of the node template
251 * @return the Response object containing the results of the API operation
253 @GetMapping("/nodetemplates/{name}/versions/{version}")
254 @ApiOperation(value = "Retrieve one version of a tosca node template",
255 notes = "Returns a particular version of a node template",
256 response = ToscaNodeTemplate.class,
257 responseContainer = "List",
259 @ResponseHeader(name = VERSION_MINOR_NAME,
260 description = VERSION_MINOR_DESCRIPTION,
261 response = String.class),
262 @ResponseHeader(name = VERSION_PATCH_NAME,
263 description = VERSION_PATCH_DESCRIPTION,
264 response = String.class),
265 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
266 response = String.class),
267 @ResponseHeader(name = REQUEST_ID_NAME,
268 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
269 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplates", },
271 @Extension(name = EXTENSION_NAME, properties = {
272 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
273 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")
277 @ApiResponses(value = {
278 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
279 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
280 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
281 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)
283 public ResponseEntity<List<ToscaNodeTemplate>> getSpecificVersionOfNodeTemplate(
284 @PathVariable("name") @ApiParam(value = "Name of the node template", required = true) String name,
285 @PathVariable("version") @ApiParam(value = "Version of the node template",
286 required = true) String version,
287 @RequestHeader(name = REQUEST_ID_NAME, required = false)
288 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
290 List<ToscaNodeTemplate> nodeTemplates = provider.fetchToscaNodeTemplates(name, version);
291 return makeOkResponse(requestId, nodeTemplates);
292 } catch (PfModelException | PfModelRuntimeException pfme) {
293 var msg = String.format("GET /nodetemplates/%s/versions/%s", name, version);
294 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
300 * Retrieves all the node templates from the tosca service template.
302 * @return the Response object containing the results of the API operation
304 @GetMapping("/nodetemplates")
305 @ApiOperation(value = "Retrieve all the available tosca node templates",
306 notes = "Returns all the node templates from the service template",
307 response = ToscaNodeTemplate.class,
308 responseContainer = "List",
310 @ResponseHeader(name = VERSION_MINOR_NAME,
311 description = VERSION_MINOR_DESCRIPTION,
312 response = String.class),
313 @ResponseHeader(name = VERSION_PATCH_NAME,
314 description = VERSION_PATCH_DESCRIPTION,
315 response = String.class),
316 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
317 response = String.class),
318 @ResponseHeader(name = REQUEST_ID_NAME,
319 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
320 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplates", },
322 @Extension(name = EXTENSION_NAME, properties = {
323 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
324 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")
328 @ApiResponses(value = {
329 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
330 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
331 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
332 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)
334 public ResponseEntity<List<ToscaNodeTemplate>> getAllNodeTemplates(
335 @RequestHeader(name = REQUEST_ID_NAME, required = false)
336 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
338 List<ToscaNodeTemplate> nodeTemplates = provider.fetchToscaNodeTemplates(null, null);
339 return makeOkResponse(requestId, nodeTemplates);
340 } catch (PfModelException | PfModelRuntimeException pfme) {
341 var msg = "GET /nodetemplates";
342 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);