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