0e30ada395c808d0ed33e2c46d8bd5e2aec0f703
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / openecomp / policy / std / ManualClientEndDMAAP.java
1 package org.openecomp.policy.std;
2
3 import java.util.List;
4
5 import org.json.JSONObject;
6 import org.openecomp.policy.api.NotificationScheme;
7 import org.openecomp.policy.api.NotificationType;
8 import org.openecomp.policy.api.PDPNotification;
9 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
10 import org.openecomp.policy.common.logging.flexlogger.Logger;
11 import org.openecomp.policy.utils.BusConsumer;
12 import org.openecomp.policy.utils.BusPublisher;
13 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
14
15 public class ManualClientEndDMAAP {
16         private static StdPDPNotification notification = null;
17         private static String resultJson = null;
18         private static Logger logger = FlexLogger.getLogger(ManualClientEndDMAAP.class.getName());
19         private static BusConsumer dmaapConsumer = null;
20         private static String uniquID = null;
21         private static String topic = null;
22         
23
24         public static PDPNotification result(NotificationScheme scheme) {
25                 if (resultJson == null || notification == null) {
26                         logger.debug("No Result" );
27                         return null;
28                 } else {
29                         if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
30                                 boolean removed = false, updated = false; 
31                                 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
32                                         removed = true;
33                                 }
34                                 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
35                                         updated = true;
36                                 }
37                                 if(removed && updated) {
38                                         notification.setNotificationType(NotificationType.BOTH);
39                                 }else if(removed){
40                                         notification.setNotificationType(NotificationType.REMOVE);
41                                 }else if(updated){
42                                         notification.setNotificationType(NotificationType.UPDATE);
43                                 }
44                                 return notification;
45                         }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
46                                 return MatchStore.checkMatch(notification);
47                         }else {
48                                 return null;
49                         }
50                 }
51         }
52
53         private static void publishMessage(String pubTopic, String uniqueID, List<String> dmaapList, String aafLogin, String aafPassword) {
54         BusPublisher pub = null;
55                 try {
56                         pub = new BusPublisher.DmaapPublisherWrapper(dmaapList, topic, aafLogin, aafPassword);
57                         final JSONObject msg1 = new JSONObject (); 
58                 msg1.put ( "JSON", "DMaaP Update Request UID=" + uniqueID);  
59                 pub.send ( "MyPartitionKey", msg1.toString () );
60                 } catch (Exception e) {
61                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Publisher: ", e);
62                 }
63         pub.close (); 
64         }
65
66         //NOTE:  should be able to remove this for DMAAP since we will not be creating topics dynamically
67         public static void createTopic (String topic, String uniquID, List<String> dmaapList, String aafLogin, String aafPassword){
68                 ManualClientEndDMAAP.topic = topic;
69                 publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword);
70         }
71         
72         
73         public static void start(List<String> dmaapList, String topic, String aafLogin, String aafPassword, String uniqueID) {
74                 
75                 ManualClientEndDMAAP.uniquID = uniqueID;
76                 ManualClientEndDMAAP.topic = topic;
77                 
78                 String id = "0";
79                 
80                 try {
81                         dmaapConsumer = new BusConsumer.DmaapConsumerWrapper(dmaapList, topic, aafLogin, aafPassword, "clientGroup", id, 15*1000, 1000);
82                 } catch (Exception e) {
83                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Consumer: ", e);
84                 }
85                 
86                 int count = 1;
87                 while (count < 4) {
88                                 publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword);
89                                 try {
90                                         for ( String msg : dmaapConsumer.fetch () )
91                                         {       
92                                                 logger.debug("Manual Notification Recieved Message " + msg + " from DMaaP server : " + dmaapList.toString());
93                                                 resultJson = msg;
94                                                 if (!msg.contains("DMaaP Update")){
95                                                         notification = NotificationUnMarshal.notificationJSON(msg);
96                                                         count = 4;
97                                                 }
98                                         }
99                                 }catch (Exception e) {
100                                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to fetch messages from DMaaP servers: ", e);
101                                 } 
102                                 count++;
103                         }               
104         }
105 }