bug fix - avoid npe in pnf flow 68/95468/2
authorLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Wed, 11 Sep 2019 07:37:21 +0000 (09:37 +0200)
committerLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Wed, 11 Sep 2019 09:04:07 +0000 (11:04 +0200)
Change-Id: I8e848c2bdcec0822ae08280223297b4825e9b7c2
Issue-ID: SO-2289
Signed-off-by: Lukasz Muszkieta <lukasz.muszkieta@nokia.com>
bpmn/mso-infrastructure-bpmn/src/test/java/org/onap/so/bpmn/infrastructure/pnf/delegate/DmaapClientTestImpl.java
bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/delegate/InformDmaapClient.java
bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/DmaapClient.java
bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java
bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/delegate/DmaapClientTestImpl.java

index dd993bc..0783178 100644 (file)
@@ -20,9 +20,8 @@
 
 package org.onap.so.bpmn.infrastructure.pnf.delegate;
 
-import java.util.HashMap;
+import java.util.Map;
 import java.util.Objects;
-import java.util.Optional;
 import org.onap.so.bpmn.infrastructure.pnf.dmaap.DmaapClient;
 import org.springframework.context.annotation.Primary;
 import org.springframework.stereotype.Component;
@@ -35,8 +34,7 @@ public class DmaapClientTestImpl implements DmaapClient {
     private Runnable informConsumer;
 
     @Override
-    public void registerForUpdate(String pnfCorrelationId, Runnable informConsumer,
-            Optional<HashMap<String, String>> updateInfo) {
+    public void registerForUpdate(String pnfCorrelationId, Runnable informConsumer, Map<String, String> updateInfo) {
         this.pnfCorrelationId = pnfCorrelationId;
         this.informConsumer = informConsumer;
     }
index 2ababac..a55f32a 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP - SO
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019 Nokia.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.so.bpmn.infrastructure.pnf.delegate;
 
+import java.util.Map;
 import org.camunda.bpm.engine.RuntimeService;
 import org.camunda.bpm.engine.delegate.DelegateExecution;
 import org.camunda.bpm.engine.delegate.JavaDelegate;
-import org.camunda.bpm.engine.runtime.Execution;
-import org.onap.aai.domain.yang.v13.Metadatum;
 import org.onap.so.bpmn.common.recipe.ResourceInput;
 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder;
-import org.onap.so.bpmn.core.json.JsonUtils;
 import org.onap.so.bpmn.infrastructure.pnf.dmaap.DmaapClient;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -39,7 +38,7 @@ import java.util.Optional;
 @Component
 public class InformDmaapClient implements JavaDelegate {
 
-    private Logger logger = LoggerFactory.getLogger(getClass());
+    private static final Logger LOGGER = LoggerFactory.getLogger(InformDmaapClient.class);
     private DmaapClient dmaapClient;
 
     @Override
@@ -47,25 +46,34 @@ public class InformDmaapClient implements JavaDelegate {
         String pnfCorrelationId = (String) execution.getVariable(ExecutionVariableNames.PNF_CORRELATION_ID);
         RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService();
         String processBusinessKey = execution.getProcessBusinessKey();
-        HashMap<String, String> updateInfo = createUpdateInfo(execution);
-        updateInfo.put("pnfCorrelationId", pnfCorrelationId);
-        dmaapClient
-                .registerForUpdate(pnfCorrelationId,
-                        () -> runtimeService.createMessageCorrelation("WorkflowMessage")
-                                .processInstanceBusinessKey(processBusinessKey).correlateWithResult(),
-                        Optional.of(updateInfo));
+        dmaapClient.registerForUpdate(pnfCorrelationId,
+                () -> runtimeService.createMessageCorrelation("WorkflowMessage")
+                        .processInstanceBusinessKey(processBusinessKey).correlateWithResult(),
+                createUpdateInfoMap(execution));
     }
 
-    private HashMap<String, String> createUpdateInfo(DelegateExecution execution) {
-        HashMap<String, String> map = new HashMap();
-
-        ResourceInput resourceInputObj = ResourceRequestBuilder
+    private Map<String, String> createUpdateInfoMap(DelegateExecution execution) {
+        Map<String, String> updateInfoMap = new HashMap<>();
+        updateInfoMap.put("pnfCorrelationId",
+                (String) execution.getVariable(ExecutionVariableNames.PNF_CORRELATION_ID));
+        getResourceInput(execution).ifPresent(resourceInput -> {
+            updateInfoMap.put("globalSubscriberID", resourceInput.getGlobalSubscriberId());
+            updateInfoMap.put("serviceType", resourceInput.getServiceType());
+            updateInfoMap.put("serviceInstanceId", resourceInput.getServiceInstanceId());
+        });
+        return updateInfoMap;
+    }
 
-                .getJsonObject((String) execution.getVariable("resourceInput"), ResourceInput.class);
-        map.put("globalSubscriberID", resourceInputObj.getGlobalSubscriberId());
-        map.put("serviceType", resourceInputObj.getServiceType());
-        map.put("serviceInstanceId", resourceInputObj.getServiceInstanceId());
-        return map;
+    private Optional<ResourceInput> getResourceInput(DelegateExecution execution) {
+        ResourceInput resourceInput = null;
+        if (execution.getVariable("resourceInput") != null) {
+            resourceInput = ResourceRequestBuilder.getJsonObject((String) execution.getVariable("resourceInput"),
+                    ResourceInput.class);
+        } else {
+            LOGGER.warn("resourceInput value is null for correlation id: {}",
+                    execution.getVariable(ExecutionVariableNames.PNF_CORRELATION_ID));
+        }
+        return Optional.ofNullable(resourceInput);
     }
 
     @Autowired
index d513684..bafb749 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP - SO
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019 Nokia.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
 
-import java.util.HashMap;
-import java.util.Optional;
+import java.util.Map;
 
 public interface DmaapClient {
 
-    void registerForUpdate(String pnfCorrelationId, Runnable informConsumer,
-            Optional<HashMap<String, String>> updateInfo);
+    void registerForUpdate(String pnfCorrelationId, Runnable informConsumer, Map<String, String> updateInfo);
 
     Runnable unregister(String pnfCorrelationId);
 }
index 48061db..02303a6 100644 (file)
@@ -54,8 +54,7 @@ public class PnfEventReadyDmaapClient implements DmaapClient {
     private int topicListenerDelayInSeconds;
     private volatile ScheduledThreadPoolExecutor executor;
     private volatile boolean dmaapThreadListenerIsRunning;
-
-    public volatile List<HashMap<String, String>> updateInfoMap;
+    private volatile List<Map<String, String>> listOfUpdateInfoMap;
 
     @Autowired
     public PnfEventReadyDmaapClient(Environment env) {
@@ -68,18 +67,15 @@ public class PnfEventReadyDmaapClient implements DmaapClient {
                 .port(env.getProperty("pnf.dmaap.port", Integer.class)).path(env.getProperty("pnf.dmaap.topicName"))
                 .path(env.getProperty("pnf.dmaap.consumerGroup")).path(env.getProperty("pnf.dmaap.consumerId"))
                 .build());
-        updateInfoMap = new ArrayList<>();
+        listOfUpdateInfoMap = new ArrayList<>();
     }
 
     @Override
     public synchronized void registerForUpdate(String pnfCorrelationId, Runnable informConsumer,
-            Optional<HashMap<String, String>> updateInfo) {
+            Map<String, String> updateInfo) {
         logger.debug("registering for pnf ready dmaap event for pnf correlation id: {}", pnfCorrelationId);
-        HashMap<String, String> map = updateInfo.get();
-        if (map != null && map.size() > 0) {
-            synchronized (updateInfoMap) {
-                updateInfoMap.add(map);
-            }
+        synchronized (listOfUpdateInfoMap) {
+            listOfUpdateInfoMap.add(updateInfo);
         }
         pnfCorrelationIdToThreadMap.put(pnfCorrelationId, informConsumer);
         if (!dmaapThreadListenerIsRunning) {
@@ -91,14 +87,14 @@ public class PnfEventReadyDmaapClient implements DmaapClient {
     public synchronized Runnable unregister(String pnfCorrelationId) {
         logger.debug("unregistering from pnf ready dmaap event for pnf correlation id: {}", pnfCorrelationId);
         Runnable runnable = pnfCorrelationIdToThreadMap.remove(pnfCorrelationId);
-        synchronized (updateInfoMap) {
-            for (int i = updateInfoMap.size() - 1; i >= 0; i--) {
-                if (!updateInfoMap.get(i).containsKey("pnfCorrelationId"))
+        synchronized (listOfUpdateInfoMap) {
+            for (int i = listOfUpdateInfoMap.size() - 1; i >= 0; i--) {
+                if (!listOfUpdateInfoMap.get(i).containsKey("pnfCorrelationId"))
                     continue;
-                String id = updateInfoMap.get(i).get("pnfCorrelationId");
+                String id = listOfUpdateInfoMap.get(i).get("pnfCorrelationId");
                 if (id != pnfCorrelationId)
                     continue;
-                updateInfoMap.remove(i);
+                listOfUpdateInfoMap.remove(i);
             }
         }
         if (pnfCorrelationIdToThreadMap.isEmpty()) {
@@ -174,8 +170,8 @@ public class PnfEventReadyDmaapClient implements DmaapClient {
             String customerId = null;
             String serviceType = null;
             String serId = null;
-            synchronized (updateInfoMap) {
-                for (HashMap<String, String> map : updateInfoMap) {
+            synchronized (listOfUpdateInfoMap) {
+                for (Map<String, String> map : listOfUpdateInfoMap) {
                     if (!map.containsKey("pnfCorrelationId"))
                         continue;
                     if (pnfCorrelationId != map.get("pnfCorrelationId"))
index 2634f03..598582b 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP - SO
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019 Nokia.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.so.bpmn.infrastructure.pnf.delegate;
 
+import java.util.Map;
 import org.onap.so.bpmn.infrastructure.pnf.dmaap.DmaapClient;
-import java.util.HashMap;
 import java.util.Objects;
-import java.util.Optional;
 
 public class DmaapClientTestImpl implements DmaapClient {
 
@@ -31,8 +31,7 @@ public class DmaapClientTestImpl implements DmaapClient {
     private Runnable informConsumer;
 
     @Override
-    public void registerForUpdate(String pnfCorrelationId, Runnable informConsumer,
-            Optional<HashMap<String, String>> updateInfo) {
+    public void registerForUpdate(String pnfCorrelationId, Runnable informConsumer, Map<String, String> updateInfo) {
         this.pnfCorrelationId = pnfCorrelationId;
         this.informConsumer = informConsumer;
     }