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;
34 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
35 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
36 import org.openecomp.policy.common.logging.flexlogger.Logger;
38 import com.att.nsa.cambria.client.CambriaClientBuilders;
39 import com.att.nsa.cambria.client.CambriaClientFactory;
40 import com.att.nsa.cambria.client.CambriaConsumer;
41 import com.att.nsa.cambria.client.CambriaClientBuilders.ConsumerBuilder;
43 * Create a UEB Consumer to receive policy update notification.
48 @SuppressWarnings("deprecation")
49 public class AutoClientUEB implements Runnable {
50 private static StdPDPNotification notification = null;
51 private static NotificationScheme scheme = null;
52 private static NotificationHandler handler = null;
53 private static String topic = null;
54 private static String url = null;
55 private static boolean status = false;
56 private static Logger logger = FlexLogger.getLogger(AutoClientUEB.class.getName());
57 private static String notficatioinType = null;
58 private static CambriaConsumer CConsumer = null;
59 private static String apiKey = null;
60 private static String apiSecret = null;
61 private static List<String> uebURLList = null;
62 public volatile boolean isRunning = false;
65 public AutoClientUEB(String url, List<String> uebURLList, String apiKey, String apiSecret) {
66 AutoClientUEB.url = url;
67 AutoClientUEB.uebURLList = uebURLList;
68 AutoClientUEB.apiKey = apiKey;
69 AutoClientUEB.apiKey = apiKey;
72 public void setAuto(NotificationScheme scheme,
73 NotificationHandler handler) {
74 AutoClientUEB.scheme = scheme;
75 AutoClientUEB.handler = handler;
78 public static void setScheme(NotificationScheme scheme) {
79 AutoClientUEB.scheme = scheme;
82 public static boolean getStatus(){
83 return AutoClientUEB.status;
86 public static String getURL() {
87 return AutoClientUEB.url;
90 public static String getNotficationType(){
91 return AutoClientUEB.notficatioinType;
94 public synchronized boolean isRunning() {
95 return this.isRunning;
98 public synchronized void terminate() {
99 this.isRunning = false;
105 this.isRunning = true;
107 String group = UUID.randomUUID ().toString ();
109 //String topic = null;
110 // Stop and Start needs to be done.
111 if (scheme != null && handler!=null) {
112 if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS) || scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
115 aURL = new URL(AutoClientUEB.topic);
116 topic = aURL.getHost() + aURL.getPort();
117 } catch (MalformedURLException e) {
118 topic = AutoClientUEB.url.replace("[:/]", "");
121 //TODO create a loop to listen for messages from UEB cluster
124 //CConsumer = CambriaClientFactory.createConsumer ( null, uebURLList, topic, group, id, 15*1000, 1000 );
125 ConsumerBuilder builder = new CambriaClientBuilders.ConsumerBuilder();
126 builder.knownAs(group, id)
127 .usingHosts(uebURLList)
129 .waitAtServer(15*1000)
130 .receivingAtMost(1000)
131 .authenticatedBy(apiKey, apiSecret);
133 CConsumer = builder.build();
135 } catch (MalformedURLException e1) {
136 e1.printStackTrace();
137 } catch (GeneralSecurityException e1) {
138 e1.printStackTrace();
140 while (this.isRunning() )
143 for ( String msg : CConsumer.fetch () )
145 logger.debug("Auto Notification Recieved Message " + msg + " from UEB cluster : " + uebURLList.toString());
146 notification = NotificationUnMarshal.notificationJSON(msg);
149 } catch (Exception e) {
150 // TODO Auto-generated catch block
151 logger.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Error in processing UEB message" + e.getMessage());
155 logger.debug("Stopping UEB Consumer loop will not logger fetch messages from the cluster");
160 private static void callHandler() {
161 if (handler != null && scheme != null) {
162 if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)) {
163 boolean removed = false, updated = false;
164 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
167 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
170 if (removed && updated) {
171 notification.setNotificationType(NotificationType.BOTH);
172 } else if (removed) {
173 notification.setNotificationType(NotificationType.REMOVE);
174 } else if (updated) {
175 notification.setNotificationType(NotificationType.UPDATE);
177 handler.notificationReceived(notification);
178 } else if (scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
179 PDPNotification newNotification = MatchStore.checkMatch(notification);
180 if (newNotification.getNotificationType() != null) {
181 handler.notificationReceived(newNotification);