From 570fcbcdbb44511121447e9445db5b0ada3786ca Mon Sep 17 00:00:00 2001 From: Lukasz Muszkieta Date: Wed, 11 Sep 2019 09:37:21 +0200 Subject: bug fix - avoid npe in pnf flow Change-Id: I8e848c2bdcec0822ae08280223297b4825e9b7c2 Issue-ID: SO-2289 Signed-off-by: Lukasz Muszkieta --- .../pnf/delegate/InformDmaapClient.java | 48 +++++++++++++--------- .../bpmn/infrastructure/pnf/dmaap/DmaapClient.java | 7 ++-- .../pnf/dmaap/PnfEventReadyDmaapClient.java | 28 ++++++------- .../pnf/delegate/DmaapClientTestImpl.java | 7 ++-- 4 files changed, 46 insertions(+), 44 deletions(-) (limited to 'bpmn/so-bpmn-infrastructure-common') diff --git a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/delegate/InformDmaapClient.java b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/delegate/InformDmaapClient.java index 2ababac7e3..a55f32aaaa 100644 --- a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/delegate/InformDmaapClient.java +++ b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/delegate/InformDmaapClient.java @@ -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. @@ -20,14 +21,12 @@ 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 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 createUpdateInfo(DelegateExecution execution) { - HashMap map = new HashMap(); - - ResourceInput resourceInputObj = ResourceRequestBuilder + private Map createUpdateInfoMap(DelegateExecution execution) { + Map 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 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 diff --git a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/DmaapClient.java b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/DmaapClient.java index d513684659..bafb749e15 100644 --- a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/DmaapClient.java +++ b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/DmaapClient.java @@ -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. @@ -20,13 +21,11 @@ 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> updateInfo); + void registerForUpdate(String pnfCorrelationId, Runnable informConsumer, Map updateInfo); Runnable unregister(String pnfCorrelationId); } diff --git a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java index 48061db887..02303a6b23 100644 --- a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java +++ b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java @@ -54,8 +54,7 @@ public class PnfEventReadyDmaapClient implements DmaapClient { private int topicListenerDelayInSeconds; private volatile ScheduledThreadPoolExecutor executor; private volatile boolean dmaapThreadListenerIsRunning; - - public volatile List> updateInfoMap; + private volatile List> 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> updateInfo) { + Map updateInfo) { logger.debug("registering for pnf ready dmaap event for pnf correlation id: {}", pnfCorrelationId); - HashMap 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 map : updateInfoMap) { + synchronized (listOfUpdateInfoMap) { + for (Map map : listOfUpdateInfoMap) { if (!map.containsKey("pnfCorrelationId")) continue; if (pnfCorrelationId != map.get("pnfCorrelationId")) diff --git a/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/delegate/DmaapClientTestImpl.java b/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/delegate/DmaapClientTestImpl.java index 2634f03d4b..598582bfd8 100644 --- a/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/delegate/DmaapClientTestImpl.java +++ b/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/delegate/DmaapClientTestImpl.java @@ -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. @@ -20,10 +21,9 @@ 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> updateInfo) { + public void registerForUpdate(String pnfCorrelationId, Runnable informConsumer, Map updateInfo) { this.pnfCorrelationId = pnfCorrelationId; this.informConsumer = informConsumer; } -- cgit 1.2.3-korg