2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.std;
23 import java.net.MalformedURLException;
25 import java.security.GeneralSecurityException;
26 import java.util.List;
27 import java.util.UUID;
29 import org.openecomp.policy.api.NotificationHandler;
30 import org.openecomp.policy.api.NotificationScheme;
31 import org.openecomp.policy.api.NotificationType;
32 import org.openecomp.policy.api.PDPNotification;
33 import org.openecomp.policy.std.StdPDPNotification;
35 import com.att.nsa.cambria.client.CambriaClientFactory;
36 import com.att.nsa.cambria.client.CambriaConsumer;
37 import org.openecomp.policy.common.logging.flexlogger.*;
39 * Create a UEB Consumer to receive policy update notification.
44 public class AutoClientUEB implements Runnable {
45 private static StdPDPNotification notification = null;
46 private static NotificationScheme scheme = null;
47 private static NotificationHandler handler = null;
48 private static String url = null;
49 private static boolean status = false;
50 private static Logger logger = FlexLogger.getLogger(AutoClientUEB.class.getName());
51 private static String notficatioinType = null;
52 private static CambriaConsumer CConsumer = null;
53 // private volatile boolean stop = false;
54 private static List<String> uebURLList = null;
55 public volatile boolean isRunning = false;
58 public AutoClientUEB(String url, List<String> uebURLList) {
59 AutoClientUEB.url = url;
60 AutoClientUEB.uebURLList = uebURLList;
63 public void setAuto(NotificationScheme scheme,
64 NotificationHandler handler) {
65 AutoClientUEB.scheme = scheme;
66 AutoClientUEB.handler = handler;
69 public static void setScheme(NotificationScheme scheme) {
70 AutoClientUEB.scheme = scheme;
73 public static boolean getStatus(){
74 return AutoClientUEB.status;
77 public static String getURL() {
78 return AutoClientUEB.url;
81 public static String getNotficationType(){
82 return AutoClientUEB.notficatioinType;
85 public synchronized boolean isRunning() {
86 return this.isRunning;
89 public synchronized void terminate() {
90 this.isRunning = false;
92 @SuppressWarnings("deprecation")
96 this.isRunning = true;
98 String group = UUID.randomUUID ().toString ();
101 // Stop and Start needs to be done.
102 if (scheme != null && handler!=null) {
103 if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS) || scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
104 //Check if the Notification Type is UEB t if (notficationType.equals("ueb")){
107 aURL = new URL(AutoClientUEB.url);
108 topic = aURL.getHost() + aURL.getPort();
109 } catch (MalformedURLException e) {
110 topic = AutoClientUEB.url.replace("[:/]", "");
113 //TODO create a loop to listen for messages from UEB cluster
115 CConsumer = CambriaClientFactory.createConsumer ( null, uebURLList, topic, group, id, 15*1000, 1000 );
116 } catch (MalformedURLException e1) {
117 // TODO Auto-generated catch block
118 e1.printStackTrace();
119 } catch (GeneralSecurityException e1) {
120 // TODO Auto-generated catch block
121 e1.printStackTrace();
123 while (this.isRunning() )
126 for ( String msg : CConsumer.fetch () )
128 logger.debug("Auto Notification Recieved Message " + msg + " from UEB cluster : " + uebURLList.toString());
129 notification = NotificationUnMarshal.notificationJSON(msg);
132 } catch (Exception e) {
133 // TODO Auto-generated catch block
134 logger.debug("Error in processing UEB message");
138 logger.debug("Stopping UEB Consuer loop will not logger fetch messages from the cluser");
143 private static void callHandler() {
144 if (handler != null && scheme != null) {
145 if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)) {
146 boolean removed = false, updated = false;
147 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
150 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
153 if (removed && updated) {
154 notification.setNotificationType(NotificationType.BOTH);
155 } else if (removed) {
156 notification.setNotificationType(NotificationType.REMOVE);
157 } else if (updated) {
158 notification.setNotificationType(NotificationType.UPDATE);
160 handler.notificationReceived(notification);
161 } else if (scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
162 PDPNotification newNotification = MatchStore.checkMatch(notification);
163 if (newNotification.getNotificationType() != null) {
164 handler.notificationReceived(newNotification);