e94657fee96c95392a58bf5b66245efdd7d1816f
[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<>();
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                         if (logger.isDebugEnabled()) {
73                                 logger.debug(Serialization.gsonPretty.toJson(notification));
74                         }
75                 }
76                 if (obj instanceof Request) {
77                         Request request = (Request) obj;
78                         logger.debug("Request: {} subrequest {}", request.getAction(), request.getCommonHeader().getSubRequestID());
79                 }
80                 else if (obj instanceof LCMRequestWrapper) {
81                         LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) obj;
82                         logger.debug("Request: {} subrequest {}", dmaapRequest.getBody().getAction(), dmaapRequest.getBody().getCommonHeader().getSubRequestId());
83                 }
84                 //
85                 // Does the bus exist?
86                 //
87                 if (!busMap.containsKey(busType)) {
88                         logger.debug("creating new bus type {}", busType);
89                         //
90                         // Create the bus
91                         //
92                         busMap.put(busType, new HashMap<>());
93                 }
94                 //
95                 // Get the bus
96                 //
97                 Map<String, Queue<Object>> topicMap = busMap.get(busType);
98                 //
99                 // Does the topic exist?
100                 //
101                 if (!topicMap.containsKey(topic)) {
102                         logger.debug("creating new topic {}", topic);
103                         //
104                         // Create the topic
105                         //
106                         topicMap.put(topic, new LinkedList<>());
107                 }
108                 //
109                 // Get the topic queue
110                 //
111                 logger.debug("queueing");
112                 boolean res = topicMap.get(topic).add(obj);
113                 notifyListeners(topic);
114                 return res;
115         }
116
117         public Object subscribe(String busType, String topic) {
118                 //
119                 // Does the bus exist?
120                 //
121                 if (busMap.containsKey(busType)) {
122                         //
123                         // Get the bus
124                         //
125                         Map<String, Queue<Object>> topicMap = busMap.get(busType);
126                         //
127                         // Does the topic exist?
128                         //
129                         if (topicMap.containsKey(topic)) {
130                                 logger.debug("The queue has {}", topicMap.get(topic).size());
131                                 return topicMap.get(topic).poll();
132                         } else {
133                                 logger.error("No topic exists {}", topic);
134                         }
135                 } else {
136                         logger.error("No bus exists {}", busType);
137                 }
138                 return null;
139         }
140
141 }