2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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;
29 import org.onap.policy.appc.Request;
30 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
31 import org.onap.policy.controlloop.ControlLoopNotification;
32 import org.onap.policy.controlloop.util.Serialization;
33 import org.onap.policy.drools.PolicyEngine;
34 import org.onap.policy.drools.PolicyEngineListener;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class PolicyEngineJUnitImpl implements PolicyEngine {
40 private static final Logger logger = LoggerFactory.getLogger(PolicyEngineJUnitImpl.class);
41 private Map<String, Map<String, Queue<Object>>> busMap = new HashMap<>();
42 private List<PolicyEngineListener> listeners = new ArrayList<>();
45 * Adds all objects that implement PolicyEngineListener to the notification list when an event
48 * @param listener an object that is interest in knowing about events published to the
51 public void addListener(PolicyEngineListener listener) {
52 listeners.add(listener);
56 * Notifies all listeners about a new event.
58 * @param topic the topic in which the notification was sent to
60 public void notifyListeners(String topic) {
61 for (PolicyEngineListener listener : listeners) {
62 listener.newEventNotification(topic);
67 public boolean deliver(String busType, String topic, Object obj) {
68 if (obj instanceof ControlLoopNotification) {
69 ControlLoopNotification notification = (ControlLoopNotification) obj;
70 if (logger.isDebugEnabled()) {
71 logger.debug(Serialization.gsonPretty.toJson(notification));
74 if (obj instanceof Request) {
75 Request request = (Request) obj;
76 logger.debug("Request: {} subrequest {}", request.getAction(), request.getCommonHeader().getSubRequestId());
77 } else if (obj instanceof AppcLcmDmaapWrapper) {
78 AppcLcmDmaapWrapper dmaapRequest = (AppcLcmDmaapWrapper) obj;
79 logger.debug("Request: {} subrequest {}", dmaapRequest.getBody().getInput().getAction(),
80 dmaapRequest.getBody().getInput().getCommonHeader().getSubRequestId());
83 // Does the bus exist?
85 if (!busMap.containsKey(busType)) {
86 logger.debug("creating new bus type {}", busType);
90 busMap.put(busType, new HashMap<>());
95 Map<String, Queue<Object>> topicMap = busMap.get(busType);
97 // Does the topic exist?
99 if (!topicMap.containsKey(topic)) {
100 logger.debug("creating new topic {}", topic);
104 topicMap.put(topic, new LinkedList<>());
107 // Get the topic queue
109 logger.debug("queueing");
110 boolean res = topicMap.get(topic).add(obj);
111 notifyListeners(topic);
116 * Subscribe to a topic on a bus.
118 * @param busType the bus type
119 * @param topic the topic
120 * @return the head of the queue, or <code>null</code> if the queue or bus does not exist or the
123 public Object subscribe(String busType, String topic) {
125 // Does the bus exist?
127 if (busMap.containsKey(busType)) {
131 Map<String, Queue<Object>> topicMap = busMap.get(busType);
133 // Does the topic exist?
135 if (topicMap.containsKey(topic)) {
136 logger.debug("The queue has {}", topicMap.get(topic).size());
137 return topicMap.get(topic).poll();
139 logger.error("No topic exists {}", topic);
142 logger.error("No bus exists {}", busType);