bb5cec8da3b05b8c2a9fb93b7f856a5ce94e1f2c
[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.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 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import org.onap.policy.drools.PolicyEngine;
35
36 public class PolicyEngineJUnitImpl implements PolicyEngine {
37
38         private static final Logger logger = LoggerFactory.getLogger(PolicyEngineJUnitImpl.class);
39         private Map<String, Map<String, Queue<Object>>> busMap = new HashMap<String, Map<String, Queue<Object>>>();
40
41         @Override
42         public boolean deliver(String busType, String topic, Object obj) {
43                 if (obj instanceof ControlLoopNotification) {
44                         ControlLoopNotification notification = (ControlLoopNotification) obj;
45                         //logger.debug("Notification: " + notification.notification + " " + (notification.message == null ? "" : notification.message) + " " + notification.history);
46                         logger.debug(Serialization.gsonPretty.toJson(notification));
47                 }
48                 if (obj instanceof Request) {
49                         Request request = (Request) obj;
50                         logger.debug("Request: {} subrequst {}", request.Action, request.CommonHeader.SubRequestID);
51                 }
52                 //
53                 // Does the bus exist?
54                 //
55                 if (busMap.containsKey(busType) == false) {
56                         logger.debug("creating new bus type {}", busType);
57                         //
58                         // Create the bus
59                         //
60                         busMap.put(busType, new HashMap<String, Queue<Object>>());
61                 }
62                 //
63                 // Get the bus
64                 //
65                 Map<String, Queue<Object>> topicMap = busMap.get(busType);
66                 //
67                 // Does the topic exist?
68                 //
69                 if (topicMap.containsKey(topic) == false) {
70                         logger.debug("creating new topic {}", topic);
71                         //
72                         // Create the topic
73                         //
74                         topicMap.put(topic, new LinkedList<Object>());
75                 }
76                 //
77                 // Get the topic queue
78                 //
79                 logger.debug("queueing");
80                 return topicMap.get(topic).add(obj);
81         }
82         
83         public Object   subscribe(String busType, String topic) {
84                 //
85                 // Does the bus exist?
86                 //
87                 if (busMap.containsKey(busType)) {
88                         //
89                         // Get the bus
90                         //
91                         Map<String, Queue<Object>> topicMap = busMap.get(busType);
92                         //
93                         // Does the topic exist?
94                         //
95                         if (topicMap.containsKey(topic)) {
96                                 logger.debug("The queue has {}", topicMap.get(topic).size());
97                                 return topicMap.get(topic).poll();
98                         } else {
99                                 logger.error("No topic exists {}", topic);
100                         }
101                 } else {
102                         logger.error("No bus exists {}", busType);
103                 }
104                 return null;
105         }
106
107 }