c04736e8661c116ab1c14a7ee3438129f342ee03
[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;
66                         boolean 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                 }
84                 return null;
85         }
86
87         private static void publishMessage(String pubTopic, String uniqueID , List<String> uebURLList) {
88                 
89                 String UEBlist = uebURLList.toString();
90                 UEBlist = UEBlist.substring(1,UEBlist.length()-1);
91         CambriaPublisher pub = null;
92                 try {
93                         pub = CambriaClientFactory.createSimplePublisher(null, UEBlist, pubTopic);
94                 } catch (Exception e1) {
95                         logger.error("Exception Occured"+e1);
96                 }
97         final JSONObject msg1 = new JSONObject (); 
98
99         msg1.put ( "JSON", "UEB Update Ruest UID=" + uniqueID);  
100         if(pub != null){
101                  try {
102                         pub.send ( "MyPartitionKey", msg1.toString () );
103                         pub.close ();   
104                 } catch (IOException e) {
105                         logger.error("Exception Occured"+e);
106                 }
107         }       
108         }
109
110         public static void createTopic (String url, String uniquID, List<String> uebURLList){
111                 URL aURL;
112                 try {
113                         aURL = new URL(url);
114                         topic = aURL.getHost() + aURL.getPort();
115                 } catch (MalformedURLException e) {
116                         topic = url.replace("[:/]", "");
117                 }
118
119                 publishMessage(topic+ uniquID , uniquID, uebURLList);
120                 
121         }
122         public static void start(String url, List<String> uebURLList,
123                         String uniqueID) {
124                 ManualClientEndUEB.uebURLList  = uebURLList;
125                 ManualClientEndUEB.url = url;
126                 ManualClientEndUEB.uniquID = uniqueID;
127                 URL aURL;
128                 try {
129                         aURL = new URL(url);
130                         ManualClientEndUEB.topic = aURL.getHost() + aURL.getPort();
131                 } catch (MalformedURLException e) {
132                         ManualClientEndUEB.topic = url.replace("[:/]", "");
133                 }
134                 String id = "0";
135                 try {
136                         CConsumer = CambriaClientFactory.createConsumer ( null, uebURLList, topic + uniquID, "clientGroup", id, 15*1000, 1000 );
137                 } catch (Exception e1) {
138                         logger.error("Exception Occured"+e1);
139                 }               
140                 int count = 1;
141                 while (count < 4) {
142                                 publishMessage(topic + "UpdateRequest", uniquID, uebURLList);
143                                 try {
144                                         for ( String msg : CConsumer.fetch () )
145                                         {       
146                                                 
147                                                 logger.debug("Manual Notification Recieved Message " + msg + " from UEB cluster : " + uebURLList.toString());
148                                                 resultJson = msg;
149                                                 if (!msg.contains("UEB Update")){
150                                                         notification = NotificationUnMarshal.notificationJSON(msg);
151                                                         count = 4;
152                                                 }
153                                         }
154                                 }catch (Exception e) {
155                                         logger.error("Error in Manual CLient UEB notification ", e);
156                                 } 
157                                 count++;
158                         }               
159         }
160         
161 }