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