--- /dev/null
+/*\r
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+package org.opendaylight.controller.sal.demo;\r
+\r
+import java.util.Collection;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import org.opendaylight.controller.sal.core.api.Consumer;\r
+import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;\r
+import org.opendaylight.controller.sal.core.api.notify.NotificationListener;\r
+import org.opendaylight.controller.sal.core.api.notify.NotificationService;\r
+import org.opendaylight.controller.yang.common.QName;\r
+import org.opendaylight.controller.yang.data.api.CompositeNode;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+\r
+public class DemoConsumerImpl implements Consumer {\r
+\r
+ private ConsumerSession session;\r
+ private NotificationService notificationService;\r
+ private final String name;\r
+ private static Logger log = LoggerFactory.getLogger("AlertLogger");\r
+\r
+ private boolean changeAware;\r
+\r
+ public DemoConsumerImpl(String name) {\r
+ this.name = name;\r
+ }\r
+\r
+ private NotificationListener alertLogger = new NotificationListener() {\r
+\r
+ @Override\r
+ public void onNotification(CompositeNode notification) {\r
+ System.out.println(name\r
+ + ": Received alert: "\r
+ + notification.getFirstSimpleByName(\r
+ DemoUtils.contentNodeName).getValue());\r
+ log.info("AlertLogger: Received notification: " + notification);\r
+ }\r
+\r
+ @Override\r
+ public Set<QName> getSupportedNotifications() {\r
+ Set<QName> supported = new HashSet<QName>();\r
+ supported.add(DemoUtils.alertNotification);\r
+ return supported;\r
+ }\r
+ };\r
+\r
+ private NotificationListener changeLogger = new NotificationListener() {\r
+\r
+ @Override\r
+ public void onNotification(CompositeNode notification) {\r
+ System.out.println(name\r
+ + ": Received change: "\r
+ + notification.getFirstSimpleByName(\r
+ DemoUtils.contentNodeName).getValue());\r
+ log.info("ChangeLogger: Received notification: " + notification);\r
+ }\r
+\r
+ @Override\r
+ public Set<QName> getSupportedNotifications() {\r
+ Set<QName> supported = new HashSet<QName>();\r
+ supported.add(DemoUtils.alertNotification);\r
+ return supported;\r
+ }\r
+ };\r
+\r
+ @Override\r
+ public void onSessionInitiated(ConsumerSession session) {\r
+ this.session = session;\r
+ this.notificationService = session\r
+ .getService(NotificationService.class);\r
+ notificationService.addNotificationListener(\r
+ DemoUtils.alertNotification, alertLogger);\r
+ if (isChangeAware()) {\r
+ notificationService.addNotificationListener(\r
+ DemoUtils.changeNotification, changeLogger);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Collection<ConsumerFunctionality> getConsumerFunctionality() {\r
+ Set<ConsumerFunctionality> func = new HashSet<ConsumerFunctionality>();\r
+ func.add(alertLogger);\r
+ return func;\r
+ }\r
+\r
+ public void closeSession() {\r
+ session.close();\r
+ }\r
+\r
+ public boolean isChangeAware() {\r
+ return changeAware;\r
+ }\r
+\r
+ public void setChangeAware(boolean changeAware) {\r
+ this.changeAware = changeAware;\r
+ }\r
+\r
+}\r