Work around potential NullPointerExceptions in `ToscaOperationFacade`
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ToscaOperationFacade.java
index 5b3aea0..cde3ef1 100644 (file)
@@ -177,24 +177,30 @@ public class ToscaOperationFacade {
         return getToscaElementByOperation(componentV, new ComponentParametersView());
     }
 
-    private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV, ComponentParametersView filters) {
-        VertexTypeEnum label = componentV.getLabel();
-
-        ToscaElementOperation toscaOperation = getToscaElementOperation(componentV);
-        log.debug("getToscaElementByOperation: toscaOperation={}", toscaOperation.getClass());
-        Either<ToscaElement, StorageOperationStatus> toscaElement;
-        String componentId = componentV.getUniqueId();
-        if (toscaOperation != null) {
-            log.debug("Need to fetch tosca element for id {}", componentId);
-            toscaElement = toscaOperation.getToscaElement(componentV, filters);
+    private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV,
+        ComponentParametersView filters) {
+        if (componentV == null) {
+            log.debug("Unexpected null value for `componentV`");
+            return Either.right(StorageOperationStatus.GENERAL_ERROR);
         } else {
-            log.debug("not supported tosca type {} for id {}", label, componentId);
-            toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
-        }
-        if (toscaElement.isRight()) {
-            return Either.right(toscaElement.right().value());
+            VertexTypeEnum label = componentV.getLabel();
+
+            ToscaElementOperation toscaOperation = getToscaElementOperation(componentV);
+            if (toscaOperation != null) {
+                log.debug("getToscaElementByOperation: toscaOperation={}", toscaOperation.getClass());
+            }
+
+            Either<ToscaElement, StorageOperationStatus> toscaElement;
+            String componentId = componentV.getUniqueId();
+            if (toscaOperation != null) {
+                log.debug("Need to fetch tosca element for id {}", componentId);
+                toscaElement = toscaOperation.getToscaElement(componentV, filters);
+            } else {
+                log.debug("not supported tosca type {} for id {}", label, componentId);
+                toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
+            }
+            return toscaElement.left().map(ModelConverter::convertFromToscaElement);
         }
-        return Either.left(ModelConverter.convertFromToscaElement(toscaElement.left().value()));
     }
 
     // endregion
@@ -357,9 +363,9 @@ public class ToscaOperationFacade {
         return getLatestCertifiedByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata);
     }
 
-    public Either<Resource, StorageOperationStatus> getLatestCertifiedByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType, JsonParseFlagEnum parseFlag) {
+    public Either<Resource, StorageOperationStatus> getLatestCertifiedByToscaResourceName(String toscaResourceName,
+        VertexTypeEnum vertexType, JsonParseFlagEnum parseFlag) {
 
-        Either<Resource, StorageOperationStatus> result = null;
         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
@@ -367,25 +373,34 @@ public class ToscaOperationFacade {
         Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestRes = janusGraphDao
             .getByCriteria(vertexType, props, parseFlag);
 
-        if (getLatestRes.isRight()) {
-            JanusGraphOperationStatus status = getLatestRes.right().value();
-            CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status);
-            result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
-        }
-        if (result == null) {
-            List<GraphVertex> resources = getLatestRes.left().value();
-            double version = 0.0;
-            GraphVertex highestResource = null;
-            for (GraphVertex resource : resources) {
-                double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
-                if (resourceVersion > version) {
-                    version = resourceVersion;
-                    highestResource = resource;
+        return getLatestRes
+            .right().map(
+                status -> {
+                    CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ",
+                        vertexType, toscaResourceName, status);
+                    return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
                 }
-            }
-            result = getToscaFullElement(highestResource.getUniqueId());
-        }
-        return result;
+            )
+            .left().bind(
+                resources -> {
+                    double version = 0.0;
+                    GraphVertex highestResource = null;
+                    for (GraphVertex resource : resources) {
+                        double resourceVersion = Double
+                            .parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
+                        if (resourceVersion > version) {
+                            version = resourceVersion;
+                            highestResource = resource;
+                        }
+                    }
+                    if (highestResource != null) {
+                        return getToscaFullElement(highestResource.getUniqueId());
+                    } else {
+                        log.debug("The vertex with the highest version could not be found for {}", toscaResourceName);
+                        return Either.right(StorageOperationStatus.GENERAL_ERROR);
+                    }
+                }
+            );
     }
 
     public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExists(String templateName) {
@@ -540,12 +555,20 @@ public class ToscaOperationFacade {
         ToscaElementOperation toscaElementOperation = getToscaElementOperation(elementV);
 
         ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(componentToUpdate);
-        Either<ToscaElement, StorageOperationStatus> updateToscaElement = toscaElementOperation.updateToscaElement(toscaElementToUpdate, elementV, filterResult);
-        if (updateToscaElement.isRight()) {
-            log.debug("Failed to update tosca element {} error {}", componentId, updateToscaElement.right().value());
-            return Either.right(updateToscaElement.right().value());
+        Either<ToscaElement, StorageOperationStatus> updateToscaElement = null;
+        if (toscaElementOperation != null) {
+            updateToscaElement = toscaElementOperation.updateToscaElement(toscaElementToUpdate, elementV, filterResult);
+        } else {
+            log.debug("Null value returned by `getToscaElementOperation` with value {}", elementV);
+            updateToscaElement = Either.right(StorageOperationStatus.GENERAL_ERROR);
         }
-        return Either.left(ModelConverter.convertFromToscaElement(updateToscaElement.left().value()));
+
+        return updateToscaElement.bimap(
+            ModelConverter::convertFromToscaElement,
+            status -> {
+                log.debug("Failed to update tosca element {} error {}", componentId, status);
+                return status;
+            });
     }
 
     private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName, JsonParseFlagEnum parseFlag) {