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