aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris André <chris.andre@yoppworks.com>2020-05-25 12:51:38 -0400
committerOfir Sonsino <ofir.sonsino@intl.att.com>2020-06-03 06:58:23 +0000
commitf1922cef9735e44b3e5116e86a89c7e2449b1ce1 (patch)
tree54c13935bcff99166e4b5cdd097fdc74ab90b087
parent8a5f3aebc6b5681c5c44307b7a0cdf868802e790 (diff)
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 <chris.andre@yoppworks.com> Change-Id: I52faffdaf2e99a9de8c2e566da26a5be6e7fd667
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementLifecycleOperation.java114
1 files 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<ToscaElement, StorageOperationStatus> undoCheckout(String toscaElementId) {
- Either<ToscaElement, StorageOperationStatus> result = null;
- Either<GraphVertex, JanusGraphOperationStatus> getToscaElementRes = null;
- Iterator<Edge> 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<P2<GraphVertex, JanusGraphVertex>, StorageOperationStatus> retrieveAndUpdatePreviousVersion(
+ final GraphVertex currVersionV) {
+ if (!hasPreviousVersion(currVersionV)) {
+ return Either.left(p(currVersionV, null));
+ } else {
+ // find previous version
+ Iterator<Edge> 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<GraphVertex, JanusGraphVertex> 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<ToscaElement, StorageOperationStatus> updateEdgeToCatalogRootAndReturnPreVersionElement(
+ final P2<GraphVertex, JanusGraphVertex> 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<ToscaElement, StorageOperationStatus> 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) {