AuthenticationKeyDataListener uses DataTreeChangeListener. 25/37725/2
authorJozef Gloncak <jgloncak@cisco.com>
Mon, 18 Apr 2016 06:51:33 +0000 (08:51 +0200)
committerJozef Gloncak <jgloncak@cisco.com>
Tue, 19 Apr 2016 07:32:31 +0000 (09:32 +0200)
Rewritting of class to use DataTreeChangeListener.

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

index 1e1f90ab09fb0a3a983d5bb79eab7f5b209b4f94..0025594046b4abde8a3863ae9c9c2ae70face5f8 100644 (file)
@@ -7,19 +7,18 @@
  */
 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.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
 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;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
 
 /**
  * DataListener for all AuthenticationKey modification events.
@@ -27,7 +26,7 @@ import org.slf4j.LoggerFactory;
  * @author Lorand Jakab
  *
  */
-public class AuthenticationKeyDataListener extends AbstractDataListener {
+public class AuthenticationKeyDataListener extends NewAbstractDataListener<AuthenticationKey> {
     private static final Logger LOG = LoggerFactory.getLogger(AuthenticationKeyDataListener.class);
     private IMappingSystem mapSystem;
 
@@ -41,49 +40,34 @@ public class AuthenticationKeyDataListener extends AbstractDataListener {
     }
 
     @Override
-    public void onDataChanged(
-            AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
-
-        // Process newly created authentication keys
-        Map<InstanceIdentifier<?>, DataObject> createdData = change.getCreatedData();
-        for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : createdData.entrySet()) {
-            if (entry.getValue() instanceof AuthenticationKey) {
-                AuthenticationKey authkey = (AuthenticationKey)entry.getValue();
-
-                LOG.trace("Received created data");
-                LOG.trace("Key: {}", entry.getKey());
-                LOG.trace("Value: {}", authkey);
-
-                mapSystem.addAuthenticationKey(authkey.getEid(), authkey.getMappingAuthkey());
-            }
-        }
+    public void onDataTreeChanged(Collection<DataTreeModification<AuthenticationKey>> changes) {
+        for (DataTreeModification<AuthenticationKey> change : changes) {
+            final DataObjectModification<AuthenticationKey> mod = change.getRootNode();
 
-        // Process updated authentication keys
-        Map<InstanceIdentifier<?>, DataObject> updatedData = change.getUpdatedData();
-        for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : updatedData.entrySet()) {
-            if (entry.getValue() instanceof AuthenticationKey) {
-                AuthenticationKey authkey = (AuthenticationKey)entry.getValue();
+            if (ModificationType.DELETE == mod.getModificationType()) {
+                final AuthenticationKey authKey = mod.getDataBefore();
 
-                LOG.trace("Received changed data");
-                LOG.trace("Key: {}", entry.getKey());
-                LOG.trace("Value: {}", authkey);
-
-                mapSystem.addAuthenticationKey(authkey.getEid(), authkey.getMappingAuthkey());
-            }
-        }
+                LOG.trace("Received deleted data");
+                LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
+                LOG.trace("Value: {}", authKey);
 
-        // Process deleted authentication keys
-        Set<InstanceIdentifier<?>> removedData = change.getRemovedPaths();
-        for (InstanceIdentifier<?> entry : removedData) {
-            DataObject dataObject = change.getOriginalData().get(entry);
-            if (dataObject instanceof AuthenticationKey) {
-                AuthenticationKey authkey = (AuthenticationKey)dataObject;
+                mapSystem.removeAuthenticationKey(authKey.getEid());
+            } else if (ModificationType.WRITE == mod.getModificationType() || ModificationType.SUBTREE_MODIFIED == mod
+                    .getModificationType()) {
+                if (ModificationType.WRITE == mod.getModificationType()) {
+                    LOG.trace("Received created data");
+                } else {
+                    LOG.trace("Received updated data");
+                }
+                // Process newly created or updated authentication keys
+                final AuthenticationKey authKey = mod.getDataAfter();
 
-                LOG.trace("Received deleted data");
-                LOG.trace("Key: {}", entry);
-                LOG.trace("Value: {}", authkey);
+                LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
+                LOG.trace("Value: {}", authKey);
 
-                mapSystem.removeAuthenticationKey(authkey.getEid());
+                mapSystem.addAuthenticationKey(authKey.getEid(), authKey.getMappingAuthkey());
+            } else {
+                LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
             }
         }
     }
index 09304df3d472f1039307fab1a9853c59544eaee4..8a84ae0749567d69c397502bfebc295abe04557b 100644 (file)
@@ -27,6 +27,7 @@ public abstract class NewAbstractDataListener<T extends DataObject> implements D
     public void registerDataChangeListener() {
         final DataTreeIdentifier<T> dataTreeIdentifier = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
                 path);
+
         registration = broker.registerDataTreeChangeListener(dataTreeIdentifier, this);
     }