Added early check for null value for `containerV` 62/106162/4
authorChris André <chris.andre@yoppworks.com>
Thu, 16 Apr 2020 21:48:06 +0000 (17:48 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Thu, 23 Apr 2020 05:38:30 +0000 (05:38 +0000)
- Formatting code following project's code style
- Added early check for null value for `containerV`

Issue-ID: SDC-2905
Signed-off-by: Chris Andre <chris.andre@yoppworks.com>
Change-Id: I648e9a4a04be3f2e7cd3e30a70c04de2257a99d3

asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig2002/SdcCollapsingRolesCERTIFIEDstateMigration.java

index 1d7d3d1..9cf56cf 100644 (file)
@@ -70,7 +70,7 @@ public class SdcCollapsingRolesCERTIFIEDstateMigration extends InstanceMigration
     public MigrationResult migrate() {
         StorageOperationStatus status = updateServiceLifeCycleState();
         return status == StorageOperationStatus.OK ?
-                MigrationResult.success() : MigrationResult.error("failed to service state. Error : " + status);
+            MigrationResult.success() : MigrationResult.error("failed to service state. Error : " + status);
     }
 
     protected StorageOperationStatus updateServiceLifeCycleState() {
@@ -79,7 +79,9 @@ public class SdcCollapsingRolesCERTIFIEDstateMigration extends InstanceMigration
         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
-        Either<List<GraphVertex>, JanusGraphOperationStatus> byCriteria = janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
+        Either<List<GraphVertex>, JanusGraphOperationStatus> byCriteria = janusGraphDao
+            .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch,
+                JsonParseFlagEnum.ParseAll);
         return byCriteria.either(this::proceed, this::handleError);
     }
 
@@ -87,51 +89,64 @@ public class SdcCollapsingRolesCERTIFIEDstateMigration extends InstanceMigration
     protected StorageOperationStatus handleOneContainer(GraphVertex containerVorig) {
         StorageOperationStatus status = StorageOperationStatus.NOT_FOUND;
         GraphVertex containerV = getVertexById(containerVorig.getUniqueId());
-        try {
 
-            //update edges to meet above change
-            // update LS eges from RFC to  NOT_CERTIFIED_CHECKIN
+        if (containerV == null) {
+            log.error("Unexpected null value for `containerV`");
+        } else {
+            try {
 
-            updateEdgeProperty(EdgePropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name(), getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_STATE));
+                //update edges to meet above change
+                // update LS eges from RFC to  NOT_CERTIFIED_CHECKIN
 
-            if (containerV.getMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS).equals(DistributionStatusEnum.DISTRIBUTION_APPROVED.name()) || containerV.getMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS).equals(DistributionStatusEnum.DISTRIBUTION_REJECTED.name())) {
+                updateEdgeProperty(EdgePropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name(),
+                    getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_STATE));
 
-                // update vertex state property from DISTRIBUTION_APPROVED/REJECTED to DISTRIBUTION_NOT_APPROVED state
+                if (containerV.getMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS)
+                    .equals(DistributionStatusEnum.DISTRIBUTION_APPROVED.name()) || containerV
+                    .getMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS)
+                    .equals(DistributionStatusEnum.DISTRIBUTION_REJECTED.name())) {
 
-                Map<GraphPropertyEnum, Object> metadataProperties = containerV.getMetadataProperties();
-                metadataProperties.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
-                containerV.setMetadataProperties(metadataProperties);
+                    // update vertex state property from DISTRIBUTION_APPROVED/REJECTED to DISTRIBUTION_NOT_APPROVED state
 
-                //update edges to meet above change
-                //delete LAST_DISTRIBUTION_STATE_MODIFIER edge
+                    Map<GraphPropertyEnum, Object> metadataProperties = containerV.getMetadataProperties();
+                    metadataProperties.put(GraphPropertyEnum.DISTRIBUTION_STATUS,
+                        DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
+                    containerV.setMetadataProperties(metadataProperties);
 
-                removeEdges(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER));
+                    //update edges to meet above change
+                    //delete LAST_DISTRIBUTION_STATE_MODIFIER edge
 
-            }
+                    removeEdges(
+                        getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER));
+
+                }
 
-            status = updateVertexAndCommit(containerV);
+                status = updateVertexAndCommit(containerV);
 
-        } catch (NullPointerException e) {
-            log.error("Null Pointer Exception occurred - this mean we have zombie vertex, migration task will continue anyway", e);
-            status = StorageOperationStatus.EXEUCTION_FAILED;
-        } catch (Exception e) {
-            //it is happy flow as well
-            log.error("Exception occurred:", e);
-            log.error("Migration task will continue anyway, please find below vertex details related to this exception", e);
-            if (containerV != null) {
+            } catch (NullPointerException e) {
+                log.error(
+                    "Null Pointer Exception occurred - this mean we have zombie vertex, migration task will continue anyway",
+                    e);
+                status = StorageOperationStatus.EXEUCTION_FAILED;
+            } catch (Exception e) {
+                //it is happy flow as well
+                log.error("Exception occurred:", e);
+                log.error(
+                    "Migration task will continue anyway, please find below vertex details related to this exception",
+                    e);
                 log.error("containerV.getUniqueId() ---> {}  ", containerV.getUniqueId());
-            }
 
-        } finally {
-            if (status != StorageOperationStatus.OK) {
-                janusGraphDao.rollback();
-                log.info("failed to update vertex ID {} ", containerV.getUniqueId());
-                log.info("Storage Operation Status {}", status.toString());
-            } else {
-                log.info("vertex ID {} successfully updated", containerV.getUniqueId());
+            } finally {
+                if (status != StorageOperationStatus.OK) {
+                    janusGraphDao.rollback();
+                    log.info("failed to update vertex ID {} ", containerV.getUniqueId());
+                    log.info("Storage Operation Status {}", status.toString());
+                } else {
+                    log.info("vertex ID {} successfully updated", containerV.getUniqueId());
+                }
             }
-
         }
+
         return status;
     }