Fix sonar issues
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / notification / LifecycleChangeNotificationManager.java
index 6a31e83..3ff0499 100644 (file)
@@ -20,10 +20,12 @@ import com.google.common.collect.Ordering;
 import com.google.gson.Gson;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
-import com.nokia.cbam.lcm.v32.ApiException;
 import com.nokia.cbam.lcm.v32.api.OperationExecutionsApi;
 import com.nokia.cbam.lcm.v32.api.VnfsApi;
 import com.nokia.cbam.lcm.v32.model.*;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
@@ -34,17 +36,14 @@ import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import java.util.List;
-import java.util.Optional;
-import java.util.Set;
+import static java.util.Optional.empty;
+import static java.util.Optional.of;
 
 import static com.google.common.collect.Iterables.find;
 import static com.google.common.collect.Iterables.tryFind;
 import static com.google.common.collect.Sets.newConcurrentHashSet;
 import static com.google.common.collect.Sets.newHashSet;
 import static com.nokia.cbam.lcm.v32.model.OperationType.INSTANTIATE;
-import static java.util.Optional.empty;
-import static java.util.Optional.of;
 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION;
@@ -118,7 +117,7 @@ public class LifecycleChangeNotificationManager implements ILifecycleChangeNotif
         }
         VnfsApi cbamLcmApi = restApiProvider.getCbamLcmApi(driverProperties.getVnfmId());
         try {
-            List<VnfInfo> vnfs = cbamLcmApi.vnfsGet(NOKIA_LCM_API_VERSION);
+            List<VnfInfo> vnfs = cbamLcmApi.vnfsGet(NOKIA_LCM_API_VERSION).blockingFirst();
             com.google.common.base.Optional<VnfInfo> currentVnf = tryFind(vnfs, vnf -> vnf.getId().equals(receivedNotification.getVnfInstanceId()));
             String vnfHeader = "The VNF with " + receivedNotification.getVnfInstanceId() + " identifier";
             if (!currentVnf.isPresent()) {
@@ -126,7 +125,7 @@ public class LifecycleChangeNotificationManager implements ILifecycleChangeNotif
                 //swallow LCN
                 return;
             } else {
-                VnfInfo vnf = cbamLcmApi.vnfsVnfInstanceIdGet(receivedNotification.getVnfInstanceId(), NOKIA_LCN_API_VERSION);
+                VnfInfo vnf = cbamLcmApi.vnfsVnfInstanceIdGet(receivedNotification.getVnfInstanceId(), NOKIA_LCN_API_VERSION).blockingFirst();
                 com.google.common.base.Optional<VnfProperty> externalVnfmId = tryFind(vnf.getExtensions(), prop -> prop.getName().equals(LifecycleManager.EXTERNAL_VNFM_ID));
                 if (!externalVnfmId.isPresent()) {
                     logger.warn(vnfHeader + " is not a managed VNF");
@@ -141,18 +140,24 @@ public class LifecycleChangeNotificationManager implements ILifecycleChangeNotif
             throw buildFatalFailure(logger, "Unable to list VNFs / query VNF", e);
         }
         OperationExecutionsApi cbamOperationExecutionApi = restApiProvider.getCbamOperationExecutionApi(driverProperties.getVnfmId());
+        List<OperationExecution> operationExecutions;
         try {
-            List<OperationExecution> operationExecutions = cbamLcmApi.vnfsVnfInstanceIdOperationExecutionsGet(receivedNotification.getVnfInstanceId(), NOKIA_LCM_API_VERSION);
-            OperationExecution operationExecution = cbamOperationExecutionApi.operationExecutionsOperationExecutionIdGet(receivedNotification.getLifecycleOperationOccurrenceId(), NOKIA_LCM_API_VERSION);
-            OperationExecution closestInstantiationToOperation = findLastInstantiationBefore(operationExecutions, operationExecution);
-            String vimId = getVimId(cbamOperationExecutionApi, closestInstantiationToOperation);
-            notificationSender.processNotification(receivedNotification, operationExecution, buildAffectedCps(operationExecution), vimId);
-            if (isTerminationFinished(receivedNotification)) {
-                //signal LifecycleManager to continue the deletion of the VNF
-                processedNotifications.add(new ProcessedNotification(receivedNotification.getLifecycleOperationOccurrenceId(), receivedNotification.getStatus()));
-            }
-        } catch (ApiException e) {
-            throw buildFatalFailure(logger, "Unable to retrieve the current VNF " + receivedNotification.getVnfInstanceId(), e);
+            operationExecutions = cbamLcmApi.vnfsVnfInstanceIdOperationExecutionsGet(receivedNotification.getVnfInstanceId(), NOKIA_LCM_API_VERSION).blockingFirst();
+        } catch (Exception e) {
+            throw buildFatalFailure(logger, "Unable to retrieve the operation executions for the VNF " + receivedNotification.getVnfInstanceId(), e);
+        }
+        OperationExecution operationExecution;
+        try {
+            operationExecution = cbamOperationExecutionApi.operationExecutionsOperationExecutionIdGet(receivedNotification.getLifecycleOperationOccurrenceId(), NOKIA_LCM_API_VERSION).blockingFirst();
+        } catch (Exception e) {
+            throw buildFatalFailure(logger, "Unable to retrieve the operation execution with " + receivedNotification.getLifecycleOperationOccurrenceId() + " identifier", e);
+        }
+        OperationExecution closestInstantiationToOperation = findLastInstantiationBefore(operationExecutions, operationExecution);
+        String vimId = getVimId(cbamOperationExecutionApi, closestInstantiationToOperation);
+        notificationSender.processNotification(receivedNotification, operationExecution, buildAffectedCps(operationExecution), vimId);
+        if (isTerminationFinished(receivedNotification)) {
+            //signal LifecycleManager to continue the deletion of the VNF
+            processedNotifications.add(new ProcessedNotification(receivedNotification.getLifecycleOperationOccurrenceId(), receivedNotification.getStatus()));
         }
     }
 
@@ -162,7 +167,7 @@ public class LifecycleChangeNotificationManager implements ILifecycleChangeNotif
 
     private String getVimId(OperationExecutionsApi cbamOperationExecutionApi, OperationExecution closestInstantiationToOperation) {
         try {
-            Object operationParams = cbamOperationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(closestInstantiationToOperation.getId(), NOKIA_LCM_API_VERSION);
+            Object operationParams = cbamOperationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(closestInstantiationToOperation.getId(), NOKIA_LCM_API_VERSION).blockingFirst();
             return getVimId(operationParams);
         } catch (Exception e) {
             throw buildFatalFailure(logger, "Unable to detect last instantiation operation", e);
@@ -187,6 +192,10 @@ public class LifecycleChangeNotificationManager implements ILifecycleChangeNotif
     }
 
     private Optional<ReportedAffectedConnectionPoints> buildAffectedCps(OperationExecution operationExecution) {
+        if (!isTerminal(operationExecution.getStatus())) {
+            //connection points can only be calculated after the operation has finished
+            return Optional.empty();
+        }
         if (operationExecution.getOperationType() == OperationType.TERMINATE) {
             String terminationType = childElement(new Gson().toJsonTree(operationExecution.getOperationParams()).getAsJsonObject(), "terminationType").getAsString();
             if (TerminationType.FORCEFUL.name().equals(terminationType)) {
@@ -194,6 +203,8 @@ public class LifecycleChangeNotificationManager implements ILifecycleChangeNotif
                 //calculated from operation execution result
                 logger.warn("Unable to send information related to affected connection points during forceful termination");
                 return empty();
+            } else {
+                //graceful termination should be handled as any other operation
             }
         }
         try {