Bump versions to 2.0.0-SNAPSHOT
[netconf.git] / netconf / messagebus-netconf / src / main / java / org / opendaylight / netconf / messagebus / eventsources / netconf / NetconfEventSourceManager.java
index 516c595ef7e98a8e3f17d97100e6cb5088180d0d..e9fcfc098036d9f06a79fab063159f4b014c68cc 100644 (file)
@@ -5,20 +5,24 @@
  * 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.netconf.messagebus.eventsources.netconf;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
+import java.util.Collection;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
-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.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
-import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.DOMMountPointService;
+import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
@@ -27,7 +31,6 @@ 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;
@@ -36,7 +39,8 @@ import org.slf4j.LoggerFactory;
  * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
  * updating and removing registrations of event sources.
  */
-public final class NetconfEventSourceManager implements DataChangeListener, AutoCloseable {
+@Deprecated(forRemoval = true)
+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(
@@ -49,68 +53,60 @@ public final class NetconfEventSourceManager implements DataChangeListener, Auto
             new ConcurrentHashMap<>();
     private final DOMNotificationPublishService publishService;
     private final DOMMountPointService domMounts;
-    private ListenerRegistration<DataChangeListener> listenerRegistration;
+    private ListenerRegistration<NetconfEventSourceManager> listenerRegistration;
     private final EventSourceRegistry eventSourceRegistry;
     private final DataBroker dataBroker;
+    private final BindingNormalizedNodeSerializer serializer;
 
     public NetconfEventSourceManager(final DataBroker dataBroker,
+                                     final BindingNormalizedNodeSerializer serializer,
                                      final DOMNotificationPublishService domPublish,
                                      final DOMMountPointService domMount,
                                      final EventSourceRegistry eventSourceRegistry) {
-        Preconditions.checkNotNull(dataBroker);
-        Preconditions.checkNotNull(domPublish);
-        Preconditions.checkNotNull(domMount);
-        Preconditions.checkNotNull(eventSourceRegistry);
-        this.dataBroker = dataBroker;
-        this.domMounts = domMount;
-        this.publishService = domPublish;
-        this.eventSourceRegistry = eventSourceRegistry;
+        this.dataBroker = requireNonNull(dataBroker);
+        this.serializer = requireNonNull(serializer);
+        this.domMounts = requireNonNull(domMount);
+        this.publishService = requireNonNull(domPublish);
+        this.eventSourceRegistry = requireNonNull(eventSourceRegistry);
     }
 
     /**
      * Invoked by blueprint.
      */
     public void initialize() {
-        Preconditions.checkNotNull(dataBroker);
-        listenerRegistration = dataBroker
-                .registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH, this,
-                        DataChangeScope.SUBTREE);
+        listenerRegistration = verifyNotNull(dataBroker).registerDataTreeChangeListener(DataTreeIdentifier.create(
+                LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH), this);
         LOG.info("NetconfEventSourceManager initialized.");
     }
 
     @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());
-            }
-        }
-
-        for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
-            if (changeEntry.getValue() instanceof Node) {
-                nodeUpdated(changeEntry.getKey(), (Node) changeEntry.getValue());
+    public void onDataTreeChanged(final 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 (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)) {
-            LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key.toString());
+            LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key);
             return;
         }
-        LOG.info("Netconf event source [{}] is creating...", key.toString());
-        NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
+        LOG.info("Netconf event source [{}] is creating...", key);
+        NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(serializer, requireNonNull(key),
+            node, this);
         if (nesr != null) {
             NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
             if (nesrOld != null) {
@@ -119,36 +115,16 @@ public final class NetconfEventSourceManager implements DataChangeListener, Auto
         }
     }
 
-    private void nodeUpdated(final InstanceIdentifier<?> key, final Node node) {
-        Preconditions.checkNotNull(key);
-        if (!validateNode(node)) {
-            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());
-        NetconfEventSourceRegistration nesr = registrationMap.remove(key);
+        LOG.info("Netconf event source [{}] is removing...", key);
+        NetconfEventSourceRegistration nesr = registrationMap.remove(requireNonNull(key));
         if (nesr != null) {
             nesr.close();
         }
     }
 
-    private boolean validateNode(final Node node) {
-        if (node == null) {
-            return false;
-        }
-        return isNetconfNode(node);
+    private static boolean validateNode(final Node node) {
+        return node == null ? false : isNetconfNode(node);
     }
 
     Map<String, String> getStreamMap() {
@@ -172,12 +148,12 @@ public final class NetconfEventSourceManager implements DataChangeListener, Auto
      *
      * @param streamMap Stream map
      */
-    public void setStreamMap(Map<String, String> streamMap) {
+    public void setStreamMap(final Map<String, String> streamMap) {
         this.streamMap = streamMap;
     }
 
-    private boolean isNetconfNode(final Node node) {
-        return node.getAugmentation(NetconfNode.class) != null;
+    private static boolean isNetconfNode(final Node node) {
+        return node.augmentation(NetconfNode.class) != null;
     }
 
     @Override
@@ -188,5 +164,4 @@ public final class NetconfEventSourceManager implements DataChangeListener, Auto
         }
         registrationMap.clear();
     }
-
-}
\ No newline at end of file
+}