36feaaf93ed76473412e488204f3254623d40179
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy engine
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
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.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;
36
37 import org.onap.policy.drools.PolicyEngine;
38 import org.onap.policy.drools.PolicyEngineListener;
39
40 public class PolicyEngineJUnitImpl implements PolicyEngine {
41
42         private static final Logger logger = LoggerFactory.getLogger(PolicyEngineJUnitImpl.class);
43         private Map<String, Map<String, Queue<Object>>> busMap = new HashMap<String, Map<String, Queue<Object>>>();
44         private List<PolicyEngineListener> listeners = new ArrayList<>();
45         
46         /**
47          * Adds all objects that implement PolicyEngineListener
48      * to the notification list when an event occurs
49      * 
50          * @param listener an object that is interest in knowing
51          * about events published to the PolicyEngine
52          */
53         public void addListener(PolicyEngineListener listener) {
54             listeners.add(listener);
55         }
56         
57         /**
58          * Notifies all listeners about a new event
59          * @param topic the topic in which the notification
60          * was sent to
61          */
62         public void notifyListeners(String topic) {
63             for (PolicyEngineListener listener: listeners) {
64                 listener.newEventNotification(topic);
65             }
66         }
67         
68         @Override
69         public boolean deliver(String busType, String topic, Object obj) {
70                 if (obj instanceof ControlLoopNotification) {
71                         ControlLoopNotification notification = (ControlLoopNotification) obj;
72                         //logger.debug("Notification: " + notification.notification + " " + (notification.message == null ? "" : notification.message) + " " + notification.history);
73                         logger.debug(Serialization.gsonPretty.toJson(notification));
74                 }
75                 if (obj instanceof Request) {
76                         Request request = (Request) obj;
77                         logger.debug("Request: {} subrequest {}", request.getAction(), request.getCommonHeader().getSubRequestID());
78                 }
79                 else if (obj instanceof LCMRequestWrapper) {
80                     LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) obj;
81                     logger.debug("Request: {} subrequest {}", dmaapRequest.getBody().getAction(), dmaapRequest.getBody().getCommonHeader().getSubRequestId());
82                 }
83                 //
84                 // Does the bus exist?
85                 //
86                 if (busMap.containsKey(busType) == false) {
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) == false) {
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         public Object   subscribe(String busType, String topic) {
117                 //
118                 // Does the bus exist?
119                 //
120                 if (busMap.containsKey(busType)) {
121                         //
122                         // Get the bus
123                         //
124                         Map<String, Queue<Object>> topicMap = busMap.get(busType);
125                         //
126                         // Does the topic exist?
127                         //
128                         if (topicMap.containsKey(topic)) {
129                                 logger.debug("The queue has {}", topicMap.get(topic).size());
130                                 return topicMap.get(topic).poll();
131                         } else {
132                                 logger.error("No topic exists {}", topic);
133                         }
134                 } else {
135                         logger.error("No bus exists {}", busType);
136                 }
137                 return null;
138         }
139
140 }