Fix reading mappings and keys with RPCs 48/39948/3
authorLorand Jakab <lojakab@cisco.com>
Tue, 7 Jun 2016 17:35:06 +0000 (20:35 +0300)
committerLorand Jakab <lojakab@cisco.com>
Tue, 7 Jun 2016 22:46:58 +0000 (01:46 +0300)
Change-Id: Ib01933c756d4ac97d423804aa810ef421e3884eb
Signed-off-by: Lorand Jakab <lojakab@cisco.com>
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingService.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/mdsal/MappingDataListener.java
mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MappingServiceTest.java
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/util/LispAddressUtil.java

index a84663861093ad6d578f75776c7075b884938da3..643c70c1bf25aabb377b2926957492de15413c54 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.lispflowmapping.implementation;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.concurrent.Future;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -24,9 +26,13 @@ import org.opendaylight.lispflowmapping.implementation.util.DSBEInputUtil;
 import org.opendaylight.lispflowmapping.implementation.util.RPCInputConvertorUtil;
 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeysInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingInput;
@@ -154,7 +160,7 @@ public class MappingService implements OdlMappingserviceService, IMappingService
 
         RpcResultBuilder<Void> rpcResultBuilder;
 
-        MappingAuthkey key = mappingSystem.getAuthenticationKey(input.getEid());
+        MappingAuthkey key = mappingSystem.getAuthenticationKey(convertToBinaryIfNecessary(input.getEid()));
 
         if (key != null) {
             String message = "Key already exists! Please use update-key if you want to change it.";
@@ -190,7 +196,7 @@ public class MappingService implements OdlMappingserviceService, IMappingService
 
         RpcResultBuilder<GetKeyOutput> rpcResultBuilder;
 
-        MappingAuthkey key = mappingSystem.getAuthenticationKey(input.getEid());
+        MappingAuthkey key = mappingSystem.getAuthenticationKey(convertToBinaryIfNecessary(input.getEid()));
 
         if (key == null) {
             String message = "Key was not found in the mapping database";
@@ -210,14 +216,15 @@ public class MappingService implements OdlMappingserviceService, IMappingService
 
         RpcResultBuilder<GetMappingOutput> rpcResultBuilder;
 
-        MappingRecord reply = (MappingRecord) mappingSystem.getMapping(input.getEid());
+        MappingRecord reply = (MappingRecord) mappingSystem.getMapping(convertToBinaryIfNecessary(input.getEid()));
 
         if (reply == null) {
             String message = "No mapping was found in the mapping database";
             rpcResultBuilder = RpcResultBuilder.<GetMappingOutput>failed()
                     .withError(RpcError.ErrorType.APPLICATION, NOT_FOUND_TAG, message);
         } else {
-            rpcResultBuilder = RpcResultBuilder.success(new GetMappingOutputBuilder().setMappingRecord(reply));
+            final MappingRecord convertedReply = convertFromBinaryIfNecessary(reply);
+            rpcResultBuilder = RpcResultBuilder.success(new GetMappingOutputBuilder().setMappingRecord(convertedReply));
         }
 
         return Futures.immediateFuture(rpcResultBuilder.build());
@@ -258,7 +265,7 @@ public class MappingService implements OdlMappingserviceService, IMappingService
 
         RpcResultBuilder<Void> rpcResultBuilder;
 
-        MappingAuthkey key = mappingSystem.getAuthenticationKey(input.getEid());
+        MappingAuthkey key = mappingSystem.getAuthenticationKey(convertToBinaryIfNecessary(input.getEid()));
 
         if (key == null) {
             String message = "Key doesn't exist! Please use add-key if you want to create a new authentication key.";
@@ -454,4 +461,50 @@ public class MappingService implements OdlMappingserviceService, IMappingService
         mappingSystem.cleanCaches();
         dsbe.removeAllDatastoreContent();
     }
+
+    private static Eid convertToBinaryIfNecessary(Eid eid) {
+        if (LispAddressUtil.addressNeedsConversionToBinary(eid.getAddress())) {
+            return LispAddressUtil.convertToBinary(eid);
+        }
+        return eid;
+    }
+
+    private static MappingRecord convertFromBinaryIfNecessary(MappingRecord originalRecord) {
+        List<LocatorRecord> originalLocators = originalRecord.getLocatorRecord();
+
+        List<LocatorRecord> convertedLocators = null;
+        if (originalLocators != null) {
+            // If convertedLocators is non-null, while originalLocators is also non-null, conversion has been made
+            convertedLocators = convertFromBinaryIfNecessary(originalLocators);
+        }
+
+        if (LispAddressUtil.addressNeedsConversionFromBinary(originalRecord.getEid().getAddress()) ||
+                (originalLocators != null && convertedLocators != null)) {
+            MappingRecordBuilder mrb = new MappingRecordBuilder(originalRecord);
+            mrb.setEid(LispAddressUtil.convertFromBinary(originalRecord.getEid()));
+            if (convertedLocators != null) {
+                mrb.setLocatorRecord(convertedLocators);
+            }
+            return mrb.build();
+        }
+        return originalRecord;
+    }
+
+    private static List<LocatorRecord> convertFromBinaryIfNecessary(List<LocatorRecord> originalLocators) {
+        List<LocatorRecord> convertedLocators = null;
+        for (LocatorRecord record : originalLocators) {
+            if (LispAddressUtil.addressNeedsConversionFromBinary(record.getRloc().getAddress())) {
+                LocatorRecordBuilder lrb = new LocatorRecordBuilder(record);
+                lrb.setRloc(LispAddressUtil.convertFromBinary(record.getRloc()));
+                if (convertedLocators == null) {
+                    convertedLocators = new ArrayList<LocatorRecord>();
+                }
+                convertedLocators.add(lrb.build());
+            }
+        }
+        if (convertedLocators != null) {
+            return convertedLocators;
+        }
+        return originalLocators;
+    }
 }
index 45f3c631d244c4a7d839cf591bd3912e45ec2e9b..d094897b835d703d9f9bfb98c543eed41f3c4c49 100644 (file)
@@ -156,9 +156,9 @@ public class MappingDataListener extends AbstractDataListener<Mapping> {
         return mapping;
     }
 
-    private static List<LocatorRecord> convertToBinaryIfNecessary(List<LocatorRecord> locators) {
+    private static List<LocatorRecord> convertToBinaryIfNecessary(List<LocatorRecord> originalLocators) {
         List<LocatorRecord> convertedLocators = null;
-        for (LocatorRecord record : locators) {
+        for (LocatorRecord record : originalLocators) {
             if (LispAddressUtil.addressNeedsConversionToBinary(record.getRloc().getAddress())) {
                 LocatorRecordBuilder lrb = new LocatorRecordBuilder(record);
                 lrb.setRloc(LispAddressUtil.convertToBinary(record.getRloc()));
@@ -168,6 +168,9 @@ public class MappingDataListener extends AbstractDataListener<Mapping> {
                 convertedLocators.add(lrb.build());
             }
         }
-        return convertedLocators;
+        if (convertedLocators != null) {
+            return convertedLocators;
+        }
+        return originalLocators;
     }
 }
index eb62809e6c4bff2a64386e4e608f743e73b0a42d..086a5fc1caf15f2ed6a97bd5736dc514a2da2fd5 100644 (file)
@@ -13,6 +13,7 @@ import static org.junit.Assert.assertNull;
 import java.util.ArrayList;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
+
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
@@ -29,6 +30,7 @@ import org.opendaylight.lispflowmapping.implementation.util.DSBEInputUtil;
 import org.opendaylight.lispflowmapping.implementation.util.RPCInputConvertorUtil;
 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
@@ -234,11 +236,13 @@ public class MappingServiceTest {
         // input
         final GetMappingInput getMappingInput = new GetMappingInputBuilder().setEid(IPV4_EID).build();
         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build();
+        final MappingRecord nonBinaryMappingRecord = getDefaultMappingRecordBuilder()
+                .setEid(LispAddressUtil.toEid(new Ipv4Address(IPV4_STRING), null)).build();
 
         Mockito.when(mappingSystem.getMapping(getMappingInput.getEid())).thenReturn(mappingRecord);
 
         final RpcResult<GetMappingOutput> rpc = RpcResultBuilder
-                .success(new GetMappingOutputBuilder().setMappingRecord(mappingRecord)).build();
+                .success(new GetMappingOutputBuilder().setMappingRecord(nonBinaryMappingRecord)).build();
 
         //result
         final Future<RpcResult<GetMappingOutput>> result = (mappingService.getMapping(getMappingInput));
index 93ddf85427998b3f05ece463472e698ce5871472..66d9f735f6e64ae686f94a2a4119cbae48de094e 100644 (file)
@@ -742,6 +742,13 @@ public final class LispAddressUtil {
         return pb.build();
     }
 
+    private static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
+            .lisp.address.address.Ipv4Prefix convertFromBinary(Ipv4PrefixBinary prefix) {
+        return new Ipv4PrefixBuilder().setIpv4Prefix(IetfInetUtil.INSTANCE.ipv4PrefixFor(
+                prefix.getIpv4AddressBinary().getValue(),
+                prefix.getIpv4MaskLength())).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();
@@ -751,16 +758,33 @@ public final class LispAddressUtil {
         return pb.build();
     }
 
+    private static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
+            .lisp.address.address.Ipv6Prefix convertFromBinary(Ipv6PrefixBinary prefix) {
+        return new Ipv6PrefixBuilder().setIpv6Prefix(IetfInetUtil.INSTANCE.ipv6PrefixFor(
+                prefix.getIpv6AddressBinary().getValue(),
+                prefix.getIpv6MaskLength())).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 Ipv4 convertFromBinary(Ipv4Binary address) {
+        return new Ipv4Builder().setIpv4(IetfInetUtil.INSTANCE.ipv4AddressFor(address.getIpv4Binary().getValue()))
+                .build();
+    }
+
     private static Ipv6Binary convertToBinary(Ipv6 address) {
         byte[] addr = InetAddresses.forString(address.getIpv6().getValue()).getAddress();
         return new Ipv6BinaryBuilder().setIpv6Binary(new Ipv6AddressBinary(addr)).build();
     }
 
+    private static Ipv6 convertFromBinary(Ipv6Binary address) {
+        return new Ipv6Builder().setIpv6(IetfInetUtil.INSTANCE.ipv6AddressFor(address.getIpv6Binary().getValue()))
+                .build();
+    }
+
     private static Pair<Class<? extends LispAddressFamily>, Address> convertToBinary(Address addr) {
         Address convAddr = null;
         Class<? extends LispAddressFamily> convType = null;
@@ -784,6 +808,25 @@ public final class LispAddressUtil {
         return new ImmutablePair<Class<? extends LispAddressFamily>, Address>(convType, convAddr);
     }
 
+    private static Pair<Class<? extends LispAddressFamily>, Address> convertFromBinary(Address addr) {
+        Address convAddr = null;
+        Class<? extends LispAddressFamily> convType = null;
+        if (addr instanceof Ipv4PrefixBinary) {
+            convAddr = convertFromBinary((Ipv4PrefixBinary) addr);
+            convType = Ipv4PrefixAfi.class;
+        } else if (addr instanceof Ipv6PrefixBinary) {
+            convAddr = convertFromBinary((Ipv6PrefixBinary) addr);
+            convType = Ipv6PrefixAfi.class;
+        } else if (addr instanceof Ipv4Binary) {
+            convAddr = convertFromBinary((Ipv4Binary) addr);
+            convType = Ipv4Afi.class;
+        } else if (addr instanceof Ipv6Binary) {
+            convAddr = convertFromBinary((Ipv6Binary) addr);
+            convType = Ipv6Afi.class;
+        }
+        return new ImmutablePair<Class<? extends LispAddressFamily>, Address>(convType, convAddr);
+    }
+
     public static Eid convertToBinary(Eid eid) {
         Pair<Class<? extends LispAddressFamily>, Address> converted = convertToBinary(eid.getAddress());
         if (converted.getRight() == null) {
@@ -795,6 +838,17 @@ public final class LispAddressUtil {
         return eb.build();
     }
 
+    public static Eid convertFromBinary(Eid eid) {
+        Pair<Class<? extends LispAddressFamily>, Address> converted = convertFromBinary(eid.getAddress());
+        if (converted.getRight() == null) {
+            return eid;
+        }
+        EidBuilder eb = new EidBuilder(eid);
+        eb.setAddressType(converted.getLeft());
+        eb.setAddress(converted.getRight());
+        return eb.build();
+    }
+
     public static Rloc convertToBinary(Rloc rloc) {
         Pair<Class<? extends LispAddressFamily>, Address> converted = convertToBinary(rloc.getAddress());
         if (converted.getRight() == null) {
@@ -806,6 +860,17 @@ public final class LispAddressUtil {
         return rb.build();
     }
 
+    public static Rloc convertFromBinary(Rloc rloc) {
+        Pair<Class<? extends LispAddressFamily>, Address> converted = convertFromBinary(rloc.getAddress());
+        if (converted.getRight() == null) {
+            return rloc;
+        }
+        RlocBuilder rb = new RlocBuilder(rloc);
+        rb.setAddressType(converted.getLeft());
+        rb.setAddress(converted.getRight());
+        return rb.build();
+    }
+
     public static boolean addressNeedsConversionToBinary(Address address) {
         if (address instanceof Ipv4 || address instanceof Ipv6 ||
                 address instanceof org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
@@ -817,6 +882,14 @@ public final class LispAddressUtil {
         return false;
     }
 
+    public static boolean addressNeedsConversionFromBinary(Address address) {
+        if (address instanceof Ipv4Binary || address instanceof Ipv6Binary ||
+                address instanceof Ipv4PrefixBinary || address instanceof Ipv6PrefixBinary) {
+            return true;
+        }
+        return false;
+    }
+
     public static int compareIpAddressByteArrays(byte[] a, byte[] b) {
         int i;
         if (a.length < b.length) {