Auto-convert String IP EIDs in AuthenticationKey 73/39773/1
authorLorand Jakab <lojakab@cisco.com>
Thu, 2 Jun 2016 13:47:17 +0000 (16:47 +0300)
committerLorand Jakab <lojakab@cisco.com>
Thu, 2 Jun 2016 14:35:16 +0000 (17:35 +0300)
When performing operations with AuthenticationKey objects from the
Northbound, auto-convert the IP addresses and prefixes inside the object
to binary, if sent in String form, when storing to the InMemoryDb. Keep
the original objects in MD-SAL.

Change-Id: I0afb41d765f3a782d160ef1a01d01a4c101ea946
Signed-off-by: Lorand Jakab <lojakab@cisco.com>
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/mdsal/AuthenticationKeyDataListener.java
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/util/LispAddressUtil.java
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/AuthenticationKeyDataListener.java

index 5ff9336a2eedca657fcaa6c136a7dc25eab13f82..cdefd3d6fe8debaae0837c0908b02d3d9c9c71bb 100644 (file)
@@ -8,12 +8,20 @@
 package org.opendaylight.lispflowmapping.implementation.mdsal;
 
 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.DataTreeModification;
 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
 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.db.instance.AuthenticationKeyBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
@@ -51,7 +59,9 @@ public class AuthenticationKeyDataListener extends AbstractDataListener<Authenti
                 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
                 LOG.trace("Value: {}", authKey);
 
-                mapSystem.removeAuthenticationKey(authKey.getEid());
+                final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
+
+                mapSystem.removeAuthenticationKey(convertedAuthKey.getEid());
             } else if (ModificationType.WRITE == mod.getModificationType() || ModificationType.SUBTREE_MODIFIED == mod
                     .getModificationType()) {
                 if (ModificationType.WRITE == mod.getModificationType()) {
@@ -65,13 +75,26 @@ public class AuthenticationKeyDataListener extends AbstractDataListener<Authenti
                 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
                 LOG.trace("Value: {}", authKey);
 
-                mapSystem.addAuthenticationKey(authKey.getEid(), authKey.getMappingAuthkey());
+                final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
+
+                mapSystem.addAuthenticationKey(convertedAuthKey.getEid(), convertedAuthKey.getMappingAuthkey());
             } else {
                 LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
             }
         }
     }
 
+    private static AuthenticationKey convertToBinaryIfNecessary(AuthenticationKey authKey) {
+        Eid originalEid = authKey.getEid();
+        if (originalEid.getAddress() instanceof Ipv4Prefix || originalEid.getAddress() instanceof Ipv6Prefix ||
+                originalEid.getAddress() instanceof Ipv4 || originalEid.getAddress() instanceof Ipv6) {
+            AuthenticationKeyBuilder akb = new AuthenticationKeyBuilder(authKey);
+            akb.setEid(LispAddressUtil.convertToBinary(originalEid));
+            return akb.build();
+        }
+        return authKey;
+    }
+
     void setMappingSystem(IMappingSystem msmr) {
         this.mapSystem = msmr;
     }
index fce1cb94bfee2a1845e2017a1151cb7e3b07112d..0398ce4db418064656571e7842d1a97c7478855a 100644 (file)
@@ -694,6 +694,57 @@ public final class LispAddressUtil {
         }
     }
 
+    private static Ipv4PrefixBinary convertToBinary(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+            .lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix prefix) {
+        Ipv4PrefixBinaryBuilder pb = new Ipv4PrefixBinaryBuilder();
+        byte[] address = InetAddresses.forString(MaskUtil.getAddressStringForIpv4Prefix(prefix)).getAddress();
+        pb.setIpv4AddressBinary(new Ipv4AddressBinary(address));
+        pb.setIpv4MaskLength(MaskUtil.getMaskForAddress(prefix));
+        return pb.build();
+    }
+
+    private static Ipv6PrefixBinary convertToBinary(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+            .lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix prefix) {
+        Ipv6PrefixBinaryBuilder pb = new Ipv6PrefixBinaryBuilder();
+        byte[] address = InetAddresses.forString(MaskUtil.getAddressStringForIpv6Prefix(prefix)).getAddress();
+        pb.setIpv6AddressBinary(new Ipv6AddressBinary(address));
+        pb.setIpv6MaskLength(MaskUtil.getMaskForAddress(prefix));
+        return pb.build();
+    }
+
+    private static Ipv4Binary convertToBinary(Ipv4 address) {
+        byte[] addr = InetAddresses.forString(address.getIpv4().getValue()).getAddress();
+        return new Ipv4BinaryBuilder().setIpv4Binary(new Ipv4AddressBinary(addr)).build();
+    }
+
+    private static Ipv6Binary convertToBinary(Ipv6 address) {
+        byte[] addr = InetAddresses.forString(address.getIpv6().getValue()).getAddress();
+        return new Ipv6BinaryBuilder().setIpv6Binary(new Ipv6AddressBinary(addr)).build();
+    }
+
+    public static Eid convertToBinary(Eid eid) {
+        EidBuilder eb = new EidBuilder(eid);
+        Address addr = eid.getAddress();
+        Address convAddr = null;
+        if (addr instanceof org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+                .lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix) {
+            convAddr = convertToBinary((org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+                    .lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix) addr);
+        } else if (addr instanceof org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+                .lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix) {
+            convAddr = convertToBinary((org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+                    .lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix) addr);
+        } else if (addr instanceof Ipv4) {
+            convAddr = convertToBinary((Ipv4) addr);
+        } else if (addr instanceof Ipv6) {
+            convAddr = convertToBinary((Ipv6) addr);
+        } else {
+            return eid;
+        }
+        eb.setAddress(convAddr);
+        return eb.build();
+    }
+
     public static int compareIpAddressByteArrays(byte[] a, byte[] b) {
         int i;
         if (a.length < b.length) {
index 79cc6b29d851c85dbf6e0b889be8836b3ae286d7..5cab1364557d10ccaa2ce7d644f683a47b042614 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.lispflowmapping.southbound.lisp;
 
 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;
@@ -15,9 +16,16 @@ 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.lispflowmapping.lisp.util.LispAddressUtil;
 import org.opendaylight.lispflowmapping.mapcache.SimpleMapCache;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
 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.db.instance.AuthenticationKeyBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
@@ -64,7 +72,9 @@ public class AuthenticationKeyDataListener implements DataTreeChangeListener<Aut
                 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
                 LOG.trace("Value: {}", authKey);
 
-                smc.removeAuthenticationKey(authKey.getEid());
+                final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
+
+                smc.removeAuthenticationKey(convertedAuthKey.getEid());
             } else if (ModificationType.WRITE == mod.getModificationType() || ModificationType.SUBTREE_MODIFIED == mod
                     .getModificationType()) {
                 if (ModificationType.WRITE == mod.getModificationType()) {
@@ -78,11 +88,24 @@ public class AuthenticationKeyDataListener implements DataTreeChangeListener<Aut
                 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
                 LOG.trace("Value: {}", authKey);
 
-                smc.addAuthenticationKey(authKey.getEid(), authKey.getMappingAuthkey());
+                final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
+
+                smc.addAuthenticationKey(convertedAuthKey.getEid(), convertedAuthKey.getMappingAuthkey());
             } else {
                 LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
             }
         }
     }
 
+    private static AuthenticationKey convertToBinaryIfNecessary(AuthenticationKey authKey) {
+        Eid originalEid = authKey.getEid();
+        if (originalEid.getAddress() instanceof Ipv4Prefix || originalEid.getAddress() instanceof Ipv6Prefix ||
+                originalEid.getAddress() instanceof Ipv4 || originalEid.getAddress() instanceof Ipv6) {
+            AuthenticationKeyBuilder akb = new AuthenticationKeyBuilder(authKey);
+            akb.setEid(LispAddressUtil.convertToBinary(originalEid));
+            return akb.build();
+        }
+        return authKey;
+    }
+
 }