[POLICY-11] Sample Query with variable arguments
[policy/drools-applications.git] / controlloop / common / eventmanager / src / main / java / org / onap / policy / drools / impl / PolicyEngineJUnitImpl.java
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.HashMap;
24 import java.util.LinkedList;
25 import java.util.Map;
26 import java.util.Queue;
27
28 import org.onap.policy.appc.Request;
29 import org.onap.policy.controlloop.ControlLoopNotification;
30 import org.onap.policy.controlloop.util.Serialization;
31
32 import org.onap.policy.drools.PolicyEngine;
33
34 public class PolicyEngineJUnitImpl implements PolicyEngine {
35
36         private Map<String, Map<String, Queue<Object>>> busMap = new HashMap<String, Map<String, Queue<Object>>>();
37
38         @Override
39         public boolean deliver(String busType, String topic, Object obj) {
40                 if (obj instanceof ControlLoopNotification) {
41                         ControlLoopNotification notification = (ControlLoopNotification) obj;
42                         //System.out.println("Notification: " + notification.notification + " " + (notification.message == null ? "" : notification.message) + " " + notification.history);
43                         System.out.println(Serialization.gsonPretty.toJson(notification));
44                 }
45                 if (obj instanceof Request) {
46                         Request request = (Request) obj;
47                         System.out.println("Request: " + request.Action + " subrequest " + request.CommonHeader.SubRequestID);
48                 }
49                 //
50                 // Does the bus exist?
51                 //
52                 if (busMap.containsKey(busType) == false) {
53                         System.out.println("creating new bus type " + busType);
54                         //
55                         // Create the bus
56                         //
57                         busMap.put(busType, new HashMap<String, Queue<Object>>());
58                 }
59                 //
60                 // Get the bus
61                 //
62                 Map<String, Queue<Object>> topicMap = busMap.get(busType);
63                 //
64                 // Does the topic exist?
65                 //
66                 if (topicMap.containsKey(topic) == false) {
67                         System.out.println("creating new topic " + topic);
68                         //
69                         // Create the topic
70                         //
71                         topicMap.put(topic, new LinkedList<Object>());
72                 }
73                 //
74                 // Get the topic queue
75                 //
76                 System.out.println("queueing");
77                 return topicMap.get(topic).add(obj);
78         }
79         
80         public Object   subscribe(String busType, String topic) {
81                 //
82                 // Does the bus exist?
83                 //
84                 if (busMap.containsKey(busType)) {
85                         //
86                         // Get the bus
87                         //
88                         Map<String, Queue<Object>> topicMap = busMap.get(busType);
89                         //
90                         // Does the topic exist?
91                         //
92                         if (topicMap.containsKey(topic)) {
93                                 System.out.println("The queue has " + topicMap.get(topic).size());
94                                 return topicMap.get(topic).poll();
95                         } else {
96                                 System.err.println("No topic exists " + topic);
97                         }
98                 } else {
99                         System.err.println("No bus exists " + busType);
100                 }
101                 return null;
102         }
103
104 }