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.onap.policy.std;
23 import java.net.MalformedURLException;
25 import java.util.List;
26 import java.util.UUID;
28 import org.onap.policy.api.NotificationHandler;
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 import org.onap.policy.xacml.api.XACMLErrorConstants;
36 import com.att.nsa.cambria.client.CambriaClientBuilders;
37 import com.att.nsa.cambria.client.CambriaClientBuilders.ConsumerBuilder;
38 import com.att.nsa.cambria.client.CambriaConsumer;
40 * Create a UEB Consumer to receive policy update notification.
45 public class AutoClientUEB implements Runnable {
46 private static StdPDPNotification notification = null;
47 private static NotificationScheme scheme = null;
48 private static NotificationHandler handler = null;
49 private static String topic = null;
50 private static String url = null;
51 private static boolean status = false;
52 private static Logger logger = FlexLogger.getLogger(AutoClientUEB.class.getName());
53 private static String notficatioinType = null;
54 private static CambriaConsumer cConsumer = null;
55 private static String apiKey = null;
56 private static String apiSecret = null;
57 private static List<String> uebURLList = null;
58 private volatile boolean running = false;
61 public AutoClientUEB(String url, List<String> uebURLList, String apiKey, String apiSecret) {
62 AutoClientUEB.url = url;
63 AutoClientUEB.uebURLList = uebURLList;
64 AutoClientUEB.apiKey = apiKey;
65 AutoClientUEB.apiKey = apiKey;
68 public static void setAuto(NotificationScheme scheme,
69 NotificationHandler handler) {
70 AutoClientUEB.scheme = scheme;
71 AutoClientUEB.handler = handler;
74 public static void setScheme(NotificationScheme scheme) {
75 AutoClientUEB.scheme = scheme;
78 public static boolean getStatus(){
79 return AutoClientUEB.status;
82 public static String getURL() {
83 return AutoClientUEB.url;
86 public static String getNotficationType(){
87 return AutoClientUEB.notficatioinType;
90 public synchronized boolean isRunning() {
94 public synchronized void terminate() {
103 String group = UUID.randomUUID ().toString ();
105 // Stop and Start needs to be done.
106 if (scheme != null && handler!=null) {
107 if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS) || scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
110 aURL = new URL(AutoClientUEB.topic);
111 setTopic(aURL.getHost() + aURL.getPort());
112 } catch (MalformedURLException e) {
113 setTopic(AutoClientUEB.url.replace("[:/]", ""));
116 ConsumerBuilder builder = new CambriaClientBuilders.ConsumerBuilder();
117 builder.knownAs(group, id)
118 .usingHosts(uebURLList)
120 .waitAtServer(15*1000)
121 .receivingAtMost(1000)
122 .authenticatedBy(apiKey, apiSecret);
123 setConsumer(builder.build());
124 } catch (Exception e1) {
125 logger.error("Exception Occured" + e1);
127 while (this.isRunning()) {
129 for (String msg : cConsumer.fetch()) {
130 logger.debug("Auto Notification Recieved Message " + msg + " from UEB cluster : "
131 + uebURLList.toString());
132 setNotification(NotificationUnMarshal.notificationJSON(msg));
135 } catch (Exception e) {
136 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Error in processing UEB message"
137 + e.getMessage(), e);
141 logger.debug("Stopping UEB Consumer loop will not logger fetch messages from the cluster");
146 private static void setNotification(StdPDPNotification notificationJSON) {
147 notification = notificationJSON;
150 private static void setConsumer(CambriaConsumer build) {
154 private static void setTopic(String topic) {
155 AutoClientUEB.topic = topic;
158 private static void callHandler() {
159 if (handler != null && scheme != null) {
160 if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)) {
161 boolean removed = false;
162 boolean updated = false;
163 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
166 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
169 if (removed && updated) {
170 notification.setNotificationType(NotificationType.BOTH);
171 } else if (removed) {
172 notification.setNotificationType(NotificationType.REMOVE);
173 } else if (updated) {
174 notification.setNotificationType(NotificationType.UPDATE);
176 handler.notificationReceived(notification);
177 } else if (scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
178 PDPNotification newNotification = MatchStore.checkMatch(notification);
179 if (newNotification.getNotificationType() != null) {
180 handler.notificationReceived(newNotification);