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