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.config.EcompErrorName;
35 import org.openecomp.sdc.exception.ResponseFormat;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.web.context.WebApplicationContext;
40 import javax.inject.Singleton;
41 import javax.servlet.ServletContext;
42 import javax.servlet.http.HttpServletRequest;
44 import javax.ws.rs.core.Context;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
47 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
49 @Api(value = "Additional Information Servlet", description = "Additional Information Servlet")
51 public class AdditionalInformationServlet extends BeGenericServlet {
53 private static final Logger log = LoggerFactory.getLogger(AdditionalInformationServlet.class);
64 @Path("/resources/{resourceId}/additionalinfo")
65 @Consumes(MediaType.APPLICATION_JSON)
66 @Produces(MediaType.APPLICATION_JSON)
67 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "POST", notes = "Returns created Additional Inforamtion property", response = Response.class)
68 @ApiResponses(value = { @ApiResponse(code = 201, message = "Additional information created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
69 @ApiResponse(code = 409, message = "Additional information key already exist") })
70 public Response createResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
71 @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) {
73 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userUserId, data);
86 @Path("/services/{serviceId}/additionalinfo")
87 @Consumes(MediaType.APPLICATION_JSON)
88 @Produces(MediaType.APPLICATION_JSON)
89 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "POST", notes = "Returns created Additional Inforamtion property", response = Response.class)
90 @ApiResponses(value = { @ApiResponse(code = 201, message = "Additional information created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
91 @ApiResponse(code = 409, message = "Additional information key already exist") })
92 public Response createServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
93 @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) {
95 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userUserId, data);
109 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
110 @Consumes(MediaType.APPLICATION_JSON)
111 @Produces(MediaType.APPLICATION_JSON)
112 @ApiOperation(value = "Update Additional Information Label and Value", httpMethod = "PUT", notes = "Returns updated Additional Inforamtion property", response = Response.class)
113 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
114 @ApiResponse(code = 409, message = "Additional information key already exist") })
115 public Response updateResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
116 @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,
117 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
119 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId, data);
133 @Path("/services/{serviceId}/additionalinfo/{labelId}")
134 @Consumes(MediaType.APPLICATION_JSON)
135 @Produces(MediaType.APPLICATION_JSON)
136 @ApiOperation(value = "Update Additional Information Label and Value", httpMethod = "PUT", notes = "Returns updated Additional Inforamtion property", response = Response.class)
137 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
138 @ApiResponse(code = 409, message = "Additional information key already exist") })
139 public Response updateServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
140 @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,
141 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
143 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId, data);
156 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
157 @Consumes(MediaType.APPLICATION_JSON)
158 @Produces(MediaType.APPLICATION_JSON)
159 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "DELETE", notes = "Returns deleted Additional Inforamtion property", response = Response.class)
160 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
161 @ApiResponse(code = 409, message = "Additional information key already exist") })
162 public Response updateResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
163 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
165 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
178 @Path("/services/{serviceId}/additionalinfo/{labelId}")
179 @Consumes(MediaType.APPLICATION_JSON)
180 @Produces(MediaType.APPLICATION_JSON)
181 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "DELETE", notes = "Returns deleted Additional Inforamtion property", response = Response.class)
182 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
183 @ApiResponse(code = 409, message = "Additional information key already exist") })
184 public Response deleteServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
185 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
187 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
200 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
201 @Consumes(MediaType.APPLICATION_JSON)
202 @Produces(MediaType.APPLICATION_JSON)
203 @ApiOperation(value = "Get Additional Information by id", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
204 @ApiResponses(value = { @ApiResponse(code = 200, message = "fetched additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
205 @ApiResponse(code = 409, message = "Additional information key already exist") })
206 public Response getResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
207 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
209 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
222 @Path("/services/{serviceId}/additionalinfo/{labelId}")
223 @Consumes(MediaType.APPLICATION_JSON)
224 @Produces(MediaType.APPLICATION_JSON)
225 @ApiOperation(value = "Get Additional Information by id", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
226 @ApiResponses(value = { @ApiResponse(code = 200, message = "fetched additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
227 @ApiResponse(code = 409, message = "Additional information key already exist") })
228 public Response getServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
229 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
231 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
243 @Path("/resources/{resourceId}/additionalinfo")
244 @Consumes(MediaType.APPLICATION_JSON)
245 @Produces(MediaType.APPLICATION_JSON)
246 @ApiOperation(value = "Get all Additional Information under resource", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
247 @ApiResponses(value = { @ApiResponse(code = 200, message = "list of additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
248 @ApiResponse(code = 409, message = "Additional information key already exist") })
249 public Response getAllResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId, @Context final HttpServletRequest request,
250 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
252 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userId);
264 @Path("/services/{serviceId}/additionalinfo")
265 @Consumes(MediaType.APPLICATION_JSON)
266 @Produces(MediaType.APPLICATION_JSON)
267 @ApiOperation(value = "Get all Additional Information under service", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
268 @ApiResponses(value = { @ApiResponse(code = 200, message = "list of additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
269 @ApiResponse(code = 409, message = "Additional information key already exist") })
270 public Response getAllServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId, @Context final HttpServletRequest request,
271 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
273 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userId);
279 * Create additional information property under given resource/service
288 protected Response createAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, HttpServletRequest request, String userId, String data) {
290 ServletContext context = request.getSession().getServletContext();
292 String url = request.getMethod() + " " + request.getRequestURI();
293 log.debug("Start handle request of {}", url);
294 log.debug("modifier id is {}", userId);
295 log.debug("data is {}", data);
298 // convert json to AdditionalInfoParameterInfo
299 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
301 // create the new property
302 AdditionalInformationBusinessLogic businessLogic = getBL(context);
304 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.createAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
306 if (either.isRight()) {
307 ResponseFormat responseFormat = either.right().value();
308 log.info("Failed to create additional information {}. Reason - {}", additionalInfoParameterInfo, responseFormat);
309 return buildErrorResponse(responseFormat);
312 AdditionalInfoParameterInfo createdAI = either.left().value();
314 log.debug("Additional information {}={} created successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
316 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
317 return buildOkResponse(responseFormat, createdAI);
319 } catch (Exception e) {
320 log.debug("Create additional information failed with exception", e);
321 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
322 return buildErrorResponse(responseFormat);
328 * Update additional information property by id under given resource/service
338 protected Response updateAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId, String data) {
340 ServletContext context = request.getSession().getServletContext();
342 String url = request.getMethod() + " " + request.getRequestURI();
343 log.debug("Start handle request of {}", url);
344 log.debug("modifier id is {}", userId);
345 log.debug("data is {}", data);
348 // convert json to AdditionalInfoParameterInfo
349 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
351 // create the new property
352 AdditionalInformationBusinessLogic businessLogic = getBL(context);
354 additionalInfoParameterInfo.setUniqueId(labelId);
356 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.updateAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
358 if (either.isRight()) {
359 ResponseFormat responseFormat = either.right().value();
360 log.info("Failed to update additional information property. Reason - {}", responseFormat);
361 return buildErrorResponse(responseFormat);
364 AdditionalInfoParameterInfo createdAI = either.left().value();
366 log.debug("Additional information {}={} updated successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
368 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
369 return buildOkResponse(responseFormat, createdAI);
371 } catch (Exception e) {
372 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Additional Information");
373 log.debug("Update additional information failed with exception", e);
374 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
375 return buildErrorResponse(responseFormat);
382 * Delete an additional information property by id under given resource/service
391 protected Response deleteAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId) {
393 ServletContext context = request.getSession().getServletContext();
395 String url = request.getMethod() + " " + request.getRequestURI();
396 log.debug("Start handle request of {}", url);
397 log.debug("modifier id is {}", userId);
401 AdditionalInformationBusinessLogic businessLogic = getBL(context);
403 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
404 additionalInfoParameterInfo.setUniqueId(labelId);
406 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.deleteAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
408 if (either.isRight()) {
409 ResponseFormat responseFormat = either.right().value();
410 log.info("Failed to update additional information property. Reason - {}", responseFormat);
411 return buildErrorResponse(responseFormat);
414 AdditionalInfoParameterInfo createdAI = either.left().value();
416 log.debug("Additional information {}={} deleted successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
418 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
419 return buildOkResponse(responseFormat, createdAI);
421 } catch (Exception e) {
422 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Additional Information");
423 log.debug("Delete additional information failed with exception", e);
424 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
425 return buildErrorResponse(responseFormat);
431 * Get a specific additional information property by a given id under given resource/service
440 protected Response getAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId) {
442 ServletContext context = request.getSession().getServletContext();
444 String url = request.getMethod() + " " + request.getRequestURI();
445 log.debug("Start handle request of {}", url);
446 log.debug("modifier id is {}", userId);
450 // create the new property
451 AdditionalInformationBusinessLogic businessLogic = getBL(context);
453 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
454 additionalInfoParameterInfo.setUniqueId(labelId);
456 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.getAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, null, userId);
458 if (either.isRight()) {
459 ResponseFormat responseFormat = either.right().value();
460 log.info("Failed to update additional information property. Reason - {}", responseFormat);
461 return buildErrorResponse(responseFormat);
464 AdditionalInfoParameterInfo createdAI = either.left().value();
466 log.debug("Additional information {}={} fetched successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
468 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
469 return buildOkResponse(responseFormat, createdAI);
471 } catch (Exception e) {
472 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Additional Information");
474 log.debug("get additional information failed with exception", e);
475 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
476 return buildErrorResponse(responseFormat);
482 * Get all additional information properties under given resource/service
490 protected Response getAllAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, HttpServletRequest request, String userId) {
492 ServletContext context = request.getSession().getServletContext();
494 String url = request.getMethod() + " " + request.getRequestURI();
495 log.debug("Start handle request of {}", url);
496 log.debug("modifier id is {}", userId);
500 AdditionalInformationBusinessLogic businessLogic = getBL(context);
502 Either<AdditionalInformationDefinition, ResponseFormat> either = businessLogic.getAllAdditionalInformation(nodeType, uniqueId, null, userId);
503 if (either.isRight()) {
504 ResponseFormat responseFormat = either.right().value();
505 log.info("Failed to update additional information property. Reason - {}", responseFormat);
506 return buildErrorResponse(responseFormat);
509 AdditionalInformationDefinition additionalInformationDefinition = either.left().value();
511 log.debug("All Additional information retrieved for component {} is {}", uniqueId, additionalInformationDefinition);
513 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
514 return buildOkResponse(responseFormat, additionalInformationDefinition);
516 } catch (Exception e) {
517 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get All Additional Information");
518 log.debug("Get all addiotanl information properties failed with exception", e);
519 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
520 return buildErrorResponse(responseFormat);
525 private AdditionalInformationBusinessLogic getBL(ServletContext context) {
526 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
527 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
528 AdditionalInformationBusinessLogic bl = webApplicationContext.getBean(AdditionalInformationBusinessLogic.class);