From 63a216338c392a587eb88f9fa89010495f79e03c Mon Sep 17 00:00:00 2001 From: priyanshu Date: Thu, 27 Dec 2018 14:50:30 +0530 Subject: Revert "Interface operation feature enhancements" This reverts commit 01f825bc Change-Id: I9e41da46a410be335283a27758cb1e77a087046f Issue-ID: SDC-1999 Signed-off-by: priyanshu --- .../jsontitan/operations/InterfaceOperation.java | 224 ++++++++-- .../operations/TopologyTemplateOperation.java | 36 +- .../be/model/jsontitan/utils/InterfaceUtils.java | 62 +++ .../api/IInterfaceLifecycleOperation.java | 2 +- .../sdc/be/ui/model/UiComponentDataTransfer.java | 11 +- .../org/openecomp/sdc/be/model/ResourceTest.java | 1 + .../operations/InterfaceOperationTest.java | 469 -------------------- .../operations/InterfacesOperationTest.java | 492 +++++++++++++++++++++ .../model/jsontitan/utils/InterfaceUtilsTest.java | 50 +++ 9 files changed, 829 insertions(+), 518 deletions(-) create mode 100644 catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtils.java delete mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfaceOperationTest.java create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfacesOperationTest.java create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtilsTest.java (limited to 'catalog-model') 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 4aec886092..f9f2ce9b35 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,75 +17,223 @@ package org.openecomp.sdc.be.model.jsontitan.operations; import fj.data.Either; +import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; +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.elements.InterfaceDataDefinition; 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.Operation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; +import org.springframework.beans.factory.annotation.Autowired; @org.springframework.stereotype.Component("interfaces-operation") public class InterfaceOperation extends BaseOperation { - public Either, StorageOperationStatus> addInterfaces(String componentId, List interfaceDefinitions) { - return addOrUpdateInterfaces(false, componentId, interfaceDefinitions); - } + @Autowired + private ArtifactCassandraDao artifactCassandraDao; + + public Either addInterface(String componentId, + InterfaceDefinition interfaceDefinition) { + return addOrUpdateInterface(false, componentId, interfaceDefinition); + } + + public Either updateInterface(String componentId, + InterfaceDefinition interfaceDefinition) { + return addOrUpdateInterface(true, componentId, interfaceDefinition); + } + + private Either addOrUpdateInterface( + boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDefinition) { - public Either, StorageOperationStatus> updateInterfaces(String componentId, List interfaceDefinitions) { - return addOrUpdateInterfaces(true, componentId, interfaceDefinitions); + StorageOperationStatus statusRes; + Either getToscaElementRes; + + getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getToscaElementRes.isRight()) { + TitanOperationStatus status = getToscaElementRes.right().value(); + statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status); + return Either.right(statusRes); + } + GraphVertex componentVertex = getToscaElementRes.left().value(); + if (!isUpdateAction) { + interfaceDefinition.setUniqueId(UUID.randomUUID().toString()); } + statusRes = performUpdateToscaAction(isUpdateAction, componentVertex, + Collections.singletonList(interfaceDefinition), EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE); + if (!statusRes.equals(StorageOperationStatus.OK)) { + return Either.right(statusRes); + } + return Either.left(interfaceDefinition); + } - private Either, StorageOperationStatus> addOrUpdateInterfaces(boolean isUpdateAction, String componentId, List interfaceDefinitions) { + public Either addInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) { + return addOrUpdateInterfaceOperation(false, componentId, interfaceDef, interfaceOperation); + } - StorageOperationStatus statusRes; - Either getToscaElementRes; + public Either updateInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) { + return addOrUpdateInterfaceOperation(true, componentId, interfaceDef, interfaceOperation); + } - getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); - if (getToscaElementRes.isRight()) { - return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getToscaElementRes.right().value())); - } - GraphVertex componentVertex = getToscaElementRes.left().value(); + private Either addOrUpdateInterfaceOperation(boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDef, Operation operation) { - List interfaceDataDefinitions = interfaceDefinitions.stream().map(interfaceDefinition -> new InterfaceDataDefinition(interfaceDefinition)).collect(Collectors.toList()); - statusRes = performUpdateToscaAction(isUpdateAction, componentVertex, interfaceDataDefinitions, EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE); - if (!statusRes.equals(StorageOperationStatus.OK)) { - return Either.right(statusRes); + StorageOperationStatus statusRes; + Either getToscaElementRes; + Either getToscaElementInt; + + if(isUpdateAction && operation.getImplementationArtifact() != null){ + String artifactUUID = operation.getImplementationArtifact().getArtifactUUID(); + Either artifactCount = artifactCassandraDao.getCountOfArtifactById(artifactUUID); + if(artifactCount.isLeft()){ + CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(artifactUUID); + if (cassandraStatus != CassandraOperationStatus.OK) { + return Either.right(DaoStatusConverter.convertCassandraStatusToStorageStatus(cassandraStatus)); } - return Either.left(interfaceDefinitions); + } + } + + getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getToscaElementRes.isRight()) { + TitanOperationStatus status = getToscaElementRes.right().value(); + 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(); + statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status); + return Either.right(statusRes); + } + GraphVertex interfaceVertex = getToscaElementInt.left().value(); + + statusRes = performUpdateToscaAction(isUpdateAction, interfaceVertex, + Collections.singletonList(operation), EdgeLabelEnum.INTERFACE_OPERATION, VertexTypeEnum.INTERFACE_OPERATION); + if (!statusRes.equals(StorageOperationStatus.OK)) { + return Either.right(statusRes); + } + + getUpdatedInterfaceDef(interfaceDef, operation, operation.getUniqueId()); + Either intUpdateStatus = updateInterface(componentId, interfaceDef); + if (intUpdateStatus.isRight() && !intUpdateStatus.right().value().equals(StorageOperationStatus.OK)) { + return Either.right(statusRes); } - public Either deleteInterface(String componentId, String interfacesToDelete) { - StorageOperationStatus statusRes; - Either getToscaElementRes; + return Either.left(operation); + } - getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); - if (getToscaElementRes.isRight()) { - return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getToscaElementRes.right().value())); + public Either deleteInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, String operationToDelete) { + Either getInterfaceVertex; + Either getComponentVertex; + Operation operation = new Operation(); + StorageOperationStatus status; + + 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())); + } + + if (!interfaceDef.getOperationsMap().isEmpty()) { + Either getInterfaceOpVertex = + titanDao.getChildVertex(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, + JsonParseFlagEnum.NoParse); + if (getInterfaceOpVertex.isRight()) { + List toscaDataList = new ArrayList<>(interfaceDef.getOperationsMap().values()); + StorageOperationStatus statusRes = + addToscaDataToToscaElement(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, + VertexTypeEnum.INTERFACE_OPERATION, toscaDataList, JsonPresentationFields.UNIQUE_ID); + if (!statusRes.equals(StorageOperationStatus.OK)) { + return Either.right(statusRes); + } + } + } + + 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(); + if(implementationArtifact != null){ + String artifactUUID = implementationArtifact.getArtifactUUID(); + CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(artifactUUID); + if (cassandraStatus != CassandraOperationStatus.OK) { + return Either.right(DaoStatusConverter.convertCassandraStatusToStorageStatus(cassandraStatus)); + } + } + + if(interfaceDef.getOperationsMap().size() > 1){ + status = deleteToscaDataElements(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, Collections.singletonList(operationToDelete)); + if (status != StorageOperationStatus.OK) { + return Either.right(status); } + } else { + status = removeToscaDataVertex(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, VertexTypeEnum.INTERFACE_OPERATION); + if (status != StorageOperationStatus.OK) { + return Either.right(status); + } + } - statusRes = deleteToscaDataElements(componentId, EdgeLabelEnum.INTERFACE, Collections.singletonList(interfacesToDelete)); - if (statusRes != StorageOperationStatus.OK) { - return Either.right(statusRes); + getUpdatedInterfaceDef(interfaceDef, null, operationToDelete); + if (interfaceDef.getOperations().isEmpty()) { + status = deleteToscaDataElements(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, Collections.singletonList(interfaceDef.getUniqueId())); + if (status != StorageOperationStatus.OK) { + return Either.right(status); + } + 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); + } - return Either.left(interfacesToDelete); + 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 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 getUpdatedInterfaceDef(InterfaceDefinition interfaceDef, Operation operation, String operationId){ + Map operationMap = interfaceDef.getOperationsMap(); + if(operation != null){ + operationMap.put(operationId, operation); + interfaceDef.setOperationsMap(operationMap); + } + else { + operationMap.remove(operationId); + interfaceDef.setOperationsMap(operationMap); } + } + +} + -} \ No newline at end of file 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 3bdec2a30a..9a87874b2a 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 @@ -44,6 +44,7 @@ import org.openecomp.sdc.be.model.category.CategoryDefinition; import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate; import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement; import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum; +import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum; import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter; import org.openecomp.sdc.be.model.operations.StorageException; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; @@ -728,6 +729,15 @@ public class TopologyTemplateOperation extends ToscaElementOperation { if (assosiateElementToData.isRight()) { return assosiateElementToData.right().value(); } + else { + Map operationMap = interfaceMap.values().stream().filter(op -> MapUtils.isNotEmpty(op.getOperations())).flatMap(a -> a.getOperations().entrySet().stream()).collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue())); + if(MapUtils.isNotEmpty(operationMap)) { + Either assosiateOpToInterface = associateElementToData(assosiateElementToData.left().value(), VertexTypeEnum.INTERFACE_OPERATION, EdgeLabelEnum.INTERFACE_OPERATION, operationMap); + if (assosiateOpToInterface.isRight()) { + return assosiateOpToInterface.right().value(); + } + } + } } return StorageOperationStatus.OK; } @@ -1052,17 +1062,25 @@ public class TopologyTemplateOperation extends ToscaElementOperation { log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status); Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); } - status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INTERFACE); - if (status != TitanOperationStatus.OK) { - log.debug("Failed to disassociate interfaces for {} error {}", toscaElementVertex.getUniqueId(), status); - Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); - } - titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS); - if (status != TitanOperationStatus.OK) { - log.debug("Failed to disassociate instance artifact for {} error {}", toscaElementVertex.getUniqueId(), status); - Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); + + Either getInterfaceVertex = titanDao.getChildVertex(toscaElementVertex, EdgeLabelEnum.INTERFACE, JsonParseFlagEnum.NoParse); + if (getInterfaceVertex.isLeft()) { + status = titanDao.disassociateAndDeleteLast(getInterfaceVertex.left().value(), Direction.OUT, EdgeLabelEnum.INTERFACE_OPERATION); + if (status != TitanOperationStatus.OK) { + log.debug("Failed to disassociate interface operations for {} error {}", getInterfaceVertex.left().value().getUniqueId(), status); + Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); + } + else { + status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INTERFACE); + if (status != TitanOperationStatus.OK) { + log.debug("Failed to disassociate interfaces for {} error {}", toscaElementVertex.getUniqueId(), status); + Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); + } + } + } + titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS); toscaElementVertex.getVertex().remove(); log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId()); 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 new file mode 100644 index 0000000000..feef31cbc1 --- /dev/null +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtils.java @@ -0,0 +1,62 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openecomp.sdc.be.model.jsontitan.utils; + +import org.apache.commons.collections.CollectionUtils; +import org.openecomp.sdc.be.model.InterfaceDefinition; + +import java.util.Collection; +import java.util.Formatter; +import java.util.Optional; +import java.util.stream.Collectors; +import org.openecomp.sdc.common.util.ValidationUtils; + +public class InterfaceUtils { + + public static final String INTERFACE_TOSCA_RESOURCE_NAME = "org.openecomp.interfaces.node.lifecycle.%s"; + + public static final Optional getInterfaceDefinitionFromToscaName( + Collection interfaces, + String componentName) { + if (CollectionUtils.isEmpty(interfaces)) { + return Optional.empty(); + } + + String toscaName = createInterfaceToscaResourceName(componentName); + return interfaces.stream().filter( + interfaceDefinition -> interfaceDefinition.getToscaResourceName() != null && interfaceDefinition + .getToscaResourceName().equals(toscaName)).findAny(); + } + + public static Collection getInterfaceDefinitionListFromToscaName(Collection interfaces, + String componentName) { + if (CollectionUtils.isEmpty(interfaces)) { + return CollectionUtils.EMPTY_COLLECTION; + } + + String toscaName = createInterfaceToscaResourceName(componentName); + return interfaces.stream().filter( + interfaceDefinition -> interfaceDefinition.getToscaResourceName() != null && interfaceDefinition + .getToscaResourceName().equals(toscaName)).collect(Collectors.toList()); + } + + public static String createInterfaceToscaResourceName(String componentName) { + StringBuilder sb = new StringBuilder(); + try (Formatter formatter = new Formatter(sb)) { + return formatter.format(INTERFACE_TOSCA_RESOURCE_NAME, ValidationUtils.convertToSystemName(componentName)).toString(); + } + } +} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/IInterfaceLifecycleOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/IInterfaceLifecycleOperation.java index 2f12226d95..06622ebba6 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/IInterfaceLifecycleOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/IInterfaceLifecycleOperation.java @@ -51,5 +51,5 @@ public interface IInterfaceLifecycleOperation { public String getShortInterfaceName(InterfaceDataDefinition interfaceDefinition); - public Either,StorageOperationStatus> getAllInterfaceLifecycleTypes(); + Either,StorageOperationStatus> getAllInterfaceLifecycleTypes(); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/ui/model/UiComponentDataTransfer.java b/catalog-model/src/main/java/org/openecomp/sdc/be/ui/model/UiComponentDataTransfer.java index 43df6da2e6..0090f86f0e 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/ui/model/UiComponentDataTransfer.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/ui/model/UiComponentDataTransfer.java @@ -20,7 +20,7 @@ package org.openecomp.sdc.be.ui.model; - +import org.openecomp.sdc.be.datatypes.elements.InterfaceOperationDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.model.AdditionalInformationDefinition; import org.openecomp.sdc.be.model.ArtifactDefinition; @@ -78,8 +78,17 @@ public class UiComponentDataTransfer { protected List additionalInformation; + private Map interfaceOperations; private Map interfaces; + public Map getInterfaceOperations() { + return interfaceOperations; + } + + public void setInterfaceOperations(Map interfaceOperations) { + this.interfaceOperations = interfaceOperations; + } + public Map getInterfaces() { return interfaces; } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/ResourceTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/ResourceTest.java index 0d5614f176..2ecb4b444c 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/ResourceTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/ResourceTest.java @@ -3,6 +3,7 @@ package org.openecomp.sdc.be.model; import mockit.Deencapsulation; import org.junit.Assert; import org.junit.Test; +import org.openecomp.sdc.be.datatypes.elements.InterfaceOperationDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.unittests.utils.ModelConfDependentTest; diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfaceOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfaceOperationTest.java deleted file mode 100644 index f5835b6c4c..0000000000 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfaceOperationTest.java +++ /dev/null @@ -1,469 +0,0 @@ -package org.openecomp.sdc.be.model.jsontitan.operations; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; - -import fj.data.Either; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.annotation.Resource; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.jsongraph.TitanDao; -import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; -import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; -import org.openecomp.sdc.be.datatypes.elements.MapDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; -import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; -import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; -import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; -import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.InterfaceDefinition; -import org.openecomp.sdc.be.model.LifecycleStateEnum; -import org.openecomp.sdc.be.model.ModelTestBase; -import org.openecomp.sdc.be.model.Operation; -import org.openecomp.sdc.be.model.Service; -import org.openecomp.sdc.be.model.category.CategoryDefinition; -import org.openecomp.sdc.be.model.category.SubCategoryDefinition; -import org.openecomp.sdc.be.model.jsontitan.datamodel.NodeType; -import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate; -import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; -import org.openecomp.sdc.common.util.ValidationUtils; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("classpath:application-context-test.xml") -public class InterfaceOperationTest extends ModelTestBase { - @Resource - protected TitanDao titanDao; - - @Resource - private InterfaceOperation interfaceOperation; - - @Resource - protected NodeTypeOperation nodeTypeOperation; - - @Resource - protected TopologyTemplateOperation topologyTemplateOperation; - - @Resource - private ToscaElementLifecycleOperation lifecycleOperation; - - private static final String RESOURCE_NAME = "Resource Name"; - private static final String RESOURCE_ID = "resourceID"; - - private static final String SERVICE_NAME = "Service Name"; - private static final String SERVICE_ID = "serviceID"; - - private final String categoryName = "category"; - private final String subcategory = "mycategory"; - - private GraphVertex ownerVertex; - - private final Service service = createService(); - private final org.openecomp.sdc.be.model.Resource resource = createResource(); - - @BeforeClass - public static void initInterfacesOperation() { - init(); - } - - @Before - public void setupBefore() { - GraphTestUtils.clearGraph(titanDao); - createUsers(); - createResourceCategory(); - createServiceCategory(); - GraphTestUtils.createRootCatalogVertex(titanDao); - createRootNodeType(); - createNodeType("resource", RESOURCE_ID); - createNodeType("service", SERVICE_ID); - createTopologyTemplate("firstService"); - } - - @After - public void cleanAfter() { - GraphTestUtils.clearGraph(titanDao); - } - - @Test - public void testAddInterface_Service(){testAddSingleInterface(service);} - - @Test - public void testAddInterface_Resource(){testAddMultipleInterface(resource);} - - @Test - public void testUpdateInterface_Service(){testUpdateInterface(service);} - - @Test - public void testUpdateInterface_Resource(){testUpdateInterface(resource);} - - @Test - public void testDeleteInterface_Service(){testDeleteInterface(service);} - - @Test - public void testDeleteInterface_Resource(){testDeleteInterface(resource);} - - @Test - public void testUpdateInterfaceShouldFailWhenNOtCreatedFirst() { - Component component = createResource(); - InterfaceDefinition interfaceDefinition = buildInterfaceDefinitionWithoutOperation(); - interfaceDefinition.setOperationsMap(createMockOperationMap()); - Either, StorageOperationStatus> res = interfaceOperation.updateInterfaces(component.getUniqueId(), Collections.singletonList(interfaceDefinition)); - Assert.assertTrue(res.isRight()); - } - - private void testAddSingleInterface(Component component) { - InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1"); - Either, StorageOperationStatus> res = interfaceOperation.addInterfaces(component.getUniqueId(), Collections.singletonList(interfaceDefinition)); - Assert.assertTrue(res.isLeft()); - Assert.assertEquals("1", res.left().value().get(0).getUniqueId()); - } - - private void testAddMultipleInterface(Component component) { - InterfaceDefinition interfaceDefinition1 = buildInterfaceDefinition("1"); - InterfaceDefinition interfaceDefinition2 = buildInterfaceDefinition("2"); - List interfaceDefinitions = new ArrayList<>(); - interfaceDefinitions.add(interfaceDefinition1); - interfaceDefinitions.add(interfaceDefinition2); - Either, StorageOperationStatus> res = interfaceOperation.addInterfaces(component.getUniqueId(), interfaceDefinitions); - Assert.assertTrue(res.isLeft()); - Assert.assertEquals(2, res.left().value().size()); - } - - private void testUpdateInterface(Component component) { - InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1"); - Either, StorageOperationStatus> res = interfaceOperation.addInterfaces(component.getUniqueId(), Collections.singletonList(interfaceDefinition)); - Assert.assertTrue(res.isLeft()); - List value = res.left().value(); - InterfaceDefinition createdInterfaceDef = value.get(0); - String new_description = "New Description"; - createdInterfaceDef.setDescription(new_description); - res = interfaceOperation.updateInterfaces(component.getUniqueId(), Collections.singletonList(createdInterfaceDef)); - assertTrue(res.isLeft()); - assertEquals(new_description,res.left().value().get(0).getDescription()); - } - - private void testDeleteInterface(Component component) { - InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1"); - Either, StorageOperationStatus> res = interfaceOperation.addInterfaces(component.getUniqueId(), Collections.singletonList(interfaceDefinition)); - Assert.assertTrue(res.isLeft()); - List value = res.left().value(); - Either deleteInterfaceOperationRes = interfaceOperation.deleteInterface(component.getUniqueId(), value.get(0).getUniqueId()); - assertTrue(deleteInterfaceOperationRes.isLeft()); - } - - private InterfaceDefinition buildInterfaceDefinition(String uniqueId) { - InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); - interfaceDefinition.setType("tosca.interfaces.standard"); - interfaceDefinition.setUniqueId(uniqueId); - interfaceDefinition.setOperationsMap(createMockOperationMap()); - return interfaceDefinition; - } - - private InterfaceDefinition buildInterfaceDefinitionWithoutOperation() { - InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); - interfaceDefinition.setType("tosca.interfaces.standard"); - return interfaceDefinition; - } - - private org.openecomp.sdc.be.model.Resource createResource() { - org.openecomp.sdc.be.model.Resource resource = new org.openecomp.sdc.be.model.Resource(); - resource.setUniqueId(RESOURCE_ID); - resource.setName(RESOURCE_NAME); - resource.setDescription("My short description"); - resource.setInterfaces(createMockInterfaceDefinition()); - return resource; - } - - private Service createService() { - Service service = new Service(); - service.setUniqueId(SERVICE_ID); - service.setName(SERVICE_NAME); - service.setDescription("My short description"); - service.setInterfaces(createMockInterfaceDefinition()); - return service; - } - - private InterfaceDefinition createInterface(String uniqueID, String description, String type, String toscaResourceName, - Map op) { - InterfaceDefinition id = new InterfaceDefinition(); - id.setType(type); - id.setDescription(description); - id.setUniqueId(uniqueID); - id.setToscaResourceName(toscaResourceName); - id.setOperationsMap(op); - return id; - } - - private Map createMockInterfaceDefinition() { - Map operationMap = createMockOperationMap(); - Map interfaceDefinitionMap = new HashMap<>(); - interfaceDefinitionMap.put("int1", createInterface("int1", "Interface 1", - "lifecycle", "tosca", operationMap)); - return interfaceDefinitionMap; - } - - private Map createMockOperationMap() { - Map operationMap = new HashMap<>(); - operationMap.put("op1", createMockOperation()); - return operationMap; - } - - private Operation createMockOperation() { - Operation operation = new Operation(); - operation.setDefinition(false); - operation.setName("create"); - operation.setUniqueId("op1"); - return operation; - } - - private void createResourceCategory() { - GraphVertex cat = new GraphVertex(VertexTypeEnum.RESOURCE_CATEGORY); - Map metadataProperties = new HashMap<>(); - String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.RESOURCE_CATEGORY); - cat.setUniqueId(catId); - metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId); - metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_CATEGORY.getName()); - metadataProperties.put(GraphPropertyEnum.NAME, categoryName); - metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName)); - cat.setMetadataProperties(metadataProperties); - cat.updateMetadataJsonWithCurrentMetadataProperties(); - - GraphVertex subCat = new GraphVertex(VertexTypeEnum.RESOURCE_SUBCATEGORY); - metadataProperties = new HashMap<>(); - String subCatId = UniqueIdBuilder.buildSubCategoryUid(cat.getUniqueId(), subcategory); - subCat.setUniqueId(subCatId); - metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, subCatId); - metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_SUBCATEGORY.getName()); - metadataProperties.put(GraphPropertyEnum.NAME, subcategory); - metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(subcategory)); - subCat.setMetadataProperties(metadataProperties); - subCat.updateMetadataJsonWithCurrentMetadataProperties(); - - Either catRes = titanDao.createVertex(cat); - Either subCatRes = titanDao.createVertex(subCat); - titanDao.createEdge(catRes.left().value().getVertex(), subCatRes.left().value().getVertex(), EdgeLabelEnum.SUB_CATEGORY, new HashMap<>()); - } - - private void createServiceCategory() { - GraphVertex cat = new GraphVertex(VertexTypeEnum.SERVICE_CATEGORY); - Map metadataProperties = new HashMap<>(); - String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.SERVICE_CATEGORY); - cat.setUniqueId(catId); - metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId); - metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.SERVICE_CATEGORY.getName()); - metadataProperties.put(GraphPropertyEnum.NAME, categoryName); - metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName)); - cat.setMetadataProperties(metadataProperties); - cat.updateMetadataJsonWithCurrentMetadataProperties(); - titanDao.createVertex(cat); - } - - private void createTopologyTemplate(String name) { - TopologyTemplate service = new TopologyTemplate(); - String uniqueId = UniqueIdBuilder.buildResourceUniqueId(); - service.setUniqueId(uniqueId); - service.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID)); - service.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), name); - service.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId); - service.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1"); - service.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name()); - service.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE); - List categories = new ArrayList<>(); - CategoryDefinition cat = new CategoryDefinition(); - categories.add(cat); - cat.setName(categoryName); - service.setCategories(categories); - - service.setComponentType(ComponentTypeEnum.SERVICE); - Either createRes = topologyTemplateOperation.createTopologyTemplate(service); - Either getNodeTyeRes = titanDao.getVertexById(createRes.left().value().getUniqueId()); - - getNodeTyeRes.left().value(); - } - - private void createNodeType(String nodeTypeName, String uniqueId) { - NodeType vf = new NodeType(); - vf.setUniqueId(uniqueId); - vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID)); - vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), nodeTypeName); - vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId); - vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1"); - vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name()); - vf.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE); - List categories = new ArrayList<>(); - CategoryDefinition cat = new CategoryDefinition(); - categories.add(cat); - cat.setName(categoryName); - List subCategories = new ArrayList<>(); - SubCategoryDefinition subCat = new SubCategoryDefinition(); - subCat.setName(subcategory); - subCategories.add(subCat); - cat.setSubcategories(subCategories); - vf.setCategories(categories); - - List derivedFrom = new ArrayList<>(); - derivedFrom.add("root"); - vf.setDerivedFrom(derivedFrom); - - vf.setComponentType(ComponentTypeEnum.RESOURCE); - Either createVFRes = nodeTypeOperation.createNodeType(vf); - - Either getNodeTyeRes = titanDao.getVertexById(createVFRes.left().value().getUniqueId()); - - GraphVertex vfVertex = getNodeTyeRes.left().value(); - - List addProperties = new ArrayList<>(); - PropertyDataDefinition prop11 = new PropertyDataDefinition(); - prop11.setName("prop11"); - prop11.setDefaultValue("def11"); - - addProperties.add(prop11); - - PropertyDataDefinition prop22 = new PropertyDataDefinition(); - prop22.setName("prop22"); - prop22.setDefaultValue("def22"); - addProperties.add(prop22); - - StorageOperationStatus status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, addProperties, JsonPresentationFields.NAME); - assertSame(StorageOperationStatus.OK, status); - - PropertyDataDefinition prop33 = new PropertyDataDefinition(); - prop33.setName("prop33"); - prop33.setDefaultValue("def33"); - - status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop33, JsonPresentationFields.NAME); - assertSame(StorageOperationStatus.OK, status); - - PropertyDataDefinition prop44 = new PropertyDataDefinition(); - prop44.setName("prop44"); - prop44.setDefaultValue("def44"); - - status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop44, JsonPresentationFields.NAME); - assertSame(StorageOperationStatus.OK, status); - - PropertyDataDefinition capProp = new PropertyDataDefinition(); - capProp.setName("capProp"); - capProp.setDefaultValue("capPropDef"); - - MapDataDefinition dataToCreate = new MapPropertiesDataDefinition(); - dataToCreate.put("capProp", capProp); - - Map capProps = new HashMap<>(); - capProps.put("capName", dataToCreate); - - nodeTypeOperation.associateElementToData( - vfVertex, VertexTypeEnum.CAPABILITIES_PROPERTIES, EdgeLabelEnum.CAPABILITIES_PROPERTIES, capProps); - - List pathKeys = new ArrayList<>(); - pathKeys.add("capName"); - capProp.setDefaultValue("BBBB"); - nodeTypeOperation.updateToscaDataDeepElementOfToscaElement(vfVertex, EdgeLabelEnum.CAPABILITIES_PROPERTIES, VertexTypeEnum.CAPABILITIES_PROPERTIES, capProp, pathKeys, JsonPresentationFields.NAME); - } - - private void createRootNodeType() { - NodeType vf = new NodeType(); - String uniqueId = UniqueIdBuilder.buildResourceUniqueId(); - vf.setUniqueId(uniqueId); - vf.setComponentType(ComponentTypeEnum.RESOURCE); - vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID)); - vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), "root"); - vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId); - vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "1.0"); - vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VFC.name()); - vf.getMetadata().put(JsonPresentationFields.LIFECYCLE_STATE.getPresentation(), LifecycleStateEnum.CERTIFIED.name()); - vf.getMetadata().put(JsonPresentationFields.TOSCA_RESOURCE_NAME.getPresentation(), "root"); - vf.getMetadata().put(JsonPresentationFields.HIGHEST_VERSION.getPresentation(), true); - - List categories = new ArrayList<>(); - CategoryDefinition cat = new CategoryDefinition(); - categories.add(cat); - cat.setName(categoryName); - List subCategories = new ArrayList<>(); - SubCategoryDefinition subCat = new SubCategoryDefinition(); - subCat.setName(subcategory); - subCategories.add(subCat); - cat.setSubcategories(subCategories); - vf.setCategories(categories); - - List derivedFrom = new ArrayList<>(); - vf.setDerivedFrom(derivedFrom); - - Map properties = new HashMap<>(); - PropertyDataDefinition prop1 = new PropertyDataDefinition(); - prop1.setName("derived1"); - prop1.setDefaultValue("deriveddef1"); - properties.put("derived1", prop1); - - PropertyDataDefinition prop2 = new PropertyDataDefinition(); - prop2.setUniqueId("derived2"); - prop2.setName("deriveddef2"); - properties.put("derived2", prop2); - - PropertyDataDefinition prop3 = new PropertyDataDefinition(); - prop3.setName("derived3"); - prop3.setDefaultValue("deriveddef3"); - properties.put("derived3", prop3); - - vf.setProperties(properties); - vf.setComponentType(ComponentTypeEnum.RESOURCE); - Either createVFRes = nodeTypeOperation.createNodeType(vf); - - Either getNodeTyeRes = titanDao.getVertexById(createVFRes.left().value().getUniqueId()); - getNodeTyeRes.left().value(); - } - - private void createUsers() { - GraphVertex ownerV = new GraphVertex(VertexTypeEnum.USER); - ownerV.setUniqueId("user1"); - - Map metadataProperties = new HashMap<>(); - metadataProperties.put(GraphPropertyEnum.USERID, ownerV.getUniqueId()); - metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName()); - metadataProperties.put(GraphPropertyEnum.NAME, "user1"); - ownerV.setMetadataProperties(metadataProperties); - ownerV.updateMetadataJsonWithCurrentMetadataProperties(); - ownerV.setJson(new HashMap<>()); - Either createUserRes = titanDao.createVertex(ownerV); - - ownerVertex = createUserRes.left().value(); - - GraphVertex modifierV = new GraphVertex(VertexTypeEnum.USER); - modifierV.setUniqueId("user2"); - - metadataProperties = new HashMap<>(); - metadataProperties.put(GraphPropertyEnum.USERID, modifierV.getUniqueId()); - metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName()); - metadataProperties.put(GraphPropertyEnum.NAME, "user2"); - modifierV.setMetadataProperties(metadataProperties); - modifierV.updateMetadataJsonWithCurrentMetadataProperties(); - modifierV.setJson(new HashMap<>()); - createUserRes = titanDao.createVertex(modifierV); - createUserRes.left().value(); - - lifecycleOperation.findUser(ownerVertex.getUniqueId()); - } - - @After - public void teardown() { - GraphTestUtils.clearGraph(titanDao); - } - -} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfacesOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfacesOperationTest.java new file mode 100644 index 0000000000..375208d24e --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/InterfacesOperationTest.java @@ -0,0 +1,492 @@ +package org.openecomp.sdc.be.model.jsontitan.operations; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import fj.data.Either; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.annotation.Resource; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.MapDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; +import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.ModelTestBase; +import org.openecomp.sdc.be.model.Operation; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.category.CategoryDefinition; +import org.openecomp.sdc.be.model.category.SubCategoryDefinition; +import org.openecomp.sdc.be.model.jsontitan.datamodel.NodeType; +import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate; +import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; +import org.openecomp.sdc.common.util.ValidationUtils; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:application-context-test.xml") +public class InterfacesOperationTest extends ModelTestBase { + @Resource + protected TitanDao titanDao; + + @Resource + private InterfaceOperation interfaceOperation; + + @Resource + protected NodeTypeOperation nodeTypeOperation; + + @Resource + protected TopologyTemplateOperation topologyTemplateOperation; + + @Resource + private ToscaElementLifecycleOperation lifecycleOperation; + + private static final String RESOURCE_NAME = "Resource Name"; + private static final String RESOURCE_ID = "resourceID"; + + private static final String SERVICE_NAME = "Service Name"; + private static final String SERVICE_ID = "serviceID"; + + private final String categoryName = "category"; + private final String subcategory = "mycategory"; + + private GraphVertex ownerVertex; + + private final Service service = createService(); + private final org.openecomp.sdc.be.model.Resource resource = createResource(); + + @BeforeClass + public static void initInterfacesOperation() { + init(); + } + + @Before + public void setupBefore() { + GraphTestUtils.clearGraph(titanDao); + createUsers(); + createResourceCategory(); + createServiceCategory(); + GraphTestUtils.createRootCatalogVertex(titanDao); + createRootNodeType(); + createNodeType("resource", RESOURCE_ID); + createNodeType("service", SERVICE_ID); + createTopologyTemplate("firstService"); + } + + @After + public void cleanAfter() { + GraphTestUtils.clearGraph(titanDao); + } + + @Test + public void testAddInterface_Service(){testAddInterface(service);} + + @Test + public void testAddInterface_Resource(){testAddInterface(resource);} + + @Test + public void testUpdateInterface_Service(){testUpdateInterface(service);} + + @Test + public void testUpdateInterface_Resource(){testUpdateInterface(resource);} + + @Test + public void testAddInterfaceOperation_Service(){testAddInterfaceOperation(service);} + + @Test + public void testAddInterfaceOperation_Resource(){testAddInterfaceOperation(resource);} + + @Test + public void testUpdateInterfaceOperation_Service(){testUpdateInterfaceOperation(service);} + + @Test + public void testUpdateInterfaceOperation_Resource(){testUpdateInterfaceOperation(resource);} + + @Test + public void testDeleteInterfaceOperation_Service(){testDeleteInterfaceOperation(service);} + + @Test + public void testDeleteInterfaceOperation_Resource(){testDeleteInterfaceOperation(resource);} + + @Test + public void testUpdateInterfaceShouldFailWhenNOtCreatedFirst() { + Component component = createResource(); + InterfaceDefinition interfaceDefinition = buildInterfaceDefinition(); + interfaceDefinition.setOperationsMap(createMockOperationMap()); + Either res = interfaceOperation.updateInterface(component.getUniqueId(), + interfaceDefinition); + Assert.assertTrue(res.isRight()); + } + + private void testAddInterface(Component component) { + InterfaceDefinition interfaceDefinition = buildInterfaceDefinition(); + Either res = interfaceOperation.addInterface(component.getUniqueId(), + interfaceDefinition); + Assert.assertTrue(res.isLeft()); + } + + private void testUpdateInterface(Component component) { + InterfaceDefinition interfaceDefinition = buildInterfaceDefinition(); + interfaceDefinition.setOperationsMap(createMockOperationMap()); + Either res = interfaceOperation.addInterface(component.getUniqueId(), interfaceDefinition); + Assert.assertTrue(res.isLeft()); + InterfaceDefinition value = res.left().value(); + String new_description = "New Description"; + value.setDescription(new_description); + res = interfaceOperation.updateInterface(component.getUniqueId(), interfaceDefinition); + assertTrue(res.isLeft()); + assertEquals(new_description,res.left().value().getDescription()); + } + + private void testAddInterfaceOperation(Component component) { + InterfaceDefinition interfaceDefinition = buildInterfaceDefinition(); + Either res = interfaceOperation.addInterface(component.getUniqueId(), interfaceDefinition); + Assert.assertTrue(res.isLeft()); + InterfaceDefinition value = res.left().value(); + Operation op = createMockOperation(); + Either addInterfaceOperationRes = interfaceOperation.addInterfaceOperation(component.getUniqueId(), value, op); + assertTrue(addInterfaceOperationRes.isLeft()); + } + + private void testUpdateInterfaceOperation(Component component) { + InterfaceDefinition interfaceDefinition = buildInterfaceDefinition(); + Either res = interfaceOperation.addInterface(component.getUniqueId(), interfaceDefinition); + Assert.assertTrue(res.isLeft()); + InterfaceDefinition value = res.left().value(); + Operation op = createMockOperation(); + Either addInterfaceOperationRes = interfaceOperation.addInterfaceOperation(component.getUniqueId(), value, op); + Assert.assertTrue(addInterfaceOperationRes.isLeft()); + Operation resultOp = addInterfaceOperationRes.left().value(); + resultOp.setName("New_Create"); + Either updateInterfaceOperationRes = interfaceOperation.updateInterfaceOperation(component.getUniqueId(), value, resultOp); + assertTrue(updateInterfaceOperationRes.isLeft()); + assertEquals("New_Create", updateInterfaceOperationRes.left().value().getName()); + } + + private void testDeleteInterfaceOperation(Component component) { + InterfaceDefinition interfaceDefinition = buildInterfaceDefinition(); + Either res = interfaceOperation.addInterface(component.getUniqueId(), interfaceDefinition); + Assert.assertTrue(res.isLeft()); + InterfaceDefinition value = res.left().value(); + Operation op = createMockOperation(); + Either addInterfaceOperationRes = interfaceOperation.addInterfaceOperation(component.getUniqueId(), value, op); + Assert.assertTrue(addInterfaceOperationRes.isLeft()); + Operation resultOp = addInterfaceOperationRes.left().value(); + Either deleteInterfaceOperationRes = interfaceOperation.deleteInterfaceOperation(component.getUniqueId(), value, resultOp.getUniqueId()); + assertTrue(deleteInterfaceOperationRes.isLeft()); + } + + private InterfaceDefinition buildInterfaceDefinition() { + InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); + interfaceDefinition.setType("tosca.interfaces.standard"); + interfaceDefinition.setCreationDate(101232L); + return interfaceDefinition; + } + + private org.openecomp.sdc.be.model.Resource createResource() { + org.openecomp.sdc.be.model.Resource resource = new org.openecomp.sdc.be.model.Resource(); + resource.setUniqueId(RESOURCE_ID); + resource.setName(RESOURCE_NAME); + resource.setDescription("My short description"); + resource.setInterfaces(createMockInterfaceDefinition()); + return resource; + } + + private Service createService() { + Service service = new Service(); + service.setUniqueId(SERVICE_ID); + service.setName(SERVICE_NAME); + service.setDescription("My short description"); + service.setInterfaces(createMockInterfaceDefinition()); + return service; + } + + private InterfaceDefinition createInterface(String uniqueID, String description, String type, String toscaResourceName, + Map op) { + InterfaceDefinition id = new InterfaceDefinition(); + id.setType(type); + id.setDescription(description); + id.setUniqueId(uniqueID); + id.setToscaResourceName(toscaResourceName); + id.setOperationsMap(op); + return id; + } + + private Map createMockInterfaceDefinition() { + Map operationMap = createMockOperationMap(); + Map interfaceDefinitionMap = new HashMap<>(); + interfaceDefinitionMap.put("int1", createInterface("int1", "Interface 1", + "lifecycle", "tosca", operationMap)); + return interfaceDefinitionMap; + } + + private Map createMockOperationMap() { + Map operationMap = new HashMap<>(); + operationMap.put("op1", createMockOperation()); + return operationMap; + } + + private Operation createMockOperation() { + Operation operation = new Operation(); + operation.setDefinition(false); + operation.setName("create"); + operation.setUniqueId("op1"); + return operation; + } + + private void createResourceCategory() { + GraphVertex cat = new GraphVertex(VertexTypeEnum.RESOURCE_CATEGORY); + Map metadataProperties = new HashMap<>(); + String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.RESOURCE_CATEGORY); + cat.setUniqueId(catId); + metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId); + metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_CATEGORY.getName()); + metadataProperties.put(GraphPropertyEnum.NAME, categoryName); + metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName)); + cat.setMetadataProperties(metadataProperties); + cat.updateMetadataJsonWithCurrentMetadataProperties(); + + GraphVertex subCat = new GraphVertex(VertexTypeEnum.RESOURCE_SUBCATEGORY); + metadataProperties = new HashMap<>(); + String subCatId = UniqueIdBuilder.buildSubCategoryUid(cat.getUniqueId(), subcategory); + subCat.setUniqueId(subCatId); + metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, subCatId); + metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_SUBCATEGORY.getName()); + metadataProperties.put(GraphPropertyEnum.NAME, subcategory); + metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(subcategory)); + subCat.setMetadataProperties(metadataProperties); + subCat.updateMetadataJsonWithCurrentMetadataProperties(); + + Either catRes = titanDao.createVertex(cat); + Either subCatRes = titanDao.createVertex(subCat); + titanDao.createEdge(catRes.left().value().getVertex(), subCatRes.left().value().getVertex(), EdgeLabelEnum.SUB_CATEGORY, new HashMap<>()); + } + + private void createServiceCategory() { + GraphVertex cat = new GraphVertex(VertexTypeEnum.SERVICE_CATEGORY); + Map metadataProperties = new HashMap<>(); + String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.SERVICE_CATEGORY); + cat.setUniqueId(catId); + metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId); + metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.SERVICE_CATEGORY.getName()); + metadataProperties.put(GraphPropertyEnum.NAME, categoryName); + metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName)); + cat.setMetadataProperties(metadataProperties); + cat.updateMetadataJsonWithCurrentMetadataProperties(); + titanDao.createVertex(cat); + } + + private void createTopologyTemplate(String name) { + TopologyTemplate service = new TopologyTemplate(); + String uniqueId = UniqueIdBuilder.buildResourceUniqueId(); + service.setUniqueId(uniqueId); + service.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID)); + service.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), name); + service.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId); + service.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1"); + service.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name()); + service.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE); + List categories = new ArrayList<>(); + CategoryDefinition cat = new CategoryDefinition(); + categories.add(cat); + cat.setName(categoryName); + service.setCategories(categories); + + service.setComponentType(ComponentTypeEnum.SERVICE); + Either createRes = topologyTemplateOperation.createTopologyTemplate(service); + Either getNodeTyeRes = titanDao.getVertexById(createRes.left().value().getUniqueId()); + + getNodeTyeRes.left().value(); + } + + private void createNodeType(String nodeTypeName, String uniqueId) { + NodeType vf = new NodeType(); + vf.setUniqueId(uniqueId); + vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID)); + vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), nodeTypeName); + vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId); + vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1"); + vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name()); + vf.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE); + List categories = new ArrayList<>(); + CategoryDefinition cat = new CategoryDefinition(); + categories.add(cat); + cat.setName(categoryName); + List subCategories = new ArrayList<>(); + SubCategoryDefinition subCat = new SubCategoryDefinition(); + subCat.setName(subcategory); + subCategories.add(subCat); + cat.setSubcategories(subCategories); + vf.setCategories(categories); + + List derivedFrom = new ArrayList<>(); + derivedFrom.add("root"); + vf.setDerivedFrom(derivedFrom); + + vf.setComponentType(ComponentTypeEnum.RESOURCE); + Either createVFRes = nodeTypeOperation.createNodeType(vf); + + Either getNodeTyeRes = titanDao.getVertexById(createVFRes.left().value().getUniqueId()); + + GraphVertex vfVertex = getNodeTyeRes.left().value(); + + List addProperties = new ArrayList<>(); + PropertyDataDefinition prop11 = new PropertyDataDefinition(); + prop11.setName("prop11"); + prop11.setDefaultValue("def11"); + + addProperties.add(prop11); + + PropertyDataDefinition prop22 = new PropertyDataDefinition(); + prop22.setName("prop22"); + prop22.setDefaultValue("def22"); + addProperties.add(prop22); + + StorageOperationStatus status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, addProperties, JsonPresentationFields.NAME); + assertSame(status, StorageOperationStatus.OK); + + PropertyDataDefinition prop33 = new PropertyDataDefinition(); + prop33.setName("prop33"); + prop33.setDefaultValue("def33"); + + status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop33, JsonPresentationFields.NAME); + assertSame(status, StorageOperationStatus.OK); + + PropertyDataDefinition prop44 = new PropertyDataDefinition(); + prop44.setName("prop44"); + prop44.setDefaultValue("def44"); + + status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop44, JsonPresentationFields.NAME); + assertSame(status, StorageOperationStatus.OK); + + PropertyDataDefinition capProp = new PropertyDataDefinition(); + capProp.setName("capProp"); + capProp.setDefaultValue("capPropDef"); + + MapDataDefinition dataToCreate = new MapPropertiesDataDefinition(); + dataToCreate.put("capProp", capProp); + + Map capProps = new HashMap<>(); + capProps.put("capName", dataToCreate); + + nodeTypeOperation.associateElementToData( + vfVertex, VertexTypeEnum.CAPABILITIES_PROPERTIES, EdgeLabelEnum.CAPABILITIES_PROPERTIES, capProps); + + List pathKeys = new ArrayList<>(); + pathKeys.add("capName"); + capProp.setDefaultValue("BBBB"); + nodeTypeOperation.updateToscaDataDeepElementOfToscaElement(vfVertex, EdgeLabelEnum.CAPABILITIES_PROPERTIES, VertexTypeEnum.CAPABILITIES_PROPERTIES, capProp, pathKeys, JsonPresentationFields.NAME); + } + + private void createRootNodeType() { + NodeType vf = new NodeType(); + String uniqueId = UniqueIdBuilder.buildResourceUniqueId(); + vf.setUniqueId(uniqueId); + vf.setComponentType(ComponentTypeEnum.RESOURCE); + vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID)); + vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), "root"); + vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId); + vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "1.0"); + vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VFC.name()); + vf.getMetadata().put(JsonPresentationFields.LIFECYCLE_STATE.getPresentation(), LifecycleStateEnum.CERTIFIED.name()); + vf.getMetadata().put(JsonPresentationFields.TOSCA_RESOURCE_NAME.getPresentation(), "root"); + vf.getMetadata().put(JsonPresentationFields.HIGHEST_VERSION.getPresentation(), true); + + List categories = new ArrayList<>(); + CategoryDefinition cat = new CategoryDefinition(); + categories.add(cat); + cat.setName(categoryName); + List subCategories = new ArrayList<>(); + SubCategoryDefinition subCat = new SubCategoryDefinition(); + subCat.setName(subcategory); + subCategories.add(subCat); + cat.setSubcategories(subCategories); + vf.setCategories(categories); + + List derivedFrom = new ArrayList<>(); + vf.setDerivedFrom(derivedFrom); + + Map properties = new HashMap<>(); + PropertyDataDefinition prop1 = new PropertyDataDefinition(); + prop1.setName("derived1"); + prop1.setDefaultValue("deriveddef1"); + properties.put("derived1", prop1); + + PropertyDataDefinition prop2 = new PropertyDataDefinition(); + prop2.setUniqueId("derived2"); + prop2.setName("deriveddef2"); + properties.put("derived2", prop2); + + PropertyDataDefinition prop3 = new PropertyDataDefinition(); + prop3.setName("derived3"); + prop3.setDefaultValue("deriveddef3"); + properties.put("derived3", prop3); + + vf.setProperties(properties); + vf.setComponentType(ComponentTypeEnum.RESOURCE); + Either createVFRes = nodeTypeOperation.createNodeType(vf); + + Either getNodeTyeRes = titanDao.getVertexById(createVFRes.left().value().getUniqueId()); + getNodeTyeRes.left().value(); + } + + private void createUsers() { + GraphVertex ownerV = new GraphVertex(VertexTypeEnum.USER); + ownerV.setUniqueId("user1"); + + Map metadataProperties = new HashMap<>(); + metadataProperties.put(GraphPropertyEnum.USERID, ownerV.getUniqueId()); + metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName()); + metadataProperties.put(GraphPropertyEnum.NAME, "user1"); + ownerV.setMetadataProperties(metadataProperties); + ownerV.updateMetadataJsonWithCurrentMetadataProperties(); + ownerV.setJson(new HashMap<>()); + Either createUserRes = titanDao.createVertex(ownerV); + + ownerVertex = createUserRes.left().value(); + + GraphVertex modifierV = new GraphVertex(VertexTypeEnum.USER); + modifierV.setUniqueId("user2"); + + metadataProperties = new HashMap<>(); + metadataProperties.put(GraphPropertyEnum.USERID, modifierV.getUniqueId()); + metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName()); + metadataProperties.put(GraphPropertyEnum.NAME, "user2"); + modifierV.setMetadataProperties(metadataProperties); + modifierV.updateMetadataJsonWithCurrentMetadataProperties(); + modifierV.setJson(new HashMap<>()); + createUserRes = titanDao.createVertex(modifierV); + createUserRes.left().value(); + + lifecycleOperation.findUser(ownerVertex.getUniqueId()); + } + + @After + public void teardown() { + GraphTestUtils.clearGraph(titanDao); + } + +} 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 new file mode 100644 index 0000000000..fbf01bc91d --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/utils/InterfaceUtilsTest.java @@ -0,0 +1,50 @@ +package org.openecomp.sdc.be.model.jsontitan.utils; + +import org.junit.Test; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.Operation; +import org.openecomp.sdc.be.model.Resource; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class InterfaceUtilsTest { + + private InterfaceUtils createTestSubject() { + return new InterfaceUtils(); + } + + + @Test + public void testGetInterfaceDefinitionFromToscaName() throws Exception { + Collection interfaces = null; + String resourceName = ""; + Optional result; + + // default test + result = InterfaceUtils.getInterfaceDefinitionFromToscaName(interfaces, resourceName); + } + + + @Test + public void testGetInterfaceDefinitionListFromToscaName() throws Exception { + Collection interfaces = null; + String resourceName = ""; + Collection result; + + // default test + result = InterfaceUtils.getInterfaceDefinitionListFromToscaName(interfaces, resourceName); + } + + + @Test + public void testCreateInterfaceToscaResourceName() throws Exception { + String resourceName = ""; + String result; + + // default test + result = InterfaceUtils.createInterfaceToscaResourceName(resourceName); + } +} \ No newline at end of file -- cgit 1.2.3-korg