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=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import javax.inject.Singleton;
24 import javax.servlet.ServletContext;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.HeaderParam;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.PUT;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
39 import org.openecomp.sdc.be.components.impl.AdditionalInformationBusinessLogic;
40 import org.openecomp.sdc.be.config.BeEcompErrorManager;
41 import org.openecomp.sdc.be.dao.api.ActionStatus;
42 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterInfo;
43 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
44 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
45 import org.openecomp.sdc.be.model.AdditionalInformationDefinition;
46 import org.openecomp.sdc.common.api.Constants;
47 import org.openecomp.sdc.exception.ResponseFormat;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.web.context.WebApplicationContext;
52 import com.jcabi.aspects.Loggable;
54 import fj.data.Either;
55 import io.swagger.annotations.Api;
56 import io.swagger.annotations.ApiOperation;
57 import io.swagger.annotations.ApiParam;
58 import io.swagger.annotations.ApiResponse;
59 import io.swagger.annotations.ApiResponses;
60 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
62 @Api(value = "Additional Information Servlet", description = "Additional Information Servlet")
64 public class AdditionalInformationServlet extends BeGenericServlet {
66 private static final Logger log = LoggerFactory.getLogger(AdditionalInformationServlet.class);
77 @Path("/resources/{resourceId}/additionalinfo")
78 @Consumes(MediaType.APPLICATION_JSON)
79 @Produces(MediaType.APPLICATION_JSON)
80 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "POST", notes = "Returns created Additional Inforamtion property", response = Response.class)
81 @ApiResponses(value = { @ApiResponse(code = 201, message = "Additional information created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
82 @ApiResponse(code = 409, message = "Additional information key already exist") })
83 public Response createResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
84 @ApiParam(value = "Additional information key value to be created", required = true) String data, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userUserId) {
86 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userUserId, data);
99 @Path("/services/{serviceId}/additionalinfo")
100 @Consumes(MediaType.APPLICATION_JSON)
101 @Produces(MediaType.APPLICATION_JSON)
102 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "POST", notes = "Returns created Additional Inforamtion property", response = Response.class)
103 @ApiResponses(value = { @ApiResponse(code = 201, message = "Additional information created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
104 @ApiResponse(code = 409, message = "Additional information key already exist") })
105 public Response createServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
106 @ApiParam(value = "Additional information key value to be created", required = true) String data, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userUserId) {
108 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userUserId, data);
122 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
123 @Consumes(MediaType.APPLICATION_JSON)
124 @Produces(MediaType.APPLICATION_JSON)
125 @ApiOperation(value = "Update Additional Information Label and Value", httpMethod = "PUT", notes = "Returns updated Additional Inforamtion property", response = Response.class)
126 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
127 @ApiResponse(code = 409, message = "Additional information key already exist") })
128 public Response updateResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
129 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @ApiParam(value = "Additional information key value to be created", required = true) String data, @Context final HttpServletRequest request,
130 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
132 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId, data);
146 @Path("/services/{serviceId}/additionalinfo/{labelId}")
147 @Consumes(MediaType.APPLICATION_JSON)
148 @Produces(MediaType.APPLICATION_JSON)
149 @ApiOperation(value = "Update Additional Information Label and Value", httpMethod = "PUT", notes = "Returns updated Additional Inforamtion property", response = Response.class)
150 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
151 @ApiResponse(code = 409, message = "Additional information key already exist") })
152 public Response updateServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
153 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @ApiParam(value = "Additional information key value to be created", required = true) String data, @Context final HttpServletRequest request,
154 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
156 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId, data);
169 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
170 @Consumes(MediaType.APPLICATION_JSON)
171 @Produces(MediaType.APPLICATION_JSON)
172 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "DELETE", notes = "Returns deleted Additional Inforamtion property", response = Response.class)
173 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
174 @ApiResponse(code = 409, message = "Additional information key already exist") })
175 public Response updateResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
176 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
178 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
191 @Path("/services/{serviceId}/additionalinfo/{labelId}")
192 @Consumes(MediaType.APPLICATION_JSON)
193 @Produces(MediaType.APPLICATION_JSON)
194 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "DELETE", notes = "Returns deleted Additional Inforamtion property", response = Response.class)
195 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
196 @ApiResponse(code = 409, message = "Additional information key already exist") })
197 public Response deleteServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
198 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
200 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
213 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
214 @Consumes(MediaType.APPLICATION_JSON)
215 @Produces(MediaType.APPLICATION_JSON)
216 @ApiOperation(value = "Get Additional Information by id", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
217 @ApiResponses(value = { @ApiResponse(code = 200, message = "fetched additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
218 @ApiResponse(code = 409, message = "Additional information key already exist") })
219 public Response getResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
220 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
222 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
235 @Path("/services/{serviceId}/additionalinfo/{labelId}")
236 @Consumes(MediaType.APPLICATION_JSON)
237 @Produces(MediaType.APPLICATION_JSON)
238 @ApiOperation(value = "Get Additional Information by id", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
239 @ApiResponses(value = { @ApiResponse(code = 200, message = "fetched additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
240 @ApiResponse(code = 409, message = "Additional information key already exist") })
241 public Response getServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
242 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
244 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
256 @Path("/resources/{resourceId}/additionalinfo")
257 @Consumes(MediaType.APPLICATION_JSON)
258 @Produces(MediaType.APPLICATION_JSON)
259 @ApiOperation(value = "Get all Additional Information under resource", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
260 @ApiResponses(value = { @ApiResponse(code = 200, message = "list of additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
261 @ApiResponse(code = 409, message = "Additional information key already exist") })
262 public Response getAllResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId, @Context final HttpServletRequest request,
263 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
265 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userId);
277 @Path("/services/{serviceId}/additionalinfo")
278 @Consumes(MediaType.APPLICATION_JSON)
279 @Produces(MediaType.APPLICATION_JSON)
280 @ApiOperation(value = "Get all Additional Information under service", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
281 @ApiResponses(value = { @ApiResponse(code = 200, message = "list of additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
282 @ApiResponse(code = 409, message = "Additional information key already exist") })
283 public Response getAllServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId, @Context final HttpServletRequest request,
284 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
286 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userId);
292 * Create additional information property under given resource/service
301 protected Response createAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, HttpServletRequest request, String userId, String data) {
303 ServletContext context = request.getSession().getServletContext();
305 String url = request.getMethod() + " " + request.getRequestURI();
306 log.debug("Start handle request of {}", url);
307 log.debug("modifier id is {}", userId);
308 log.debug("data is {}", data);
311 // convert json to AdditionalInfoParameterInfo
312 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
314 // create the new property
315 AdditionalInformationBusinessLogic businessLogic = getBL(context);
317 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.createAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
319 if (either.isRight()) {
320 ResponseFormat responseFormat = either.right().value();
321 log.info("Failed to create additional information {}. Reason - {}", additionalInfoParameterInfo, responseFormat);
322 return buildErrorResponse(responseFormat);
325 AdditionalInfoParameterInfo createdAI = either.left().value();
327 log.debug("Additional information {}={} created successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
329 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
330 return buildOkResponse(responseFormat, createdAI);
332 } catch (Exception e) {
333 log.debug("Create additional information failed with exception", e);
334 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
335 return buildErrorResponse(responseFormat);
341 * Update additional information property by id under given resource/service
351 protected Response updateAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId, String data) {
353 ServletContext context = request.getSession().getServletContext();
355 String url = request.getMethod() + " " + request.getRequestURI();
356 log.debug("Start handle request of {}", url);
357 log.debug("modifier id is {}", userId);
358 log.debug("data is {}", data);
361 // convert json to AdditionalInfoParameterInfo
362 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
364 // create the new property
365 AdditionalInformationBusinessLogic businessLogic = getBL(context);
367 additionalInfoParameterInfo.setUniqueId(labelId);
369 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.updateAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
371 if (either.isRight()) {
372 ResponseFormat responseFormat = either.right().value();
373 log.info("Failed to update additional information property. Reason - {}", responseFormat);
374 return buildErrorResponse(responseFormat);
377 AdditionalInfoParameterInfo createdAI = either.left().value();
379 log.debug("Additional information {}={} updated successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
381 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
382 return buildOkResponse(responseFormat, createdAI);
384 } catch (Exception e) {
385 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Additional Information");
386 log.debug("Update additional information failed with exception", e);
387 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
388 return buildErrorResponse(responseFormat);
395 * Delete an additional information property by id under given resource/service
404 protected Response deleteAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId) {
406 ServletContext context = request.getSession().getServletContext();
408 String url = request.getMethod() + " " + request.getRequestURI();
409 log.debug("Start handle request of {}", url);
410 log.debug("modifier id is {}", userId);
414 AdditionalInformationBusinessLogic businessLogic = getBL(context);
416 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
417 additionalInfoParameterInfo.setUniqueId(labelId);
419 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.deleteAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
421 if (either.isRight()) {
422 ResponseFormat responseFormat = either.right().value();
423 log.info("Failed to update additional information property. Reason - {}", responseFormat);
424 return buildErrorResponse(responseFormat);
427 AdditionalInfoParameterInfo createdAI = either.left().value();
429 log.debug("Additional information {}={} deleted successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
431 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
432 return buildOkResponse(responseFormat, createdAI);
434 } catch (Exception e) {
435 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Additional Information");
436 log.debug("Delete additional information failed with exception", e);
437 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
438 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, String userId) {
455 ServletContext context = request.getSession().getServletContext();
457 String url = request.getMethod() + " " + request.getRequestURI();
458 log.debug("Start handle request of {}", url);
459 log.debug("modifier id is {}", userId);
463 // create the new property
464 AdditionalInformationBusinessLogic businessLogic = getBL(context);
466 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
467 additionalInfoParameterInfo.setUniqueId(labelId);
469 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.getAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
471 if (either.isRight()) {
472 ResponseFormat responseFormat = either.right().value();
473 log.info("Failed to update additional information property. Reason - {}", responseFormat);
474 return buildErrorResponse(responseFormat);
477 AdditionalInfoParameterInfo createdAI = either.left().value();
479 log.debug("Additional information {}={} fetched successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
481 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
482 return buildOkResponse(responseFormat, createdAI);
484 } catch (Exception e) {
485 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Additional Information");
487 log.debug("get additional information failed with exception", e);
488 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
489 return buildErrorResponse(responseFormat);
495 * Get all additional information properties under given resource/service
503 protected Response getAllAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, HttpServletRequest request, String userId) {
505 ServletContext context = request.getSession().getServletContext();
507 String url = request.getMethod() + " " + request.getRequestURI();
508 log.debug("Start handle request of {}", url);
509 log.debug("modifier id is {}", userId);
513 AdditionalInformationBusinessLogic businessLogic = getBL(context);
515 Either<AdditionalInformationDefinition, ResponseFormat> either = businessLogic.getAllAdditionalInformation(nodeType, uniqueId, null, userId);
516 if (either.isRight()) {
517 ResponseFormat responseFormat = either.right().value();
518 log.info("Failed to update additional information property. Reason - {}", responseFormat);
519 return buildErrorResponse(responseFormat);
522 AdditionalInformationDefinition additionalInformationDefinition = either.left().value();
524 log.debug("All Additional information retrieved for component {} is {}", uniqueId, additionalInformationDefinition);
526 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
527 return buildOkResponse(responseFormat, additionalInformationDefinition);
529 } catch (Exception e) {
530 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get All Additional Information");
531 log.debug("Get all addiotanl information properties failed with exception", e);
532 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
533 return buildErrorResponse(responseFormat);
538 private AdditionalInformationBusinessLogic getBL(ServletContext context) {
539 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
540 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
541 AdditionalInformationBusinessLogic bl = webApplicationContext.getBean(AdditionalInformationBusinessLogic.class);