From f1922cef9735e44b3e5116e86a89c7e2449b1ce1 Mon Sep 17 00:00:00 2001 From: Chris André Date: Mon, 25 May 2020 12:51:38 -0400 Subject: Refactor method 'undoCheckout' to simplify it and remove potential NPE - Extracted methods 'retrieveAndUpdatePreviousVersion' and 'updateEdgeToCatalogRootAndReturnPreVersionElement' from 'undoCheckout' Issue-ID: SDC-2965 Signed-off-by: Chris Andre Change-Id: I52faffdaf2e99a9de8c2e566da26a5be6e7fd667 --- .../operations/ToscaElementLifecycleOperation.java | 114 +++++++++++++-------- 1 file changed, 70 insertions(+), 44 deletions(-) diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementLifecycleOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementLifecycleOperation.java index 273d5001f6..372264cff2 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementLifecycleOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementLifecycleOperation.java @@ -20,6 +20,9 @@ package org.openecomp.sdc.be.model.jsonjanusgraph.operations; +import static fj.P.p; + +import fj.P2; import org.janusgraph.core.JanusGraphVertex; import fj.data.Either; import org.apache.commons.collections.CollectionUtils; @@ -223,54 +226,77 @@ public class ToscaElementLifecycleOperation extends BaseOperation { * @return */ public Either undoCheckout(String toscaElementId) { - Either result = null; - Either getToscaElementRes = null; - Iterator nextVersionComponentIter = null; - ToscaElementOperation operation; - Vertex preVersionVertex = null; try { - getToscaElementRes = janusGraphDao.getVertexById(toscaElementId, JsonParseFlagEnum.ParseMetadata); - if (getToscaElementRes.isRight()) { - CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId); - result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getToscaElementRes.right().value())); - } - GraphVertex currVersionV = getToscaElementRes.left().value(); - if (result == null && hasPreviousVersion(currVersionV)) { - // find previous version - nextVersionComponentIter = currVersionV.getVertex().edges(Direction.IN, EdgeLabelEnum.VERSION.name()); - if (nextVersionComponentIter == null || !nextVersionComponentIter.hasNext()) { - CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch previous version of tosca element with name {}. ", currVersionV.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME).toString()); - result = Either.right(StorageOperationStatus.NOT_FOUND); - } - if (result == null) { - preVersionVertex = nextVersionComponentIter.next().outVertex(); - StorageOperationStatus updateOldResourceResult = updateOldToscaElementBeforeUndoCheckout(preVersionVertex); - if (updateOldResourceResult != StorageOperationStatus.OK) { - result = Either.right(updateOldResourceResult); - } - } - } - if (result == null) { - StorageOperationStatus updateCatalogRes = updateEdgeToCatalogRootByUndoCheckout((JanusGraphVertex) preVersionVertex, currVersionV); - if (updateCatalogRes != StorageOperationStatus.OK) { - return Either.right(updateCatalogRes); - } - operation = getToscaElementOperation(currVersionV.getLabel()); - result = operation.deleteToscaElement(currVersionV); - if (result.isRight()) { - return result; - } - if(preVersionVertex != null){ - String uniqueIdPreVer = (String) janusGraphDao.getProperty((JanusGraphVertex) preVersionVertex, GraphPropertyEnum.UNIQUE_ID.getProperty()); - result = operation.getToscaElement(uniqueIdPreVer); - }else{ - result = Either.left(null); + return janusGraphDao.getVertexById(toscaElementId, JsonParseFlagEnum.ParseMetadata) + .right().map(errorStatus -> { + CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId); + return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(errorStatus); + }) + .left().bind(this::retrieveAndUpdatePreviousVersion) + .left().bind(this::updateEdgeToCatalogRootAndReturnPreVersionElement); + } catch (Exception e) { + CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occurred during undo checkout tosca element {}. {}", toscaElementId, e.getMessage()); + return null; + } + } + + private Either, StorageOperationStatus> retrieveAndUpdatePreviousVersion( + final GraphVertex currVersionV) { + if (!hasPreviousVersion(currVersionV)) { + return Either.left(p(currVersionV, null)); + } else { + // find previous version + Iterator nextVersionComponentIter = currVersionV.getVertex() + .edges(Direction.IN, EdgeLabelEnum.VERSION.name()); + + if (nextVersionComponentIter == null || !nextVersionComponentIter.hasNext()) { + CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, + "Failed to fetch previous version of tosca element with name {}. ", + currVersionV.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME).toString()); + return Either.right(StorageOperationStatus.NOT_FOUND); + } else { + Vertex preVersionVertex = nextVersionComponentIter.next().outVertex(); + StorageOperationStatus updateOldResourceResult = updateOldToscaElementBeforeUndoCheckout( + preVersionVertex); + if (updateOldResourceResult != StorageOperationStatus.OK) { + return Either.right(updateOldResourceResult); + } else { + P2 result = p(currVersionV, (JanusGraphVertex) preVersionVertex); + + return Either.left(result); } } - } catch (Exception e) { - CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during undo checkout tosca element {}. {}", toscaElementId, e.getMessage()); } - return result; + } + + private Either updateEdgeToCatalogRootAndReturnPreVersionElement( + final P2 tuple) { + + final GraphVertex currVersionV = tuple._1(); + final JanusGraphVertex preVersionVertex = tuple._2(); + + StorageOperationStatus updateCatalogRes = updateEdgeToCatalogRootByUndoCheckout(preVersionVertex, currVersionV); + if (updateCatalogRes != StorageOperationStatus.OK) { + return Either.right(updateCatalogRes); + } else { + final ToscaElementOperation operation = getToscaElementOperation(currVersionV.getLabel()); + + return operation.deleteToscaElement(currVersionV) + .left().bind(discarded -> getUpdatedPreVersionElement(operation, preVersionVertex)); + } + } + + private Either getUpdatedPreVersionElement( + final ToscaElementOperation operation, + final JanusGraphVertex preVersionVertex) { + + if (preVersionVertex == null) { + return Either.left(null); + } else { + String uniqueIdPreVer = (String) janusGraphDao + .getProperty(preVersionVertex, GraphPropertyEnum.UNIQUE_ID.getProperty()); + return operation.getToscaElement(uniqueIdPreVer); + } } private boolean hasPreviousVersion(GraphVertex toscaElementVertex) { -- cgit 1.2.3-korg