2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.std;
24 import java.io.IOException;
25 import java.net.MalformedURLException;
27 import java.util.List;
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;
36 import com.att.nsa.cambria.client.CambriaClientFactory;
37 import com.att.nsa.cambria.client.CambriaConsumer;
38 import com.att.nsa.cambria.client.CambriaPublisher;
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;
57 private ManualClientEndUEB() {
61 public static PDPNotification result(NotificationScheme scheme) {
62 if (resultJson == null || notification == null) {
63 logger.debug("No Result" );
66 if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
67 boolean removed = false;
68 boolean updated = false;
69 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
72 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
75 if(removed && updated) {
76 notification.setNotificationType(NotificationType.BOTH);
78 notification.setNotificationType(NotificationType.REMOVE);
80 notification.setNotificationType(NotificationType.UPDATE);
83 }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
84 return MatchStore.checkMatch(notification);
89 private static void publishMessage(String pubTopic, String uniqueID , List<String> uebURLList) {
91 String UEBlist = uebURLList.toString();
92 UEBlist = UEBlist.substring(1,UEBlist.length()-1);
93 CambriaPublisher pub = null;
95 pub = CambriaClientFactory.createSimplePublisher(null, UEBlist, pubTopic);
96 } catch (Exception e1) {
97 logger.error("Exception Occured"+e1);
99 final JSONObject msg1 = new JSONObject ();
101 msg1.put ( "JSON", "UEB Update Ruest UID=" + uniqueID);
104 pub.send ( "MyPartitionKey", msg1.toString () );
106 } catch (IOException e) {
107 logger.error("Exception Occured"+e);
112 public static void createTopic (String url, String uniquID, List<String> uebURLList){
116 topic = aURL.getHost() + aURL.getPort();
117 } catch (MalformedURLException e) {
118 topic = url.replace("[:/]", "");
121 publishMessage(topic+ uniquID , uniquID, uebURLList);
124 public static void start(String url, List<String> uebURLList,
126 ManualClientEndUEB.uebURLList = uebURLList;
127 ManualClientEndUEB.url = url;
128 ManualClientEndUEB.uniquID = uniqueID;
132 ManualClientEndUEB.topic = aURL.getHost() + aURL.getPort();
133 } catch (MalformedURLException e) {
134 ManualClientEndUEB.topic = url.replace("[:/]", "");
138 CConsumer = CambriaClientFactory.createConsumer ( null, uebURLList, topic + uniquID, "clientGroup", id, 15*1000, 1000 );
139 } catch (Exception e1) {
140 logger.error("Exception Occured"+e1);
143 boolean isSuccess = false;
144 while (retries < RETRY_LIMIT && !isSuccess) {
145 isSuccess = publishMessageAndSetNotification(uebURLList);
150 private static boolean publishMessageAndSetNotification(List<String> uebURLList) {
151 publishMessage(topic + "UpdateRequest", uniquID, uebURLList);
153 for ( String msg : CConsumer.fetch () ) {
154 logger.debug("Manual Notification Recieved Message " + msg + " from UEB cluster : " + uebURLList.toString());
156 if (!msg.contains("UEB Update")){
157 notification = NotificationUnMarshal.notificationJSON(msg);
161 }catch (Exception e) {
162 logger.error("Error in Manual CLient UEB notification ", e);