85fc13c9e15ac7313e6911f47ac806c7289fcdd6
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy engine
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.impl;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Map;
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;
37
38 public class PolicyEngineJUnitImpl implements PolicyEngine {
39
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<>();
43
44     /**
45      * Adds all objects that implement PolicyEngineListener to the notification list when an event
46      * occurs.
47      *
48      * @param listener an object that is interest in knowing about events published to the
49      *        PolicyEngine
50      */
51     public void addListener(PolicyEngineListener listener) {
52         listeners.add(listener);
53     }
54
55     /**
56      * Notifies all listeners about a new event.
57      *
58      * @param topic the topic in which the notification was sent to
59      */
60     public void notifyListeners(String topic) {
61         for (PolicyEngineListener listener : listeners) {
62             listener.newEventNotification(topic);
63         }
64     }
65
66     @Override
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));
72             }
73         }
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());
81         }
82         //
83         // Does the bus exist?
84         //
85         if (!busMap.containsKey(busType)) {
86             logger.debug("creating new bus type {}", busType);
87             //
88             // Create the bus
89             //
90             busMap.put(busType, new HashMap<>());
91         }
92         //
93         // Get the bus
94         //
95         Map<String, Queue<Object>> topicMap = busMap.get(busType);
96         //
97         // Does the topic exist?
98         //
99         if (!topicMap.containsKey(topic)) {
100             logger.debug("creating new topic {}", topic);
101             //
102             // Create the topic
103             //
104             topicMap.put(topic, new LinkedList<>());
105         }
106         //
107         // Get the topic queue
108         //
109         logger.debug("queueing");
110         boolean res = topicMap.get(topic).add(obj);
111         notifyListeners(topic);
112         return res;
113     }
114
115     /**
116      * Subscribe to a topic on a bus.
117      *
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
121      *         queue is empty
122      */
123     public Object subscribe(String busType, String topic) {
124         //
125         // Does the bus exist?
126         //
127         if (busMap.containsKey(busType)) {
128             //
129             // Get the bus
130             //
131             Map<String, Queue<Object>> topicMap = busMap.get(busType);
132             //
133             // Does the topic exist?
134             //
135             if (topicMap.containsKey(topic)) {
136                 logger.debug("The queue has {}", topicMap.get(topic).size());
137                 return topicMap.get(topic).poll();
138             } else {
139                 logger.error("No topic exists {}", topic);
140             }
141         } else {
142             logger.error("No bus exists {}", busType);
143         }
144         return null;
145     }
146
147 }