2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.controller.sal.core.impl.notify;
\r
10 import java.util.Collection;
\r
11 import java.util.Collections;
\r
12 import java.util.HashSet;
\r
13 import java.util.Map;
\r
14 import java.util.Map.Entry;
\r
15 import java.util.Set;
\r
17 import org.opendaylight.controller.sal.core.api.BrokerService;
\r
18 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
\r
19 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
\r
20 import org.opendaylight.controller.sal.core.api.Consumer.ConsumerFunctionality;
\r
21 import org.opendaylight.controller.sal.core.api.Provider.ProviderFunctionality;
\r
22 import org.opendaylight.controller.sal.core.api.notify.NotificationListener;
\r
23 import org.opendaylight.controller.sal.core.api.notify.NotificationProviderService;
\r
24 import org.opendaylight.controller.sal.core.api.notify.NotificationService;
\r
25 import org.opendaylight.controller.sal.core.impl.BrokerServiceImpl;
\r
26 import org.opendaylight.controller.sal.core.spi.BrokerModule;
\r
27 import org.opendaylight.controller.yang.common.QName;
\r
28 import org.opendaylight.controller.yang.data.api.CompositeNode;
\r
29 import org.slf4j.Logger;
\r
30 import org.slf4j.LoggerFactory;
\r
32 import com.google.common.collect.HashMultimap;
\r
33 import com.google.common.collect.ImmutableSet;
\r
34 import com.google.common.collect.Multimap;
\r
36 public class NotificationModule implements BrokerModule {
\r
37 private static Logger log = LoggerFactory
\r
38 .getLogger(NotificationModule.class);
\r
40 private Multimap<QName, NotificationListener> listeners = HashMultimap
\r
43 private static final Set<Class<? extends BrokerService>> providedServices = ImmutableSet
\r
44 .of((Class<? extends BrokerService>) NotificationService.class,
\r
45 NotificationProviderService.class);
\r
48 public Set<Class<? extends BrokerService>> getProvidedServices() {
\r
49 return providedServices;
\r
53 public Set<Class<? extends ConsumerFunctionality>> getSupportedConsumerFunctionality() {
\r
55 Set<Class<? extends ConsumerFunctionality>> ret = new HashSet<Class<? extends ConsumerFunctionality>>();
\r
56 ret.add(NotificationListener.class);
\r
61 public <T extends BrokerService> T getServiceForSession(Class<T> service,
\r
62 ConsumerSession session) {
\r
63 if (NotificationProviderService.class.equals(service)
\r
64 && session instanceof ProviderSession) {
\r
65 @SuppressWarnings("unchecked")
\r
66 T ret = (T) newNotificationProviderService(session);
\r
68 } else if (NotificationService.class.equals(service)) {
\r
70 @SuppressWarnings("unchecked")
\r
71 T ret = (T) newNotificationConsumerService(session);
\r
75 throw new IllegalArgumentException(
\r
76 "The requested session-specific service is not provided by this module.");
\r
79 private void sendNotification(CompositeNode notification) {
\r
80 QName type = notification.getNodeType();
\r
81 Collection<NotificationListener> toNotify = listeners.get(type);
\r
82 log.info("Publishing notification " + type);
\r
84 if (toNotify == null) {
\r
85 // No listeners were registered - returns.
\r
89 for (NotificationListener listener : toNotify) {
\r
91 // FIXME: ensure that notification is immutable
\r
92 listener.onNotification(notification);
\r
93 } catch (Exception e) {
\r
94 log.error("Uncaught exception in NotificationListener", e);
\r
100 private NotificationService newNotificationConsumerService(
\r
101 ConsumerSession session) {
\r
102 return new NotificationConsumerSessionImpl();
\r
105 private NotificationProviderService newNotificationProviderService(
\r
106 ConsumerSession session) {
\r
107 return new NotificationProviderSessionImpl();
\r
110 private class NotificationConsumerSessionImpl extends BrokerServiceImpl
\r
111 implements NotificationService {
\r
113 private Multimap<QName, NotificationListener> consumerListeners = HashMultimap
\r
115 private boolean closed = false;
\r
118 public void addNotificationListener(QName notification,
\r
119 NotificationListener listener) {
\r
120 checkSessionState();
\r
121 if (notification == null) {
\r
122 throw new IllegalArgumentException(
\r
123 "Notification type must not be null.");
\r
125 if (listener == null) {
\r
126 throw new IllegalArgumentException("Listener must not be null.");
\r
129 consumerListeners.put(notification, listener);
\r
130 listeners.put(notification, listener);
\r
131 log.info("Registered listener for notification: " + notification);
\r
135 public void removeNotificationListener(QName notification,
\r
136 NotificationListener listener) {
\r
137 checkSessionState();
\r
138 if (notification == null) {
\r
139 throw new IllegalArgumentException(
\r
140 "Notification type must not be null.");
\r
142 if (listener == null) {
\r
143 throw new IllegalArgumentException("Listener must not be null.");
\r
145 consumerListeners.remove(notification, listener);
\r
146 listeners.remove(notification, listener);
\r
150 public void closeSession() {
\r
152 Map<QName, Collection<NotificationListener>> toRemove = consumerListeners
\r
154 for (Entry<QName, Collection<NotificationListener>> entry : toRemove
\r
156 listeners.remove(entry.getKey(), entry.getValue());
\r
160 protected void checkSessionState() {
\r
162 throw new IllegalStateException("Session is closed");
\r
166 private class NotificationProviderSessionImpl extends
\r
167 NotificationConsumerSessionImpl implements
\r
168 NotificationProviderService {
\r
171 public void sendNotification(CompositeNode notification) {
\r
172 checkSessionState();
\r
173 if (notification == null)
\r
174 throw new IllegalArgumentException(
\r
175 "Notification must not be null.");
\r
176 NotificationModule.this.sendNotification(notification);
\r
181 public Set<Class<? extends ProviderFunctionality>> getSupportedProviderFunctionality() {
\r
182 return Collections.emptySet();
\r