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.springframework.http.ResponseEntity;
51 import org.springframework.web.bind.annotation.DeleteMapping;
52 import org.springframework.web.bind.annotation.GetMapping;
53 import org.springframework.web.bind.annotation.PathVariable;
54 import org.springframework.web.bind.annotation.PostMapping;
55 import org.springframework.web.bind.annotation.PutMapping;
56 import org.springframework.web.bind.annotation.RequestBody;
57 import org.springframework.web.bind.annotation.RequestHeader;
58 import org.springframework.web.bind.annotation.RequestMapping;
59 import org.springframework.web.bind.annotation.RestController;
62 * Class to provide REST API services for Tosca Node templates.
65 @RequestMapping(path = "/policy/api/v1", produces = { "application/json", "application/yaml" })
66 @Api(value = "Tosca Node template Design API")
69 description = "Tosca Node template Design API is publicly exposed for clients to Create/Read/Update/Delete"
70 + " node templates which can be recognized"
71 + " and executable by incorporated policy engines. It is an"
72 + " independent component running rest service that takes all node templates design API calls"
73 + " from clients and then assign them to different API working functions.",
74 version = "1.0.0", title = "Tosca Node template Design",
75 extensions = {@Extension(properties = {@ExtensionProperty(name = "planned-retirement-date", value = "tbd"),
76 @ExtensionProperty(name = "component", value = "Policy Framework")})}),
77 schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},
78 securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")}))
79 @RequiredArgsConstructor
80 public class NodeTemplateController extends CommonRestController {
82 private final ToscaServiceTemplateService toscaServiceTemplateService;
85 * Creates one or more new tosca node templates in one call.
87 * @param body the body of the node templates in TOSCA definition
89 * @return the Response object containing the results of the API operation
91 @PostMapping("/nodetemplates")
92 @ApiOperation(value = "Create one or more new node templates",
93 notes = "Client should provide TOSCA body of the new node templates",
94 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
95 response = ToscaServiceTemplate.class,
97 @ResponseHeader(name = VERSION_MINOR_NAME,
98 description = VERSION_MINOR_DESCRIPTION,
99 response = String.class),
100 @ResponseHeader(name = VERSION_PATCH_NAME,
101 description = VERSION_PATCH_DESCRIPTION,
102 response = String.class),
103 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
104 response = String.class),
105 @ResponseHeader(name = REQUEST_ID_NAME,
106 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
108 @Extension(name = EXTENSION_NAME, properties = {
109 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
110 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
111 @ApiResponses(value = {
112 @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
113 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
114 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
115 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
116 @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
117 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
118 public ResponseEntity<ToscaServiceTemplate> createToscaNodeTemplates(
119 @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
120 @RequestBody @ApiParam(value = "Entity body of tosca node templates", required = true)
121 ToscaServiceTemplate body) {
123 if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
124 NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
128 ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.createToscaNodeTemplates(body);
129 return makeOkResponse(requestId, nodeTemplates);
130 } catch (PfModelException | PfModelRuntimeException pfme) {
131 final var msg = "POST /nodetemplates";
132 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
138 * Updates one or more node templates in one call.
140 * @param body the body of the node templates in TOSCA definition
142 * @return the Response object containing the results of the API operation
144 @PutMapping("/nodetemplates")
145 @ApiOperation(value = "Updates one or more new node templates",
146 notes = "Client should provide TOSCA body of the updated node templates",
147 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
148 response = ToscaServiceTemplate.class,
150 @ResponseHeader(name = VERSION_MINOR_NAME,
151 description = VERSION_MINOR_DESCRIPTION,
152 response = String.class),
153 @ResponseHeader(name = VERSION_PATCH_NAME,
154 description = VERSION_PATCH_DESCRIPTION,
155 response = String.class),
156 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
157 response = String.class),
158 @ResponseHeader(name = REQUEST_ID_NAME,
159 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
161 @Extension(name = EXTENSION_NAME, properties = {
162 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
163 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
164 @ApiResponses(value = {
165 @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
166 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
167 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
168 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
169 @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
170 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
171 public ResponseEntity<ToscaServiceTemplate> updateToscaNodeTemplates(
172 @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
173 @RequestBody @ApiParam(value = "Entity body of tosca node templates", required = true)
174 ToscaServiceTemplate body) {
176 if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
177 NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
181 ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.updateToscaNodeTemplates(body);
182 return makeOkResponse(requestId, nodeTemplates);
183 } catch (PfModelException | PfModelRuntimeException pfme) {
184 final var msg = "PUT /nodetemplates";
185 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
191 * Deletes a node template with specific name and version.
193 * @param name the name of node template
194 * @param version the version of node template
195 * @return the Response object containing the results of the API operation
197 @DeleteMapping("/nodetemplates/{name}/versions/{version}")
198 @ApiOperation(value = "Updates one or more new node templates",
199 notes = "Client should provide TOSCA body of the updated node templates",
200 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
201 response = ToscaServiceTemplate.class,
203 @ResponseHeader(name = VERSION_MINOR_NAME,
204 description = VERSION_MINOR_DESCRIPTION,
205 response = String.class),
206 @ResponseHeader(name = VERSION_PATCH_NAME,
207 description = VERSION_PATCH_DESCRIPTION,
208 response = String.class),
209 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
210 response = String.class),
211 @ResponseHeader(name = REQUEST_ID_NAME,
212 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
214 @Extension(name = EXTENSION_NAME, properties = {
215 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
216 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
217 @ApiResponses(value = {
218 @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
219 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
220 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
221 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
222 @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
223 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
224 public ResponseEntity<ToscaServiceTemplate> deleteToscaNodeTemplates(
225 @PathVariable("name") @ApiParam(value = "Name of the node template", required = true) String name,
226 @PathVariable("version") @ApiParam(value = "Version of the node template",
227 required = true) String version,
228 @RequestHeader(name = REQUEST_ID_NAME, required = false)
229 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
231 ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.deleteToscaNodeTemplate(name, version);
232 return makeOkResponse(requestId, nodeTemplates);
233 } catch (PfModelException | PfModelRuntimeException pfme) {
234 final var msg = String.format("DELETE /nodetemplates/%s/versions/%s", name, version);
235 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
241 * Retrieves the specified version of a node template.
243 * @param name the name of the node template
244 * @param version the version of the node template
246 * @return the Response object containing the results of the API operation
248 @GetMapping("/nodetemplates/{name}/versions/{version}")
249 @ApiOperation(value = "Retrieve one version of a tosca node template",
250 notes = "Returns a particular version of a node template",
251 response = ToscaNodeTemplate.class,
252 responseContainer = "List",
254 @ResponseHeader(name = VERSION_MINOR_NAME,
255 description = VERSION_MINOR_DESCRIPTION,
256 response = String.class),
257 @ResponseHeader(name = VERSION_PATCH_NAME,
258 description = VERSION_PATCH_DESCRIPTION,
259 response = String.class),
260 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
261 response = String.class),
262 @ResponseHeader(name = REQUEST_ID_NAME,
263 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
264 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplates", },
266 @Extension(name = EXTENSION_NAME, properties = {
267 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
268 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")
272 @ApiResponses(value = {
273 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
274 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
275 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
276 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)
278 public ResponseEntity<List<ToscaNodeTemplate>> getSpecificVersionOfNodeTemplate(
279 @PathVariable("name") @ApiParam(value = "Name of the node template", required = true) String name,
280 @PathVariable("version") @ApiParam(value = "Version of the node template",
281 required = true) String version,
282 @RequestHeader(name = REQUEST_ID_NAME, required = false)
283 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
285 List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(name, version);
286 return makeOkResponse(requestId, nodeTemplates);
287 } catch (PfModelException | PfModelRuntimeException pfme) {
288 var msg = String.format("GET /nodetemplates/%s/versions/%s", name, version);
289 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
295 * Retrieves all the node templates from the tosca service template.
297 * @return the Response object containing the results of the API operation
299 @GetMapping("/nodetemplates")
300 @ApiOperation(value = "Retrieve all the available tosca node templates",
301 notes = "Returns all the node templates from the service template",
302 response = ToscaNodeTemplate.class,
303 responseContainer = "List",
305 @ResponseHeader(name = VERSION_MINOR_NAME,
306 description = VERSION_MINOR_DESCRIPTION,
307 response = String.class),
308 @ResponseHeader(name = VERSION_PATCH_NAME,
309 description = VERSION_PATCH_DESCRIPTION,
310 response = String.class),
311 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
312 response = String.class),
313 @ResponseHeader(name = REQUEST_ID_NAME,
314 description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
315 authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplates", },
317 @Extension(name = EXTENSION_NAME, properties = {
318 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
319 @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")
323 @ApiResponses(value = {
324 @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
325 @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
326 @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
327 @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)
329 public ResponseEntity<List<ToscaNodeTemplate>> getAllNodeTemplates(
330 @RequestHeader(name = REQUEST_ID_NAME, required = false)
331 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
333 List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(null, null);
334 return makeOkResponse(requestId, nodeTemplates);
335 } catch (PfModelException | PfModelRuntimeException pfme) {
336 var msg = "GET /nodetemplates";
337 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);