Fix findbugs violations in netconf
[netconf.git] / netconf / messagebus-netconf / src / main / java / org / opendaylight / netconf / messagebus / eventsources / netconf / NetconfEventSourceManager.java
index ae3ef9ea4d0db6eb4520ddbbfe61303433e5edc8..6360136b9fd3f401cf1b6c2e2f0b558ebb8f705a 100644 (file)
@@ -9,16 +9,14 @@
 package org.opendaylight.netconf.messagebus.eventsources.netconf;
 
 import com.google.common.base.Preconditions;
-import java.util.HashMap;
-import java.util.List;
+import java.util.Collection;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import org.opendaylight.controller.config.yang.messagebus.netconf.NamespaceToStream;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.MountPointService;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
@@ -31,102 +29,78 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public final class NetconfEventSourceManager implements DataChangeListener, AutoCloseable {
+/**
+ * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
+ * updating and removing registrations of event sources.
+ */
+public final class NetconfEventSourceManager implements DataTreeChangeListener<Node>, AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSourceManager.class);
     private static final TopologyKey NETCONF_TOPOLOGY_KEY = new TopologyKey(
-        new TopologyId(TopologyNetconf.QNAME.getLocalName()));
+            new TopologyId(TopologyNetconf.QNAME.getLocalName()));
     private static final InstanceIdentifier<Node> NETCONF_DEVICE_PATH = InstanceIdentifier.create(NetworkTopology.class)
-        .child(Topology.class, NETCONF_TOPOLOGY_KEY).child(Node.class);
+            .child(Topology.class, NETCONF_TOPOLOGY_KEY).child(Node.class);
 
-    private final Map<String, String> streamMap;
-    private final ConcurrentHashMap<InstanceIdentifier<?>, NetconfEventSourceRegistration> registrationMap = new ConcurrentHashMap<>();
+    private Map<String, String> streamMap;
+    private final ConcurrentHashMap<InstanceIdentifier<?>, NetconfEventSourceRegistration> registrationMap =
+            new ConcurrentHashMap<>();
     private final DOMNotificationPublishService publishService;
     private final DOMMountPointService domMounts;
-    private final MountPointService mountPointService;
-    private ListenerRegistration<DataChangeListener> listenerRegistration;
+    private ListenerRegistration<NetconfEventSourceManager> listenerRegistration;
     private final EventSourceRegistry eventSourceRegistry;
+    private final DataBroker dataBroker;
 
-    public static NetconfEventSourceManager create(final DataBroker dataBroker,
-        final DOMNotificationPublishService domPublish, final DOMMountPointService domMount,
-        final MountPointService bindingMount, final EventSourceRegistry eventSourceRegistry,
-        final List<NamespaceToStream> namespaceMapping) {
-
-        final NetconfEventSourceManager eventSourceManager = new NetconfEventSourceManager(domPublish, domMount,
-            bindingMount, eventSourceRegistry, namespaceMapping);
-
-        eventSourceManager.initialize(dataBroker);
-
-        return eventSourceManager;
-
-    }
-
-    private NetconfEventSourceManager(final DOMNotificationPublishService domPublish,
-        final DOMMountPointService domMount, final MountPointService bindingMount,
-        final EventSourceRegistry eventSourceRegistry, final List<NamespaceToStream> namespaceMapping) {
-
+    public NetconfEventSourceManager(final DataBroker dataBroker,
+                                     final DOMNotificationPublishService domPublish,
+                                     final DOMMountPointService domMount,
+                                     final EventSourceRegistry eventSourceRegistry) {
+        Preconditions.checkNotNull(dataBroker);
         Preconditions.checkNotNull(domPublish);
         Preconditions.checkNotNull(domMount);
-        Preconditions.checkNotNull(bindingMount);
         Preconditions.checkNotNull(eventSourceRegistry);
-        Preconditions.checkNotNull(namespaceMapping);
-        this.streamMap = namespaceToStreamMapping(namespaceMapping);
+        this.dataBroker = dataBroker;
         this.domMounts = domMount;
-        this.mountPointService = bindingMount;
         this.publishService = domPublish;
         this.eventSourceRegistry = eventSourceRegistry;
     }
 
-    private void initialize(final DataBroker dataBroker) {
+    /**
+     * Invoked by blueprint.
+     */
+    public void initialize() {
         Preconditions.checkNotNull(dataBroker);
-        listenerRegistration = dataBroker
-            .registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH, this,
-                DataChangeScope.SUBTREE);
+        listenerRegistration = dataBroker.registerDataTreeChangeListener(new DataTreeIdentifier<>(
+                LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH), this);
         LOG.info("NetconfEventSourceManager initialized.");
     }
 
-    private Map<String, String> namespaceToStreamMapping(final List<NamespaceToStream> namespaceMapping) {
-        final Map<String, String> streamMap = new HashMap<>(namespaceMapping.size());
-
-        for (final NamespaceToStream nToS : namespaceMapping) {
-            streamMap.put(nToS.getUrnPrefix(), nToS.getStreamName());
-        }
-
-        return streamMap;
-    }
-
-    @Override public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
-
-        LOG.debug("[DataChangeEvent<InstanceIdentifier<?>, DataObject>: {}]", event);
-        for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getCreatedData().entrySet()) {
-            if (changeEntry.getValue() instanceof Node) {
-                nodeCreated(changeEntry.getKey(), (Node) changeEntry.getValue());
+    @Override
+    public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
+        for (DataTreeModification<Node> change: changes) {
+            LOG.debug("DataTreeModification: {}", change);
+            final DataObjectModification<Node> rootNode = change.getRootNode();
+            final InstanceIdentifier<Node> identifier = change.getRootPath().getRootIdentifier();
+            switch (rootNode.getModificationType()) {
+                case WRITE:
+                case SUBTREE_MODIFIED:
+                    nodeCreated(identifier, rootNode.getDataAfter());
+                    break;
+                case DELETE:
+                    nodeRemoved(identifier);
+                    break;
+                default:
+                    break;
             }
         }
-
-        for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
-            if (changeEntry.getValue() instanceof Node) {
-                nodeUpdated(changeEntry.getKey(), (Node) changeEntry.getValue());
-            }
-        }
-
-        for (InstanceIdentifier<?> removePath : event.getRemovedPaths()) {
-            DataObject removeObject = event.getOriginalData().get(removePath);
-            if (removeObject instanceof Node) {
-                nodeRemoved(removePath);
-            }
-        }
-
     }
 
     private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
         Preconditions.checkNotNull(key);
-        if (validateNode(node) == false) {
+        if (!validateNode(node)) {
             LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key.toString());
             return;
         }
@@ -140,22 +114,6 @@ public final class NetconfEventSourceManager implements DataChangeListener, Auto
         }
     }
 
-    private void nodeUpdated(final InstanceIdentifier<?> key, final Node node) {
-        Preconditions.checkNotNull(key);
-        if (validateNode(node) == false) {
-            LOG.warn("NodeUpdated event : Node [{}] is null or not valid.", key.toString());
-            return;
-        }
-
-        LOG.info("Netconf event source [{}] is updating...", key.toString());
-        NetconfEventSourceRegistration nesr = registrationMap.get(key);
-        if (nesr != null) {
-            nesr.updateStatus();
-        } else {
-            nodeCreated(key, node);
-        }
-    }
-
     private void nodeRemoved(final InstanceIdentifier<?> key) {
         Preconditions.checkNotNull(key);
         LOG.info("Netconf event source [{}] is removing...", key.toString());
@@ -188,20 +146,25 @@ public final class NetconfEventSourceManager implements DataChangeListener, Auto
         return eventSourceRegistry;
     }
 
-    MountPointService getMountPointService() {
-        return mountPointService;
+    /**
+     * Invoked by blueprint.
+     *
+     * @param streamMap Stream map
+     */
+    public void setStreamMap(Map<String, String> streamMap) {
+        this.streamMap = streamMap;
     }
 
     private boolean isNetconfNode(final Node node) {
         return node.getAugmentation(NetconfNode.class) != null;
     }
 
-    @Override public void close() {
+    @Override
+    public void close() {
         listenerRegistration.close();
         for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
             reg.close();
         }
         registrationMap.clear();
     }
-
-}
\ No newline at end of file
+}