diff options
Diffstat (limited to 'controlloop/common/eventmanager/src/main/java/org/onap/policy/drools/impl/PolicyEngineJUnitImpl.java')
-rw-r--r-- | controlloop/common/eventmanager/src/main/java/org/onap/policy/drools/impl/PolicyEngineJUnitImpl.java | 203 |
1 files changed, 105 insertions, 98 deletions
diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/drools/impl/PolicyEngineJUnitImpl.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/drools/impl/PolicyEngineJUnitImpl.java index e94657fee..d5f73c741 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/drools/impl/PolicyEngineJUnitImpl.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/drools/impl/PolicyEngineJUnitImpl.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy engine * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. 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. @@ -31,111 +31,118 @@ import org.onap.policy.appc.Request; import org.onap.policy.appclcm.LCMRequestWrapper; import org.onap.policy.controlloop.ControlLoopNotification; import org.onap.policy.controlloop.util.Serialization; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.onap.policy.drools.PolicyEngine; import org.onap.policy.drools.PolicyEngineListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class PolicyEngineJUnitImpl implements PolicyEngine { - private static final Logger logger = LoggerFactory.getLogger(PolicyEngineJUnitImpl.class); - private Map<String, Map<String, Queue<Object>>> busMap = new HashMap<>(); - private List<PolicyEngineListener> listeners = new ArrayList<>(); + private static final Logger logger = LoggerFactory.getLogger(PolicyEngineJUnitImpl.class); + private Map<String, Map<String, Queue<Object>>> busMap = new HashMap<>(); + private List<PolicyEngineListener> listeners = new ArrayList<>(); - /** - * Adds all objects that implement PolicyEngineListener - * to the notification list when an event occurs - * - * @param listener an object that is interest in knowing - * about events published to the PolicyEngine - */ - public void addListener(PolicyEngineListener listener) { - listeners.add(listener); - } + /** + * Adds all objects that implement PolicyEngineListener to the notification list when an event + * occurs. + * + * @param listener an object that is interest in knowing about events published to the + * PolicyEngine + */ + public void addListener(PolicyEngineListener listener) { + listeners.add(listener); + } - /** - * Notifies all listeners about a new event - * @param topic the topic in which the notification - * was sent to - */ - public void notifyListeners(String topic) { - for (PolicyEngineListener listener: listeners) { - listener.newEventNotification(topic); - } - } + /** + * Notifies all listeners about a new event. + * + * @param topic the topic in which the notification was sent to + */ + public void notifyListeners(String topic) { + for (PolicyEngineListener listener : listeners) { + listener.newEventNotification(topic); + } + } - @Override - public boolean deliver(String busType, String topic, Object obj) { - if (obj instanceof ControlLoopNotification) { - ControlLoopNotification notification = (ControlLoopNotification) obj; - if (logger.isDebugEnabled()) { - logger.debug(Serialization.gsonPretty.toJson(notification)); - } - } - if (obj instanceof Request) { - Request request = (Request) obj; - logger.debug("Request: {} subrequest {}", request.getAction(), request.getCommonHeader().getSubRequestID()); - } - else if (obj instanceof LCMRequestWrapper) { - LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) obj; - logger.debug("Request: {} subrequest {}", dmaapRequest.getBody().getAction(), dmaapRequest.getBody().getCommonHeader().getSubRequestId()); - } - // - // Does the bus exist? - // - if (!busMap.containsKey(busType)) { - logger.debug("creating new bus type {}", busType); - // - // Create the bus - // - busMap.put(busType, new HashMap<>()); - } - // - // Get the bus - // - Map<String, Queue<Object>> topicMap = busMap.get(busType); - // - // Does the topic exist? - // - if (!topicMap.containsKey(topic)) { - logger.debug("creating new topic {}", topic); - // - // Create the topic - // - topicMap.put(topic, new LinkedList<>()); - } - // - // Get the topic queue - // - logger.debug("queueing"); - boolean res = topicMap.get(topic).add(obj); - notifyListeners(topic); - return res; - } + @Override + public boolean deliver(String busType, String topic, Object obj) { + if (obj instanceof ControlLoopNotification) { + ControlLoopNotification notification = (ControlLoopNotification) obj; + if (logger.isDebugEnabled()) { + logger.debug(Serialization.gsonPretty.toJson(notification)); + } + } + if (obj instanceof Request) { + Request request = (Request) obj; + logger.debug("Request: {} subrequest {}", request.getAction(), request.getCommonHeader().getSubRequestID()); + } else if (obj instanceof LCMRequestWrapper) { + LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) obj; + logger.debug("Request: {} subrequest {}", dmaapRequest.getBody().getAction(), + dmaapRequest.getBody().getCommonHeader().getSubRequestId()); + } + // + // Does the bus exist? + // + if (!busMap.containsKey(busType)) { + logger.debug("creating new bus type {}", busType); + // + // Create the bus + // + busMap.put(busType, new HashMap<>()); + } + // + // Get the bus + // + Map<String, Queue<Object>> topicMap = busMap.get(busType); + // + // Does the topic exist? + // + if (!topicMap.containsKey(topic)) { + logger.debug("creating new topic {}", topic); + // + // Create the topic + // + topicMap.put(topic, new LinkedList<>()); + } + // + // Get the topic queue + // + logger.debug("queueing"); + boolean res = topicMap.get(topic).add(obj); + notifyListeners(topic); + return res; + } - public Object subscribe(String busType, String topic) { - // - // Does the bus exist? - // - if (busMap.containsKey(busType)) { - // - // Get the bus - // - Map<String, Queue<Object>> topicMap = busMap.get(busType); - // - // Does the topic exist? - // - if (topicMap.containsKey(topic)) { - logger.debug("The queue has {}", topicMap.get(topic).size()); - return topicMap.get(topic).poll(); - } else { - logger.error("No topic exists {}", topic); - } - } else { - logger.error("No bus exists {}", busType); - } - return null; - } + /** + * Subscribe to a topic on a bus. + * + * @param busType the bus type + * @param topic the topic + * @return the head of the queue, or <code>null</code> if the queue or bus does not exist or the + * queue is empty + */ + public Object subscribe(String busType, String topic) { + // + // Does the bus exist? + // + if (busMap.containsKey(busType)) { + // + // Get the bus + // + Map<String, Queue<Object>> topicMap = busMap.get(busType); + // + // Does the topic exist? + // + if (topicMap.containsKey(topic)) { + logger.debug("The queue has {}", topicMap.get(topic).size()); + return topicMap.get(topic).poll(); + } else { + logger.error("No topic exists {}", topic); + } + } else { + logger.error("No bus exists {}", busType); + } + return null; + } } |