From 0e1a3ddc9fae2b9ce74be0f14c4f6e08fac7e85b Mon Sep 17 00:00:00 2001 From: Fiete Ostkamp Date: Thu, 11 Apr 2024 11:59:10 +0200 Subject: Fix: Listing archived catalog resources fails randomly - filter out vertices that do not have Metadata property when checking if component exists in catalog - log filtered vertex with as much information as possible Issue-ID: SDC-4685 Signed-off-by: Fiete Ostkamp Change-Id: Id7a88729c72ac5d3504ca6e3f3097d65475b932b --- .../sdc/be/dao/janusgraph/JanusGraphDao.java | 24 ++--- .../operations/ToscaElementOperation.java | 113 ++++++++++++--------- .../ToscaElementOperationCatalogTest.java | 31 +++--- 3 files changed, 95 insertions(+), 73 deletions(-) diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java index 38dc522247..606caeb576 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java @@ -619,31 +619,31 @@ public class JanusGraphDao { } public Either, JanusGraphOperationStatus> getCatalogOrArchiveVertices(boolean isCatalog) { - Either graph = janusGraphClient.getGraph(); - if (graph.isLeft()) { + Either graphEither = janusGraphClient.getGraph(); + if (graphEither.isLeft()) { try { - JanusGraph tGraph = graph.left().value(); - String name = isCatalog ? VertexTypeEnum.CATALOG_ROOT.getName() : VertexTypeEnum.ARCHIVE_ROOT.getName(); - Iterable vCatalogIter = tGraph.query().has(GraphPropertyEnum.LABEL.getProperty(), name).vertices(); - if (vCatalogIter == null) { + JanusGraph graph = graphEither.left().value(); + String vertexType = isCatalog ? VertexTypeEnum.CATALOG_ROOT.getName() : VertexTypeEnum.ARCHIVE_ROOT.getName(); + Iterable vertexIterable = graph.query().has(GraphPropertyEnum.LABEL.getProperty(), vertexType).vertices(); + if (vertexIterable == null) { logger.debug("Failed to fetch catalog vertex"); return Either.right(JanusGraphOperationStatus.GENERAL_ERROR); } - JanusGraphVertex catalogV = vCatalogIter.iterator().next(); - if (catalogV == null) { + JanusGraphVertex catalogVertex = vertexIterable.iterator().next(); + if (catalogVertex == null) { logger.debug("Failed to fetch catalog vertex"); return Either.right(JanusGraphOperationStatus.GENERAL_ERROR); } String edgeLabel = isCatalog ? EdgeLabelEnum.CATALOG_ELEMENT.name() : EdgeLabelEnum.ARCHIVE_ELEMENT.name(); - Iterator vertices = catalogV.vertices(Direction.OUT, edgeLabel); - return Either.left(vertices); + Iterator adjacentVertices = catalogVertex.vertices(Direction.OUT, edgeLabel); + return Either.left(adjacentVertices); } catch (Exception e) { logger.debug("Failed get by criteria: ", e); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - logger.debug("Failed get by criteria : {}", graph.right().value()); - return Either.right(graph.right().value()); + logger.debug("Failed get by criteria : {}", graphEither.right().value()); + return Either.right(graphEither.right().value()); } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperation.java index 24e1e910d3..0bc84506ee 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperation.java @@ -37,13 +37,13 @@ import java.util.Map.Entry; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.tinkerpop.gremlin.structure.Direction; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.apache.tinkerpop.gremlin.structure.VertexProperty; +import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyVertexProperty; import org.janusgraph.core.JanusGraphVertex; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; @@ -83,6 +83,7 @@ import org.openecomp.sdc.be.utils.TypeUtils; import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; import org.openecomp.sdc.common.jsongraph.util.CommonUtility; import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.common.util.ValidationUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -1347,7 +1348,13 @@ public abstract class ToscaElementOperation extends BaseOperation { } Iterator vertices = verticesEither.left().value(); while (vertices.hasNext()) { - handleCatalogComponent(existInCatalog, vertices.next(), excludeTypes); + Vertex vertex = vertices.next(); + VertexProperty vertexProperty = vertex.property(GraphPropertiesDictionary.METADATA.getProperty()); + if(!(vertexProperty instanceof EmptyVertexProperty)) { + handleCatalogComponent(existInCatalog, vertex, excludeTypes); + } else { + logVertex(vertex); + } } stopWatch.stop(); String timeToFetchElements = stopWatch.prettyPrint(); @@ -1355,64 +1362,76 @@ public abstract class ToscaElementOperation extends BaseOperation { return Either.left(existInCatalog.values().stream().collect(Collectors.toList())); } + private void logVertex(Vertex vertex) { + StringBuilder sb = new StringBuilder(); + vertex.keys().forEach(key -> { + Object value = vertex.property(key); + sb.append("[").append(key).append(": ").append(value).append("] "); + }); + log.warn(EcompLoggerErrorCode.DATA_ERROR, "catalog-model", "Vertex has no metadata property: ", sb.toString()); + } + private void handleCatalogComponent(Map existInCatalog, Vertex vertex, List excludeTypes) { VertexProperty property = vertex.property(GraphPropertiesDictionary.METADATA.getProperty()); String json = (String) property.value(); Map metadatObj = JsonParserUtils.toMap(json); String uniqueId = (String) metadatObj.get(JsonPresentationFields.UNIQUE_ID.getPresentation()); Boolean isDeleted = (Boolean) metadatObj.get(JsonPresentationFields.IS_DELETED.getPresentation()); - if (isAddToCatalog(excludeTypes, metadatObj) && (existInCatalog.get(uniqueId) == null && (isDeleted == null || !isDeleted.booleanValue()))) { - CatalogComponent catalogComponent = new CatalogComponent(); - catalogComponent.setUniqueId(uniqueId); - catalogComponent.setModel((String) metadatObj.get(JsonPresentationFields.MODEL.getPresentation())); - catalogComponent - .setComponentType(ComponentTypeEnum.valueOf((String) metadatObj.get(JsonPresentationFields.COMPONENT_TYPE.getPresentation()))); - catalogComponent.setVersion((String) metadatObj.get(JsonPresentationFields.VERSION.getPresentation())); - catalogComponent.setName((String) metadatObj.get(JsonPresentationFields.NAME.getPresentation())); - catalogComponent.setIcon((String) metadatObj.get(JsonPresentationFields.ICON.getPresentation())); - catalogComponent.setLifecycleState((String) metadatObj.get(JsonPresentationFields.LIFECYCLE_STATE.getPresentation())); - Object lastUpdateDate = metadatObj.get(JsonPresentationFields.LAST_UPDATE_DATE.getPresentation()); - catalogComponent.setLastUpdateDate((lastUpdateDate != null ? (Long) lastUpdateDate : 0L)); - catalogComponent.setDistributionStatus((String) metadatObj.get(JsonPresentationFields.DISTRIBUTION_STATUS.getPresentation())); - catalogComponent.setDescription((String) metadatObj.get(JsonPresentationFields.DESCRIPTION.getPresentation())); - catalogComponent.setTenant((String) metadatObj.get(JsonPresentationFields.TENANT.getPresentation())); - catalogComponent.setSystemName((String) metadatObj.get(JsonPresentationFields.SYSTEM_NAME.getPresentation())); - catalogComponent.setUuid((String) metadatObj.get(JsonPresentationFields.UUID.getPresentation())); - catalogComponent.setInvariantUUID((String) metadatObj.get(JsonPresentationFields.INVARIANT_UUID.getPresentation())); - catalogComponent.setIsHighestVersion((Boolean) metadatObj.get(JsonPresentationFields.HIGHEST_VERSION.getPresentation())); - Iterator edges = vertex.edges(Direction.IN, EdgeLabelEnum.STATE.name()); - if (edges.hasNext()) { - catalogComponent - .setLastUpdaterUserId((String) edges.next().outVertex().property(GraphPropertiesDictionary.USERID.getProperty()).value()); - } - Object resourceType = metadatObj.get(JsonPresentationFields.RESOURCE_TYPE.getPresentation()); - if (resourceType != null) { - catalogComponent.setResourceType((String) resourceType); - } - if (catalogComponent.getComponentType() == ComponentTypeEnum.SERVICE) { - setServiceCategoryFromGraphV(vertex, catalogComponent); - } else { - setResourceCategoryFromGraphV(vertex, catalogComponent); - } - List tags = (List) metadatObj.get(JsonPresentationFields.TAGS.getPresentation()); - if (tags != null) { - catalogComponent.setTags(tags); - } + if (isNotExcluded(excludeTypes, metadatObj) && (existInCatalog.get(uniqueId) == null && (isDeleted == null || !isDeleted.booleanValue()))) { + CatalogComponent catalogComponent = createCatalogComponent(vertex, metadatObj, uniqueId); existInCatalog.put(uniqueId, catalogComponent); } } - private boolean isAddToCatalog(List excludeTypes, Map metadatObj) { + private CatalogComponent createCatalogComponent(Vertex vertex, Map metadatObj, String uniqueId) { + CatalogComponent catalogComponent = new CatalogComponent(); + catalogComponent.setUniqueId(uniqueId); + catalogComponent.setModel((String) metadatObj.get(JsonPresentationFields.MODEL.getPresentation())); + catalogComponent + .setComponentType(ComponentTypeEnum.valueOf((String) metadatObj.get(JsonPresentationFields.COMPONENT_TYPE.getPresentation()))); + catalogComponent.setVersion((String) metadatObj.get(JsonPresentationFields.VERSION.getPresentation())); + catalogComponent.setName((String) metadatObj.get(JsonPresentationFields.NAME.getPresentation())); + catalogComponent.setIcon((String) metadatObj.get(JsonPresentationFields.ICON.getPresentation())); + catalogComponent.setLifecycleState((String) metadatObj.get(JsonPresentationFields.LIFECYCLE_STATE.getPresentation())); + Object lastUpdateDate = metadatObj.get(JsonPresentationFields.LAST_UPDATE_DATE.getPresentation()); + catalogComponent.setLastUpdateDate((lastUpdateDate != null ? (Long) lastUpdateDate : 0L)); + catalogComponent.setDistributionStatus((String) metadatObj.get(JsonPresentationFields.DISTRIBUTION_STATUS.getPresentation())); + catalogComponent.setDescription((String) metadatObj.get(JsonPresentationFields.DESCRIPTION.getPresentation())); + catalogComponent.setTenant((String) metadatObj.get(JsonPresentationFields.TENANT.getPresentation())); + catalogComponent.setSystemName((String) metadatObj.get(JsonPresentationFields.SYSTEM_NAME.getPresentation())); + catalogComponent.setUuid((String) metadatObj.get(JsonPresentationFields.UUID.getPresentation())); + catalogComponent.setInvariantUUID((String) metadatObj.get(JsonPresentationFields.INVARIANT_UUID.getPresentation())); + catalogComponent.setIsHighestVersion((Boolean) metadatObj.get(JsonPresentationFields.HIGHEST_VERSION.getPresentation())); + Iterator edges = vertex.edges(Direction.IN, EdgeLabelEnum.STATE.name()); + if (edges.hasNext()) { + catalogComponent + .setLastUpdaterUserId((String) edges.next().outVertex().property(GraphPropertiesDictionary.USERID.getProperty()).value()); + } + Object resourceType = metadatObj.get(JsonPresentationFields.RESOURCE_TYPE.getPresentation()); + if (resourceType != null) { + catalogComponent.setResourceType((String) resourceType); + } + if (catalogComponent.getComponentType() == ComponentTypeEnum.SERVICE) { + setServiceCategoryFromGraphV(vertex, catalogComponent); + } else { + setResourceCategoryFromGraphV(vertex, catalogComponent); + } + List tags = (List) metadatObj.get(JsonPresentationFields.TAGS.getPresentation()); + if (tags != null) { + catalogComponent.setTags(tags); + } + return catalogComponent; + } + + private boolean isNotExcluded(List excludeTypes, Map metadatObj) { boolean isAddToCatalog = true; Object resourceTypeStr = metadatObj.get(JsonPresentationFields.RESOURCE_TYPE.getPresentation()); - if (resourceTypeStr != null) { + if (resourceTypeStr != null && excludeTypes != null) { ResourceTypeEnum resourceType = ResourceTypeEnum.getType((String) resourceTypeStr); - if (!CollectionUtils.isEmpty(excludeTypes)) { - Optional op = excludeTypes.stream().filter(rt -> rt == resourceType).findAny(); - if (op.isPresent()) { - isAddToCatalog = false; - } - } + return !excludeTypes.stream() + .filter(type -> type == resourceType) + .findAny() + .isPresent(); } return isAddToCatalog; } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperationCatalogTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperationCatalogTest.java index 28f9fc8652..571a6d4b1f 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperationCatalogTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperationCatalogTest.java @@ -25,6 +25,7 @@ import org.apache.tinkerpop.gremlin.structure.Direction; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.apache.tinkerpop.gremlin.structure.VertexProperty; +import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyVertexProperty; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -51,20 +52,13 @@ public class ToscaElementOperationCatalogTest { private static final String UPDATER_ID = "m08740"; private ArrayList vertexList = new ArrayList<>(); - @Mock - Vertex vertex; - @Mock - Edge edge; - @Mock - Vertex outVertex; - @Mock - Iterator edges; - @Mock - JanusGraphDao janusGraphDao; - @Mock - VertexProperty property; - @Mock - VertexProperty updaterProperty; + @Mock Edge edge; + @Mock Vertex vertex; + @Mock Vertex outVertex; + @Mock Iterator edges; + @Mock JanusGraphDao janusGraphDao; + @Mock VertexProperty property; + @Mock VertexProperty updaterProperty; @InjectMocks private ToscaElementOperation toscaOperation = new TopologyTemplateOperation(); @@ -119,4 +113,13 @@ public class ToscaElementOperationCatalogTest { assertEquals(UPDATER_ID, componentList.get(0).getLastUpdaterUserId()); } + @Test + public void thatVertexIsSkippedIfItHasNoMetadataProperty() { + VertexProperty emptyVertexProperty = new EmptyVertexProperty<>(); + when(vertex.property(GraphPropertiesDictionary.METADATA.getProperty())).thenReturn(emptyVertexProperty); + + List componentList = toscaOperation.getElementCatalogData(true, null).left().value(); + assertEquals(0, componentList.size()); + } + } -- cgit 1.2.3-korg