Refactor code for nested stmts in policy std
[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  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.std;
23
24 import java.io.IOException;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.util.List;
28
29 import org.json.JSONObject;
30 import org.onap.policy.api.NotificationScheme;
31 import org.onap.policy.api.NotificationType;
32 import org.onap.policy.api.PDPNotification;
33 import org.onap.policy.common.logging.flexlogger.FlexLogger;
34 import org.onap.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     private static int RETRY_LIMIT = 4;
56
57     private ManualClientEndUEB() {
58         // Empty constructor
59     }
60
61     public static PDPNotification result(NotificationScheme scheme) {
62         if (resultJson == null || notification == null) {
63             logger.debug("No Result" );
64             return null;
65         }
66         if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
67             boolean removed = false;
68             boolean updated = false;
69             if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
70                 removed = true;
71             }
72             if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
73                 updated = true;
74             }
75             if(removed && updated) {
76                 notification.setNotificationType(NotificationType.BOTH);
77             }else if(removed){
78                 notification.setNotificationType(NotificationType.REMOVE);
79             }else if(updated){
80                 notification.setNotificationType(NotificationType.UPDATE);
81             }
82             return notification;
83         }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
84             return MatchStore.checkMatch(notification);
85         }
86         return null;
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 (Exception e1) {
97             logger.error("Exception Occured"+e1);
98         }
99         final JSONObject msg1 = new JSONObject (); 
100
101         msg1.put ( "JSON", "UEB Update Ruest UID=" + uniqueID);  
102         if(pub != null){
103              try {
104                 pub.send ( "MyPartitionKey", msg1.toString () );
105                 pub.close ();
106             } catch (IOException e) {
107                 logger.error("Exception Occured"+e);
108             }
109         }       
110     }
111
112     public static void createTopic (String url, String uniquID, List<String> uebURLList){
113         URL aURL;
114         try {
115             aURL = new URL(url);
116             topic = aURL.getHost() + aURL.getPort();
117         } catch (MalformedURLException e) {
118             topic = url.replace("[:/]", "");
119         }
120
121         publishMessage(topic+ uniquID , uniquID, uebURLList);
122
123     }
124     public static void start(String url, List<String> uebURLList,
125             String uniqueID) {
126         ManualClientEndUEB.uebURLList  = uebURLList;
127         ManualClientEndUEB.url = url;
128         ManualClientEndUEB.uniquID = uniqueID;
129         URL aURL;
130         try {
131             aURL = new URL(url);
132             ManualClientEndUEB.topic = aURL.getHost() + aURL.getPort();
133         } catch (MalformedURLException e) {
134             ManualClientEndUEB.topic = url.replace("[:/]", "");
135         }
136         String id = "0";
137         try {
138             CConsumer = CambriaClientFactory.createConsumer ( null, uebURLList, topic + uniquID, "clientGroup", id, 15*1000, 1000 );
139         } catch (Exception e1) {
140             logger.error("Exception Occured"+e1);
141         }
142         int retries = 1;
143         boolean isSuccess = false;
144         while (retries < RETRY_LIMIT && !isSuccess) {
145             isSuccess = publishMessageAndSetNotification(uebURLList);
146             retries++;
147         }
148     }
149
150     private static boolean publishMessageAndSetNotification(List<String> uebURLList) {
151         publishMessage(topic + "UpdateRequest", uniquID, uebURLList);
152         try {
153             for ( String msg : CConsumer.fetch () ) {
154                 logger.debug("Manual Notification Recieved Message " + msg + " from UEB cluster : " + uebURLList.toString());
155                 resultJson = msg;
156                 if (!msg.contains("UEB Update")){
157                     notification = NotificationUnMarshal.notificationJSON(msg);
158                     return true;
159                 }
160             }
161         }catch (Exception e) {
162             logger.error("Error in Manual CLient UEB notification ", e);
163         }
164         return false;
165     }
166 }