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