2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import org.onap.policy.drools.PolicyEngine;
38 import org.onap.policy.drools.PolicyEngineListener;
40 public class PolicyEngineJUnitImpl implements PolicyEngine {
42 private static final Logger logger = LoggerFactory.getLogger(PolicyEngineJUnitImpl.class);
43 private Map<String, Map<String, Queue<Object>>> busMap = new HashMap<>();
44 private List<PolicyEngineListener> listeners = new ArrayList<>();
47 * Adds all objects that implement PolicyEngineListener
48 * to the notification list when an event occurs
50 * @param listener an object that is interest in knowing
51 * about events published to the PolicyEngine
53 public void addListener(PolicyEngineListener listener) {
54 listeners.add(listener);
58 * Notifies all listeners about a new event
59 * @param topic the topic in which the notification
62 public void notifyListeners(String topic) {
63 for (PolicyEngineListener listener: listeners) {
64 listener.newEventNotification(topic);
69 public boolean deliver(String busType, String topic, Object obj) {
70 if (obj instanceof ControlLoopNotification) {
71 ControlLoopNotification notification = (ControlLoopNotification) obj;
72 if (logger.isDebugEnabled()) {
73 logger.debug(Serialization.gsonPretty.toJson(notification));
76 if (obj instanceof Request) {
77 Request request = (Request) obj;
78 logger.debug("Request: {} subrequest {}", request.getAction(), request.getCommonHeader().getSubRequestID());
80 else if (obj instanceof LCMRequestWrapper) {
81 LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) obj;
82 logger.debug("Request: {} subrequest {}", dmaapRequest.getBody().getAction(), dmaapRequest.getBody().getCommonHeader().getSubRequestId());
85 // Does the bus exist?
87 if (!busMap.containsKey(busType)) {
88 logger.debug("creating new bus type {}", busType);
92 busMap.put(busType, new HashMap<>());
97 Map<String, Queue<Object>> topicMap = busMap.get(busType);
99 // Does the topic exist?
101 if (!topicMap.containsKey(topic)) {
102 logger.debug("creating new topic {}", topic);
106 topicMap.put(topic, new LinkedList<>());
109 // Get the topic queue
111 logger.debug("queueing");
112 boolean res = topicMap.get(topic).add(obj);
113 notifyListeners(topic);
117 public Object subscribe(String busType, String topic) {
119 // Does the bus exist?
121 if (busMap.containsKey(busType)) {
125 Map<String, Queue<Object>> topicMap = busMap.get(busType);
127 // Does the topic exist?
129 if (topicMap.containsKey(topic)) {
130 logger.debug("The queue has {}", topicMap.get(topic).size());
131 return topicMap.get(topic).poll();
133 logger.error("No topic exists {}", topic);
136 logger.error("No bus exists {}", busType);