Add ArtifactsBusinessLogic::fetchLatestComponentMetadata 14/107414/7
authorFrancis Toth <francis.toth@yoppworks.com>
Fri, 8 May 2020 22:45:14 +0000 (18:45 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Wed, 27 May 2020 06:40:43 +0000 (06:40 +0000)
Signed-off-by: Francis Toth <francis.toth@yoppworks.com>
Change-Id: Ibb8621a91551a2e4ffcac1145f85ed7983063d02
Issue-ID: SDC-2812

catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java

index c56517c..17f8bde 100644 (file)
@@ -3958,13 +3958,8 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic {
         String origMd5 = request.getHeader(Constants.MD5_HEADER);
         String userId = request.getHeader(Constants.USER_ID_HEADER);
 
-        Either<ComponentMetadataData, StorageOperationStatus> getComponentRes =
-                toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
-        if (getComponentRes.isRight()) {
-            StorageOperationStatus status = getComponentRes.right().value();
-            log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
-            throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status, componentType), componentUuid);
-        }
+        Either<ComponentMetadataData, ActionStatus> getComponentRes =
+            fetchLatestComponentMetadataOrThrow(componentType, componentUuid);
 
         ComponentMetadataDataDefinition componentMetadataDataDefinition = getComponentRes.left().value().getMetadataDataDefinition();
         componentId = componentMetadataDataDefinition.getUniqueId();
@@ -4007,12 +4002,9 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic {
         String userId = request.getHeader(Constants.USER_ID_HEADER);
 
         ImmutablePair<Component, ComponentInstance> componentRiPair = null;
-        Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
-        if (getComponentRes.isRight()) {
-            StorageOperationStatus status = getComponentRes.right().value();
-            log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
-            throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status, componentType), resourceInstanceName);
-        }
+
+        Either<ComponentMetadataData, ActionStatus> getComponentRes =
+            fetchLatestComponentMetadataOrThrow(componentType, componentUuid, resourceInstanceName);
         if (!getComponentRes.left()
                 .value()
                 .getMetadataDataDefinition()
@@ -4062,12 +4054,8 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic {
         String origMd5 = request.getHeader(Constants.MD5_HEADER);
         String userId = request.getHeader(Constants.USER_ID_HEADER);
 
-        Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
-        if (getComponentRes.isRight()) {
-            StorageOperationStatus status = getComponentRes.right().value();
-            log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
-            throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
-        }
+        Either<ComponentMetadataData, ActionStatus> getComponentRes =
+            fetchLatestComponentMetadataOrThrow(componentType, componentUuid);
         componentId = getComponentRes.left().value().getMetadataDataDefinition().getUniqueId();
         String componentName = getComponentRes.left().value().getMetadataDataDefinition().getName();
 
@@ -4119,12 +4107,8 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic {
         String userId = request.getHeader(Constants.USER_ID_HEADER);
 
         ImmutablePair<Component, ComponentInstance> componentRiPair = null;
-        Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
-        if (getComponentRes.isRight()) {
-            StorageOperationStatus status = getComponentRes.right().value();
-            log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
-            throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
-        }
+        Either<ComponentMetadataData, ActionStatus> getComponentRes =
+            fetchLatestComponentMetadataOrThrow(componentType, componentUuid);
         if (!getComponentRes.left()
                 .value()
                 .getMetadataDataDefinition()
@@ -4229,11 +4213,14 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic {
         ArtifactDefinition existingArtifactInfo = null;
         String interfaceName = null;
 
-        Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
-        if (getComponentRes.isRight()) {
-            StorageOperationStatus status = getComponentRes.right().value();
-            log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
-            errorWrapper.setInnerElement(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(status)));
+        Either<ComponentMetadataData, ActionStatus> getComponentRes =
+            fetchLatestComponentMetadata(componentType, componentUuid).right().map(as -> {
+                errorWrapper.setInnerElement(componentsUtils.getResponseFormat(as));
+                return as;
+            });
+
+        if(getComponentRes.isRight()) {
+            errorWrapper.setInnerElement(componentsUtils.getResponseFormat(getComponentRes.right().value()));
         }
 
         if (errorWrapper.isEmpty()) {
@@ -4309,6 +4296,32 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic {
         return updateArtifactResult;
     }
 
+
+    private Either<ComponentMetadataData, ActionStatus> fetchLatestComponentMetadataOrThrow(
+        ComponentTypeEnum componentType, String componentUuid
+    ) {
+        return fetchLatestComponentMetadataOrThrow(componentType, componentUuid, componentUuid);
+    }
+
+    private Either<ComponentMetadataData, ActionStatus> fetchLatestComponentMetadataOrThrow(
+        ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName
+    ) {
+        return fetchLatestComponentMetadata(componentType, componentUuid).right().map(as -> {
+            throw new ByActionStatusComponentException(as, resourceInstanceName);
+        });
+    }
+
+    private Either<ComponentMetadataData, ActionStatus> fetchLatestComponentMetadata(
+        ComponentTypeEnum componentType, String componentUuid
+    ) {
+        return toscaOperationFacade
+            .getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true)
+            .right().map(sos -> {
+                log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, sos);
+                return componentsUtils.convertFromStorageResponse(sos, componentType);
+            });
+    }
+
     private Either<String, ResponseFormat> fetchInterfaceName(String componentId, String interfaceUUID) {
         Either<Component, StorageOperationStatus> componentStorageOperationStatusEither = toscaOperationFacade.getToscaElement(componentId);
         if (componentStorageOperationStatusEither.isRight()) {
@@ -4347,12 +4360,8 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic {
         String origMd5 = request.getHeader(Constants.MD5_HEADER);
         String userId = request.getHeader(Constants.USER_ID_HEADER);
 
-        Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
-        if (getComponentRes.isRight()) {
-            StorageOperationStatus status = getComponentRes.right().value();
-            log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
-            throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status, componentType), componentUuid);
-        }
+        Either<ComponentMetadataData, ActionStatus> getComponentRes =
+            fetchLatestComponentMetadataOrThrow(componentType, componentUuid);
         componentId = getComponentRes.left().value().getMetadataDataDefinition().getUniqueId();
         String componentName = getComponentRes.left().value().getMetadataDataDefinition().getName();
         if (!getComponentRes.left()
@@ -4395,13 +4404,8 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic {
         String origMd5 = request.getHeader(Constants.MD5_HEADER);
         String userId = request.getHeader(Constants.USER_ID_HEADER);
         ImmutablePair<Component, ComponentInstance> componentRiPair = null;
-        Either<ComponentMetadataData, StorageOperationStatus> getComponentRes =
-                toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
-        if (getComponentRes.isRight()) {
-            StorageOperationStatus status = getComponentRes.right().value();
-            log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
-            throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
-        }
+        Either<ComponentMetadataData, ActionStatus> getComponentRes =
+            fetchLatestComponentMetadataOrThrow(componentType, componentUuid);
         if (!getComponentRes.left()
                 .value()
                 .getMetadataDataDefinition()