72ad7ce4a6709f63e2edf56c59710ed17e4c45ad
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / onap / policy / std / ManualClientEndDMAAP.java
1 package org.onap.policy.std;
2
3 import java.util.List;
4
5 import org.json.JSONObject;
6 import org.onap.policy.api.NotificationScheme;
7 import org.onap.policy.api.NotificationType;
8 import org.onap.policy.api.PDPNotification;
9 import org.onap.policy.common.logging.flexlogger.FlexLogger;
10 import org.onap.policy.common.logging.flexlogger.Logger;
11 import org.onap.policy.utils.BusConsumer;
12 import org.onap.policy.utils.BusPublisher;
13 import org.onap.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                 if(pub != null){
64                 pub.close (); 
65                 }
66         }
67
68         //NOTE:  should be able to remove this for DMAAP since we will not be creating topics dynamically
69         public static void createTopic (String topic, String uniquID, List<String> dmaapList, String aafLogin, String aafPassword){
70                 ManualClientEndDMAAP.topic = topic;
71                 publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword);
72         }
73         
74         
75         public static void start(List<String> dmaapList, String topic, String aafLogin, String aafPassword, String uniqueID) {
76                 
77                 ManualClientEndDMAAP.uniquID = uniqueID;
78                 ManualClientEndDMAAP.topic = topic;
79                 
80                 String id = "0";
81                 
82                 try {
83                         dmaapConsumer = new BusConsumer.DmaapConsumerWrapper(dmaapList, topic, aafLogin, aafPassword, "clientGroup", id, 15*1000, 1000);
84                 } catch (Exception e) {
85                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Consumer: ", e);
86                 }
87                 
88                 int count = 1;
89                 while (count < 4) {
90                                 publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword);
91                                 try {
92                                         for ( String msg : dmaapConsumer.fetch () )
93                                         {       
94                                                 logger.debug("Manual Notification Recieved Message " + msg + " from DMaaP server : " + dmaapList.toString());
95                                                 resultJson = msg;
96                                                 if (!msg.contains("DMaaP Update")){
97                                                         notification = NotificationUnMarshal.notificationJSON(msg);
98                                                         count = 4;
99                                                 }
100                                         }
101                                 }catch (Exception e) {
102                                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to fetch messages from DMaaP servers: ", e);
103                                 } 
104                                 count++;
105                         }               
106         }
107 }