aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main/java
diff options
context:
space:
mode:
authorChris André <chris.andre@yoppworks.com>2020-06-02 14:18:43 -0400
committerChris André <chris.andre@yoppworks.com>2020-06-03 11:01:57 -0400
commit116c26e1c243f26258558148e9f69e42b3787367 (patch)
treede741c90500074653505e404746d31bfe57914fe /catalog-model/src/main/java
parentf1922cef9735e44b3e5116e86a89c7e2449b1ce1 (diff)
Rewrite of `certifyToscaElement` to remove NPE
- Modularized `certifyToscaElement` in smaller, less complex methods (`handleRelationsBeforeCertifyingAndProcessClone`, `cloneToscaElementAndHandleRelations`, `handleRelationsOfNewestCertifiedToscaElementAndReturn` & `logDebugMessageAndReturnStorageOperationStatus`) - Removed potential NPE flagged by Sonar in `certifyToscaElement` Issue-ID: SDC-2966 Signed-off-by: Chris Andre <chris.andre@yoppworks.com> Change-Id: Id65871cbfd611abdcc878e61c1541f01ff972828
Diffstat (limited to 'catalog-model/src/main/java')
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementLifecycleOperation.java119
1 files changed, 77 insertions, 42 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 372264cff2..9c588d9596 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
@@ -307,51 +307,86 @@ public class ToscaElementLifecycleOperation extends BaseOperation {
return hasPreviousVersion;
}
- public Either<ToscaElement, StorageOperationStatus> certifyToscaElement(String toscaElementId, String modifierId, String ownerId) {
- Either<ToscaElement, StorageOperationStatus> result = null;
- Either<GraphVertex, StorageOperationStatus> cloneRes = null;
- GraphVertex toscaElement = null;
- GraphVertex modifier = null;
- GraphVertex certifiedToscaElement = null;
- Integer majorVersion = null;
-
- StorageOperationStatus status;
+ public Either<ToscaElement, StorageOperationStatus> certifyToscaElement(
+ String toscaElementId,
+ String modifierId,
+ String ownerId) {
try {
- Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
- .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
- if (getVerticesRes.isRight()) {
- CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
- result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
- }
- if (result == null) {
- toscaElement = getVerticesRes.left().value().get(toscaElementId);
- modifier = getVerticesRes.left().value().get(modifierId);
- majorVersion = getMajorVersion((String) toscaElement.getMetadataProperty(GraphPropertyEnum.VERSION));
- status = handleRelationsOfPreviousToscaElementBeforeCertifying(toscaElement, modifier, majorVersion);
- if (status != StorageOperationStatus.OK) {
- CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations of previous tosca element before certifying {}. Status is {}. ", toscaElement.getUniqueId(), status);
- }
- }
- if (result == null) {
- cloneRes = cloneToscaElementForCertify(toscaElement, modifier, majorVersion);
- if (cloneRes.isRight()) {
- CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to clone tosca element during certification. ");
- result = Either.right(cloneRes.right().value());
- } else {
- certifiedToscaElement = cloneRes.left().value();
- status = handleRelationsOfNewestCertifiedToscaElement(toscaElement, certifiedToscaElement);
- if (status != StorageOperationStatus.OK) {
- CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations of newest certified tosca element {}. Status is {}. ", certifiedToscaElement.getUniqueId(), status);
- }
- }
- }
- if (result == null) {
- return getToscaElementOperation(toscaElement.getLabel()).getToscaElement(certifiedToscaElement.getUniqueId());
- }
+ return janusGraphDao
+ .getVerticesByUniqueIdAndParseFlag(
+ prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId))
+ .right().map(status ->
+ logDebugMessageAndReturnStorageOperationStatus(
+ DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status),
+ FAILED_TO_GET_VERTICES,
+ toscaElementId))
+ .left().bind(verticesRes -> {
+ GraphVertex toscaElement = verticesRes.get(toscaElementId);
+ GraphVertex modifier = verticesRes.get(modifierId);
+ Integer majorVersion = getMajorVersion(
+ (String) toscaElement.getMetadataProperty(GraphPropertyEnum.VERSION));
+
+ return handleRelationsBeforeCertifyingAndProcessClone(toscaElement,
+ modifier, majorVersion);
+ });
} catch (Exception e) {
- CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during certification tosca element {}. {}", toscaElementId, e.getMessage());
+ CommonUtility
+ .addRecordToLog(log, LogLevelEnum.DEBUG,
+ "Exception occurred during certification tosca element {}.",
+ toscaElementId, e);
+ return Either.right(StorageOperationStatus.GENERAL_ERROR);
}
- return result;
+ }
+
+ private Either<ToscaElement, StorageOperationStatus> handleRelationsBeforeCertifyingAndProcessClone(
+ GraphVertex toscaElement,
+ GraphVertex modifier,
+ Integer majorVersion) {
+ StorageOperationStatus status = handleRelationsOfPreviousToscaElementBeforeCertifying(toscaElement,
+ modifier, majorVersion);
+ if (status != StorageOperationStatus.OK) {
+ return Either.right(
+ logDebugMessageAndReturnStorageOperationStatus(status,
+ "Failed to handle relations of previous tosca element before certifying {}. Status is {}. ",
+ toscaElement.getUniqueId(), status));
+ } else {
+ return cloneToscaElementAndHandleRelations(toscaElement, modifier, majorVersion);
+ }
+ }
+
+ private Either<ToscaElement, StorageOperationStatus> cloneToscaElementAndHandleRelations(
+ GraphVertex toscaElement,
+ GraphVertex modifier,
+ Integer majorVersion) {
+ return cloneToscaElementForCertify(toscaElement, modifier, majorVersion)
+ .right().map(status -> logDebugMessageAndReturnStorageOperationStatus(status,
+ "Failed to clone tosca element during certification. "))
+ .left().bind(certifiedToscaElement ->
+ handleRelationsOfNewestCertifiedToscaElementAndReturn(toscaElement, certifiedToscaElement));
+ }
+
+ private Either<ToscaElement, StorageOperationStatus> handleRelationsOfNewestCertifiedToscaElementAndReturn(
+ GraphVertex toscaElement,
+ GraphVertex certifiedToscaElement) {
+ StorageOperationStatus status = handleRelationsOfNewestCertifiedToscaElement(toscaElement,
+ certifiedToscaElement);
+ if (status != StorageOperationStatus.OK) {
+ return Either.right(
+ logDebugMessageAndReturnStorageOperationStatus(status,
+ "Failed to handle relations of newest certified tosca element {}. Status is {}. ",
+ certifiedToscaElement.getUniqueId(), status));
+ } else {
+ return getToscaElementOperation(toscaElement.getLabel())
+ .getToscaElement(certifiedToscaElement.getUniqueId());
+ }
+ }
+
+ private static StorageOperationStatus logDebugMessageAndReturnStorageOperationStatus(
+ final StorageOperationStatus status,
+ final String msg,
+ final Object... args) {
+ CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, msg, args);
+ return status;
}
private StorageOperationStatus handleRelationsOfNewestCertifiedToscaElement(GraphVertex toscaElement, GraphVertex certifiedToscaElement) {