2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.servlets;
22 import com.jcabi.aspects.Loggable;
23 import fj.data.Either;
24 import io.swagger.v3.oas.annotations.Operation;
25 import io.swagger.v3.oas.annotations.Parameter;
26 import io.swagger.v3.oas.annotations.media.ArraySchema;
27 import io.swagger.v3.oas.annotations.media.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.servers.Server;
31 import io.swagger.v3.oas.annotations.servers.Servers;
32 import io.swagger.v3.oas.annotations.tags.Tag;
33 import io.swagger.v3.oas.annotations.tags.Tags;
34 import javax.inject.Inject;
35 import javax.servlet.ServletContext;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.ws.rs.Consumes;
38 import javax.ws.rs.DELETE;
39 import javax.ws.rs.GET;
40 import javax.ws.rs.HeaderParam;
41 import javax.ws.rs.POST;
42 import javax.ws.rs.PUT;
43 import javax.ws.rs.Path;
44 import javax.ws.rs.PathParam;
45 import javax.ws.rs.Produces;
46 import javax.ws.rs.core.Context;
47 import javax.ws.rs.core.MediaType;
48 import javax.ws.rs.core.Response;
49 import org.openecomp.sdc.be.components.impl.AdditionalInformationBusinessLogic;
50 import org.openecomp.sdc.be.config.BeEcompErrorManager;
51 import org.openecomp.sdc.be.dao.api.ActionStatus;
52 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterInfo;
53 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
54 import org.openecomp.sdc.be.impl.ComponentsUtils;
55 import org.openecomp.sdc.be.model.AdditionalInformationDefinition;
56 import org.openecomp.sdc.common.api.Constants;
57 import org.openecomp.sdc.common.log.wrappers.Logger;
58 import org.openecomp.sdc.exception.ResponseFormat;
59 import org.springframework.stereotype.Controller;
61 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
63 @Tags({@Tag(name = "SDCE-2 APIs")})
64 @Servers({@Server(url = "/sdc2/rest")})
66 public class AdditionalInformationServlet extends BeGenericServlet {
68 private static final Logger log = Logger.getLogger(AdditionalInformationServlet.class);
69 private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}";
70 private static final String MODIFIER_ID_IS = "modifier id is {}";
71 private static final String FAILED_TO_UPDATE_ADDITIONAL_INFO_PROPERTY = "Failed to update additional information property. Reason - {}";
72 private final AdditionalInformationBusinessLogic businessLogic;
75 public AdditionalInformationServlet(ComponentsUtils componentsUtils,
76 AdditionalInformationBusinessLogic businessLogic) {
77 super(componentsUtils);
78 this.businessLogic = businessLogic;
89 @Path("/resources/{resourceId}/additionalinfo")
90 @Consumes(MediaType.APPLICATION_JSON)
91 @Produces(MediaType.APPLICATION_JSON)
92 @Operation(description = "Create Additional Information Label and Value", method = "POST", summary = "Returns created Additional Inforamtion property", responses = {
93 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
94 @ApiResponse(responseCode = "201", description = "Additional information created"),
95 @ApiResponse(responseCode = "403", description = "Restricted operation"),
96 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
97 @ApiResponse(responseCode = "409", description = "Additional information key already exist")})
98 public Response createResourceAdditionalInformationLabel(
99 @Parameter(description = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
100 @Parameter(description = "Additional information key value to be created", required = true) String data,
101 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userUserId) {
102 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userUserId, data);
113 @Path("/services/{serviceId}/additionalinfo")
114 @Consumes(MediaType.APPLICATION_JSON)
115 @Produces(MediaType.APPLICATION_JSON)
116 @Operation(description = "Create Additional Information Label and Value", method = "POST", summary = "Returns created Additional Inforamtion property", responses = {
117 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
118 @ApiResponse(responseCode = "201", description = "Additional information created"),
119 @ApiResponse(responseCode = "403", description = "Restricted operation"),
120 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
121 @ApiResponse(responseCode = "409", description = "Additional information key already exist")})
122 public Response createServiceAdditionalInformationLabel(
123 @Parameter(description = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
124 @Parameter(description = "Additional information key value to be created", required = true) String data,
125 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userUserId) {
126 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userUserId, data);
138 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
139 @Consumes(MediaType.APPLICATION_JSON)
140 @Produces(MediaType.APPLICATION_JSON)
141 @Operation(description = "Update Additional Information Label and Value", method = "PUT", summary = "Returns updated Additional Inforamtion property", responses = {
142 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
143 @ApiResponse(responseCode = "200", description = "Additional information updated"),
144 @ApiResponse(responseCode = "403", description = "Restricted operation"),
145 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
146 @ApiResponse(responseCode = "409", description = "Additional information key already exist")})
147 public Response updateResourceAdditionalInformationLabel(
148 @Parameter(description = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
149 @Parameter(description = "label id", required = true) @PathParam("labelId") final String labelId,
150 @Parameter(description = "Additional information key value to be created", required = true) String data,
151 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
152 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId, data);
164 @Path("/services/{serviceId}/additionalinfo/{labelId}")
165 @Consumes(MediaType.APPLICATION_JSON)
166 @Produces(MediaType.APPLICATION_JSON)
167 @Operation(description = "Update Additional Information Label and Value", method = "PUT", summary = "Returns updated Additional Inforamtion property", responses = {
168 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
169 @ApiResponse(responseCode = "200", description = "Additional information updated"),
170 @ApiResponse(responseCode = "403", description = "Restricted operation"),
171 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
172 @ApiResponse(responseCode = "409", description = "Additional information key already exist")})
173 public Response updateServiceAdditionalInformationLabel(
174 @Parameter(description = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
175 @Parameter(description = "label id", required = true) @PathParam("labelId") final String labelId,
176 @Parameter(description = "Additional information key value to be created", required = true) String data,
177 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
178 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId, data);
189 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
190 @Consumes(MediaType.APPLICATION_JSON)
191 @Produces(MediaType.APPLICATION_JSON)
192 @Operation(description = "Create Additional Information Label and Value", method = "DELETE", summary = "Returns deleted Additional Inforamtion property", responses = {
193 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
194 @ApiResponse(responseCode = "200", description = "Additional information deleted"),
195 @ApiResponse(responseCode = "403", description = "Restricted operation"),
196 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
197 @ApiResponse(responseCode = "409", description = "Additional information key already exist")})
198 public Response updateResourceAdditionalInformationLabel(
199 @Parameter(description = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
200 @Parameter(description = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request,
201 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
202 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
213 @Path("/services/{serviceId}/additionalinfo/{labelId}")
214 @Consumes(MediaType.APPLICATION_JSON)
215 @Produces(MediaType.APPLICATION_JSON)
216 @Operation(description = "Create Additional Information Label and Value", method = "DELETE", summary = "Returns deleted Additional Inforamtion property", responses = {
217 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
218 @ApiResponse(responseCode = "200", description = "Additional information deleted"),
219 @ApiResponse(responseCode = "403", description = "Restricted operation"),
220 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
221 @ApiResponse(responseCode = "409", description = "Additional information key already exist")})
222 public Response deleteServiceAdditionalInformationLabel(
223 @Parameter(description = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
224 @Parameter(description = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request,
225 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
226 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
237 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
238 @Consumes(MediaType.APPLICATION_JSON)
239 @Produces(MediaType.APPLICATION_JSON)
240 @Operation(description = "Get Additional Information by id", method = "GET", summary = "Returns Additional Inforamtion property", responses = {
241 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
242 @ApiResponse(responseCode = "200", description = "fetched additional information"),
243 @ApiResponse(responseCode = "403", description = "Restricted operation"),
244 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
245 @ApiResponse(responseCode = "409", description = "Additional information key already exist")})
246 public Response getResourceAdditionalInformationLabel(
247 @Parameter(description = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
248 @Parameter(description = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request,
249 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
250 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
261 @Path("/services/{serviceId}/additionalinfo/{labelId}")
262 @Consumes(MediaType.APPLICATION_JSON)
263 @Produces(MediaType.APPLICATION_JSON)
264 @Operation(description = "Get Additional Information by id", method = "GET", summary = "Returns Additional Inforamtion property", responses = {
265 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
266 @ApiResponse(responseCode = "200", description = "fetched additional information"),
267 @ApiResponse(responseCode = "403", description = "Restricted operation"),
268 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
269 @ApiResponse(responseCode = "409", description = "Additional information key already exist")})
270 public Response getServiceAdditionalInformationLabel(
271 @Parameter(description = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
272 @Parameter(description = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request,
273 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
274 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
284 @Path("/resources/{resourceId}/additionalinfo")
285 @Consumes(MediaType.APPLICATION_JSON)
286 @Produces(MediaType.APPLICATION_JSON)
287 @Operation(description = "Get all Additional Information under resource", method = "GET", summary = "Returns Additional Inforamtion property", responses = {
288 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
289 @ApiResponse(responseCode = "200", description = "list of additional information"),
290 @ApiResponse(responseCode = "403", description = "Restricted operation"),
291 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
292 @ApiResponse(responseCode = "409", description = "Additional information key already exist")})
293 public Response getAllResourceAdditionalInformationLabel(
294 @Parameter(description = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
295 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
296 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userId);
306 @Path("/services/{serviceId}/additionalinfo")
307 @Consumes(MediaType.APPLICATION_JSON)
308 @Produces(MediaType.APPLICATION_JSON)
309 @Operation(description = "Get all Additional Information under service", method = "GET", summary = "Returns Additional Inforamtion property", responses = {
310 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
311 @ApiResponse(responseCode = "200", description = "list of additional information"),
312 @ApiResponse(responseCode = "403", description = "Restricted operation"),
313 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
314 @ApiResponse(responseCode = "409", description = "Additional information key already exist")})
315 public Response getAllServiceAdditionalInformationLabel(
316 @Parameter(description = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
317 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
318 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userId);
322 * Create additional information property under given resource/service
331 protected Response createAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, HttpServletRequest request, String userId,
333 ServletContext context = request.getSession().getServletContext();
334 String url = request.getMethod() + " " + request.getRequestURI();
335 log.debug(START_HANDLE_REQUEST_OF, url);
336 log.debug(MODIFIER_ID_IS, userId);
337 log.debug("data is {}", data);
339 // convert json to AdditionalInfoParameterInfo
340 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
341 // create the new property
342 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic
343 .createAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, userId);
344 if (either.isRight()) {
345 ResponseFormat responseFormat = either.right().value();
346 log.info("Failed to create additional information {}. Reason - {}", additionalInfoParameterInfo, responseFormat);
347 return buildErrorResponse(responseFormat);
349 AdditionalInfoParameterInfo createdAI = either.left().value();
350 log.debug("Additional information {}={} created successfully with id {}", createdAI.getKey(), createdAI.getValue(),
351 createdAI.getUniqueId());
352 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
353 return buildOkResponse(responseFormat, createdAI);
354 } catch (Exception e) {
355 log.debug("Create additional information failed with exception", e);
356 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
357 return buildErrorResponse(responseFormat);
362 * Update additional information property by id under given resource/service
372 protected Response updateAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId,
373 HttpServletRequest request, String userId, String data) {
374 ServletContext context = request.getSession().getServletContext();
375 String url = request.getMethod() + " " + request.getRequestURI();
376 log.debug(START_HANDLE_REQUEST_OF, url);
377 log.debug(MODIFIER_ID_IS, userId);
378 log.debug("data is {}", data);
380 // convert json to AdditionalInfoParameterInfo
381 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
382 // create the new property
383 additionalInfoParameterInfo.setUniqueId(labelId);
384 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic
385 .updateAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, userId);
386 if (either.isRight()) {
387 ResponseFormat responseFormat = either.right().value();
388 log.info(FAILED_TO_UPDATE_ADDITIONAL_INFO_PROPERTY, responseFormat);
389 return buildErrorResponse(responseFormat);
391 AdditionalInfoParameterInfo createdAI = either.left().value();
392 log.debug("Additional information {}={} updated successfully with id {}", createdAI.getKey(), createdAI.getValue(),
393 createdAI.getUniqueId());
394 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
395 return buildOkResponse(responseFormat, createdAI);
396 } catch (Exception e) {
397 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Additional Information");
398 log.debug("Update additional information failed with exception", e);
399 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
400 return buildErrorResponse(responseFormat);
405 * Delete an additional information property by id under given resource/service
414 protected Response deleteAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId,
415 HttpServletRequest request, String userId) {
416 ServletContext context = request.getSession().getServletContext();
417 String url = request.getMethod() + " " + request.getRequestURI();
418 log.debug(START_HANDLE_REQUEST_OF, url);
419 log.debug(MODIFIER_ID_IS, userId);
421 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
422 additionalInfoParameterInfo.setUniqueId(labelId);
423 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic
424 .deleteAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, userId);
425 if (either.isRight()) {
426 ResponseFormat responseFormat = either.right().value();
427 log.info(FAILED_TO_UPDATE_ADDITIONAL_INFO_PROPERTY, responseFormat);
428 return buildErrorResponse(responseFormat);
430 AdditionalInfoParameterInfo createdAI = either.left().value();
431 log.debug("Additional information {}={} deleted successfully with id {}", createdAI.getKey(), createdAI.getValue(),
432 createdAI.getUniqueId());
433 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
434 return buildOkResponse(responseFormat, createdAI);
435 } catch (Exception e) {
436 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Additional Information");
437 log.debug("Delete additional information failed with exception", e);
438 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
439 return buildErrorResponse(responseFormat);
444 * Get a specific additional information property by a given id under given resource/service
453 protected Response getAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request,
455 ServletContext context = request.getSession().getServletContext();
456 String url = request.getMethod() + " " + request.getRequestURI();
457 log.debug(START_HANDLE_REQUEST_OF, url);
458 log.debug(MODIFIER_ID_IS, userId);
460 // create the new property
461 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
462 additionalInfoParameterInfo.setUniqueId(labelId);
463 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic
464 .getAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, userId);
465 if (either.isRight()) {
466 ResponseFormat responseFormat = either.right().value();
467 log.info(FAILED_TO_UPDATE_ADDITIONAL_INFO_PROPERTY, responseFormat);
468 return buildErrorResponse(responseFormat);
470 AdditionalInfoParameterInfo createdAI = either.left().value();
471 log.debug("Additional information {}={} fetched successfully with id {}", createdAI.getKey(), createdAI.getValue(),
472 createdAI.getUniqueId());
473 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
474 return buildOkResponse(responseFormat, createdAI);
475 } catch (Exception e) {
476 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Additional Information");
477 log.debug("get additional information failed with exception", e);
478 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
479 return buildErrorResponse(responseFormat);
484 * Get all additional information properties under given resource/service
492 protected Response getAllAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, HttpServletRequest request,
494 ServletContext context = request.getSession().getServletContext();
495 String url = request.getMethod() + " " + request.getRequestURI();
496 log.debug(START_HANDLE_REQUEST_OF, url);
497 log.debug(MODIFIER_ID_IS, userId);
499 Either<AdditionalInformationDefinition, ResponseFormat> either = businessLogic.getAllAdditionalInformation(nodeType, uniqueId, userId);
500 if (either.isRight()) {
501 ResponseFormat responseFormat = either.right().value();
502 log.info(FAILED_TO_UPDATE_ADDITIONAL_INFO_PROPERTY, responseFormat);
503 return buildErrorResponse(responseFormat);
505 AdditionalInformationDefinition additionalInformationDefinition = either.left().value();
506 log.debug("All Additional information retrieved for component {} is {}", uniqueId, additionalInformationDefinition);
507 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
508 return buildOkResponse(responseFormat, additionalInformationDefinition);
509 } catch (Exception e) {
510 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get All Additional Information");
511 log.debug("Get all addiotanl information properties failed with exception", e);
512 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
513 return buildErrorResponse(responseFormat);