From: Tony Tkacik Date: Mon, 25 Mar 2013 15:50:06 +0000 (+0100) Subject: Updated registration of Binding Aware notification listeners. X-Git-Tag: releasepom-0.1.0~628^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=b9fd4c64c8c03aacdd26cd996846b98bc8c9fa8b Updated registration of Binding Aware notification listeners. Signed-off-by: Tony Tkacik --- diff --git a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/BindingBrokerImpl.java b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/BindingBrokerImpl.java index 4cc23b4bbf..2209f84cac 100644 --- a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/BindingBrokerImpl.java +++ b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/BindingBrokerImpl.java @@ -1,3 +1,10 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.controller.sal.binding.impl; import java.util.HashMap; diff --git a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/BrokerUtils.java b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/BrokerUtils.java new file mode 100644 index 0000000000..e174559c88 --- /dev/null +++ b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/BrokerUtils.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.sal.binding.impl; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class BrokerUtils { + private BrokerUtils() { + + } + + + public static void addToMap(Map, List> map, Class key, V value) { + List list = map.get(key); + if (list == null) { + list = new ArrayList(); + map.put(key, list); + } + list.add(value); + } + + public static void removeFromMap(Map, List> map, Class key, + V value) { + List list = map.get(key); + if (list == null) { + return; + } + list.remove(value); + if (list.isEmpty()) { + map.remove(key); + } + } +} diff --git a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationInvoker.java b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationInvoker.java index 810a0cbd30..640089affb 100644 --- a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationInvoker.java +++ b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationInvoker.java @@ -1,3 +1,10 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.controller.sal.binding.impl; import org.opendaylight.controller.sal.binding.spi.MappingProvider.MappingExtension; diff --git a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationModule.java b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationModule.java index 189c58555f..f029a2f9f4 100644 --- a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationModule.java +++ b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationModule.java @@ -1,6 +1,12 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.controller.sal.binding.impl; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -17,7 +23,7 @@ import org.opendaylight.controller.sal.binding.spi.SALBindingModule; import org.opendaylight.controller.sal.binding.spi.Mapper; import org.opendaylight.controller.sal.binding.spi.MappingProvider; import org.opendaylight.controller.sal.binding.spi.MappingProvider.MappingExtensionFactory; -import org.opendaylight.controller.sal.core.api.Provider; + import org.opendaylight.controller.sal.core.api.Broker.ProviderSession; import org.opendaylight.controller.yang.binding.DataObject; import org.opendaylight.controller.yang.binding.Notification; @@ -35,7 +41,10 @@ public class NotificationModule implements SALBindingModule { private MappingProvider mappingProvider; private Map, List> listeners = new HashMap, List>(); - private static Logger log = LoggerFactory.getLogger(NotificationModule.class); + private Set biNotifications = new HashSet(); + private static final Logger log = LoggerFactory + .getLogger(NotificationModule.class); + private final BindingIndependentListener biListener = new BindingIndependentListener(); @Override public Set> getProvidedServices() { @@ -150,6 +159,31 @@ public class NotificationModule implements SALBindingModule { biNotifyService.sendNotification(domNotification); } + private void addBAListener(Class notificationType, + NotificationListener listener) { + + BrokerUtils.addToMap(listeners, notificationType, listener); + Mapper mapper = mappingProvider + .getMapper(notificationType); + QName biType = mapper.getQName(); + if (false == biNotifications.contains(biType)) { + // The listener is not registered for binding independent + // notification + biNotifications.add(biType); + + if (biNotifyService != null) { + biNotifyService.addNotificationListener(biType, biListener); + } + } + + } + + private void removeBAListener( + Class notificationType, + NotificationListener listener) { + BrokerUtils.removeFromMap(listeners, notificationType, listener); + } + private class NotificationSession implements NotificationService { private final ConsumerSession session; @@ -163,16 +197,20 @@ public class NotificationModule implements SALBindingModule { public void addNotificationListener( Class notificationType, NotificationListener listener) { - // TODO Implement this method - throw new UnsupportedOperationException("Not implemented"); + + NotificationModule.this.addBAListener(notificationType, listener); + BrokerUtils.addToMap(sessionListeners, notificationType, listener); + } @Override public void removeNotificationListener( Class notificationType, NotificationListener listener) { - // TODO Implement this method - throw new UnsupportedOperationException("Not implemented"); + BrokerUtils.removeFromMap(sessionListeners, notificationType, + listener); + NotificationModule.this + .removeBAListener(notificationType, listener); } } @@ -190,41 +228,47 @@ public class NotificationModule implements SALBindingModule { } } - - private class BindingIndependentListener implements org.opendaylight.controller.sal.core.api.notify.NotificationListener { + + private class BindingIndependentListener + implements + org.opendaylight.controller.sal.core.api.notify.NotificationListener { @Override public Set getSupportedNotifications() { - return Collections.emptySet(); + return biNotifications; } @Override public void onNotification(CompositeNode notification) { - NotificationModule.this.onBindingIndependentNotification(notification); + NotificationModule.this + .onBindingIndependentNotification(notification); } - + } private void onBindingIndependentNotification(CompositeNode biNotification) { QName biType = biNotification.getNodeType(); - + Mapper mapper = mappingProvider.getMapper(biType); - if(mapper == null) { + if (mapper == null) { log.info("Received notification does not have a binding defined."); return; } Class type = mapper.getDataObjectClass(); - + // We check if the received QName / type is really Notification - if(Notification.class.isAssignableFrom(type)) { - Notification notification = (Notification) mapper.objectFromDom(biNotification); + if (Notification.class.isAssignableFrom(type)) { + Notification notification = (Notification) mapper + .objectFromDom(biNotification); notifyBindingAware(notification); } else { - // The generated type for this QName does not inherits from notification - // something went wrong - generated APIs and/or provider sending notification + // The generated type for this QName does not inherits from + // notification something went wrong - generated APIs and/or + // provider sending notification // which was incorectly described in the YANG schema. - log.error("Received notification "+ biType +" is not binded as notification"); + log.error("Received notification " + biType + + " is not binded as notification"); } - + } } diff --git a/opendaylight/sal/yang-prototype/sal/sal-core-api/src/main/java/org/opendaylight/controller/sal/core/api/notify/NotificationProviderService.java b/opendaylight/sal/yang-prototype/sal/sal-core-api/src/main/java/org/opendaylight/controller/sal/core/api/notify/NotificationProviderService.java index 9367645d4c..6f7e740cdf 100644 --- a/opendaylight/sal/yang-prototype/sal/sal-core-api/src/main/java/org/opendaylight/controller/sal/core/api/notify/NotificationProviderService.java +++ b/opendaylight/sal/yang-prototype/sal/sal-core-api/src/main/java/org/opendaylight/controller/sal/core/api/notify/NotificationProviderService.java @@ -8,7 +8,6 @@ package org.opendaylight.controller.sal.core.api.notify; import org.opendaylight.controller.sal.core.api.Broker; -import org.opendaylight.controller.sal.core.api.BrokerService; import org.opendaylight.controller.sal.core.api.Provider; import org.opendaylight.controller.yang.data.api.CompositeNode; @@ -30,7 +29,7 @@ import org.opendaylight.controller.yang.data.api.CompositeNode; * * */ -public interface NotificationProviderService extends BrokerService { +public interface NotificationProviderService extends NotificationService { /** * Publishes a notification.