2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.impl;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.LinkedList;
26 import java.util.List;
28 import java.util.Queue;
30 import org.onap.policy.appc.Request;
31 import org.onap.policy.appclcm.LcmRequestWrapper;
32 import org.onap.policy.controlloop.ControlLoopNotification;
33 import org.onap.policy.controlloop.util.Serialization;
34 import org.onap.policy.drools.PolicyEngine;
35 import org.onap.policy.drools.PolicyEngineListener;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class PolicyEngineJUnitImpl implements PolicyEngine {
41 private static final Logger logger = LoggerFactory.getLogger(PolicyEngineJUnitImpl.class);
42 private Map<String, Map<String, Queue<Object>>> busMap = new HashMap<>();
43 private List<PolicyEngineListener> listeners = new ArrayList<>();
46 * Adds all objects that implement PolicyEngineListener to the notification list when an event
49 * @param listener an object that is interest in knowing about events published to the
52 public void addListener(PolicyEngineListener listener) {
53 listeners.add(listener);
57 * Notifies all listeners about a new event.
59 * @param topic the topic in which the notification was sent to
61 public void notifyListeners(String topic) {
62 for (PolicyEngineListener listener : listeners) {
63 listener.newEventNotification(topic);
68 public boolean deliver(String busType, String topic, Object obj) {
69 if (obj instanceof ControlLoopNotification) {
70 ControlLoopNotification notification = (ControlLoopNotification) obj;
71 if (logger.isDebugEnabled()) {
72 logger.debug(Serialization.gsonPretty.toJson(notification));
75 if (obj instanceof Request) {
76 Request request = (Request) obj;
77 logger.debug("Request: {} subrequest {}", request.getAction(), request.getCommonHeader().getSubRequestId());
78 } else if (obj instanceof LcmRequestWrapper) {
79 LcmRequestWrapper dmaapRequest = (LcmRequestWrapper) obj;
80 logger.debug("Request: {} subrequest {}", dmaapRequest.getBody().getAction(),
81 dmaapRequest.getBody().getCommonHeader().getSubRequestId());
84 // Does the bus exist?
86 if (!busMap.containsKey(busType)) {
87 logger.debug("creating new bus type {}", busType);
91 busMap.put(busType, new HashMap<>());
96 Map<String, Queue<Object>> topicMap = busMap.get(busType);
98 // Does the topic exist?
100 if (!topicMap.containsKey(topic)) {
101 logger.debug("creating new topic {}", topic);
105 topicMap.put(topic, new LinkedList<>());
108 // Get the topic queue
110 logger.debug("queueing");
111 boolean res = topicMap.get(topic).add(obj);
112 notifyListeners(topic);
117 * Subscribe to a topic on a bus.
119 * @param busType the bus type
120 * @param topic the topic
121 * @return the head of the queue, or <code>null</code> if the queue or bus does not exist or the
124 public Object subscribe(String busType, String topic) {
126 // Does the bus exist?
128 if (busMap.containsKey(busType)) {
132 Map<String, Queue<Object>> topicMap = busMap.get(busType);
134 // Does the topic exist?
136 if (topicMap.containsKey(topic)) {
137 logger.debug("The queue has {}", topicMap.get(topic).size());
138 return topicMap.get(topic).poll();
140 logger.error("No topic exists {}", topic);
143 logger.error("No bus exists {}", busType);