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.common.config.EcompErrorName;
48 import org.openecomp.sdc.exception.ResponseFormat;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.web.context.WebApplicationContext;
53 import com.jcabi.aspects.Loggable;
54 import io.swagger.annotations.Api;
55 import io.swagger.annotations.ApiOperation;
56 import io.swagger.annotations.ApiParam;
57 import io.swagger.annotations.ApiResponse;
58 import io.swagger.annotations.ApiResponses;
60 import fj.data.Either;
62 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
64 @Api(value = "Additional Information Servlet", description = "Additional Information Servlet")
66 public class AdditionalInformationServlet extends BeGenericServlet {
68 private static Logger log = LoggerFactory.getLogger(AdditionalInformationServlet.class.getName());
79 @Path("/resources/{resourceId}/additionalinfo")
80 @Consumes(MediaType.APPLICATION_JSON)
81 @Produces(MediaType.APPLICATION_JSON)
82 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "POST", notes = "Returns created Additional Inforamtion property", response = Response.class)
83 @ApiResponses(value = { @ApiResponse(code = 201, message = "Additional information created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
84 @ApiResponse(code = 409, message = "Additional information key already exist") })
85 public Response createResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
86 @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) {
88 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userUserId, data);
101 @Path("/services/{serviceId}/additionalinfo")
102 @Consumes(MediaType.APPLICATION_JSON)
103 @Produces(MediaType.APPLICATION_JSON)
104 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "POST", notes = "Returns created Additional Inforamtion property", response = Response.class)
105 @ApiResponses(value = { @ApiResponse(code = 201, message = "Additional information created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
106 @ApiResponse(code = 409, message = "Additional information key already exist") })
107 public Response createServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
108 @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) {
110 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userUserId, data);
124 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
125 @Consumes(MediaType.APPLICATION_JSON)
126 @Produces(MediaType.APPLICATION_JSON)
127 @ApiOperation(value = "Update Additional Information Label and Value", httpMethod = "PUT", notes = "Returns updated Additional Inforamtion property", response = Response.class)
128 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
129 @ApiResponse(code = 409, message = "Additional information key already exist") })
130 public Response updateResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
131 @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,
132 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
134 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId, data);
148 @Path("/services/{serviceId}/additionalinfo/{labelId}")
149 @Consumes(MediaType.APPLICATION_JSON)
150 @Produces(MediaType.APPLICATION_JSON)
151 @ApiOperation(value = "Update Additional Information Label and Value", httpMethod = "PUT", notes = "Returns updated Additional Inforamtion property", response = Response.class)
152 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
153 @ApiResponse(code = 409, message = "Additional information key already exist") })
154 public Response updateServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
155 @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,
156 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
158 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId, data);
171 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
172 @Consumes(MediaType.APPLICATION_JSON)
173 @Produces(MediaType.APPLICATION_JSON)
174 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "DELETE", notes = "Returns deleted Additional Inforamtion property", response = Response.class)
175 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
176 @ApiResponse(code = 409, message = "Additional information key already exist") })
177 public Response updateResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
178 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
180 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
193 @Path("/services/{serviceId}/additionalinfo/{labelId}")
194 @Consumes(MediaType.APPLICATION_JSON)
195 @Produces(MediaType.APPLICATION_JSON)
196 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "DELETE", notes = "Returns deleted Additional Inforamtion property", response = Response.class)
197 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
198 @ApiResponse(code = 409, message = "Additional information key already exist") })
199 public Response deleteServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
200 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
202 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
215 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
216 @Consumes(MediaType.APPLICATION_JSON)
217 @Produces(MediaType.APPLICATION_JSON)
218 @ApiOperation(value = "Get Additional Information by id", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
219 @ApiResponses(value = { @ApiResponse(code = 200, message = "fetched additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
220 @ApiResponse(code = 409, message = "Additional information key already exist") })
221 public Response getResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
222 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
224 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
237 @Path("/services/{serviceId}/additionalinfo/{labelId}")
238 @Consumes(MediaType.APPLICATION_JSON)
239 @Produces(MediaType.APPLICATION_JSON)
240 @ApiOperation(value = "Get Additional Information by id", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
241 @ApiResponses(value = { @ApiResponse(code = 200, message = "fetched additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
242 @ApiResponse(code = 409, message = "Additional information key already exist") })
243 public Response getServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
244 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
246 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
258 @Path("/resources/{resourceId}/additionalinfo")
259 @Consumes(MediaType.APPLICATION_JSON)
260 @Produces(MediaType.APPLICATION_JSON)
261 @ApiOperation(value = "Get all Additional Information under resource", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
262 @ApiResponses(value = { @ApiResponse(code = 200, message = "list of additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
263 @ApiResponse(code = 409, message = "Additional information key already exist") })
264 public Response getAllResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId, @Context final HttpServletRequest request,
265 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
267 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userId);
279 @Path("/services/{serviceId}/additionalinfo")
280 @Consumes(MediaType.APPLICATION_JSON)
281 @Produces(MediaType.APPLICATION_JSON)
282 @ApiOperation(value = "Get all Additional Information under service", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
283 @ApiResponses(value = { @ApiResponse(code = 200, message = "list of additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
284 @ApiResponse(code = 409, message = "Additional information key already exist") })
285 public Response getAllServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId, @Context final HttpServletRequest request,
286 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
288 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userId);
294 * Create additional information property under given resource/service
303 protected Response createAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, HttpServletRequest request, String userId, String data) {
305 ServletContext context = request.getSession().getServletContext();
307 String url = request.getMethod() + " " + request.getRequestURI();
308 log.debug("Start handle request of {}", url);
309 log.debug("modifier id is {}", userId);
310 log.debug("data is {}", data);
313 // convert json to AdditionalInfoParameterInfo
314 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
316 // create the new property
317 AdditionalInformationBusinessLogic businessLogic = getBL(context);
319 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.createAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
321 if (either.isRight()) {
322 ResponseFormat responseFormat = either.right().value();
323 log.info("Failed to create additional information {}. Reason - {}", additionalInfoParameterInfo, responseFormat);
324 return buildErrorResponse(responseFormat);
327 AdditionalInfoParameterInfo createdAI = either.left().value();
329 log.debug("Additional information {}={} created successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
331 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
332 return buildOkResponse(responseFormat, createdAI);
334 } catch (Exception e) {
335 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "Create Additional Information");
336 log.debug("Create additional information failed with exception", e);
337 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
338 return buildErrorResponse(responseFormat);
344 * Update additional information property by id under given resource/service
354 protected Response updateAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId, String data) {
356 ServletContext context = request.getSession().getServletContext();
358 String url = request.getMethod() + " " + request.getRequestURI();
359 log.debug("Start handle request of {}", url);
360 log.debug("modifier id is {}", userId);
361 log.debug("data is {}", data);
364 // convert json to AdditionalInfoParameterInfo
365 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
367 // create the new property
368 AdditionalInformationBusinessLogic businessLogic = getBL(context);
370 additionalInfoParameterInfo.setUniqueId(labelId);
372 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.updateAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
374 if (either.isRight()) {
375 ResponseFormat responseFormat = either.right().value();
376 log.info("Failed to update additional information property. Reason - {}", responseFormat);
377 return buildErrorResponse(responseFormat);
380 AdditionalInfoParameterInfo createdAI = either.left().value();
382 log.debug("Additional information {}={} updated successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
384 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
385 return buildOkResponse(responseFormat, createdAI);
387 } catch (Exception e) {
388 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "Update Additional Information");
389 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Additional Information");
390 log.debug("Update additional information failed with exception", e);
391 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
392 return buildErrorResponse(responseFormat);
399 * Delete an additional information property by id under given resource/service
408 protected Response deleteAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId) {
410 ServletContext context = request.getSession().getServletContext();
412 String url = request.getMethod() + " " + request.getRequestURI();
413 log.debug("Start handle request of {}", url);
414 log.debug("modifier id is {}", userId);
418 AdditionalInformationBusinessLogic businessLogic = getBL(context);
420 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
421 additionalInfoParameterInfo.setUniqueId(labelId);
423 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.deleteAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
425 if (either.isRight()) {
426 ResponseFormat responseFormat = either.right().value();
427 log.info("Failed to update additional information property. Reason - {}", responseFormat);
428 return buildErrorResponse(responseFormat);
431 AdditionalInfoParameterInfo createdAI = either.left().value();
433 log.debug("Additional information {}={} deleted successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
435 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
436 return buildOkResponse(responseFormat, createdAI);
438 } catch (Exception e) {
439 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "Delete Additional Information");
440 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Additional Information");
441 log.debug("Delete additional information failed with exception", e);
442 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
443 return buildErrorResponse(responseFormat);
449 * Get a specific additional information property by a given id under given resource/service
458 protected Response getAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId) {
460 ServletContext context = request.getSession().getServletContext();
462 String url = request.getMethod() + " " + request.getRequestURI();
463 log.debug("Start handle request of {}", url);
464 log.debug("modifier id is {}", userId);
468 // create the new property
469 AdditionalInformationBusinessLogic businessLogic = getBL(context);
471 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
472 additionalInfoParameterInfo.setUniqueId(labelId);
474 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.getAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
476 if (either.isRight()) {
477 ResponseFormat responseFormat = either.right().value();
478 log.info("Failed to update additional information property. Reason - {}", responseFormat);
479 return buildErrorResponse(responseFormat);
482 AdditionalInfoParameterInfo createdAI = either.left().value();
484 log.debug("Additional information {}={} fetched successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
486 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
487 return buildOkResponse(responseFormat, createdAI);
489 } catch (Exception e) {
490 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "Get Additional Information");
491 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Additional Information");
493 log.debug("get additional information failed with exception", e);
494 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
495 return buildErrorResponse(responseFormat);
501 * Get all additional information properties under given resource/service
509 protected Response getAllAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, HttpServletRequest request, String userId) {
511 ServletContext context = request.getSession().getServletContext();
513 String url = request.getMethod() + " " + request.getRequestURI();
514 log.debug("Start handle request of {}", url);
515 log.debug("modifier id is {}", userId);
519 AdditionalInformationBusinessLogic businessLogic = getBL(context);
521 Either<AdditionalInformationDefinition, ResponseFormat> either = businessLogic.getAllAdditionalInformation(nodeType, uniqueId, null, userId);
522 if (either.isRight()) {
523 ResponseFormat responseFormat = either.right().value();
524 log.info("Failed to update additional information property. Reason - {}", responseFormat);
525 return buildErrorResponse(responseFormat);
528 AdditionalInformationDefinition additionalInformationDefinition = either.left().value();
530 log.debug("All Additional information retrieved for component {} is {}", uniqueId, additionalInformationDefinition);
532 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
533 return buildOkResponse(responseFormat, additionalInformationDefinition);
535 } catch (Exception e) {
536 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "Get All Additional Information");
537 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get All Additional Information");
538 log.debug("Get all addiotanl information properties failed with exception", e);
539 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
540 return buildErrorResponse(responseFormat);
545 private AdditionalInformationBusinessLogic getBL(ServletContext context) {
546 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
547 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
548 AdditionalInformationBusinessLogic bl = webApplicationContext.getBean(AdditionalInformationBusinessLogic.class);