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 javax.inject.Inject;
27 import org.openecomp.sdc.be.components.impl.AdditionalInformationBusinessLogic;
28 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
29 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
30 import org.openecomp.sdc.be.config.BeEcompErrorManager;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterInfo;
33 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.model.AdditionalInformationDefinition;
36 import org.openecomp.sdc.be.user.UserBusinessLogic;
37 import org.openecomp.sdc.common.api.Constants;
38 import org.openecomp.sdc.common.log.wrappers.Logger;
39 import org.openecomp.sdc.exception.ResponseFormat;
41 import javax.inject.Singleton;
42 import javax.servlet.ServletContext;
43 import javax.servlet.http.HttpServletRequest;
45 import javax.ws.rs.core.Context;
46 import javax.ws.rs.core.MediaType;
47 import javax.ws.rs.core.Response;
49 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
51 @Api(value = "Additional Information Servlet", description = "Additional Information Servlet")
53 public class AdditionalInformationServlet extends BeGenericServlet {
55 private static final Logger log = Logger.getLogger(AdditionalInformationServlet.class);
56 private final AdditionalInformationBusinessLogic businessLogic;
59 public AdditionalInformationServlet(UserBusinessLogic userBusinessLogic,
60 ComponentsUtils componentsUtils,
61 AdditionalInformationBusinessLogic businessLogic) {
62 super(userBusinessLogic, componentsUtils);
63 this.businessLogic = businessLogic;
75 @Path("/resources/{resourceId}/additionalinfo")
76 @Consumes(MediaType.APPLICATION_JSON)
77 @Produces(MediaType.APPLICATION_JSON)
78 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "POST", notes = "Returns created Additional Inforamtion property", response = Response.class)
79 @ApiResponses(value = { @ApiResponse(code = 201, message = "Additional information created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
80 @ApiResponse(code = 409, message = "Additional information key already exist") })
81 public Response createResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
82 @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) {
84 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userUserId, data);
97 @Path("/services/{serviceId}/additionalinfo")
98 @Consumes(MediaType.APPLICATION_JSON)
99 @Produces(MediaType.APPLICATION_JSON)
100 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "POST", notes = "Returns created Additional Inforamtion property", response = Response.class)
101 @ApiResponses(value = { @ApiResponse(code = 201, message = "Additional information created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
102 @ApiResponse(code = 409, message = "Additional information key already exist") })
103 public Response createServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
104 @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) {
106 return createAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userUserId, data);
120 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
121 @Consumes(MediaType.APPLICATION_JSON)
122 @Produces(MediaType.APPLICATION_JSON)
123 @ApiOperation(value = "Update Additional Information Label and Value", httpMethod = "PUT", notes = "Returns updated Additional Inforamtion property", response = Response.class)
124 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
125 @ApiResponse(code = 409, message = "Additional information key already exist") })
126 public Response updateResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
127 @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,
128 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
130 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId, data);
144 @Path("/services/{serviceId}/additionalinfo/{labelId}")
145 @Consumes(MediaType.APPLICATION_JSON)
146 @Produces(MediaType.APPLICATION_JSON)
147 @ApiOperation(value = "Update Additional Information Label and Value", httpMethod = "PUT", notes = "Returns updated Additional Inforamtion property", response = Response.class)
148 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
149 @ApiResponse(code = 409, message = "Additional information key already exist") })
150 public Response updateServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
151 @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,
152 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
154 return updateAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId, data);
167 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
168 @Consumes(MediaType.APPLICATION_JSON)
169 @Produces(MediaType.APPLICATION_JSON)
170 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "DELETE", notes = "Returns deleted Additional Inforamtion property", response = Response.class)
171 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
172 @ApiResponse(code = 409, message = "Additional information key already exist") })
173 public Response updateResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
174 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
176 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
189 @Path("/services/{serviceId}/additionalinfo/{labelId}")
190 @Consumes(MediaType.APPLICATION_JSON)
191 @Produces(MediaType.APPLICATION_JSON)
192 @ApiOperation(value = "Create Additional Information Label and Value", httpMethod = "DELETE", notes = "Returns deleted Additional Inforamtion property", response = Response.class)
193 @ApiResponses(value = { @ApiResponse(code = 200, message = "Additional information deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
194 @ApiResponse(code = 409, message = "Additional information key already exist") })
195 public Response deleteServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
196 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
198 return deleteAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
211 @Path("/resources/{resourceId}/additionalinfo/{labelId}")
212 @Consumes(MediaType.APPLICATION_JSON)
213 @Produces(MediaType.APPLICATION_JSON)
214 @ApiOperation(value = "Get Additional Information by id", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
215 @ApiResponses(value = { @ApiResponse(code = 200, message = "fetched additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
216 @ApiResponse(code = 409, message = "Additional information key already exist") })
217 public Response getResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
218 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
220 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, labelId, request, userId);
233 @Path("/services/{serviceId}/additionalinfo/{labelId}")
234 @Consumes(MediaType.APPLICATION_JSON)
235 @Produces(MediaType.APPLICATION_JSON)
236 @ApiOperation(value = "Get Additional Information by id", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
237 @ApiResponses(value = { @ApiResponse(code = 200, message = "fetched additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
238 @ApiResponse(code = 409, message = "Additional information key already exist") })
239 public Response getServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId,
240 @ApiParam(value = "label id", required = true) @PathParam("labelId") final String labelId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
242 return getAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, labelId, request, userId);
254 @Path("/resources/{resourceId}/additionalinfo")
255 @Consumes(MediaType.APPLICATION_JSON)
256 @Produces(MediaType.APPLICATION_JSON)
257 @ApiOperation(value = "Get all Additional Information under resource", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
258 @ApiResponses(value = { @ApiResponse(code = 200, message = "list of additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
259 @ApiResponse(code = 409, message = "Additional information key already exist") })
260 public Response getAllResourceAdditionalInformationLabel(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId, @Context final HttpServletRequest request,
261 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
263 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Resource, resourceId, request, userId);
275 @Path("/services/{serviceId}/additionalinfo")
276 @Consumes(MediaType.APPLICATION_JSON)
277 @Produces(MediaType.APPLICATION_JSON)
278 @ApiOperation(value = "Get all Additional Information under service", httpMethod = "GET", notes = "Returns Additional Inforamtion property", response = Response.class)
279 @ApiResponses(value = { @ApiResponse(code = 200, message = "list of additional information"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
280 @ApiResponse(code = 409, message = "Additional information key already exist") })
281 public Response getAllServiceAdditionalInformationLabel(@ApiParam(value = "service id to update with new property", required = true) @PathParam("serviceId") final String serviceId, @Context final HttpServletRequest request,
282 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
284 return getAllAdditionalInformationLabelForComponent(NodeTypeEnum.Service, serviceId, request, userId);
290 * Create additional information property under given resource/service
299 protected Response createAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, HttpServletRequest request, String userId, String data) {
301 ServletContext context = request.getSession().getServletContext();
303 String url = request.getMethod() + " " + request.getRequestURI();
304 log.debug("Start handle request of {}", url);
305 log.debug("modifier id is {}", userId);
306 log.debug("data is {}", data);
309 // convert json to AdditionalInfoParameterInfo
310 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
312 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.createAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, userId);
314 if (either.isRight()) {
315 ResponseFormat responseFormat = either.right().value();
316 log.info("Failed to create additional information {}. Reason - {}", additionalInfoParameterInfo, responseFormat);
317 return buildErrorResponse(responseFormat);
320 AdditionalInfoParameterInfo createdAI = either.left().value();
322 log.debug("Additional information {}={} created successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
324 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
325 return buildOkResponse(responseFormat, createdAI);
327 } catch (Exception e) {
328 log.debug("Create additional information failed with exception", e);
329 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
330 return buildErrorResponse(responseFormat);
336 * Update additional information property by id under given resource/service
346 protected Response updateAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId, String data) {
348 ServletContext context = request.getSession().getServletContext();
350 String url = request.getMethod() + " " + request.getRequestURI();
351 log.debug("Start handle request of {}", url);
352 log.debug("modifier id is {}", userId);
353 log.debug("data is {}", data);
356 // convert json to AdditionalInfoParameterInfo
357 AdditionalInfoParameterInfo additionalInfoParameterInfo = gson.fromJson(data, AdditionalInfoParameterInfo.class);
359 additionalInfoParameterInfo.setUniqueId(labelId);
361 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.updateAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, userId);
363 if (either.isRight()) {
364 ResponseFormat responseFormat = either.right().value();
365 log.info("Failed to update additional information property. Reason - {}", responseFormat);
366 return buildErrorResponse(responseFormat);
369 AdditionalInfoParameterInfo createdAI = either.left().value();
371 log.debug("Additional information {}={} updated successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
373 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
374 return buildOkResponse(responseFormat, createdAI);
376 } catch (Exception e) {
377 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Additional Information");
378 log.debug("Update additional information failed with exception", e);
379 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
380 return buildErrorResponse(responseFormat);
387 * Delete an additional information property by id under given resource/service
396 protected Response deleteAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId) {
398 ServletContext context = request.getSession().getServletContext();
400 String url = request.getMethod() + " " + request.getRequestURI();
401 log.debug("Start handle request of {}", url);
402 log.debug("modifier id is {}", userId);
406 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
407 additionalInfoParameterInfo.setUniqueId(labelId);
409 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.deleteAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, userId);
411 if (either.isRight()) {
412 ResponseFormat responseFormat = either.right().value();
413 log.info("Failed to update additional information property. Reason - {}", responseFormat);
414 return buildErrorResponse(responseFormat);
417 AdditionalInfoParameterInfo createdAI = either.left().value();
419 log.debug("Additional information {}={} deleted successfully with id {}", createdAI.getKey(), createdAI.getValue(), createdAI.getUniqueId());
421 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
422 return buildOkResponse(responseFormat, createdAI);
424 } catch (Exception e) {
425 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Additional Information");
426 log.debug("Delete additional information failed with exception", e);
427 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
428 return buildErrorResponse(responseFormat);
434 * Get a specific additional information property by a given id under given resource/service
443 protected Response getAdditionalInformationLabelForComponent(NodeTypeEnum nodeType, String uniqueId, String labelId, HttpServletRequest request, String userId) {
445 ServletContext context = request.getSession().getServletContext();
447 String url = request.getMethod() + " " + request.getRequestURI();
448 log.debug("Start handle request of {}", url);
449 log.debug("modifier id is {}", userId);
453 AdditionalInfoParameterInfo additionalInfoParameterInfo = new AdditionalInfoParameterInfo();
454 additionalInfoParameterInfo.setUniqueId(labelId);
456 Either<AdditionalInfoParameterInfo, ResponseFormat> either = businessLogic.getAdditionalInformation(nodeType, uniqueId, additionalInfoParameterInfo, 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);
499 Either<AdditionalInformationDefinition, ResponseFormat> either = businessLogic.getAllAdditionalInformation(nodeType, uniqueId, userId);
500 if (either.isRight()) {
501 ResponseFormat responseFormat = either.right().value();
502 log.info("Failed to update additional information property. Reason - {}", responseFormat);
503 return buildErrorResponse(responseFormat);
506 AdditionalInformationDefinition additionalInformationDefinition = either.left().value();
508 log.debug("All Additional information retrieved for component {} is {}", uniqueId, additionalInformationDefinition);
510 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
511 return buildOkResponse(responseFormat, additionalInformationDefinition);
513 } catch (Exception e) {
514 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get All Additional Information");
515 log.debug("Get all addiotanl information properties failed with exception", e);
516 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
517 return buildErrorResponse(responseFormat);