6a3be2d6a3b0082b2a848730b8901eea6930a1b6
[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
30 import org.onap.policy.appc.Request;
31 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
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;
38
39 public class PolicyEngineJUnitImpl implements PolicyEngine {
40
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<>();
44
45     /**
46      * Adds all objects that implement PolicyEngineListener to the notification list when an event
47      * occurs.
48      *
49      * @param listener an object that is interest in knowing about events published to the
50      *        PolicyEngine
51      */
52     public void addListener(PolicyEngineListener listener) {
53         listeners.add(listener);
54     }
55
56     /**
57      * Notifies all listeners about a new event.
58      *
59      * @param topic the topic in which the notification was sent to
60      */
61     public void notifyListeners(String topic) {
62         for (PolicyEngineListener listener : listeners) {
63             listener.newEventNotification(topic);
64         }
65     }
66
67     @Override
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));
73             }
74         }
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 AppcLcmDmaapWrapper) {
79             AppcLcmDmaapWrapper dmaapRequest = (AppcLcmDmaapWrapper) obj;
80             logger.debug("Request: {} subrequest {}", dmaapRequest.getBody().getInput().getAction(),
81                     dmaapRequest.getBody().getInput().getCommonHeader().getSubRequestId());
82         }
83         //
84         // Does the bus exist?
85         //
86         if (!busMap.containsKey(busType)) {
87             logger.debug("creating new bus type {}", busType);
88             //
89             // Create the bus
90             //
91             busMap.put(busType, new HashMap<>());
92         }
93         //
94         // Get the bus
95         //
96         Map<String, Queue<Object>> topicMap = busMap.get(busType);
97         //
98         // Does the topic exist?
99         //
100         if (!topicMap.containsKey(topic)) {
101             logger.debug("creating new topic {}", topic);
102             //
103             // Create the topic
104             //
105             topicMap.put(topic, new LinkedList<>());
106         }
107         //
108         // Get the topic queue
109         //
110         logger.debug("queueing");
111         boolean res = topicMap.get(topic).add(obj);
112         notifyListeners(topic);
113         return res;
114     }
115
116     /**
117      * Subscribe to a topic on a bus.
118      *
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
122      *         queue is empty
123      */
124     public Object subscribe(String busType, String topic) {
125         //
126         // Does the bus exist?
127         //
128         if (busMap.containsKey(busType)) {
129             //
130             // Get the bus
131             //
132             Map<String, Queue<Object>> topicMap = busMap.get(busType);
133             //
134             // Does the topic exist?
135             //
136             if (topicMap.containsKey(topic)) {
137                 logger.debug("The queue has {}", topicMap.get(topic).size());
138                 return topicMap.get(topic).poll();
139             } else {
140                 logger.error("No topic exists {}", topic);
141             }
142         } else {
143             logger.error("No bus exists {}", busType);
144         }
145         return null;
146     }
147
148 }