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 com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.annotations.*;
26 import org.openecomp.sdc.be.components.impl.AdditionalInformationBusinessLogic;
27 import org.openecomp.sdc.be.config.BeEcompErrorManager;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterInfo;
30 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
31 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
32 import org.openecomp.sdc.be.model.AdditionalInformationDefinition;
33 import org.openecomp.sdc.common.api.Constants;
34 import org.openecomp.sdc.common.log.wrappers.Logger;
35 import org.openecomp.sdc.exception.ResponseFormat;
36 import org.springframework.web.context.WebApplicationContext;
38 import javax.inject.Singleton;
39 import javax.servlet.ServletContext;
40 import javax.servlet.http.HttpServletRequest;
42 import javax.ws.rs.core.Context;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
46 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
48 @Api(value = "Additional Information Servlet", description = "Additional Information Servlet")
50 public class AdditionalInformationServlet extends BeGenericServlet {
52 private static final Logger log = Logger.getLogger(AdditionalInformationServlet.class);
63 @Path("/resources/{resourceId}/additionalinfo")
64 @Consumes(MediaType.APPLICATION_JSON)
65 @Produces(MediaType.APPLICATION_JSON)
66 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "POST", notes = "Returns created Additional Inforamtion property", response = Response.class)
67 @ApiResponses(value = { @ApiResponse(code = 201, message = "Additional information created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
68 @ApiResponse(code = 409, message = "Additional information key already exist") })
69 public Response createResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
70 @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) {
72 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userUserId, data);
85 @Path("/services/{serviceId}/additionalinfo")
86 @Consumes(MediaType.APPLICATION_JSON)
87 @Produces(MediaType.APPLICATION_JSON)
88 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "POST", notes = "Returns created Additional Inforamtion property", response = Response.class)
89 @ApiResponses(value = { @ApiResponse(code = 201, message = "Additional information created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
90 @ApiResponse(code = 409, message = "Additional information key already exist") })
91 public Response createServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
92 @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) {
94 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userUserId, data);
108 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
109 @Consumes(MediaType.APPLICATION_JSON)
110 @Produces(MediaType.APPLICATION_JSON)
111 @ApiOperation(value = "Update Additional Information Label and Value", httpMethod = "PUT", notes = "Returns updated Additional Inforamtion property", response = Response.class)
112 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
113 @ApiResponse(code = 409, message = "Additional information key already exist") })
114 public Response updateResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
115 @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,
116 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
118 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId, data);
132 @Path("/services/{serviceId}/additionalinfo/{labelId}")
133 @Consumes(MediaType.APPLICATION_JSON)
134 @Produces(MediaType.APPLICATION_JSON)
135 @ApiOperation(value = "Update Additional Information Label and Value", httpMethod = "PUT", notes = "Returns updated Additional Inforamtion property", response = Response.class)
136 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
137 @ApiResponse(code = 409, message = "Additional information key already exist") })
138 public Response updateServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
139 @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,
140 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
142 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId, data);
155 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
156 @Consumes(MediaType.APPLICATION_JSON)
157 @Produces(MediaType.APPLICATION_JSON)
158 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "DELETE", notes = "Returns deleted Additional Inforamtion property", response = Response.class)
159 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
160 @ApiResponse(code = 409, message = "Additional information key already exist") })
161 public Response updateResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
162 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
164 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
177 @Path("/services/{serviceId}/additionalinfo/{labelId}")
178 @Consumes(MediaType.APPLICATION_JSON)
179 @Produces(MediaType.APPLICATION_JSON)
180 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "DELETE", notes = "Returns deleted Additional Inforamtion property", response = Response.class)
181 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
182 @ApiResponse(code = 409, message = "Additional information key already exist") })
183 public Response deleteServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
184 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
186 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
199 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
200 @Consumes(MediaType.APPLICATION_JSON)
201 @Produces(MediaType.APPLICATION_JSON)
202 @ApiOperation(value = "Get Additional Information by id", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
203 @ApiResponses(value = { @ApiResponse(code = 200, message = "fetched additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
204 @ApiResponse(code = 409, message = "Additional information key already exist") })
205 public Response getResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
206 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
208 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
221 @Path("/services/{serviceId}/additionalinfo/{labelId}")
222 @Consumes(MediaType.APPLICATION_JSON)
223 @Produces(MediaType.APPLICATION_JSON)
224 @ApiOperation(value = "Get Additional Information by id", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
225 @ApiResponses(value = { @ApiResponse(code = 200, message = "fetched additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
226 @ApiResponse(code = 409, message = "Additional information key already exist") })
227 public Response getServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
228 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
230 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
242 @Path("/resources/{resourceId}/additionalinfo")
243 @Consumes(MediaType.APPLICATION_JSON)
244 @Produces(MediaType.APPLICATION_JSON)
245 @ApiOperation(value = "Get all Additional Information under resource", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
246 @ApiResponses(value = { @ApiResponse(code = 200, message = "list of additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
247 @ApiResponse(code = 409, message = "Additional information key already exist") })
248 public Response getAllResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId, @Context final HttpServletRequest request,
249 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
251 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userId);
263 @Path("/services/{serviceId}/additionalinfo")
264 @Consumes(MediaType.APPLICATION_JSON)
265 @Produces(MediaType.APPLICATION_JSON)
266 @ApiOperation(value = "Get all Additional Information under service", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
267 @ApiResponses(value = { @ApiResponse(code = 200, message = "list of additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
268 @ApiResponse(code = 409, message = "Additional information key already exist") })
269 public Response getAllServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId, @Context final HttpServletRequest request,
270 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
272 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userId);
278 * Create additional information property under given resource/service
287 protected Response createAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, HttpServletRequest request, String userId, String data) {
289 ServletContext context = request.getSession().getServletContext();
291 String url = request.getMethod() + " " + request.getRequestURI();
292 log.debug("Start handle request of {}", url);
293 log.debug("modifier id is {}", userId);
294 log.debug("data is {}", data);
297 // convert json to AdditionalInfoParameterInfo
298 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
300 // create the new property
301 AdditionalInformationBusinessLogic businessLogic = getBL(context);
303 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.createAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, userId);
305 if (either.isRight()) {
306 ResponseFormat responseFormat = either.right().value();
307 log.info("Failed to create additional information {}. Reason - {}", additionalInfoParameterInfo, responseFormat);
308 return buildErrorResponse(responseFormat);
311 AdditionalInfoParameterInfo createdAI = either.left().value();
313 log.debug("Additional information {}={} created successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
315 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
316 return buildOkResponse(responseFormat, createdAI);
318 } catch (Exception e) {
319 log.debug("Create additional information failed with exception", e);
320 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
321 return buildErrorResponse(responseFormat);
327 * Update additional information property by id under given resource/service
337 protected Response updateAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId, String data) {
339 ServletContext context = request.getSession().getServletContext();
341 String url = request.getMethod() + " " + request.getRequestURI();
342 log.debug("Start handle request of {}", url);
343 log.debug("modifier id is {}", userId);
344 log.debug("data is {}", data);
347 // convert json to AdditionalInfoParameterInfo
348 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
350 // create the new property
351 AdditionalInformationBusinessLogic businessLogic = getBL(context);
353 additionalInfoParameterInfo.setUniqueId(labelId);
355 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.updateAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, userId);
357 if (either.isRight()) {
358 ResponseFormat responseFormat = either.right().value();
359 log.info("Failed to update additional information property. Reason - {}", responseFormat);
360 return buildErrorResponse(responseFormat);
363 AdditionalInfoParameterInfo createdAI = either.left().value();
365 log.debug("Additional information {}={} updated successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
367 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
368 return buildOkResponse(responseFormat, createdAI);
370 } catch (Exception e) {
371 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Additional Information");
372 log.debug("Update additional information failed with exception", e);
373 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
374 return buildErrorResponse(responseFormat);
381 * Delete an additional information property by id under given resource/service
390 protected Response deleteAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId) {
392 ServletContext context = request.getSession().getServletContext();
394 String url = request.getMethod() + " " + request.getRequestURI();
395 log.debug("Start handle request of {}", url);
396 log.debug("modifier id is {}", userId);
400 AdditionalInformationBusinessLogic businessLogic = getBL(context);
402 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
403 additionalInfoParameterInfo.setUniqueId(labelId);
405 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.deleteAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, userId);
407 if (either.isRight()) {
408 ResponseFormat responseFormat = either.right().value();
409 log.info("Failed to update additional information property. Reason - {}", responseFormat);
410 return buildErrorResponse(responseFormat);
413 AdditionalInfoParameterInfo createdAI = either.left().value();
415 log.debug("Additional information {}={} deleted successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
417 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
418 return buildOkResponse(responseFormat, createdAI);
420 } catch (Exception e) {
421 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Additional Information");
422 log.debug("Delete additional information failed with exception", e);
423 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
424 return buildErrorResponse(responseFormat);
430 * Get a specific additional information property by a given id under given resource/service
439 protected Response getAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId) {
441 ServletContext context = request.getSession().getServletContext();
443 String url = request.getMethod() + " " + request.getRequestURI();
444 log.debug("Start handle request of {}", url);
445 log.debug("modifier id is {}", userId);
449 // create the new property
450 AdditionalInformationBusinessLogic businessLogic = getBL(context);
452 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
453 additionalInfoParameterInfo.setUniqueId(labelId);
455 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.getAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, userId);
457 if (either.isRight()) {
458 ResponseFormat responseFormat = either.right().value();
459 log.info("Failed to update additional information property. Reason - {}", responseFormat);
460 return buildErrorResponse(responseFormat);
463 AdditionalInfoParameterInfo createdAI = either.left().value();
465 log.debug("Additional information {}={} fetched successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
467 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
468 return buildOkResponse(responseFormat, createdAI);
470 } catch (Exception e) {
471 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Additional Information");
473 log.debug("get additional information failed with exception", e);
474 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
475 return buildErrorResponse(responseFormat);
481 * Get all additional information properties under given resource/service
489 protected Response getAllAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, HttpServletRequest request, String userId) {
491 ServletContext context = request.getSession().getServletContext();
493 String url = request.getMethod() + " " + request.getRequestURI();
494 log.debug("Start handle request of {}", url);
495 log.debug("modifier id is {}", userId);
499 AdditionalInformationBusinessLogic businessLogic = getBL(context);
501 Either<AdditionalInformationDefinition, ResponseFormat> either = businessLogic.getAllAdditionalInformation(nodeType, uniqueId, userId);
502 if (either.isRight()) {
503 ResponseFormat responseFormat = either.right().value();
504 log.info("Failed to update additional information property. Reason - {}", responseFormat);
505 return buildErrorResponse(responseFormat);
508 AdditionalInformationDefinition additionalInformationDefinition = either.left().value();
510 log.debug("All Additional information retrieved for component {} is {}", uniqueId, additionalInformationDefinition);
512 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
513 return buildOkResponse(responseFormat, additionalInformationDefinition);
515 } catch (Exception e) {
516 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get All Additional Information");
517 log.debug("Get all addiotanl information properties failed with exception", e);
518 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
519 return buildErrorResponse(responseFormat);
524 private AdditionalInformationBusinessLogic getBL(ServletContext context) {
525 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
526 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
527 return webApplicationContext.getBean(AdditionalInformationBusinessLogic.class);