MappingDataListener uses DataTreeChangeListener. 32/37732/2
authorJozef Gloncak <jgloncak@cisco.com>
Mon, 18 Apr 2016 12:17:14 +0000 (14:17 +0200)
committerJozef Gloncak <jgloncak@cisco.com>
Tue, 19 Apr 2016 06:33:48 +0000 (08:33 +0200)
Rewritting of class to use DataTreeChangeListener.

Change-Id: Id8b3a9ea6202756674c721ca7edc485ebc10faba
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/mdsal/MappingDataListener.java

index ee1500c58c7b895514e239655846f536c6e93f1f..976713dfd8079ea98f2aa44d4733a6b3b6c54d7b 100644 (file)
@@ -7,12 +7,12 @@
  */
 package org.opendaylight.lispflowmapping.implementation.mdsal;
 
-import java.util.Map;
-import java.util.Set;
-
+import java.util.Collection;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
 import org.opendaylight.lispflowmapping.implementation.util.MSNotificationInputUtil;
 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChange;
@@ -20,7 +20,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev15090
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier;
-import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -32,7 +31,7 @@ import org.slf4j.LoggerFactory;
  * @author Florin Coras
  *
  */
-public class MappingDataListener extends AbstractDataListener {
+public class MappingDataListener extends NewAbstractDataListener<Mapping> {
     private static final Logger LOG = LoggerFactory.getLogger(MappingDataListener.class);
     private IMappingSystem mapSystem;
     private NotificationPublishService notificationPublishService;
@@ -51,97 +50,74 @@ public class MappingDataListener extends AbstractDataListener {
         this.notificationPublishService = nps;
     }
 
+    void setMappingSystem(IMappingSystem msmr) {
+        this.mapSystem = msmr;
+    }
+
     @Override
-    public void onDataChanged(
-            AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
+    public void onDataTreeChanged(Collection<DataTreeModification<Mapping>> changes) {
+        for (DataTreeModification<Mapping> change : changes) {
+            final DataObjectModification<Mapping> mod = change.getRootNode();
+
+            if (ModificationType.DELETE == mod.getModificationType()) {
+                // Process deleted mappings
 
-     // Process newly created mappings
-        Map<InstanceIdentifier<?>, DataObject> createdData = change.getCreatedData();
-        for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : createdData.entrySet()) {
-            if (entry.getValue() instanceof Mapping) {
-                Mapping mapping = (Mapping)entry.getValue();
+                final Mapping mapping = mod.getDataBefore();
 
                 // Only treat mapping changes caused by Northbound, since Southbound changes are already handled
-                // before being persisted. XXX separate NB and SB to avoid ignoring SB notifications
+                // before being persisted.
                 if (mapping.getOrigin() == MappingOrigin.Southbound) {
                     continue;
                 }
 
-                LOG.trace("Received created data");
-                LOG.trace("Key: {}", entry.getKey());
+                LOG.trace("Received deleted data");
+                LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
                 LOG.trace("Value: {}", mapping);
 
-                mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
-                        mapping.getMappingRecord());
-
+                mapSystem.removeMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid());
                 try {
-                    // The notifications are used for sending SMR.
-                    notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping,
-                            MappingChange.Created));
+                    notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(
+                            mapping, MappingChange.Removed));
                 } catch (InterruptedException e) {
                     LOG.warn("Notification publication interrupted!");
                 }
-            }
-        }
 
-        // Process updated mappings
-        Map<InstanceIdentifier<?>, DataObject> updatedData = change.getUpdatedData();
-        for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : updatedData.entrySet()) {
-            if (entry.getValue() instanceof Mapping) {
-                Mapping mapping = (Mapping)entry.getValue();
+            } else if (ModificationType.SUBTREE_MODIFIED == mod.getModificationType() || ModificationType.WRITE == mod
+                    .getModificationType()) {
+                final Mapping mapping = mod.getDataAfter();
 
                 // Only treat mapping changes caused by Northbound, since Southbound changes are already handled
-                // before being persisted.
+                // before being persisted. XXX separate NB and SB to avoid ignoring SB notifications
                 if (mapping.getOrigin() == MappingOrigin.Southbound) {
                     continue;
                 }
 
-                LOG.trace("Received changed data");
-                LOG.trace("Key: {}", entry.getKey());
-                LOG.trace("Value: {}", entry.getValue());
+                MappingChange mappingChange;
+
+                if (ModificationType.SUBTREE_MODIFIED == mod.getModificationType()) {
+                    LOG.trace("Received update data");
+                    mappingChange = MappingChange.Updated;
+                } else {
+                    LOG.trace("Received write data");
+                    mappingChange = MappingChange.Created;
+                }
+                LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
+                LOG.trace("Value: {}", mapping);
 
                 mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
                         mapping.getMappingRecord());
 
-                // The notifications are used for sending SMR.
                 try {
+                    // The notifications are used for sending SMR.
                     notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping,
-                            MappingChange.Updated));
+                            mappingChange));
                 } catch (InterruptedException e) {
                     LOG.warn("Notification publication interrupted!");
                 }
-            }
-        }
-
-        // Process deleted mappings
-        Set<InstanceIdentifier<?>> removedData = change.getRemovedPaths();
-        for (InstanceIdentifier<?> entry : removedData) {
-            DataObject dataObject = change.getOriginalData().get(entry);
-            if (dataObject instanceof Mapping) {
-                Mapping mapping = (Mapping)dataObject;
-
-                // Only treat mapping changes caused by Northbound, since Southbound changes are already handled
-                // before being persisted.
-                if (mapping.getOrigin() == MappingOrigin.Southbound) {
-                    continue;
-                }
-
-                LOG.trace("Received deleted data");
-                LOG.trace("Key: {}", entry);
-                LOG.trace("Value: {}", dataObject);
 
-                mapSystem.removeMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid());
-                try {
-                    notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(
-                            mapping, MappingChange.Removed));
-                } catch (InterruptedException e) {
-                    LOG.warn("Notification publication interrupted!");
-                }
+            } else {
+                LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
             }
         }
     }
-
-    void setMappingSystem(IMappingSystem msmr) {
-        this.mapSystem = msmr;
-    }
 }