From e0d41e83dc9b5af7e0454e0133c8ddbb332f0c79 Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Mon, 28 Sep 2020 12:45:22 +0100 Subject: Fixing APEX eventName handling to avoid duplicate requests Change-Id: I6ee50fb455424ae1f241b7a12d1cdbf0e6ee4011 Issue-ID: POLICY-2815 Signed-off-by: a.sreekumar --- .../service/engine/main/ApexEventUnmarshaller.java | 74 ++++++++++++++-------- 1 file changed, 48 insertions(+), 26 deletions(-) diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexEventUnmarshaller.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexEventUnmarshaller.java index 8c2aee1f6..dc9854158 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexEventUnmarshaller.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexEventUnmarshaller.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +22,9 @@ package org.onap.policy.apex.service.engine.main; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.Properties; import java.util.concurrent.BlockingQueue; @@ -214,40 +218,58 @@ public class ApexEventUnmarshaller implements ApexEventReceiver, Runnable { } // Convert the incoming events to Apex events - try { - final List apexEventList = converter.toApexEvent(consumerParameters.getEventName(), event); + List apexEventList = convertToApexEvents(event); - for (final ApexEvent apexEvent : apexEventList) { - isEventFilteredOut(apexEvent); + for (final ApexEvent apexEvent : apexEventList) { + // Check if this event is filtered out by the incoming filter + if (isEventFilteredOut(apexEvent)) { + // Ignore this event + continue; + } - // Check if this event is filtered out by the incoming filter - if (isEventFilteredOut(apexEvent)) { - // Ignore this event - continue; - } + if (!generateExecutionId) { + apexEvent.setExecutionId(executionId); + } - if (!generateExecutionId) { - apexEvent.setExecutionId(executionId); - } + apexEvent.setExecutionProperties(executionProperties); - apexEvent.setExecutionProperties(executionProperties); + // Cache synchronized events that are sent + if (consumerParameters.isPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS)) { + final SynchronousEventCache synchronousEventCache = + (SynchronousEventCache) consumer.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS); + synchronousEventCache.cacheSynchronizedEventToApex(apexEvent.getExecutionId(), apexEvent); + } - // Cache synchronized events that are sent - if (consumerParameters.isPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS)) { - final SynchronousEventCache synchronousEventCache = - (SynchronousEventCache) consumer.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS); - synchronousEventCache.cacheSynchronizedEventToApex(apexEvent.getExecutionId(), apexEvent); - } + // Enqueue the event + queue.add(apexEvent); + } + } - // Enqueue the event - queue.add(apexEvent); + private List convertToApexEvents(final Object event) throws ApexEventException { + List apexEventList = null; + List eventNamesList = null; + if (consumerParameters.getEventName() != null) { + eventNamesList = Arrays.asList(consumerParameters.getEventName().split("\\|")); + } else { + eventNamesList = Collections.singletonList(null); + } + Iterator iterator = eventNamesList.iterator(); + // Incoming events in an endpoint can have different structure , for e.g., success/failure response events + // Parse the incoming event into an APEX event defined with any one of the names specified in eventName field + while (iterator.hasNext()) { + try { + String eventName = iterator.next(); + apexEventList = converter.toApexEvent(eventName, event); + break; + } catch (ApexException e) { + if (!iterator.hasNext()) { + final String errorMessage = "Error while converting event into an ApexEvent for " + name + ": " + + e.getMessage() + ", Event=" + event; + throw new ApexEventException(errorMessage, e); + } } - } catch (final ApexException e) { - final String errorMessage = "Error while converting event into an ApexEvent for " + name + ": " - + e.getMessage() + ", Event=" + event; - LOGGER.warn(errorMessage, e); - throw new ApexEventException(errorMessage, e); } + return apexEventList; } /** -- cgit 1.2.3-korg