Fixes for sonar critical issues
[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
56         public static PDPNotification result(NotificationScheme scheme) {
57                 if (resultJson == null || notification == null) {
58                         logger.debug("No Result" );
59                         return null;
60                 } else {
61                         if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
62                                 boolean removed = false, updated = false; 
63                                 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
64                                         removed = true;
65                                 }
66                                 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
67                                         updated = true;
68                                 }
69                                 if(removed && updated) {
70                                         notification.setNotificationType(NotificationType.BOTH);
71                                 }else if(removed){
72                                         notification.setNotificationType(NotificationType.REMOVE);
73                                 }else if(updated){
74                                         notification.setNotificationType(NotificationType.UPDATE);
75                                 }
76                                 return notification;
77                         }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
78                                 return MatchStore.checkMatch(notification);
79                         }else {
80                                 return null;
81                         }
82                 }
83         }
84
85         private static void publishMessage(String pubTopic, String uniqueID , List<String> uebURLList) {
86                 
87                 String UEBlist = uebURLList.toString();
88                 UEBlist = UEBlist.substring(1,UEBlist.length()-1);
89         CambriaPublisher pub = null;
90                 try {
91                         pub = CambriaClientFactory.createSimplePublisher(null, UEBlist, pubTopic);
92                 } catch (Exception e1) {
93                         logger.error("Exception Occured"+e1);
94                 }
95         final JSONObject msg1 = new JSONObject (); 
96
97         msg1.put ( "JSON", "UEB Update Ruest UID=" + uniqueID);  
98         if(pub != null){
99                  try {
100                         pub.send ( "MyPartitionKey", msg1.toString () );
101                         pub.close ();   
102                 } catch (IOException e) {
103                         logger.error("Exception Occured"+e);
104                 }
105         }       
106         }
107
108         public static void createTopic (String url, String uniquID, List<String> uebURLList){
109                 URL aURL;
110                 try {
111                         aURL = new URL(url);
112                         topic = aURL.getHost() + aURL.getPort();
113                 } catch (MalformedURLException e) {
114                         topic = url.replace("[:/]", "");
115                 }
116
117                 publishMessage(topic+ uniquID , uniquID, uebURLList);
118                 
119         }
120         public static void start(String url, List<String> uebURLList,
121                         String uniqueID) {
122                 ManualClientEndUEB.uebURLList  = uebURLList;
123                 ManualClientEndUEB.url = url;
124                 ManualClientEndUEB.uniquID = uniqueID;
125                 URL aURL;
126                 try {
127                         aURL = new URL(url);
128                         ManualClientEndUEB.topic = aURL.getHost() + aURL.getPort();
129                 } catch (MalformedURLException e) {
130                         ManualClientEndUEB.topic = url.replace("[:/]", "");
131                 }
132                 String id = "0";
133                 try {
134                         CConsumer = CambriaClientFactory.createConsumer ( null, uebURLList, topic + uniquID, "clientGroup", id, 15*1000, 1000 );
135                 } catch (Exception e1) {
136                         logger.error("Exception Occured"+e1);
137                 }               
138                 int count = 1;
139                 while (count < 4) {
140                                 publishMessage(topic + "UpdateRequest", uniquID, uebURLList);
141                                 try {
142                                         for ( String msg : CConsumer.fetch () )
143                                         {       
144                                                 
145                                                 logger.debug("Manual Notification Recieved Message " + msg + " from UEB cluster : " + uebURLList.toString());
146                                                 resultJson = msg;
147                                                 if (!msg.contains("UEB Update")){
148                                                         notification = NotificationUnMarshal.notificationJSON(msg);
149                                                         count = 4;
150                                                 }
151                                         }
152                                 }catch (Exception e) {
153                                         logger.error("Error in Manual CLient UEB notification ", e);
154                                 } 
155                                 count++;
156                         }               
157         }
158         
159 }