Reduce technical debt and add JUnit
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / onap / policy / std / AutoClientDMAAP.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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.std;
22
23 import java.util.List;
24 import java.util.UUID;
25
26 import org.onap.policy.api.NotificationHandler;
27 import org.onap.policy.api.NotificationScheme;
28 import org.onap.policy.api.NotificationType;
29 import org.onap.policy.api.PDPNotification;
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
32 import org.onap.policy.utils.BusConsumer;
33 import org.onap.policy.utils.BusConsumer.DmaapConsumerWrapper;
34 import org.onap.policy.xacml.api.XACMLErrorConstants;
35
36 public class AutoClientDMAAP implements Runnable {
37     private static StdPDPNotification notification = null;
38     private static NotificationScheme scheme = null;
39     private static NotificationHandler handler = null;
40     private static String topic = null;
41     private static boolean status = false;
42     private static Logger logger = FlexLogger.getLogger(AutoClientDMAAP.class.getName());
43     private static String notficatioinType = null;
44     private static BusConsumer dmaapConsumer = null;
45     private static List<String> dmaapList = null;
46     private static String aafLogin = null;
47     private static String aafPassword = null;
48     private volatile boolean running = false;
49
50     public AutoClientDMAAP(List<String> dmaapList, String topic, String aafLogin, String aafPassword) {
51         AutoClientDMAAP.topic = topic;
52         AutoClientDMAAP.dmaapList = dmaapList;
53         AutoClientDMAAP.aafLogin = aafLogin;
54         AutoClientDMAAP.aafPassword = aafPassword;
55     }
56
57     public static void setAuto(NotificationScheme scheme, NotificationHandler handler) {
58         AutoClientDMAAP.scheme = scheme;
59         AutoClientDMAAP.handler = handler;
60     }
61
62     public static void setScheme(NotificationScheme scheme) {
63         AutoClientDMAAP.scheme = scheme;
64     }
65
66     public static boolean getStatus() {
67         return AutoClientDMAAP.status;
68     }
69
70     public static String getTopic() {
71         return AutoClientDMAAP.topic;
72     }
73
74     public static String getNotficationType() {
75         return AutoClientDMAAP.notficatioinType;
76     }
77
78     public synchronized boolean isRunning() {
79         return this.running;
80     }
81
82     public synchronized void terminate() {
83         this.running = false;
84     }
85
86     @Override
87     public void run() {
88         synchronized (this) {
89             this.running = true;
90         }
91         String group = UUID.randomUUID().toString();
92         String id = "0";
93         
94         if (scheme == null || handler == null || 
95                 ! (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS) ||
96                         scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) ) {
97                 logger.info("no stop/start required");
98                 return;
99         }
100
101         // create a loop to listen for messages from DMaaP server
102         try {
103             setDmaapCosumer(new BusConsumer.DmaapConsumerWrapper(dmaapList, topic, aafLogin, aafPassword, group,
104                     id, 15 * 1000, 1000));
105         } catch (Exception e) {
106             logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Consumer: ", e);
107         }
108
109         while (this.isRunning()) {
110             try {
111                 for (String msg : dmaapConsumer.fetch()) {
112                     logger.debug("Auto Notification Recieved Message " + msg + " from DMAAP server : "
113                             + dmaapList.toString());
114                     setNotification(NotificationUnMarshal.notificationJSON(msg));
115                     callHandler();
116                 }
117             } catch (Exception e) {
118                 logger.debug("Error in processing DMAAP message", e);
119             }
120
121         }
122         logger.debug("Stopping DMAAP Consumer loop will no longer fetch messages from the servers");
123     }
124
125     private static void setNotification(StdPDPNotification notificationJSON) {
126         notification = notificationJSON;
127     }
128
129     private static void setDmaapCosumer(DmaapConsumerWrapper dmaapConsumerWrapper) {
130         dmaapConsumer = dmaapConsumerWrapper;
131     }
132
133     private static void callHandler() {
134         
135         if (handler == null || scheme == null) {
136                 logger.info("handler does not need to do anything");
137                 return;
138         }
139             if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)) {     
140                 boolean removed = false; 
141                 boolean updated = false;
142                 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
143                     removed = true;
144                 }
145                 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
146                     updated = true;
147                 }
148                 if (removed && updated) {
149                     notification.setNotificationType(NotificationType.BOTH);
150                 } else if (removed) {
151                     notification.setNotificationType(NotificationType.REMOVE);
152                 } else if (updated) {
153                     notification.setNotificationType(NotificationType.UPDATE);
154                 }
155                 handler.notificationReceived(notification);
156             } else if (scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
157                 PDPNotification newNotification = MatchStore.checkMatch(notification);
158                 if (newNotification.getNotificationType() != null) {
159                     handler.notificationReceived(newNotification);
160                 }
161             }
162     }
163
164 }