Improve handling 'empty'/null string in Service fields
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ToscaElementOperation.java
index bfb6cae..24e1e91 100644 (file)
@@ -39,6 +39,7 @@ 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;
@@ -150,6 +151,17 @@ public abstract class ToscaElementOperation extends BaseOperation {
         return Either.left(vertexG);
     }
 
+    protected GraphVertex getHighestVersionFrom(GraphVertex v) {
+        Either<GraphVertex, JanusGraphOperationStatus> childVertexE = janusGraphDao
+            .getChildVertex(v, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
+        GraphVertex highestVersionVertex = v;
+        while (childVertexE.isLeft()) {
+            highestVersionVertex = childVertexE.left().value();
+            childVertexE = janusGraphDao.getChildVertex(highestVersionVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
+        }
+        return highestVersionVertex;
+    }
+
     public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId) {
         return getToscaElement(uniqueId, new ComponentParametersView());
     }
@@ -206,7 +218,10 @@ public abstract class ToscaElementOperation extends BaseOperation {
             }
         }
         if (result == null) {
-            result = createModelEdge(previousToscaElement, nextToscaElement, user, createdToscaElementVertex);
+            result =
+                VertexTypeEnum.TOPOLOGY_TEMPLATE.equals(previousToscaElement.getLabel())
+                    ? createModelEdge(previousToscaElement, nextToscaElement, user, createdToscaElementVertex, EdgeLabelEnum.MODEL)
+                    : createModelEdge(previousToscaElement, nextToscaElement, user, createdToscaElementVertex, EdgeLabelEnum.MODEL_ELEMENT);
         }
         if (result == null) {
             status = janusGraphDao.createEdge(user.getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.LAST_MODIFIER, new HashMap<>());
@@ -265,24 +280,28 @@ public abstract class ToscaElementOperation extends BaseOperation {
 
     /**
      * Creates the MODEL in case it exits on the previous version
-     * @param previousToscaElement previous element version
-     * @param nextToscaElement latest element version
-     * @param user user
+     *
+     * @param previousToscaElement      previous element version
+     * @param nextToscaElement          latest element version
+     * @param user                      user
      * @param createdToscaElementVertex created tosca element
+     * @param edgeLabelEnum
      * @return
      */
     private Either<GraphVertex, StorageOperationStatus> createModelEdge(final GraphVertex previousToscaElement,
                                                                         final GraphVertex nextToscaElement, GraphVertex user,
-                                                                        final GraphVertex createdToscaElementVertex) {
+                                                                        final GraphVertex createdToscaElementVertex,
+                                                                        final EdgeLabelEnum edgeLabelEnum) {
         Either<GraphVertex, StorageOperationStatus> result = null;
         final Either<GraphVertex, JanusGraphOperationStatus> modelElementVertexResponse = janusGraphDao
-            .getParentVertex(previousToscaElement, EdgeLabelEnum.MODEL, JsonParseFlagEnum.NoParse);
+            .getParentVertex(previousToscaElement, edgeLabelEnum, JsonParseFlagEnum.NoParse);
         if (modelElementVertexResponse.isLeft()) {
             final JanusGraphOperationStatus status = janusGraphDao
-                .createEdge(nextToscaElement.getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.MODEL, new HashMap<>());
+                .createEdge(modelElementVertexResponse.left().value().getVertex(), createdToscaElementVertex.getVertex(), edgeLabelEnum,
+                    new HashMap<>());
             if (JanusGraphOperationStatus.OK != status) {
                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
-                    FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.MODEL,
+                    FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, edgeLabelEnum,
                     user.getUniqueId(), nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
             }
@@ -457,10 +476,11 @@ public abstract class ToscaElementOperation extends BaseOperation {
 
     protected StorageOperationStatus associateComponentToModel(final GraphVertex nodeTypeVertex, final ToscaElement nodeType,
                                                                final EdgeLabelEnum edgeLabelEnum) {
-        if (nodeType.getMetadataValue(JsonPresentationFields.MODEL) == null) {
+        Object metadataValue = nodeType.getMetadataValue(JsonPresentationFields.MODEL);
+        if (metadataValue == null || StringUtils.isEmpty((String) metadataValue)) {
             return StorageOperationStatus.OK;
         }
-        final String model = ((String) nodeType.getMetadataValue(JsonPresentationFields.MODEL));
+        final String model = ((String) metadataValue);
         final JanusGraphOperationStatus createEdge = janusGraphDao.createEdge(getModelVertex(model), nodeTypeVertex, edgeLabelEnum, new HashMap<>());
         if (createEdge != JanusGraphOperationStatus.OK) {
             log.trace("Failed to associate resource {} to model {}", nodeType.getUniqueId(), model);
@@ -1004,8 +1024,7 @@ public abstract class ToscaElementOperation extends BaseOperation {
     protected JanusGraphOperationStatus setResourceCategoryFromGraphV(Vertex vertex, CatalogComponent catalogComponent) {
         List<CategoryDefinition> categories = new ArrayList<>();
         SubCategoryDefinition subcategory;
-        Either<Vertex, JanusGraphOperationStatus> childVertex = janusGraphDao
-            .getChildVertex(vertex, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
+        Either<Vertex, JanusGraphOperationStatus> childVertex = janusGraphDao.getChildVertex(vertex, EdgeLabelEnum.CATEGORY);
         if (childVertex.isRight()) {
             log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, catalogComponent.getUniqueId(),
                 childVertex.right().value());
@@ -1024,8 +1043,7 @@ public abstract class ToscaElementOperation extends BaseOperation {
             .fromJson((String) subCategoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).value(), listTypeSubcat)
             : Collections.emptyList();
         subcategory.setMetadataKeys(metadataKeys);
-        Either<Vertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
-            .getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
+        Either<Vertex, JanusGraphOperationStatus> parentVertex = janusGraphDao.getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY);
         Vertex categoryV = parentVertex.left().value();
         String categoryNormalizedName = (String) categoryV.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
         catalogComponent.setCategoryNormalizedName(categoryNormalizedName);
@@ -1041,8 +1059,7 @@ public abstract class ToscaElementOperation extends BaseOperation {
 
     protected JanusGraphOperationStatus setServiceCategoryFromGraphV(Vertex vertex, CatalogComponent catalogComponent) {
         List<CategoryDefinition> categories = new ArrayList<>();
-        Either<Vertex, JanusGraphOperationStatus> childVertex = janusGraphDao
-            .getChildVertex(vertex, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
+        Either<Vertex, JanusGraphOperationStatus> childVertex = janusGraphDao.getChildVertex(vertex, EdgeLabelEnum.CATEGORY);
         if (childVertex.isRight()) {
             log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, catalogComponent.getUniqueId(),
                 childVertex.right().value());
@@ -1054,6 +1071,9 @@ public abstract class ToscaElementOperation extends BaseOperation {
         CategoryDefinition category = new CategoryDefinition();
         category.setUniqueId((String) categoryV.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value());
         category.setNormalizedName(categoryNormalizedName);
+        category.setModels(categoryV.property(GraphPropertyEnum.MODEL.getProperty()).isPresent() ? getGson()
+            .fromJson((String) categoryV.property(GraphPropertyEnum.MODEL.getProperty()).value(), new TypeToken<List<String>>() {
+            }.getType()) : Collections.emptyList());
         category.setName((String) categoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
         category.setUseServiceSubstitutionForNestedServices(
             (Boolean) categoryV.property(GraphPropertyEnum.USE_SUBSTITUTION_FOR_NESTED_SERVICES.getProperty()).orElse(false));
@@ -1062,6 +1082,10 @@ public abstract class ToscaElementOperation extends BaseOperation {
         List<MetadataKeyDataDefinition> metadataKeys = categoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).isPresent() ? getGson()
             .fromJson((String) categoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).value(), listTypeCat) : Collections.emptyList();
         category.setMetadataKeys(metadataKeys);
+        VertexProperty<Object> property = categoryV.property(GraphPropertyEnum.NOT_APPLICABLE_METADATA_KEYS.getProperty());
+        category.setNotApplicableMetadataKeys(
+            property.isPresent() ? getGson().fromJson((String) property.value(), new TypeToken<List<String>>() {
+            }.getType()) : Collections.emptyList());
         categories.add(category);
         catalogComponent.setCategories(categories);
         return JanusGraphOperationStatus.OK;
@@ -1223,9 +1247,13 @@ public abstract class ToscaElementOperation extends BaseOperation {
     }
 
     private void generateNewToscaFileName(String componentType, String componentName, ArtifactDataDefinition artifactInfo) {
-        Map<String, Object> getConfig = (Map<String, Object>) ConfigurationManager.getConfigurationManager().getConfiguration().getToscaArtifacts()
-            .entrySet().stream().filter(p -> p.getKey().equalsIgnoreCase(artifactInfo.getArtifactLabel())).findAny().get().getValue();
-        artifactInfo.setArtifactName(componentType + "-" + componentName + getConfig.get("artifactName"));
+        Optional<Entry<String, Object>> oConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getToscaArtifacts()
+            .entrySet().stream().filter(p -> p.getKey().equalsIgnoreCase(artifactInfo.getArtifactLabel())).findAny();
+        if (oConfig.isPresent()) {
+            artifactInfo.setArtifactName(componentType + "-" + componentName + ((Map<String, Object>) oConfig.get().getValue()).get("artifactName"));
+        } else {
+            artifactInfo.setArtifactName(componentType + "-" + componentName);
+        }
     }
 
     protected <T extends ToscaElement> StorageOperationStatus validateResourceCategory(T toscaElementToUpdate, GraphVertex elementV) {
@@ -1313,7 +1341,7 @@ public abstract class ToscaElementOperation extends BaseOperation {
         StopWatch stopWatch = new StopWatch();
         stopWatch.start();
         Map<String, CatalogComponent> existInCatalog = new HashMap<>();
-        Either<Iterator<Vertex>, JanusGraphOperationStatus> verticesEither = janusGraphDao.getCatalogOrArchiveVerticies(isCatalog);
+        Either<Iterator<Vertex>, JanusGraphOperationStatus> verticesEither = janusGraphDao.getCatalogOrArchiveVertices(isCatalog);
         if (verticesEither.isRight()) {
             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(verticesEither.right().value()));
         }
@@ -1336,6 +1364,7 @@ public abstract class ToscaElementOperation extends BaseOperation {
         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()));
@@ -1346,6 +1375,7 @@ public abstract class ToscaElementOperation extends BaseOperation {
             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()));
@@ -1387,7 +1417,7 @@ public abstract class ToscaElementOperation extends BaseOperation {
         return isAddToCatalog;
     }
 
-    public Either<List<GraphVertex>, JanusGraphOperationStatus> getListOfHighestComponents(ComponentTypeEnum componentType,
+    private Either<List<GraphVertex>, JanusGraphOperationStatus> getListOfHighestComponents(ComponentTypeEnum componentType,
                                                                                            List<ResourceTypeEnum> excludeTypes,
                                                                                            JsonParseFlagEnum parseFlag) {
         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
@@ -1404,7 +1434,7 @@ public abstract class ToscaElementOperation extends BaseOperation {
     }
 
     // highest + (certified && !highest)
-    public Either<List<GraphVertex>, JanusGraphOperationStatus> getListOfHighestAndAllCertifiedComponents(ComponentTypeEnum componentType,
+    private Either<List<GraphVertex>, JanusGraphOperationStatus> getListOfHighestAndAllCertifiedComponents(ComponentTypeEnum componentType,
                                                                                                           List<ResourceTypeEnum> excludeTypes) {
         long startFetchAllStates = System.currentTimeMillis();
         Either<List<GraphVertex>, JanusGraphOperationStatus> highestNodes = getListOfHighestComponents(componentType, excludeTypes,