From 1ecdbf74427467327de0b1e11cd0a839d6dc05bd Mon Sep 17 00:00:00 2001 From: priyanshu Date: Mon, 6 Aug 2018 14:31:02 +0530 Subject: Interface operations Model update 1. Interface operations Model update 2. Removed unnecessary dependencies on resource 3. Fixed the broken code after merge Change-Id: I2b6381493530f1d61f0803ee1d1d688648356f73 Issue-ID: SDC-1535 Signed-off-by: priyanshu --- .../jsontitan/operations/ArtifactsOperations.java | 2 +- .../jsontitan/operations/InterfaceOperation.java | 253 +++++++++++++++------ .../operations/TopologyTemplateOperation.java | 4 +- .../be/model/jsontitan/utils/InterfaceUtils.java | 32 +-- .../model/jsontitan/utils/InterfaceUtilsTest.java | 20 -- 5 files changed, 192 insertions(+), 119 deletions(-) (limited to 'catalog-model/src') diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ArtifactsOperations.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ArtifactsOperations.java index 009d4cac3b..3eb6a5211c 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ArtifactsOperations.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ArtifactsOperations.java @@ -153,7 +153,7 @@ public class ArtifactsOperations extends BaseOperation { } private ArtifactDataDefinition findInterfaceArtifact(String parentId, String id) { - Either, TitanOperationStatus> dataFromGraph = getDataFromGraph(parentId, EdgeLabelEnum.INTERFACE_ARTIFACTS); + Either, TitanOperationStatus> dataFromGraph = getDataFromGraph(parentId, EdgeLabelEnum.INTERFACE); if (dataFromGraph.isRight()){ log.debug("failed to fetch interfaces {} for tosca element with id {}, error {}", id, parentId ,dataFromGraph.right().value()); return null; diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfaceOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfaceOperation.java index a7587adae1..04562534a8 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfaceOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfaceOperation.java @@ -17,105 +17,226 @@ package org.openecomp.sdc.be.model.jsontitan.operations; import fj.data.Either; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.UUID; +import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; +import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; +import org.openecomp.sdc.be.model.ArtifactDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; -import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Operation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; -import org.openecomp.sdc.common.jsongraph.util.CommonUtility; +import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; import org.openecomp.sdc.common.log.wrappers.Logger; - -import java.util.*; +import org.springframework.beans.factory.annotation.Autowired; @org.springframework.stereotype.Component("interfaces-operation") public class InterfaceOperation extends BaseOperation { - private static final Logger logger = Logger.getLogger(InterfaceOperation.class.getName()); + private static final Logger logger = Logger.getLogger(InterfaceOperation.class.getName()); + @Autowired + private ArtifactCassandraDao artifactCassandraDao; - public Either, StorageOperationStatus> deleteInterface(Resource resource, - Set interfacesToDelete) { - Either, StorageOperationStatus> result = null; - Either getComponentVertex; - StorageOperationStatus status = null; + public Either addInterface(String componentId, + InterfaceDefinition interfaceDefinition) { + return addOrUpdateInterface(false, componentId, interfaceDefinition); + } - if (result == null) { - getComponentVertex = titanDao.getVertexById(resource.getUniqueId(), JsonParseFlagEnum.NoParse); - if (getComponentVertex.isRight()) { - result = Either - .right(DaoStatusConverter.convertTitanStatusToStorageStatus(getComponentVertex.right().value())); - } - } - if (result == null) { + public Either updateInterface(String componentId, + InterfaceDefinition interfaceDefinition) { + return addOrUpdateInterface(true, componentId, interfaceDefinition); + } - status = deleteToscaDataElements(resource.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS, - new ArrayList<>(interfacesToDelete)); + private Either addOrUpdateInterface( + boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDefinition) { - if (status != StorageOperationStatus.OK) { - result = Either.right(status); - } - } + StorageOperationStatus statusRes; + Either getToscaElementRes; - if (result == null) { - result = Either.left(interfacesToDelete); - } - return result; + getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getToscaElementRes.isRight()) { + TitanOperationStatus status = getToscaElementRes.right().value(); + logger.debug("Failed to get tosca element {} while adding or updating interface. Status is {}. ", componentId, status); + statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status); + return Either.right(statusRes); + } + GraphVertex componentVertex = getToscaElementRes.left().value(); + if (!isUpdateAction) { + interfaceDefinition.setUniqueId(UUID.randomUUID().toString()); + } + statusRes = performUpdateToscaAction(isUpdateAction, componentVertex, Arrays.asList(interfaceDefinition), EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE); + if (!statusRes.equals(StorageOperationStatus.OK)) { + logger.debug("Failed to add or update interface of component {}. status is {}", componentId, statusRes); + return Either.right(statusRes); + } + return Either.left(interfaceDefinition); + } + + public Either addInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) { + return addOrUpdateInterfaceOperation(false, componentId, interfaceDef, interfaceOperation); + } + + public Either updateInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) { + return addOrUpdateInterfaceOperation(true, componentId, interfaceDef, interfaceOperation); + } + + private Either addOrUpdateInterfaceOperation(boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDef, Operation operation) { + + StorageOperationStatus statusRes; + Either getToscaElementRes; + Either getToscaElementInt; + + getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getToscaElementRes.isRight()) { + TitanOperationStatus status = getToscaElementRes.right().value(); + logger.debug("Failed to get tosca element {} while adding or updating operation. Status is {}. ", componentId, status); + statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status); + return Either.right(statusRes); + } + GraphVertex componentVertex = getToscaElementRes.left().value(); + getToscaElementInt = titanDao.getChildVertex(componentVertex, EdgeLabelEnum.INTERFACE, JsonParseFlagEnum.NoParse); + if (getToscaElementInt.isRight()) { + TitanOperationStatus status = getToscaElementInt.right().value(); + logger.debug("Failed to get tosca element {} while adding or updating operation. Status is {}. ", interfaceDef.getUniqueId(), status); + statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status); + return Either.right(statusRes); + } + GraphVertex interfaceVertex = getToscaElementInt.left().value(); + if (!isUpdateAction) { + initNewOperation(operation); } - public Either addInterface(String resourceId, - InterfaceDefinition interfaceDefinition) { - return addOrUpdateInterface(false, resourceId, interfaceDefinition); + statusRes = performUpdateToscaAction(isUpdateAction, interfaceVertex, Arrays.asList(operation), + EdgeLabelEnum.INTERFACE_OPERATION, VertexTypeEnum.INTERFACE_OPERATION); + if (!statusRes.equals(StorageOperationStatus.OK)) { + logger.debug("Failed to add or update operation of interface {}. status is {}", interfaceDef.getUniqueId(), statusRes); + return Either.right(statusRes); } - public Either updateInterface(String resourceId, - InterfaceDefinition interfaceDefinition) { - return addOrUpdateInterface(true, resourceId, interfaceDefinition); + getUpdatedInterfaceDef(interfaceDef, operation, operation.getUniqueId()); + Either intUpdateStatus = updateInterface(componentId, interfaceDef); + if (intUpdateStatus.isRight() && !intUpdateStatus.right().value().equals(StorageOperationStatus.OK)) { + logger.debug("Failed to update interface details on component {}. status is {}", componentId, statusRes); + return Either.right(statusRes); } - private Either addOrUpdateInterface( - boolean isUpdateAction, String resourceId, InterfaceDefinition interfaceDefinition) { + return Either.left(operation); + } - StorageOperationStatus statusRes; - Either getToscaElementRes; + public Either deleteInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, String operationToDelete) { + Either getInterfaceVertex; + Either getComponentVertex; + Operation operation = new Operation(); + StorageOperationStatus status = null; - getToscaElementRes = titanDao.getVertexById(resourceId, JsonParseFlagEnum.NoParse); - if (getToscaElementRes.isRight()) { - TitanOperationStatus status = getToscaElementRes.right().value(); - CommonUtility.addRecordToLog(logger, CommonUtility.LogLevelEnum.DEBUG, - "Failed to get tosca element {} upon adding the properties. Status is {}. ", resourceId, status); - statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status); - return Either.right(statusRes); - } - GraphVertex resourceVertex = getToscaElementRes.left().value(); - if (!isUpdateAction) { - interfaceDefinition.setUniqueId(UUID.randomUUID().toString()); + getComponentVertex = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getComponentVertex.isRight()) { + return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getComponentVertex.right().value())); + } + + getInterfaceVertex = titanDao.getChildVertex(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, JsonParseFlagEnum.NoParse); + if (getInterfaceVertex.isRight()) { + return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getInterfaceVertex.right().value())); + } + + Optional> operationToRemove = interfaceDef.getOperationsMap().entrySet().stream() + .filter(entry -> entry.getValue().getUniqueId().equals(operationToDelete)).findAny(); + if (operationToRemove.isPresent()){ + Map.Entry stringOperationEntry = operationToRemove.get(); + operation = stringOperationEntry.getValue(); + ArtifactDefinition implementationArtifact = operation.getImplementationArtifact(); + String artifactUUID = implementationArtifact.getArtifactUUID(); + CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(artifactUUID); + if (cassandraStatus != CassandraOperationStatus.OK) { + logger.debug("Failed to delete the artifact {} from the database. ", artifactUUID); + return Either.right(DaoStatusConverter.convertCassandraStatusToStorageStatus(cassandraStatus)); + } + + if(interfaceDef.getOperationsMap().size() > 1){ + status = deleteToscaDataElements(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, Arrays.asList(operationToDelete)); + if (status != StorageOperationStatus.OK) { + return Either.right(status); } - statusRes = performUpdateToscaAction(isUpdateAction, resourceVertex, Arrays.asList(interfaceDefinition), - JsonPresentationFields.INTERFACE); - if (!statusRes.equals(StorageOperationStatus.OK)) { - logger.error("Failed to find the parent capability of capability type {}. status is {}", resourceId, - statusRes); - return Either.right(statusRes); + } else { + status = removeToscaDataVertex(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, VertexTypeEnum.INTERFACE_OPERATION); + if (status != StorageOperationStatus.OK) { + return Either.right(status); } - return Either.left(interfaceDefinition); - } + } - private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex, - List toscaDataList, JsonPresentationFields mapKeyField) { - if (isUpdate) { - return updateToscaDataOfToscaElement(graphVertex, EdgeLabelEnum.INTERFACE_ARTIFACTS, - VertexTypeEnum.INTERFACE_ARTIFACTS, toscaDataList, JsonPresentationFields.UNIQUE_ID); - } else { - return addToscaDataToToscaElement(graphVertex, EdgeLabelEnum.INTERFACE_ARTIFACTS, - VertexTypeEnum.INTERFACE_ARTIFACTS, toscaDataList, JsonPresentationFields.UNIQUE_ID); + getUpdatedInterfaceDef(interfaceDef, null, operationToDelete); + if (interfaceDef.getOperations().isEmpty()) { + status = removeToscaDataVertex(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE); + if (status != StorageOperationStatus.OK) { + return Either.right(status); } + } + else { + Either intUpdateStatus = updateInterface(componentId, interfaceDef); + if (intUpdateStatus.isRight() && !intUpdateStatus.right().value().equals(StorageOperationStatus.OK)) { + return Either.right(status); + } + } } - + return Either.left(operation); + } + + public Either getInterfaceOperation(InterfaceDefinition interfaceDef, String operationToGet) { + Operation operation = new Operation(); + Optional> operationToFetch = interfaceDef.getOperationsMap().entrySet().stream() + .filter(entry -> entry.getValue().getUniqueId().equals(operationToGet)).findAny(); + if (operationToFetch.isPresent()){ + Map.Entry stringOperationEntry = operationToFetch.get(); + operation = stringOperationEntry.getValue(); + } + return Either.left(operation); + } + + private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex, + List toscaDataList, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) { + if (isUpdate) { + return updateToscaDataOfToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID); + } else { + return addToscaDataToToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID); + } + } + + private void initNewOperation(Operation operation){ + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + String artifactUUID = UUID.randomUUID().toString(); + artifactDefinition.setArtifactUUID(artifactUUID); + artifactDefinition.setUniqueId(artifactUUID); + artifactDefinition.setArtifactType(ArtifactTypeEnum.PLAN.getType()); + artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.LIFE_CYCLE); + operation.setImplementation(artifactDefinition); + operation.setUniqueId(UUID.randomUUID().toString()); + } + + private InterfaceDefinition getUpdatedInterfaceDef(InterfaceDefinition interfaceDef, Operation operation, String operationId){ + Map operationMap = interfaceDef.getOperationsMap(); + if(operation != null && !operation.isEmpty()){ + operationMap.put(operationId, operation); + interfaceDef.setOperationsMap(operationMap); + } + else { + operationMap.remove(operationId); + interfaceDef.setOperationsMap(operationMap); + } + return interfaceDef; + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperation.java index e01e02397f..0f124fd2eb 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperation.java @@ -711,7 +711,7 @@ public class TopologyTemplateOperation extends ToscaElementOperation { } private TitanOperationStatus setInterfcesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) { - Either, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INTERFACE_ARTIFACTS); + Either, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INTERFACE); if (result.isLeft()) { topologyTemplate.setInterfaces(result.left().value()); } else { @@ -730,7 +730,7 @@ public class TopologyTemplateOperation extends ToscaElementOperation { public StorageOperationStatus associateInterfacesToComponent(GraphVertex nodeTypeVertex, Map interfaceMap) { if (interfaceMap != null && !interfaceMap.isEmpty()) { - Either assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INTERFACE_ARTIFACTS, EdgeLabelEnum.INTERFACE_ARTIFACTS, interfaceMap); + Either assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INTERFACE, EdgeLabelEnum.INTERFACE, interfaceMap); if (assosiateElementToData.isRight()) { return assosiateElementToData.right().value(); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtils.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtils.java index a350d7ea17..249de97d86 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtils.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtils.java @@ -65,27 +65,7 @@ public class InterfaceUtils { } } - public static Map getInterfaceOperationsFromInterfaces( - Map interfaces, - Resource resource) throws IllegalStateException { - if (MapUtils.isEmpty(interfaces)) { - return Collections.EMPTY_MAP; - } - Optional optionalInterface = getInterfaceDefinitionFromToscaName( - interfaces.values(), resource.getName()); - if (!optionalInterface.isPresent()) { - return Collections.EMPTY_MAP; - } - InterfaceDefinition interfaceDefinition = optionalInterface.get(); - interfaceDefinition.getOperationsMap().values().stream() - .forEach(operation -> createInputOutput(operation, resource.getInputs())); - - - return interfaceDefinition.getOperationsMap(); - - } - - private static void createInputOutput(Operation operation, List inputs) throws IllegalStateException { + public static void createInputOutput(Operation operation, List inputs) throws IllegalStateException { ListDataDefinition inputDefinitionListDataDefinition = operation.getInputs(); if (inputDefinitionListDataDefinition != null) { return; @@ -130,13 +110,5 @@ public class InterfaceUtils { } throw new IllegalStateException("Could not find output :"+ output.getLabel()); } - public static List getOperationsFromInterface(Map interfaces) { - List operationData = new ArrayList<>(); - if (!MapUtils.isEmpty(interfaces)) { - operationData = interfaces.values().stream() - .filter(a -> MapUtils.isNotEmpty(a.getOperationsMap())) - .map(a-> new ArrayList<>(a.getOperationsMap().values())).flatMap(List::stream).collect(Collectors.toList()); - } - return operationData; - } + } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtilsTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtilsTest.java index 9d44232cd0..fbf01bc91d 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtilsTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtilsTest.java @@ -47,24 +47,4 @@ public class InterfaceUtilsTest { // default test result = InterfaceUtils.createInterfaceToscaResourceName(resourceName); } - - - @Test - public void testGetInterfaceOperationsFromInterfaces() throws Exception { - Map interfaces = null; - Resource resource = null; - Map result; - - // default test - result = InterfaceUtils.getInterfaceOperationsFromInterfaces(interfaces, resource); - } - - @Test - public void testGetOperationsFromInterface() throws Exception { - Map interfaces = null; - List result; - - // default test - result = InterfaceUtils.getOperationsFromInterface(interfaces); - } } \ No newline at end of file -- cgit 1.2.3-korg