Policy TestSuite Enabled
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / openecomp / policy / std / ManualClientEndUEB.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.openecomp.policy.std;
22
23 import java.io.IOException;
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.security.GeneralSecurityException;
27 import java.util.List;
28
29 import org.json.JSONObject;
30 import org.openecomp.policy.api.NotificationScheme;
31 import org.openecomp.policy.api.NotificationType;
32 import org.openecomp.policy.api.PDPNotification;
33 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
34 import org.openecomp.policy.common.logging.flexlogger.Logger;
35
36 import com.att.nsa.cambria.client.CambriaClientFactory;
37 import com.att.nsa.cambria.client.CambriaConsumer;
38 import com.att.nsa.cambria.client.CambriaPublisher; 
39
40
41 @SuppressWarnings("deprecation")
42 public class ManualClientEndUEB {
43         private static StdPDPNotification notification = null;
44         private static String resultJson = null;
45         private static Logger logger = FlexLogger.getLogger(ManualClientEndUEB.class.getName());
46         private static CambriaConsumer CConsumer = null;
47         @SuppressWarnings("unused")
48         private static List<String> uebURLList = null; 
49         @SuppressWarnings("unused")
50         private static boolean messageNotReceived = false;
51         @SuppressWarnings("unused")
52         private static String url = null;
53         private static String uniquID = null;
54         private static String topic = null;
55         
56
57         public static PDPNotification result(NotificationScheme scheme) {
58                 if (resultJson == null || notification == null) {
59                         logger.debug("No Result" );
60                         return null;
61                 } else {
62                         if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
63                                 boolean removed = false, updated = false; 
64                                 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
65                                         removed = true;
66                                 }
67                                 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
68                                         updated = true;
69                                 }
70                                 if(removed && updated) {
71                                         notification.setNotificationType(NotificationType.BOTH);
72                                 }else if(removed){
73                                         notification.setNotificationType(NotificationType.REMOVE);
74                                 }else if(updated){
75                                         notification.setNotificationType(NotificationType.UPDATE);
76                                 }
77                                 return notification;
78                         }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
79                                 return MatchStore.checkMatch(notification);
80                         }else {
81                                 return null;
82                         }
83                 }
84         }
85
86         private static void publishMessage(String pubTopic, String uniqueID , List<String> uebURLList) {
87                 
88                 String UEBlist = uebURLList.toString();
89                 UEBlist = UEBlist.substring(1,UEBlist.length()-1);
90         CambriaPublisher pub = null;
91                 try {
92                         pub = CambriaClientFactory.createSimplePublisher(null, UEBlist, pubTopic);
93                 } catch (MalformedURLException e1) {
94                         e1.printStackTrace();
95                 } catch (GeneralSecurityException e1) {
96                         e1.printStackTrace();
97                 }
98         final JSONObject msg1 = new JSONObject (); 
99
100         msg1.put ( "JSON", "UEB Update Ruest UID=" + uniqueID);  
101
102         try {
103                         pub.send ( "MyPartitionKey", msg1.toString () );
104                 } catch (IOException e) {
105                         logger.error("Exception Occured"+e);
106                 } 
107         pub.close (); 
108                 
109         }
110
111         public static void createTopic (String url, String uniquID, List<String> uebURLList){
112                 URL aURL;
113                 try {
114                         aURL = new URL(url);
115                         topic = aURL.getHost() + aURL.getPort();
116                 } catch (MalformedURLException e) {
117                         topic = url.replace("[:/]", "");
118                 }
119
120                 publishMessage(topic+ uniquID , uniquID, uebURLList);
121                 
122         }
123         public static void start(String url, List<String> uebURLList,
124                         String uniqueID) {
125                 ManualClientEndUEB.uebURLList  = uebURLList;
126                 ManualClientEndUEB.url = url;
127                 ManualClientEndUEB.uniquID = uniqueID;
128                 URL aURL;
129                 try {
130                         aURL = new URL(url);
131                         ManualClientEndUEB.topic = aURL.getHost() + aURL.getPort();
132                 } catch (MalformedURLException e) {
133                         ManualClientEndUEB.topic = url.replace("[:/]", "");
134                 }
135                 String id = "0";
136                 try {
137                         CConsumer = CambriaClientFactory.createConsumer ( null, uebURLList, topic + uniquID, "clientGroup", id, 15*1000, 1000 );
138                 } catch (MalformedURLException e1) {
139                         e1.printStackTrace();
140                 } catch (GeneralSecurityException e1) {
141                         e1.printStackTrace();
142                 }               
143                 int count = 1;
144                 while (count < 4) {
145                                 publishMessage(topic + "UpdateRequest", uniquID, uebURLList);
146                                 try {
147                                         for ( String msg : CConsumer.fetch () )
148                                         {       
149                                                 
150                                                 logger.debug("Manual Notification Recieved Message " + msg + " from UEB cluster : " + uebURLList.toString());
151                                                 resultJson = msg;
152                                                 if (!msg.contains("UEB Update")){
153 //                                                      System.out.println("Manual Notification Recieved Message " + msg + " from UEB cluster : " + uebURLList.toString());
154                                                         notification = NotificationUnMarshal.notificationJSON(msg);
155                                                         count = 4;
156                                                 }
157                                         }
158                                 }catch (Exception e) {
159                                         
160                                 } 
161                                 count++;
162                         }               
163         }
164         
165 }