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.util.List;
24 import java.util.UUID;
26 import org.onap.policy.api.NotificationHandler;
27 import org.onap.policy.api.NotificationScheme;
28 import org.onap.policy.api.NotificationType;
29 import org.onap.policy.api.PDPNotification;
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
32 import org.onap.policy.utils.BusConsumer;
33 import org.onap.policy.utils.BusConsumer.DmaapConsumerWrapper;
34 import org.onap.policy.xacml.api.XACMLErrorConstants;
36 public class AutoClientDMAAP implements Runnable {
37 private static StdPDPNotification notification = null;
38 private static NotificationScheme scheme = null;
39 private static NotificationHandler handler = null;
40 private static String topic = null;
41 private static boolean status = false;
42 private static Logger logger = FlexLogger.getLogger(AutoClientDMAAP.class.getName());
43 private static String notficatioinType = null;
44 private static BusConsumer dmaapConsumer = null;
45 private static List<String> dmaapList = null;
46 private static String aafLogin = null;
47 private static String aafPassword = null;
48 private volatile boolean running = false;
50 public AutoClientDMAAP(List<String> dmaapList, String topic, String aafLogin, String aafPassword) {
51 AutoClientDMAAP.topic = topic;
52 AutoClientDMAAP.dmaapList = dmaapList;
53 AutoClientDMAAP.aafLogin = aafLogin;
54 AutoClientDMAAP.aafPassword = aafPassword;
57 public static void setAuto(NotificationScheme scheme, NotificationHandler handler) {
58 AutoClientDMAAP.scheme = scheme;
59 AutoClientDMAAP.handler = handler;
62 public static void setScheme(NotificationScheme scheme) {
63 AutoClientDMAAP.scheme = scheme;
66 public static boolean getStatus() {
67 return AutoClientDMAAP.status;
70 public static String getTopic() {
71 return AutoClientDMAAP.topic;
74 public static String getNotficationType() {
75 return AutoClientDMAAP.notficatioinType;
78 public synchronized boolean isRunning() {
82 public synchronized void terminate() {
91 String group = UUID.randomUUID().toString();
94 if (scheme == null || handler == null ||
95 ! (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS) ||
96 scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) ) {
97 logger.info("no stop/start required");
101 // create a loop to listen for messages from DMaaP server
103 setDmaapCosumer(new BusConsumer.DmaapConsumerWrapper(dmaapList, topic, aafLogin, aafPassword, group,
104 id, 15 * 1000, 1000));
105 } catch (Exception e) {
106 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Consumer: ", e);
109 while (this.isRunning()) {
111 for (String msg : dmaapConsumer.fetch()) {
112 logger.debug("Auto Notification Recieved Message " + msg + " from DMAAP server : "
113 + dmaapList.toString());
114 setNotification(NotificationUnMarshal.notificationJSON(msg));
117 } catch (Exception e) {
118 logger.debug("Error in processing DMAAP message", e);
122 logger.debug("Stopping DMAAP Consumer loop will no longer fetch messages from the servers");
125 private static void setNotification(StdPDPNotification notificationJSON) {
126 notification = notificationJSON;
129 private static void setDmaapCosumer(DmaapConsumerWrapper dmaapConsumerWrapper) {
130 dmaapConsumer = dmaapConsumerWrapper;
133 private static void callHandler() {
135 if (handler == null || scheme == null) {
136 logger.info("handler does not need to do anything");
139 if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)) {
140 boolean removed = false;
141 boolean updated = false;
142 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
145 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
148 if (removed && updated) {
149 notification.setNotificationType(NotificationType.BOTH);
150 } else if (removed) {
151 notification.setNotificationType(NotificationType.REMOVE);
152 } else if (updated) {
153 notification.setNotificationType(NotificationType.UPDATE);
155 handler.notificationReceived(notification);
156 } else if (scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
157 PDPNotification newNotification = MatchStore.checkMatch(notification);
158 if (newNotification.getNotificationType() != null) {
159 handler.notificationReceived(newNotification);