diff options
7 files changed, 114 insertions, 32 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManager.java index 1c5411434f..eb8df50d70 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManager.java @@ -19,7 +19,6 @@ */ package org.openecomp.sdc.be.components.impl; -import fj.data.Either; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -28,8 +27,11 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.stream.Collectors; + import javax.annotation.Resource; + import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.impl.ComponentsUtils; @@ -37,11 +39,14 @@ import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Operation; import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.stereotype.Component; +import fj.data.Either; + @Component("interfaceLifecycleTypeImportManager") public class InterfaceLifecycleTypeImportManager { @@ -53,8 +58,8 @@ public class InterfaceLifecycleTypeImportManager { @Resource private CommonImportManager commonImportManager; - public Either<List<InterfaceDefinition>, ResponseFormat> createLifecycleTypes(String interfaceLifecycleTypesYml) { - Either<List<InterfaceDefinition>, ActionStatus> interfaces = createInterfaceTypeFromYml(interfaceLifecycleTypesYml); + public Either<List<InterfaceDefinition>, ResponseFormat> createLifecycleTypes(String interfaceLifecycleTypesYml, final String modelName) { + Either<List<InterfaceDefinition>, ActionStatus> interfaces = createInterfaceTypeFromYml(interfaceLifecycleTypesYml, modelName); if (interfaces.isRight()) { ActionStatus status = interfaces.right().value(); ResponseFormat responseFormat = componentsUtils.getResponseFormatByGroupType(status, null); @@ -63,8 +68,12 @@ public class InterfaceLifecycleTypeImportManager { return createInterfacesByDao(interfaces.left().value()); } - private Either<List<InterfaceDefinition>, ActionStatus> createInterfaceTypeFromYml(final String interfaceTypesYml) { - return commonImportManager.createElementTypesFromYml(interfaceTypesYml, this::createInterfaceDefinition); + private Either<List<InterfaceDefinition>, ActionStatus> createInterfaceTypeFromYml(final String interfaceTypesYml, final String modelName) { + final Either<List<InterfaceDefinition>, ActionStatus> interfaceTypes = commonImportManager.createElementTypesFromYml(interfaceTypesYml, this::createInterfaceDefinition); + if (interfaceTypes.isLeft() && StringUtils.isNotEmpty(modelName)){ + interfaceTypes.left().value().forEach(interfaceType -> interfaceType.setModel(modelName)); + } + return interfaceTypes; } private Either<List<InterfaceDefinition>, ResponseFormat> createInterfacesByDao(List<InterfaceDefinition> interfacesToCreate) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java index 396cb42d47..0fd4206cd6 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java @@ -162,9 +162,10 @@ public class TypesUploadServlet extends AbstractValidationsServlet { @ApiResponse(responseCode = "409", description = "Interface Lifecycle Type already exist")}) @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) public Response uploadInterfaceLifecycleType(@Parameter(description = "FileInputStream") @FormDataParam("interfaceLifecycleTypeZip") File file, - @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator) { + @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, + @Parameter(description = "model") @FormDataParam("model") String modelName) { ConsumerTwoParam<Wrapper<Response>, String> createElementsMethod = (responseWrapper, ymlPayload) -> createElementsType(responseWrapper, - () -> interfaceLifecycleTypeImportManager.createLifecycleTypes(ymlPayload)); + () -> interfaceLifecycleTypeImportManager.createLifecycleTypes(ymlPayload, modelName)); return uploadElementTypeServletLogic(createElementsMethod, file, request, creator, "Interface Types"); } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManagerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManagerTest.java index 21307ad608..9cd18bc9a2 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManagerTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManagerTest.java @@ -84,7 +84,7 @@ public class InterfaceLifecycleTypeImportManagerTest { public void createLifecycleTypesTest() throws IOException { final String ymlContent = getYmlContent(); final Either<List<InterfaceDefinition>, ResponseFormat> createCapabilityTypes = - importManager.createLifecycleTypes(ymlContent); + importManager.createLifecycleTypes(ymlContent, "test"); assertTrue(createCapabilityTypes.isLeft()); final List<InterfaceDefinition> interfaceDefinitionList = createCapabilityTypes.left().value(); assertThat("Interface definitions should not be empty", interfaceDefinitionList, is(not(empty()))); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/InterfaceDefinition.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/InterfaceDefinition.java index f4b0e8e522..13d80df4ea 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/InterfaceDefinition.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/InterfaceDefinition.java @@ -19,23 +19,30 @@ */ package org.openecomp.sdc.be.model; -import com.fasterxml.jackson.annotation.JsonIgnore; import java.util.Map; import java.util.stream.Collectors; + import org.apache.commons.collections.MapUtils; import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; +import com.fasterxml.jackson.annotation.JsonIgnore; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + /** * Definition of the operations that can be performed on (instances of) a Node Type. * * @author esofer */ +@NoArgsConstructor public class InterfaceDefinition extends InterfaceDataDefinition implements IOperationParameter { - public InterfaceDefinition() { - super(); - } + @Getter + @Setter + private String model; public InterfaceDefinition(String type, String description, Map<String, Operation> operations) { super(type, description); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/InterfaceLifecycleOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/InterfaceLifecycleOperation.java index a4d8ac9152..3fa1f8933d 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/InterfaceLifecycleOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/InterfaceLifecycleOperation.java @@ -19,7 +19,6 @@ */ package org.openecomp.sdc.be.model.operations.impl; -import fj.data.Either; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; @@ -28,8 +27,10 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.stream.Collectors; + import org.apache.commons.lang3.tuple.ImmutablePair; import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge; +import org.openecomp.sdc.be.dao.graph.datatype.GraphNode; import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; @@ -45,11 +46,15 @@ import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.resources.data.ArtifactData; import org.openecomp.sdc.be.resources.data.InterfaceData; +import org.openecomp.sdc.be.resources.data.ModelData; import org.openecomp.sdc.be.resources.data.OperationData; import org.openecomp.sdc.be.resources.data.ResourceMetadataData; +import org.openecomp.sdc.be.resources.data.UniqueIdData; import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.stereotype.Component; +import fj.data.Either; + @Component("interface-operation") public class InterfaceLifecycleOperation implements IInterfaceLifecycleOperation { @@ -96,7 +101,9 @@ public class InterfaceLifecycleOperation implements IInterfaceLifecycleOperation private InterfaceDefinition convertInterfaceDataToInterfaceDefinition(InterfaceData interfaceData) { log.debug("The object returned after create interface is {}", interfaceData); - return new InterfaceDefinition(interfaceData.getInterfaceDataDefinition()); + final InterfaceDefinition interfaceDefinition = new InterfaceDefinition(interfaceData.getInterfaceDataDefinition()); + interfaceDefinition.setModel(getModelAssociatedToInterface(interfaceData.getUniqueId())); + return interfaceDefinition; } private Operation convertOperationDataToOperation(OperationData operationData) { @@ -743,8 +750,8 @@ public class InterfaceLifecycleOperation implements IInterfaceLifecycleOperation public Either<InterfaceDefinition, StorageOperationStatus> createInterfaceType(InterfaceDefinition interf, boolean inTransaction) { Either<InterfaceDefinition, StorageOperationStatus> result = null; try { + interf.setUniqueId(UniqueIdBuilder.buildInterfaceTypeUid(interf.getModel(), interf.getType())); InterfaceData interfaceData = new InterfaceData(interf); - interf.setUniqueId(interf.getType().toLowerCase()); Either<InterfaceData, JanusGraphOperationStatus> existInterface = janusGraphGenericDao .getNode(interfaceData.getUniqueIdKey(), interfaceData.getUniqueId(), InterfaceData.class); if (existInterface.isLeft()) { @@ -753,7 +760,6 @@ public class InterfaceLifecycleOperation implements IInterfaceLifecycleOperation result = Either.right(StorageOperationStatus.ENTITY_ALREADY_EXISTS); return result; } - log.debug("Before adding interface type to graph {}", interfaceData); Either<InterfaceData, JanusGraphOperationStatus> createNodeResult = janusGraphGenericDao.createNode(interfaceData, InterfaceData.class); log.debug("After adding property type to graph {}", interfaceData); if (createNodeResult.isRight()) { @@ -782,6 +788,11 @@ public class InterfaceLifecycleOperation implements IInterfaceLifecycleOperation } interfaceDefResult.setOperationsMap(newOperations); } + final Either<GraphRelation, StorageOperationStatus> modelRelationship = addInterfaceTypeToModel(interf); + if (modelRelationship.isRight()) { + return Either.right(modelRelationship.right().value()); + } + result = Either.left(interfaceDefResult); return result; } finally { @@ -797,6 +808,17 @@ public class InterfaceLifecycleOperation implements IInterfaceLifecycleOperation } } + private Either<GraphRelation, StorageOperationStatus> addInterfaceTypeToModel(final InterfaceDefinition interfaceDefinition) { + final String model = interfaceDefinition.getModel(); + if (model == null) { + return Either.left(null); + } + final GraphNode from = new UniqueIdData(NodeTypeEnum.Model, UniqueIdBuilder.buildModelUid(model)); + final GraphNode to = new UniqueIdData(NodeTypeEnum.Interface, interfaceDefinition.getUniqueId()); + log.info("Connecting model {} to type {}", from, to); + return janusGraphGenericDao.createRelation(from , to, GraphEdgeLabels.MODEL_ELEMENT, Collections.emptyMap()).right().map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus); + } + @Override public Either<InterfaceDefinition, StorageOperationStatus> getInterface(String interfaceId) { Either<InterfaceData, JanusGraphOperationStatus> getResult = janusGraphGenericDao @@ -837,8 +859,9 @@ public class InterfaceLifecycleOperation implements IInterfaceLifecycleOperation Map<String, InterfaceDefinition> interfaceTypes = new HashMap<>(); List<InterfaceData> interfaceDataList = allInterfaceLifecycleTypes.left().value(); List<InterfaceDefinition> interfaceDefinitions = interfaceDataList.stream().map(this::convertInterfaceDataToInterfaceDefinition) - .filter(interfaceDefinition -> interfaceDefinition.getUniqueId().equalsIgnoreCase((interfaceDefinition.getType()))) - .collect(Collectors.toList()); + .filter(interfaceDefinition -> interfaceDefinition.getUniqueId() + .equalsIgnoreCase(UniqueIdBuilder.buildInterfaceTypeUid(interfaceDefinition.getModel(), interfaceDefinition.getType())) + ).collect(Collectors.toList()); for (InterfaceDefinition interfaceDefinition : interfaceDefinitions) { Either<List<ImmutablePair<OperationData, GraphEdge>>, JanusGraphOperationStatus> childrenNodes = janusGraphGenericDao .getChildrenNodes(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), interfaceDefinition.getUniqueId(), @@ -855,4 +878,14 @@ public class InterfaceLifecycleOperation implements IInterfaceLifecycleOperation } return Either.left(interfaceTypes); } + + private String getModelAssociatedToInterface(String uid) { + final Either<ImmutablePair<ModelData, GraphEdge>, JanusGraphOperationStatus> model = janusGraphGenericDao.getParentNode( + UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Interface), uid, GraphEdgeLabels.MODEL_ELEMENT, + NodeTypeEnum.Model, ModelData.class); + if (model.isLeft()) { + return model.left().value().getLeft().getName(); + } + return null; + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/UniqueIdBuilder.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/UniqueIdBuilder.java index a433698ce5..8f1cbae1bb 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/UniqueIdBuilder.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/UniqueIdBuilder.java @@ -97,6 +97,10 @@ public class UniqueIdBuilder { return StringUtils.isEmpty(modelName) ? type : modelName + DOT + type; } + public static String buildInterfaceTypeUid(final String modelName, String type) { + return StringUtils.isEmpty(modelName) ? type : modelName + DOT + type; + } + public static String buildAttributeUid(String resourceId, String attName) { return buildTypeUid(NodeTypeEnum.Attribute.getName(), resourceId, attName); } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/InterfaceLifecycleOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/InterfaceLifecycleOperationTest.java index 8262d54bc4..0b5a7fae81 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/InterfaceLifecycleOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/InterfaceLifecycleOperationTest.java @@ -20,7 +20,17 @@ package org.openecomp.sdc.be.model.operations.impl; -import fj.data.Either; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.Assert; import org.junit.Before; @@ -31,6 +41,7 @@ import org.mockito.InjectMocks; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge; +import org.openecomp.sdc.be.dao.graph.datatype.GraphNode; import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; @@ -42,6 +53,7 @@ import org.openecomp.sdc.be.model.ModelTestBase; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.util.OperationTestsUtil; import org.openecomp.sdc.be.resources.data.InterfaceData; +import org.openecomp.sdc.be.resources.data.ModelData; import org.openecomp.sdc.be.resources.data.OperationData; import org.openecomp.sdc.be.resources.data.ResourceMetadataData; import org.openecomp.sdc.be.resources.data.UserData; @@ -49,22 +61,15 @@ import org.openecomp.sdc.be.resources.data.category.CategoryData; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; +import fj.data.Either; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:application-context-test.xml") public class InterfaceLifecycleOperationTest { private static String USER_ID = "muUserId"; private static String CATEGORY_NAME = "category/mycategory"; + private static String MODEL_NAME = "Test"; + private static String INTERFACE_TYPE = "tosca.interfaces.standard"; JanusGraphGenericDao janusGraphGenericDao = Mockito.mock(JanusGraphGenericDao.class); @InjectMocks @@ -91,9 +96,9 @@ public class InterfaceLifecycleOperationTest { private InterfaceDefinition buildInterfaceDefinition() { InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); - interfaceDefinition.setType("tosca.interfaces.standard"); + interfaceDefinition.setType(INTERFACE_TYPE); interfaceDefinition.setCreationDate(101232L); - + interfaceDefinition.setModel(MODEL_NAME); return interfaceDefinition; } @@ -173,9 +178,32 @@ public class InterfaceLifecycleOperationTest { List<ImmutablePair<OperationData, GraphEdge>> list = new ArrayList<>(); Either<List<ImmutablePair<OperationData, GraphEdge>>, JanusGraphOperationStatus> childrenNodes = Either.left(list); when(janusGraphGenericDao.getChildrenNodes(interfaceData.getUniqueIdKey(), interfaceData.getUniqueId(), GraphEdgeLabels.INTERFACE_OPERATION, NodeTypeEnum.InterfaceOperation, OperationData.class)).thenReturn(childrenNodes); - + when(janusGraphGenericDao.getParentNode(any(), any(), any(), any(), any())) + .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); Either<Map<String, InterfaceDefinition>, StorageOperationStatus> types = interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(); Assert.assertEquals(types.left().value().size(),1); } + @Test + public void testGetAllInterfaceLifecycleTypesWithModel() { + final var uid = UniqueIdBuilder.buildInterfaceTypeUid(MODEL_NAME, INTERFACE_TYPE); + final var modelData = new ModelData(MODEL_NAME, uid); + final ImmutablePair<GraphNode, GraphEdge> modelNode = new ImmutablePair<>(modelData, Mockito.mock(GraphEdge.class)); + + final InterfaceData interfaceData = new InterfaceData(); + interfaceData.getInterfaceDataDefinition().setUniqueId(uid); + interfaceData.getInterfaceDataDefinition().setType(INTERFACE_TYPE); + + final List<InterfaceData> interfaceTypes = new ArrayList<InterfaceData>(); + interfaceTypes.add(interfaceData); + + when(janusGraphGenericDao.getParentNode(any(), any(), any(), any(), any())) + .thenReturn(Either.left(modelNode)); + when(janusGraphGenericDao + .getByCriteria(NodeTypeEnum.Interface, Collections.emptyMap(), InterfaceData.class)).thenReturn(Either.left(interfaceTypes)); + when(janusGraphGenericDao.getChildrenNodes(interfaceData.getUniqueIdKey(), interfaceData.getUniqueId(), GraphEdgeLabels.INTERFACE_OPERATION, NodeTypeEnum.InterfaceOperation, OperationData.class)).thenReturn(Either.left(Collections.emptyList())); + + Assert.assertEquals(1, interfaceLifecycleOperation.getAllInterfaceLifecycleTypes().left().value().size()); + } + } |