Added missing license headers
[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         // Stop and Start needs to be done.
95         if (scheme != null && handler != null) {
96             if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)
97                     || scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
98
99                 // create a loop to listen for messages from DMaaP server
100                 try {
101                     setDmaapCosumer(new BusConsumer.DmaapConsumerWrapper(dmaapList, topic, aafLogin, aafPassword, group,
102                             id, 15 * 1000, 1000));
103                 } catch (Exception e) {
104                     logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Consumer: ", e);
105                 }
106
107                 while (this.isRunning()) {
108                     try {
109                         for (String msg : dmaapConsumer.fetch()) {
110                             logger.debug("Auto Notification Recieved Message " + msg + " from DMAAP server : "
111                                     + dmaapList.toString());
112                             setNotification(NotificationUnMarshal.notificationJSON(msg));
113                             callHandler();
114                         }
115                     } catch (Exception e) {
116                         logger.debug("Error in processing DMAAP message", e);
117                     }
118
119                 }
120                 logger.debug("Stopping DMAAP Consumer loop will no longer fetch messages from the servers");
121             }
122         }
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         if (handler != null && scheme != null) {
135             if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)) {
136                 boolean removed = false, updated = false;
137                 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
138                     removed = true;
139                 }
140                 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
141                     updated = true;
142                 }
143                 if (removed && updated) {
144                     notification.setNotificationType(NotificationType.BOTH);
145                 } else if (removed) {
146                     notification.setNotificationType(NotificationType.REMOVE);
147                 } else if (updated) {
148                     notification.setNotificationType(NotificationType.UPDATE);
149                 }
150                 handler.notificationReceived(notification);
151             } else if (scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
152                 PDPNotification newNotification = MatchStore.checkMatch(notification);
153                 if (newNotification.getNotificationType() != null) {
154                     handler.notificationReceived(newNotification);
155                 }
156             }
157         }
158     }
159
160 }