diff options
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceInterfaceOperationServlet.java')
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceInterfaceOperationServlet.java | 148 |
1 files changed, 51 insertions, 97 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceInterfaceOperationServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceInterfaceOperationServlet.java index d5d65a39c5..4d84e2c1a0 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceInterfaceOperationServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceInterfaceOperationServlet.java @@ -16,41 +16,45 @@ package org.openecomp.sdc.be.servlets; -import com.google.common.collect.Sets; import com.jcabi.aspects.Loggable; import fj.data.Either; -import io.swagger.annotations.*; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import java.util.Optional; +import java.util.UUID; +import javax.inject.Singleton; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.openecomp.sdc.be.components.impl.InterfaceOperationBusinessLogic; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datamodel.utils.InterfaceUIDataConverter; import org.openecomp.sdc.be.datatypes.elements.InterfaceOperationDataDefinition; -import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; -import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Operation; -import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.User; -import org.openecomp.sdc.be.model.jsontitan.utils.InterfaceUtils; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; -import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer; import org.openecomp.sdc.be.ui.model.UiResourceDataTransfer; import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.exception.ResponseFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.inject.Singleton; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.*; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.util.*; - @Loggable(prepend = true, value = Loggable.DEBUG, trim = false) @Path("/v1/catalog/resources/{resourceId}/interfaceOperations") @Consumes(MediaType.APPLICATION_JSON) @@ -78,7 +82,6 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle return createOrUpdate(data, resourceId, request, userId, false); } - @PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @@ -95,7 +98,6 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle return createOrUpdate(data, resourceId, request, userId, true); } - @DELETE @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @@ -137,20 +139,24 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle modifier.setUserId(userId); log.debug("Start get request of {} with modifier id {}", url, userId); + Response response; + try { + String resourceIdLower = resourceId.toLowerCase(); InterfaceOperationBusinessLogic businessLogic = getInterfaceOperationBL(context); - Either<UiComponentDataTransfer, ResponseFormat> resourceResponse = businessLogic.getComponentDataFilteredByParams(resourceId, modifier, Collections - .singletonList(ComponentFieldsEnum.INTERFACES.getValue())); - if (resourceResponse.isRight()) { - return buildErrorResponse(resourceResponse.right().value()); - } - - UiResourceDataTransfer uiResourceDataTransfer = (UiResourceDataTransfer) resourceResponse.left().value(); - InterfaceOperationDataDefinition interfaceOperationDataDefinition = getInterfaceOperationForResponse(interfaceOperationId, uiResourceDataTransfer.getInterfaces()); - return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), RepresentationUtils.toFilteredRepresentation(interfaceOperationDataDefinition)); + Either<Operation, ResponseFormat> actionResponse = businessLogic.getInterfaceOperation(resourceIdLower, interfaceOperationId, modifier, true); + if (actionResponse.isRight()) { + log.debug("failed to get interface operation"); + response = buildErrorResponse(actionResponse.right().value()); + return response; + } - } catch (Exception e) { + InterfaceOperationDataDefinition interfaceOperationDataDefinition = InterfaceUIDataConverter.convertOperationDataToInterfaceData(actionResponse.left().value()); + Object result = RepresentationUtils.toFilteredRepresentation(interfaceOperationDataDefinition); + return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result); + } + catch (Exception e) { BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Resource interface operations"); log.debug("get resource interface operations failed with exception", e); return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); @@ -173,25 +179,22 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle String resourceIdLower = resourceId.toLowerCase(); InterfaceOperationBusinessLogic businessLogic = getInterfaceOperationBL(context); - Either<Resource, ResponseFormat> actionResponse = businessLogic.deleteInterfaceOperation(resourceIdLower, Sets.newHashSet(interfaceOperationId), modifier, true); - + Either<Operation, ResponseFormat> actionResponse = businessLogic.deleteInterfaceOperation(resourceIdLower, interfaceOperationId, modifier, true); if (actionResponse.isRight()) { log.debug("failed to delete interface operation"); response = buildErrorResponse(actionResponse.right().value()); return response; } - Resource resource = actionResponse.left().value(); - InterfaceOperationDataDefinition interfaceOperationDataDefinition = getInterfaceOperationForResponse(interfaceOperationId, resource.getInterfaces()); + InterfaceOperationDataDefinition interfaceOperationDataDefinition = InterfaceUIDataConverter.convertOperationDataToInterfaceData(actionResponse.left().value()); Object result = RepresentationUtils.toFilteredRepresentation(interfaceOperationDataDefinition); return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result); - - } catch (Exception e) { + } + catch (Exception e) { BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Interface Operation"); log.debug("Delete interface operation with an error", e); response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); return response; - } } @@ -208,22 +211,12 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle String resourceIdLower = resourceId.toLowerCase(); InterfaceOperationBusinessLogic businessLogic = getInterfaceOperationBL(context); - Either<Resource, ResponseFormat> resourceEither = businessLogic.getResourceDetails(resourceId); - Resource origResource = resourceEither.left().value(); - - Either<Resource, ResponseFormat> convertResponse = parseToResource(data, origResource, isUpdate, modifier); - if (convertResponse.isRight()) { - log.debug("failed to parse resource"); - response = buildErrorResponse(convertResponse.right().value()); - return response; - } - - Resource updatedResource = convertResponse.left().value(); - Either<Resource, ResponseFormat> actionResponse ; + Operation operation = getMappedOperationData(data, isUpdate, modifier); + Either<Operation, ResponseFormat> actionResponse ; if (isUpdate) { - actionResponse = businessLogic.updateInterfaceOperation(resourceIdLower, updatedResource, modifier, true); + actionResponse = businessLogic.updateInterfaceOperation(resourceIdLower, operation, modifier, true); } else { - actionResponse = businessLogic.createInterfaceOperation(resourceIdLower, updatedResource, modifier, true); + actionResponse = businessLogic.createInterfaceOperation(resourceIdLower, operation, modifier, true); } if (actionResponse.isRight()) { @@ -232,67 +225,28 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle return response; } - Resource resource = actionResponse.left().value(); - List<Operation> operationData = InterfaceUtils.getOperationsFromInterface(updatedResource.getInterfaces()); - InterfaceOperationDataDefinition interfaceOperationDataDefinition = getInterfaceOperationForResponse(operationData.get(0).getUniqueId(), resource.getInterfaces()); - + InterfaceOperationDataDefinition interfaceOperationDataDefinition = InterfaceUIDataConverter.convertOperationDataToInterfaceData(actionResponse.left().value()); Object result = RepresentationUtils.toFilteredRepresentation(interfaceOperationDataDefinition); return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result); - - } catch (Exception e) { + } + catch (Exception e) { BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Interface Operation Creation or update"); log.debug("create or update interface Operation with an error", e); response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); return response; - } } - private Either<Resource, ResponseFormat> parseToResource(String resourceJson, Resource origResource, boolean isUpdate, User user) { - - Resource resource = convertToResourceObject(resourceJson, user).left().value(); - - Either<UiResourceDataTransfer, ResponseFormat> uiResourceEither = getComponentsUtils().convertJsonToObjectUsingObjectMapper(resourceJson, user, UiResourceDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.RESOURCE); + private Operation getMappedOperationData(String inputJson, boolean isUpdate, User user){ + Either<UiResourceDataTransfer, ResponseFormat> uiResourceEither = getComponentsUtils().convertJsonToObjectUsingObjectMapper(inputJson, user, UiResourceDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.RESOURCE); Optional<InterfaceOperationDataDefinition> opDef = uiResourceEither.left().value().getInterfaceOperations().values().stream().findFirst(); - InterfaceOperationDataDefinition interfaceOperationDataDefinition; + InterfaceOperationDataDefinition interfaceOperationDataDefinition = new InterfaceOperationDataDefinition(); if(opDef.isPresent()) { interfaceOperationDataDefinition = opDef.get(); - if(!isUpdate) interfaceOperationDataDefinition.setUniqueId(UUID.randomUUID().toString()); - - Map<String, Operation> interfaceOperations = new HashMap<>(); - interfaceOperations.put(interfaceOperationDataDefinition.getUniqueId(), InterfaceUIDataConverter.convertInterfaceDataToOperationData(interfaceOperationDataDefinition)); - InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); - interfaceDefinition.setUniqueId(UUID.randomUUID().toString()); - interfaceDefinition.setToscaResourceName(InterfaceUtils.createInterfaceToscaResourceName(origResource.getName())); - interfaceDefinition.setOperationsMap(interfaceOperations); - - Map<String, InterfaceDefinition> interfaceMap = new HashMap<>(); - interfaceMap.put(interfaceDefinition.getUniqueId(), interfaceDefinition); - - resource.setInterfaces(interfaceMap); - } - - return Either.left(resource); - } - - private Either<Resource, ResponseFormat> convertToResourceObject(String resourceJson, User user) { - return getComponentsUtils().convertJsonToObjectUsingObjectMapper(resourceJson, user, Resource.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.RESOURCE); - } - - private InterfaceOperationDataDefinition getInterfaceOperationForResponse(String interfaceOperationId, Map<String, InterfaceDefinition> interfaces){ - InterfaceOperationDataDefinition interfaceOperationDataDefinition = new InterfaceOperationDataDefinition(); - if(!MapUtils.isEmpty(interfaces)){ - List<Operation> operationData = InterfaceUtils.getOperationsFromInterface(interfaces); - if(CollectionUtils.isNotEmpty(operationData)){ - Optional<Operation> matchedOp = operationData.stream().filter(a -> a.getUniqueId().equals(interfaceOperationId)).findAny(); - if(matchedOp.isPresent()) { - interfaceOperationDataDefinition = InterfaceUIDataConverter.convertOperationDataToInterfaceData(matchedOp.get()); - } - } } - return interfaceOperationDataDefinition; + return InterfaceUIDataConverter.convertInterfaceDataToOperationData(interfaceOperationDataDefinition); } } |