Merge "Restoring authentication key from MDSAL."
authorLori Jakab <lorand.jakab@gmail.com>
Thu, 9 Jun 2016 00:25:31 +0000 (00:25 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 9 Jun 2016 00:25:31 +0000 (00:25 +0000)
18 files changed:
integrationtest/src/test/java/org/opendaylight/lispflowmapping/integrationtest/MultiSiteScenario.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingService.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/lisp/MapResolver.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/lisp/MapServer.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/mdsal/AuthenticationKeyDataListener.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/mdsal/MappingDataListener.java
mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/LispMappingServiceTest.java
mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MapResolverTest.java
mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MapServerTest.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
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/util/SourceDestKeyHelper.java
mappingservice/lisp-proto/src/test/java/org/opendaylight/lispflowmapping/lisp/util/LispAddressUtilTest.java
mappingservice/lisp-proto/src/test/java/org/opendaylight/lispflowmapping/lisp/util/SourceDestKeyHelperTest.java [new file with mode: 0644]
mappingservice/mapcache/src/main/java/org/opendaylight/lispflowmapping/mapcache/MultiTableMapCache.java
mappingservice/mapcache/src/test/java/org/opendaylight/lispflowmapping/mapcache/MultiTableMapCacheTest.java
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/AuthenticationKeyDataListener.java
mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/authentication/LispMACAuthenticationTest.java [new file with mode: 0644]

index f1472abfa9cc2b57bee2a23828a89e7372dadefc..2c282fcdc9b7895e71f4ebdb48852c4689ad3acc 100644 (file)
@@ -17,10 +17,12 @@ import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenario
 
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+
 import org.apache.commons.lang3.ArrayUtils;
 import org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.Site;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
@@ -28,11 +30,10 @@ import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingServic
 import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
 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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
-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.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
@@ -58,6 +59,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev15090
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.net.InetAddresses;
 
 /**
  * Contains methods for:
@@ -94,13 +96,13 @@ class MultiSiteScenario {
         this.lms = lms;
     }
 
-    private Ipv4Address verifyIpv4Address(final LocatorRecord locatorRecord) {
+    private Ipv4AddressBinary verifyIpv4Address(final LocatorRecord locatorRecord) {
         assertNotNull(locatorRecord);
         final Rloc rloc = locatorRecord.getRloc();
         assertNotNull(rloc);
         final Address address = rloc.getAddress();
-        assertTrue(address instanceof Ipv4);
-        return ((Ipv4) address).getIpv4();
+        assertTrue(address instanceof Ipv4Binary);
+        return ((Ipv4Binary) address).getIpv4Binary();
     }
 
     private List<LocatorRecord> verifyLocatorRecordExists(final MappingRecord mappingRecord) {
@@ -135,16 +137,16 @@ class MultiSiteScenario {
     }
 
     void setCommonAuthentication() {
-        Eid eid = LispAddressUtil.toEid(new Ipv4Prefix("0.0.0.0/0"), SITE_A.getVNI());
+        Eid eid = LispAddressUtil.asIpv4PrefixBinaryEid("0.0.0.0/0", SITE_A.getVNI());
         mapService.addAuthenticationKey(eid, NULL_AUTH_KEY);
 
-        eid = LispAddressUtil.toEid(new Ipv4Prefix("0.0.0.0/0"), SITE_D5.getVNI());
+        eid = LispAddressUtil.asIpv4PrefixBinaryEid("0.0.0.0/0", SITE_D5.getVNI());
         mapService.addAuthenticationKey(eid, NULL_AUTH_KEY);
         sleepForSeconds(1);
     }
 
     private Eid toEid(final String destSiteEidPrefix, final InstanceIdType vniValue, final int mask) {
-        return LispAddressUtil.toEid(new Ipv4Prefix(destSiteEidPrefix + "/" + mask), vniValue);
+        return LispAddressUtil.asIpv4PrefixBinaryEid(destSiteEidPrefix + "/" + mask, vniValue);
 
     }
 
@@ -174,8 +176,8 @@ class MultiSiteScenario {
     }
 
     void storeNorthMappingNegative(final Site dstSite, final MappingRecord.Action action) {
-        final Ipv4Prefix ipv4Prefix = new Ipv4Prefix(dstSite.getEidPrefix() + "/" + DEFAULT_NETWORK_MASK);
-        final Eid eidAsIpv4Prefix = LispAddressUtil.toEid(ipv4Prefix, dstSite.getVNI());
+        final Eid eidAsIpv4Prefix = LispAddressUtil.asIpv4PrefixBinaryEid(
+                dstSite.getEidPrefix() + "/" + DEFAULT_NETWORK_MASK, dstSite.getVNI());
 
         final MappingRecordBuilder mrbNegative = prepareMappingRecord(EidType.EID_WITH_PREFIX, null, dstSite);
         mrbNegative.setEid(eidAsIpv4Prefix);
@@ -186,8 +188,8 @@ class MultiSiteScenario {
     }
 
     void deleteNorthMappingNegative(final Site dstSite) {
-        final Ipv4Prefix ipv4Prefix = new Ipv4Prefix(dstSite.getEidPrefix() + "/" + DEFAULT_NETWORK_MASK);
-        final Eid eidAsIpv4Prefix = LispAddressUtil.toEid(ipv4Prefix, dstSite.getVNI());
+        final Eid eidAsIpv4Prefix = LispAddressUtil.asIpv4PrefixBinaryEid(
+                dstSite.getEidPrefix() + "/" + DEFAULT_NETWORK_MASK, dstSite.getVNI());
 
         mapService.removeMapping(MappingOrigin.Northbound, eidAsIpv4Prefix);
     }
@@ -233,7 +235,7 @@ class MultiSiteScenario {
         if (!dstSites[0].isForDeletion) {
             for (Site dstSite : dstSites) {
                 if (dstSite.getRloc() != null) {
-                    locatorRecords.add(provideLocatorRecord(LispAddressUtil.toRloc(new Ipv4Address(dstSite.getRloc())),
+                    locatorRecords.add(provideLocatorRecord(LispAddressUtil.asIpv4Rloc(dstSite.getRloc()),
                             dstSite.getRloc(), dstSite.getWeight(), dstSite.getPriority()));
                 }
             }
@@ -338,10 +340,11 @@ class MultiSiteScenario {
             boolean expectedTargetFound = false;
             for (LocatorRecord locatorRecord : locatorRecords) {
                 if (expectedTargetSite.getRloc().equals(rlocToString(locatorRecord))) {
-                    final Ipv4Address ipv4AddressSrcDst = verifyIpv4Address(locatorRecord);
-                    final boolean isRlocSrcDstEqual = ipv4AddressSrcDst.getValue().equals(expectedTargetSite.getRloc());
+                    final byte[] ipv4AddressSrcDst = verifyIpv4Address(locatorRecord).getValue();
+                    final byte[] rloc = InetAddresses.forString((expectedTargetSite.getRloc())).getAddress();
+                    final boolean isRlocSrcDstEqual = Arrays.equals(ipv4AddressSrcDst, rloc);
                     if (isPossibleAssertPingResultImmediately(expectedPingWorks, isRlocSrcDstEqual, "Unexpected RLOC." +
-                            "Expected value " + dstSite.getRloc() + ". Real value " + ipv4AddressSrcDst.getValue() +
+                            "Expected value " + rloc + ". Real value " + ipv4AddressSrcDst +
                             ".")) {
                         return true;
                     }
@@ -392,8 +395,7 @@ class MultiSiteScenario {
                          boolean expectedPingWorks, final Site ... additionalSitesFromMapping) {
         final MapReply mapReplyFromSrcToDst = emitMapRequestMessage(srcSite.getHost(srcHostIndex), dstSite.getHost
                 (dstHostIndex), dstSite.getVNI());
-        if (checkActionAndRloc(dstSite, expectedPingWorks, mapReplyFromSrcToDst,
-                additionalSitesFromMapping)) {
+        if (checkActionAndRloc(dstSite, expectedPingWorks, mapReplyFromSrcToDst, additionalSitesFromMapping)) {
             return;
         }
 
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 b5393f63526132fe5dc2798fbf4a0bd98cc9a536..4c534fe7462e242aea99ffd51eab1ea11a029285 100644 (file)
@@ -122,7 +122,7 @@ public class MapResolver implements IMapResolverAsync {
         // mapping will be pushed to dst as well (see sendSMRs in MapServer)
         if (mapEid.getAddressType().equals(SourceDestKeyLcaf.class)
                 && !reqEid.getAddressType().equals(SourceDestKeyLcaf.class)) {
-            subscribedEid = SourceDestKeyHelper.getDst(mapEid);
+            subscribedEid = SourceDestKeyHelper.getDstBinary(mapEid);
         }
 
         Set<SubscriberRLOC> subscribers = getSubscribers(subscribedEid);
@@ -145,7 +145,7 @@ public class MapResolver implements IMapResolverAsync {
         if (mapping.getEid().getAddress() instanceof SourceDestKey
                 && !(dstEid.getAddress() instanceof SourceDestKey)) {
             return new MappingRecordBuilder(mapping).setEid(
-                    SourceDestKeyHelper.getDst(mapping.getEid())).build();
+                    SourceDestKeyHelper.getDstBinary(mapping.getEid())).build();
         }
         return mapping;
     }
index 4b2659c922c98de3d4d9b95ff517754ff66143f5..914fd6cfcb93579f738c786593ce0ef92a66018d 100644 (file)
@@ -54,7 +54,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev15090
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceListener;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -90,7 +89,6 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener {
         boolean authFailed = false;
         boolean mappingUpdated = false;
         boolean merge = ConfigIni.getInstance().mappingMergeIsSet() && mapRegister.isMergeEnabled();
-        MappingAuthkey authkey = null;
         Set<SubscriberRLOC> subscribers = null;
         MappingRecord oldMapping;
 
@@ -202,7 +200,7 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener {
 
         // For SrcDst LCAF also send SMRs to Dst prefix
         if (eid.getAddress() instanceof SourceDestKey) {
-            Eid dstAddr = SourceDestKeyHelper.getDst(eid);
+            Eid dstAddr = SourceDestKeyHelper.getDstBinary(eid);
             Set<SubscriberRLOC> dstSubs = getSubscribers(dstAddr);
             MappingRecord newRecord = new MappingRecordBuilder(record).setEid(dstAddr).build();
             handleSmr(newRecord.getEid(), dstSubs, notifyHandler);
index cdefd3d6fe8debaae0837c0908b02d3d9c9c71bb..2c09cbd71ab1353b67def6deca7dcb75fea1eb96 100644 (file)
@@ -14,10 +14,6 @@ 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;
@@ -86,8 +82,7 @@ public class AuthenticationKeyDataListener extends AbstractDataListener<Authenti
 
     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) {
+        if (LispAddressUtil.addressNeedsConversionToBinary(originalEid.getAddress())) {
             AuthenticationKeyBuilder akb = new AuthenticationKeyBuilder(authKey);
             akb.setEid(LispAddressUtil.convertToBinary(originalEid));
             return akb.build();
index f7a0f73691130e8ef735d9c319ead3a1153bd421..d094897b835d703d9f9bfb98c543eed41f3c4c49 100644 (file)
@@ -7,7 +7,10 @@
  */
 package org.opendaylight.lispflowmapping.implementation.mdsal;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
+
 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,10 +18,16 @@ import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.lispflowmapping.implementation.util.MSNotificationInputUtil;
 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+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.MappingChange;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase;
 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.db.instance.MappingBuilder;
 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;
@@ -74,10 +83,12 @@ public class MappingDataListener extends AbstractDataListener<Mapping> {
                 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
                 LOG.trace("Value: {}", mapping);
 
-                mapSystem.removeMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid());
+                final Mapping convertedMapping = convertToBinaryIfNecessary(mapping);
+
+                mapSystem.removeMapping(convertedMapping.getOrigin(), convertedMapping.getMappingRecord().getEid());
                 try {
                     notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(
-                            mapping, MappingChange.Removed));
+                            convertedMapping, MappingChange.Removed));
                 } catch (InterruptedException e) {
                     LOG.warn("Notification publication interrupted!");
                 }
@@ -104,13 +115,15 @@ public class MappingDataListener extends AbstractDataListener<Mapping> {
                 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
                 LOG.trace("Value: {}", mapping);
 
-                mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
-                        mapping.getMappingRecord(), false);
+                final Mapping convertedMapping = convertToBinaryIfNecessary(mapping);
+
+                mapSystem.addMapping(convertedMapping.getOrigin(), convertedMapping.getMappingRecord().getEid(),
+                        convertedMapping.getMappingRecord(), false);
 
                 try {
                     // The notifications are used for sending SMR.
-                    notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping,
-                            mappingChange));
+                    notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(
+                            convertedMapping, mappingChange));
                 } catch (InterruptedException e) {
                     LOG.warn("Notification publication interrupted!");
                 }
@@ -120,4 +133,44 @@ public class MappingDataListener extends AbstractDataListener<Mapping> {
             }
         }
     }
+
+    private static Mapping convertToBinaryIfNecessary(Mapping mapping) {
+        MappingRecord originalRecord = mapping.getMappingRecord();
+        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 = convertToBinaryIfNecessary(originalLocators);
+        }
+
+        if (LispAddressUtil.addressNeedsConversionToBinary(originalRecord.getEid().getAddress()) ||
+                (originalLocators != null && convertedLocators != null)) {
+            MappingRecordBuilder mrb = new MappingRecordBuilder(originalRecord);
+            mrb.setEid(LispAddressUtil.convertToBinary(originalRecord.getEid()));
+            if (convertedLocators != null) {
+                mrb.setLocatorRecord(convertedLocators);
+            }
+            return new MappingBuilder(mapping).setMappingRecord(mrb.build()).build();
+        }
+        return mapping;
+    }
+
+    private static List<LocatorRecord> convertToBinaryIfNecessary(List<LocatorRecord> originalLocators) {
+        List<LocatorRecord> convertedLocators = null;
+        for (LocatorRecord record : originalLocators) {
+            if (LispAddressUtil.addressNeedsConversionToBinary(record.getRloc().getAddress())) {
+                LocatorRecordBuilder lrb = new LocatorRecordBuilder(record);
+                lrb.setRloc(LispAddressUtil.convertToBinary(record.getRloc()));
+                if (convertedLocators == null) {
+                    convertedLocators = new ArrayList<LocatorRecord>();
+                }
+                convertedLocators.add(lrb.build());
+            }
+        }
+        if (convertedLocators != null) {
+            return convertedLocators;
+        }
+        return originalLocators;
+    }
 }
index ea4b215cf2e93917183b35cafd9a0a718944fd5e..b8b28fa1a9a8b9d13a52cc74744a1f229c87e722 100644 (file)
@@ -12,6 +12,7 @@ import static org.junit.Assert.assertNull;
 
 import com.google.common.collect.Lists;
 
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -25,20 +26,37 @@ import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
+import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MappingKeepAlive;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
 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.eid.list.EidItemBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container
+        .MapRegisterCacheMetadata;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container
+        .MapRegisterCacheMetadataBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container
+        .map.register.cache.metadata.EidLispAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container
+        .map.register.cache.metadata.EidLispAddressBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.
         MappingRecordBuilder;
@@ -56,26 +74,33 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendM
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
 
 @RunWith(MockitoJUnitRunner.class)
 public class LispMappingServiceTest {
+    @Mock private static MapRegister mapRegisterMock;
     @Mock(name = "lispSB") private static OdlLispSbService lispSBMock;
-    @Mock(name = "mapResolver") private static IMapResolverAsync mapResolverMock;
     @Mock(name = "mapServer") private static IMapServerAsync mapServerMock;
+    @Mock(name = "mapService") private static IMappingService iMappingServiceMock;
+    @Mock(name = "mapResolver") private static IMapResolverAsync mapResolverMock;
     @Mock(name = "tlsMapReply") private static ThreadLocal<MapReply> tlsMapReplyMock;
-    @Mock(name = "tlsMapRequest") private static ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequestMock;
+    @Mock(name = "notificationService") private static NotificationService notificationServiceMock;
     @Mock(name = "tlsMapNotify") private static ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotifyMock;
-    @Mock private static MapRegister mapRegisterMock;
+    @Mock(name = "tlsMapRequest") private static ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequestMock;
     @InjectMocks private static LispMappingService lispMappingService;
 
     private static final byte[] IPV4_BYTES_1 =       new byte[] {1, 2, 3, 0};
     private static final byte[] IPV4_BYTES_2 =       new byte[] {1, 2, 4, 0};
     private static final String IPV4_STRING_1 =      "1.2.3.0";
+    private static final String IPV4_STRING_2 =      "1.2.3.1";
     private static final String IPV4_SOURCE_STRING = "192.168.0.1";
     private static final String IPV4_PREFIX_STRING = "/24";
+    private static final long TIMESTAMP = 1L;
 
     private static final Eid IPV4_PREFIX_EID_1 = LispAddressUtil.asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING);
     private static final Eid IPV4_SOURCE_EID = LispAddressUtil.asIpv4Eid(IPV4_SOURCE_STRING);
+    private static final Eid IPV4_EID_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
+    private static final Eid IPV4_EID_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
     private static final EidItemBuilder EID_ITEM_BUILDER = new EidItemBuilder()
             .setEidItemId("eid-item-id")
             .setEid(IPV4_PREFIX_EID_1);
@@ -221,6 +246,37 @@ public class LispMappingServiceTest {
         Mockito.verify(lispSBMock).sendMapReply(smrib.build());
     }
 
+    /**
+     * Tests {@link LispMappingService#onRequestMapping} method with mapReply == null.
+     */
+    @Test
+    public void onRequestMappingTest_withNullMapReply() {
+        final RequestMapping requestMapping = Mockito.mock(RequestMapping.class);
+        final org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequest
+                mapRequest = Mockito.mock(org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105
+                    .maprequestnotification.MapRequest.class);
+
+        Mockito.when(requestMapping.getMapRequest()).thenReturn(mapRequest);
+        Mockito.when(mapRequest.getEidItem()).thenReturn(Lists.newArrayList(EID_ITEM_BUILDER.build()));
+        Mockito.when(tlsMapReplyMock.get()).thenReturn(null);
+
+        lispMappingService.onRequestMapping(requestMapping);
+        Mockito.verifyZeroInteractions(lispSBMock);
+    }
+
+    /**
+     * Tests {@link LispMappingService#onMappingKeepAlive} method.
+     */
+    @Test
+    public void onMappingKeepAliveTest() {
+        final MappingKeepAlive mappingKeepAlive = Mockito.mock(MappingKeepAlive.class);
+        Mockito.when(mappingKeepAlive.getMapRegisterCacheMetadata()).thenReturn(getDefaultMapRegisterCacheMetadata());
+
+        lispMappingService.onMappingKeepAlive(mappingKeepAlive);
+        Mockito.verify(iMappingServiceMock).updateMappingRegistration(MappingOrigin.Southbound, IPV4_EID_1, TIMESTAMP);
+        Mockito.verify(iMappingServiceMock).updateMappingRegistration(MappingOrigin.Southbound, IPV4_EID_2, TIMESTAMP);
+    }
+
     /**
      * Tests {@link LispMappingService#handleSMR} method.
      */
@@ -291,14 +347,55 @@ public class LispMappingServiceTest {
     }
 
     /**
-     * Tests {@link LispMappingService#setShouldAuthenticate} method.
+     * Tests {@link LispMappingService#close} method.
      */
     @Test
-    public void setShouldAuthenticateTest() {
-        final boolean value = true;
+    public void closeTest() throws Exception {
+        lispMappingService.close();
+
+        assertNull(getField("mapResolver"));
+        assertNull(getField("mapServer"));
+    }
+
+    /**
+     * Tests {@link LispMappingService} other setter and getter methods.
+     */
+    @Test
+    public void otherTest() throws Exception {
+        assertEquals(notificationServiceMock, lispMappingService.getNotificationService());
+        assertEquals(true, lispMappingService.shouldUseSmr());
+
+        BindingAwareBroker brokerMock = Mockito.mock(BindingAwareBroker.class);
+        lispMappingService.setBindingAwareBroker(brokerMock);
+        assertEquals(brokerMock, getField("broker"));
 
-        lispMappingService.setShouldAuthenticate(value);
-        Mockito.verify(mapResolverMock).setShouldAuthenticate(value);
+        NotificationService otherNotificationServiceMock = Mockito.mock(NotificationService.class);
+        lispMappingService.setNotificationService(otherNotificationServiceMock);
+        assertEquals(otherNotificationServiceMock, getField("notificationService"));
+
+        ProviderContext sessionMock = Mockito.mock(ProviderContext.class);
+        lispMappingService.onSessionInitiated(sessionMock);
+        assertEquals(sessionMock, getField("session"));
+
+        lispMappingService.setShouldAuthenticate(true);
+        Mockito.verify(mapResolverMock).setShouldAuthenticate(true);
+
+        IMappingService mappingServiceMock = Mockito.mock(IMappingService.class);
+        lispMappingService.setMappingService(mappingServiceMock);
+        assertEquals(mappingServiceMock, getField("mapService"));
+
+        lispMappingService.onGotMapReply(Mockito.mock(GotMapReply.class));
+        lispMappingService.onGotMapNotify(Mockito.mock(GotMapNotify.class));
+        lispMappingService.onXtrRequestMapping(Mockito.mock(XtrRequestMapping.class));
+        lispMappingService.onXtrReplyMapping(Mockito.mock(XtrReplyMapping.class));
+    }
+
+    @SuppressWarnings("unchecked")
+    private static <T> T getField(String fieldName) throws NoSuchFieldException, IllegalAccessException {
+        final Field field = LispMappingService.class.getDeclaredField(fieldName);
+        field.setAccessible(true);
+
+        return (T) field.get(lispMappingService);
     }
 
     private static Pair<MapNotify, List<TransportAddress>> getDefaultMapNotifyPair() {
@@ -313,6 +410,19 @@ public class LispMappingServiceTest {
         return new ImmutablePair<>(mapRequestBuilder.build(), TRANSPORT_ADDRESS_1);
     }
 
+    private static MapRegisterCacheMetadata getDefaultMapRegisterCacheMetadata() {
+        final EidLispAddress eidLispAddress_1 = new EidLispAddressBuilder()
+                .setEidLispAddressId("id-1")
+                .setEid(IPV4_EID_1).build();
+        final EidLispAddress eidLispAddress_2 = new EidLispAddressBuilder()
+                .setEidLispAddressId("id-2")
+                .setEid(IPV4_EID_2).build();
+
+        return new MapRegisterCacheMetadataBuilder()
+                .setEidLispAddress(Lists.newArrayList(eidLispAddress_1, eidLispAddress_2))
+                .setTimestamp(TIMESTAMP).build();
+    }
+
     class TransportAddressMatch extends ArgumentMatcher<SendMapNotifyInput> {
         public boolean matches(Object sendMapNotify) {
             final SendMapNotifyInput sendMapNotifyInput = (SendMapNotifyInput) sendMapNotify;
@@ -320,6 +430,5 @@ public class LispMappingServiceTest {
             return TRANSPORT_ADDRESS_1.equals(notifyTransportAddress)
                     || TRANSPORT_ADDRESS_2.equals(notifyTransportAddress);
         }
-
     }
 }
index e0177e544d3f0c3f51b736be26077a0cb0cb7cc2..12d7633bca408a658cf0281b0bfbeafc3daa28d3 100644 (file)
@@ -201,7 +201,7 @@ public class MapResolverTest {
 
         mapResolver.handleMapRequest(mapRequestBuilder.build());
         Mockito.verify(mapServiceMock).getData(MappingOrigin.Southbound,
-                SourceDestKeyHelper.getDst(SOURCE_DEST_KEY_EID), SubKeys.SUBSCRIBERS);
+                SourceDestKeyHelper.getDstBinary(SOURCE_DEST_KEY_EID), SubKeys.SUBSCRIBERS);
     }
 
     /**
index f1d96489f64b48d9f04282dbca3d8510ddd148d1..77ad610180384d52fcb5151e7f80fd671d7cb40e 100644 (file)
@@ -245,7 +245,7 @@ public class MapServerTest {
                 .thenReturn(subscriberSetMock_1);
 
         // return a subscriberSet for SrcDestKeyEid destination MappingRecord
-        Mockito.when(mapService.getData(MappingOrigin.Southbound, SourceDestKeyHelper.getDst(SOURCE_DEST_KEY_EID),
+        Mockito.when(mapService.getData(MappingOrigin.Southbound, SourceDestKeyHelper.getDstBinary(SOURCE_DEST_KEY_EID),
                 SubKeys.SUBSCRIBERS)).thenReturn(subscriberSetMock_2);
 
         // ----------------- Stubbing for Ipv4 type Eid mapping ------------------------
@@ -275,7 +275,8 @@ public class MapServerTest {
         // for SrcDst destination mapping
         final ArgumentCaptor<MapRequest> captor_2 = ArgumentCaptor.forClass(MapRequest.class);
         Mockito.verify(notifyHandler, Mockito.times(1)).handleSMR(captor_2.capture(), Mockito.eq(RLOC_4));
-        Mockito.verify(mapService).addData(MappingOrigin.Southbound, SourceDestKeyHelper.getDst(SOURCE_DEST_KEY_EID),
+        Mockito.verify(mapService).addData(MappingOrigin.Southbound,
+                SourceDestKeyHelper.getDstBinary(SOURCE_DEST_KEY_EID),
                 SubKeys.SUBSCRIBERS, subscriberSetMock_2);
         final Eid resultEid_2 = captor_2.getValue().getEidItem().iterator().next().getEid();
         assertEquals(IPV4_SOURCE_EID_4, resultEid_2);
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 0398ce4db418064656571e7842d1a97c7478855a..4e4f4a78029df0169c3d85babf05a4d01ad806d9 100644 (file)
@@ -17,6 +17,8 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.regex.Pattern;
 
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
@@ -153,6 +155,24 @@ public final class LispAddressUtil {
         return null;
     }
 
+    public static Class<? extends LispAddressFamily> binaryAddressTypeFromInet(InetAddress address) {
+        if (address instanceof Inet4Address) {
+            return Ipv4BinaryAfi.class;
+        } else if (address instanceof Inet6Address) {
+            return Ipv6BinaryAfi.class;
+        }
+        return null;
+    }
+
+    public static Address binaryAddressFromInet(InetAddress address) {
+        if (address instanceof Inet4Address) {
+            return (Address) new Ipv4BinaryBuilder().setIpv4Binary(new Ipv4AddressBinary(address.getAddress())).build();
+        } else if (address instanceof Inet6Address) {
+            return (Address) new Ipv6BinaryBuilder().setIpv6Binary(new Ipv6AddressBinary(address.getAddress())).build();
+        }
+        return null;
+    }
+
     public static Class<? extends LispAddressFamily> addressTypeFromIpAddress(IpAddress address) {
         if (address == null) {
             return null;
@@ -267,9 +287,9 @@ public final class LispAddressUtil {
 
     public static Rloc toRloc(InetAddress address) {
         RlocBuilder builder = new RlocBuilder();
-        builder.setAddressType(addressTypeFromInet(address));
+        builder.setAddressType(binaryAddressTypeFromInet(address));
         builder.setVirtualNetworkId(null);
-        builder.setAddress(addressFromInet(address));
+        builder.setAddress(binaryAddressFromInet(address));
         return builder.build();
     }
 
@@ -462,6 +482,25 @@ public final class LispAddressUtil {
         return toEid(new Ipv4AddressBinary(InetAddresses.forString(address).getAddress()), new InstanceIdType(vni));
     }
 
+    public static Eid asBinaryEid(SimpleAddress address, InstanceIdType iid) {
+        if (address.getIpPrefix() != null) {
+            if (address.getIpPrefix().getIpv4Prefix() != null) {
+                return LispAddressUtil.asIpv4PrefixBinaryEid(address.getIpPrefix().getIpv4Prefix().getValue(), iid);
+            } else if (address.getIpPrefix().getIpv6Prefix() != null) {
+                return LispAddressUtil.asIpv6PrefixBinaryEid(address.getIpPrefix().getIpv6Prefix().getValue(), iid);
+            }
+        } else if (address.getIpAddress() != null) {
+            if (address.getIpAddress().getIpv4Address() != null) {
+                LispAddressUtil.toEid(new Ipv4AddressBinary(InetAddresses.forString(
+                        address.getIpAddress().getIpv4Address().getValue()).getAddress()), iid);
+            } else if (address.getIpAddress().getIpv6Address() != null) {
+                LispAddressUtil.toEid(new Ipv6AddressBinary(InetAddresses.forString(
+                        address.getIpAddress().getIpv6Address().getValue()).getAddress()), iid);
+            }
+        }
+        return LispAddressUtil.asEid(address, iid);
+    }
+
     public static int ipVersionFromString(String ip) {
         if (IP4_PATTERN.matcher(ip).matches()) {
             return 4;
@@ -703,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();
@@ -712,39 +758,138 @@ 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();
     }
 
-    public static Eid convertToBinary(Eid eid) {
-        EidBuilder eb = new EidBuilder(eid);
-        Address addr = eid.getAddress();
+    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;
         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);
+            convType = Ipv4PrefixBinaryAfi.class;
         } 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);
+            convType = Ipv6PrefixBinaryAfi.class;
         } else if (addr instanceof Ipv4) {
             convAddr = convertToBinary((Ipv4) addr);
+            convType = Ipv4BinaryAfi.class;
         } else if (addr instanceof Ipv6) {
             convAddr = convertToBinary((Ipv6) addr);
-        } else {
+            convType = Ipv6BinaryAfi.class;
+        }
+        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) {
+            return eid;
+        }
+        EidBuilder eb = new EidBuilder(eid);
+        eb.setAddressType(converted.getLeft());
+        eb.setAddress(converted.getRight());
+        return eb.build();
+    }
+
+    public static Eid convertFromBinary(Eid eid) {
+        Pair<Class<? extends LispAddressFamily>, Address> converted = convertFromBinary(eid.getAddress());
+        if (converted.getRight() == null) {
             return eid;
         }
-        eb.setAddress(convAddr);
+        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) {
+            return rloc;
+        }
+        RlocBuilder rb = new RlocBuilder(rloc);
+        rb.setAddressType(converted.getLeft());
+        rb.setAddress(converted.getRight());
+        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
+                        .lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix ||
+                address instanceof org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+                        .lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix) {
+            return true;
+        }
+        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) {
index 8eb1a5601fc5e4d392b632cb9d3a1a98677d232b..fc530a36c57201f937bbc841031695d826fe4518 100644 (file)
@@ -45,6 +45,22 @@ public final class SourceDestKeyHelper {
         }
     }
 
+    public static Eid getSrcBinary(Eid eid) {
+        if (eid.getAddress() instanceof SourceDestKey) {
+            return LispAddressUtil.asBinaryEid(((SourceDestKey) eid.getAddress()).getSourceDestKey().getSource(),
+                    eid.getVirtualNetworkId());
+        }
+        return eid;
+    }
+
+    public static Eid getDstBinary(Eid eid) {
+        if (eid.getAddress() instanceof SourceDestKey) {
+            return LispAddressUtil.asBinaryEid(((SourceDestKey) eid.getAddress()).getSourceDestKey().getDest(),
+                    eid.getVirtualNetworkId());
+        }
+        return eid;
+    }
+
     public static short getSrcMask(Eid eid) {
         Address addr = eid.getAddress();
         if (!isSrcDst(addr)) {
index 346b030744e8d9be855393ab1f1cd9c101f5d401..1f849f89efb5a6bbb6aac73a7b68b9f8c2a1fec4 100644 (file)
@@ -536,8 +536,8 @@ public class LispAddressUtilTest {
     public void toRloc() throws UnknownHostException {
         InetAddress ipv4AddressInet = Inet4Address.getByAddress(IPV4_ADDRESS_BYTES_A_TEST);
         final Rloc rlocFromInetAddress = LispAddressUtil.toRloc(ipv4AddressInet);
-        assertEquals(Ipv4Afi.class, rlocFromInetAddress.getAddressType());
-        assertEquals(IPV4_ADDRESS_VALUE_TEST, ((Ipv4)rlocFromInetAddress.getAddress()).getIpv4().getValue());
+        assertEquals(Ipv4BinaryAfi.class, rlocFromInetAddress.getAddressType());
+        assertEquals(IPV4_ADDRESS_BINARY_TEST, ((Ipv4Binary) rlocFromInetAddress.getAddress()).getIpv4Binary());
 
         final Rloc rlocFromIpv4Address = LispAddressUtil.toRloc(IPV4_ADDRESS_TEST);
         assertEquals(Ipv4Afi.class, rlocFromIpv4Address.getAddressType());
diff --git a/mappingservice/lisp-proto/src/test/java/org/opendaylight/lispflowmapping/lisp/util/SourceDestKeyHelperTest.java b/mappingservice/lisp-proto/src/test/java/org/opendaylight/lispflowmapping/lisp/util/SourceDestKeyHelperTest.java
new file mode 100644 (file)
index 0000000..55c306d
--- /dev/null
@@ -0,0 +1,189 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.lispflowmapping.lisp.util;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
+        .address.Ipv4PrefixBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
+        .address.SourceDestKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
+        .address.SourceDestKeyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4PrefixBinaryAfi;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address
+        .address.Ipv4PrefixBinary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address
+        .address.Ipv4PrefixBinaryBuilder;
+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.eid.container.EidBuilder;
+
+public class SourceDestKeyHelperTest {
+
+    private static final String IPV4_STRING_SOURCE = "192.168.0.1";
+    private static final String IPV4_STRING_DEST =   "192.168.0.2";
+    private static final short MASK_1 = 20;
+    private static final short MASK_2 = 24;
+
+    private static final byte[] IPV4_BYTE_SOURCE = new byte[]{-64, -88, 0, 1};
+    private static final byte[] IPV4_BYTE_DEST = new byte[]{-64, -88, 0, 2};
+
+    private static final Ipv4Prefix IPV4_PREFIX_SOURCE = new Ipv4Prefix(IPV4_STRING_SOURCE + "/" + MASK_1);
+    private static final Ipv4Prefix IPV4_PREFIX_DEST = new Ipv4Prefix(IPV4_STRING_DEST + "/" + MASK_2);
+    private static final Ipv4AddressBinary IPV4_SOURCE_ADDRESS_BINARY = new Ipv4AddressBinary(IPV4_BYTE_SOURCE);
+    private static final Ipv4AddressBinary IPV4_DEST_ADDRESS_BINARY = new Ipv4AddressBinary(IPV4_BYTE_DEST);
+    private static final Ipv4PrefixBinary IPV4_SOURCE_PREFIX_BINARY =
+            getIpv4PrefixBinary(IPV4_SOURCE_ADDRESS_BINARY, MASK_1);
+    private static final Ipv4PrefixBinary IPV4_DEST_PREFIX_BINARY =
+            getIpv4PrefixBinary(IPV4_DEST_ADDRESS_BINARY, MASK_2);
+
+    private static final SimpleAddress SOURCE = new SimpleAddress(new IpPrefix(IPV4_PREFIX_SOURCE));
+    private static final SimpleAddress DEST = new SimpleAddress(new IpPrefix(IPV4_PREFIX_DEST));
+    private static final InstanceIdType INSTANCE_ID = new InstanceIdType(100L);
+    private static final Address SOURCE_DEST_KEY = getSourceDestKeyAddress(SOURCE, DEST);
+    private static final Address IPV4_PREFIX = new Ipv4PrefixBuilder().setIpv4Prefix(IPV4_PREFIX_SOURCE).build();
+    private static final Eid EID_IPV4_PREFIX_ADDRESS = new EidBuilder().setAddress(IPV4_PREFIX).build();
+    private static final Eid EID_SOURCE_DEST_KEY = getEidSourceDestKey();
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getSrc} method with SourceDestKey address type.
+     */
+    @Test
+    public void getSrcTest_sourceDestKey() {
+        final Eid expectedResult = new EidBuilder()
+                .setAddress(new Ipv4PrefixBuilder().setIpv4Prefix(IPV4_PREFIX_SOURCE).build())
+                .setVirtualNetworkId(INSTANCE_ID)
+                .setAddressType(Ipv4PrefixAfi.class).build();
+        assertEquals(expectedResult, SourceDestKeyHelper.getSrc(EID_SOURCE_DEST_KEY));
+    }
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getSrc} method with Ipv4Prefix address type.
+     */
+    @Test
+    public void getSrcTest_ipv4Prefix() {
+        assertEquals(EID_IPV4_PREFIX_ADDRESS, SourceDestKeyHelper.getSrc(EID_IPV4_PREFIX_ADDRESS));
+    }
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getDst} method with SourceDestKey address type.
+     */
+    @Test
+    public void getDstTest_sourceDestKey() {
+        final Eid expectedResult = new EidBuilder()
+                .setAddress(new Ipv4PrefixBuilder().setIpv4Prefix(IPV4_PREFIX_DEST).build())
+                .setVirtualNetworkId(INSTANCE_ID)
+                .setAddressType(Ipv4PrefixAfi.class).build();
+        assertEquals(expectedResult, SourceDestKeyHelper.getDst(EID_SOURCE_DEST_KEY));
+    }
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getDst} method with Ipv4Prefix address type.
+     */
+    @Test
+    public void getDstTest_ipv4Prefix() {
+        assertEquals(EID_IPV4_PREFIX_ADDRESS, SourceDestKeyHelper.getDst(EID_IPV4_PREFIX_ADDRESS));
+    }
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getSrcBinary} method with SourceDestKey address type.
+     */
+    @Test
+    public void getSrcBinaryTest_sourceDestKey() {
+        final Eid expectedResult = new EidBuilder()
+                .setAddress(IPV4_SOURCE_PREFIX_BINARY)
+                .setVirtualNetworkId(INSTANCE_ID)
+                .setAddressType(Ipv4PrefixBinaryAfi.class).build();
+        assertEquals(expectedResult, SourceDestKeyHelper.getSrcBinary(EID_SOURCE_DEST_KEY));
+    }
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getSrcBinary} method with Ipv4Prefix address type.
+     */
+    @Test
+    public void getSrcBinaryTest_ipv4Prefix() {
+        assertEquals(EID_IPV4_PREFIX_ADDRESS, SourceDestKeyHelper.getSrcBinary(EID_IPV4_PREFIX_ADDRESS));
+    }
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getDstBinary} method with SourceDestKey address type.
+     */
+    @Test
+    public void getDstBinaryTest_sourceDestKey() {
+        final Eid expectedResult = new EidBuilder()
+                .setAddress(IPV4_DEST_PREFIX_BINARY)
+                .setVirtualNetworkId(INSTANCE_ID)
+                .setAddressType(Ipv4PrefixBinaryAfi.class).build();
+        assertEquals(expectedResult, SourceDestKeyHelper.getDstBinary(EID_SOURCE_DEST_KEY));
+    }
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getDstBinary} method with Ipv4Prefix address type.
+     */
+    @Test
+    public void getDstBinaryTest_ipv4Prefix() {
+        assertEquals(EID_IPV4_PREFIX_ADDRESS, SourceDestKeyHelper.getDstBinary(EID_IPV4_PREFIX_ADDRESS));
+    }
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getSrcMask} method with SourceDestKey address type.
+     */
+    @Test
+    public void getSrcMaskTest_sourceDestKey() {
+        assertEquals((short) 20, SourceDestKeyHelper.getSrcMask(EID_SOURCE_DEST_KEY));
+    }
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getSrcMask} method with Ipv4Prefix address type.
+     */
+    @Test
+    public void getSrcMaskTest_ipv4Prefix() {
+        assertEquals((short) 0, SourceDestKeyHelper.getSrcMask(EID_IPV4_PREFIX_ADDRESS));
+    }
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getDstMask} method with SourceDestKey address type.
+     */
+    @Test
+    public void getDstMaskTest_sourceDestKey() {
+        assertEquals((short) 24, SourceDestKeyHelper.getDstMask(EID_SOURCE_DEST_KEY));
+    }
+
+    /**
+     * Tests {@link SourceDestKeyHelper#getDstMask} method with Ipv4Prefix address type.
+     */
+    @Test
+    public void getDstMaskTest_ipv4Prefix() {
+        assertEquals((short) 0, SourceDestKeyHelper.getDstMask(EID_IPV4_PREFIX_ADDRESS));
+    }
+
+    private static SourceDestKey getSourceDestKeyAddress(SimpleAddress sourceAddress, SimpleAddress destAddress) {
+        return new SourceDestKeyBuilder()
+                .setSourceDestKey(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types
+                        .rev151105.lisp.address.address.source.dest.key.SourceDestKeyBuilder()
+                        .setSource(sourceAddress)
+                        .setDest(destAddress).build())
+                .build();
+    }
+
+    private static Eid getEidSourceDestKey() {
+        return new EidBuilder().setAddress(SOURCE_DEST_KEY).setVirtualNetworkId(INSTANCE_ID).build();
+    }
+
+    private static Ipv4PrefixBinary getIpv4PrefixBinary(Ipv4AddressBinary ipv4AddressBinary, short mask) {
+        return new Ipv4PrefixBinaryBuilder().setIpv4AddressBinary(ipv4AddressBinary).setIpv4MaskLength(mask).build();
+    }
+}
index 1ba18b55e2c6b49ef6f28f9cb0a4dfb187ead23e..3663c36f9c26ab4d9aad941bc1625f3226dfcbbd 100644 (file)
@@ -69,7 +69,7 @@ public class MultiTableMapCache implements IMapCache {
         ILispDAO table = getOrInstantiateVniTable(key);
 
         if (eid.getAddress() instanceof SourceDestKey) {
-            Eid srcKey = SourceDestKeyHelper.getSrc(eid);
+            Eid srcKey = SourceDestKeyHelper.getSrcBinary(eid);
             ILispDAO srcDstDao = getOrInstantiateSDInnerDao(eid, table);
             srcDstDao.put(srcKey, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
             srcDstDao.put(srcKey, new MappingEntry<>(SubKeys.RECORD, value));
@@ -162,8 +162,8 @@ public class MultiTableMapCache implements IMapCache {
 
         // a map-request for an actual SrcDst LCAF, ignore src eid
         if (dstEid.getAddress() instanceof SourceDestKey) {
-            Eid srcAddr = SourceDestKeyHelper.getSrc(dstEid);
-            Eid dstAddr = SourceDestKeyHelper.getDst(dstEid);
+            Eid srcAddr = SourceDestKeyHelper.getSrcBinary(dstEid);
+            Eid dstAddr = SourceDestKeyHelper.getDstBinary(dstEid);
             return getMappingLpmSD(srcAddr, dstAddr, table);
         }
 
@@ -181,7 +181,7 @@ public class MultiTableMapCache implements IMapCache {
         if (key.getAddress() instanceof SourceDestKey) {
             ILispDAO db = getSDInnerDao(key, table);
             if (db != null) {
-                db.removeSpecific(SourceDestKeyHelper.getSrc(key),
+                db.removeSpecific(SourceDestKeyHelper.getSrcBinary(key),
                         SubKeys.RECORD);
             }
         } else {
@@ -195,7 +195,7 @@ public class MultiTableMapCache implements IMapCache {
 
         if (key.getAddress() instanceof SourceDestKey) {
             ILispDAO srcDstDao = getOrInstantiateSDInnerDao(key, table);
-            srcDstDao.put(SourceDestKeyHelper.getSrc(key), new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
+            srcDstDao.put(SourceDestKeyHelper.getSrcBinary(key), new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
         } else {
             table.put(key, new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
         }
@@ -225,7 +225,7 @@ public class MultiTableMapCache implements IMapCache {
                 // NOTE: this is an exact match, not a longest prefix match
                 ILispDAO srcDstDao = getSDInnerDao(eid, table);
                 if (srcDstDao != null) {
-                    return getAuthKeyLpm(SourceDestKeyHelper.getSrc(eid), srcDstDao);
+                    return getAuthKeyLpm(SourceDestKeyHelper.getSrcBinary(eid), srcDstDao);
                 }
                 return null;
             } else {
@@ -263,7 +263,7 @@ public class MultiTableMapCache implements IMapCache {
     // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over src.
     // This method returns the DAO associated to a dst or creates it if it doesn't exist.
     private ILispDAO getOrInstantiateSDInnerDao(Eid address, ILispDAO dao) {
-        Eid dstKey = SourceDestKeyHelper.getDst(address);
+        Eid dstKey = SourceDestKeyHelper.getDstBinary(address);
         ILispDAO srcDstDao = (ILispDAO) dao.getSpecific(dstKey, SubKeys.LCAF_SRCDST);
         if (srcDstDao == null) {
             // inserts nested table for source
@@ -275,7 +275,7 @@ public class MultiTableMapCache implements IMapCache {
     // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over src.
     // This method returns the DAO associated to dst or null if it doesn't exist.
     private ILispDAO getSDInnerDao(Eid address, ILispDAO dao) {
-        return (ILispDAO) dao.getSpecific(SourceDestKeyHelper.getDst(address), SubKeys.LCAF_SRCDST);
+        return (ILispDAO) dao.getSpecific(SourceDestKeyHelper.getDstBinary(address), SubKeys.LCAF_SRCDST);
     }
 
     public String printMappings() {
@@ -345,7 +345,7 @@ public class MultiTableMapCache implements IMapCache {
 
         if (key.getAddress() instanceof SourceDestKey) {
             ILispDAO srcDstDao = getOrInstantiateSDInnerDao(key, table);
-            srcDstDao.put(SourceDestKeyHelper.getSrc(key), new MappingEntry<Object>(subKey, data));
+            srcDstDao.put(SourceDestKeyHelper.getSrcBinary(key), new MappingEntry<Object>(subKey, data));
         } else {
             table.put(key, new MappingEntry<Object>(subKey, data));
         }
@@ -361,7 +361,7 @@ public class MultiTableMapCache implements IMapCache {
 
         if (key.getAddress() instanceof SourceDestKey) {
             ILispDAO srcDstDao = getSDInnerDao(key, table);
-            return srcDstDao.getSpecific(SourceDestKeyHelper.getSrc(key), subKey);
+            return srcDstDao.getSpecific(SourceDestKeyHelper.getSrcBinary(key), subKey);
         } else {
             return table.getSpecific(key, subKey);
         }
@@ -377,7 +377,7 @@ public class MultiTableMapCache implements IMapCache {
         if (key.getAddress() instanceof SourceDestKey) {
             ILispDAO db = getSDInnerDao(key, table);
             if (db != null) {
-                db.removeSpecific(SourceDestKeyHelper.getSrc(key), subKey);
+                db.removeSpecific(SourceDestKeyHelper.getSrcBinary(key), subKey);
             }
         } else {
             table.removeSpecific(key, subKey);
index 9f2b8195d7d119844abf99b1080cf9addb7de8f1..1a4d22957bc8c1728c06404af033bdc2cb89aaa2 100644 (file)
@@ -105,8 +105,8 @@ public class MultiTableMapCacheTest {
     public void addMappingTest_withSourceDestKey() {
 
         final Eid normalized_Eid = MaskUtil.normalize(EID_SOURCE_DEST_KEY_TYPE);
-        final Eid dstKey = SourceDestKeyHelper.getDst(normalized_Eid);
-        final Eid srcKey = SourceDestKeyHelper.getSrc(normalized_Eid);
+        final Eid dstKey = SourceDestKeyHelper.getDstBinary(normalized_Eid);
+        final Eid srcKey = SourceDestKeyHelper.getSrcBinary(normalized_Eid);
 
         when(daoMock.getSpecific(VNI, SubKeys.VNI)).thenReturn(tableDaoMock);
         when(tableDaoMock.getSpecific(dstKey, SubKeys.LCAF_SRCDST)).thenReturn(srcDstDaoMock);
@@ -135,10 +135,10 @@ public class MultiTableMapCacheTest {
     public void getMappingTest_withSourceDestKey() {
         when(daoMock.getSpecific(VNI, SubKeys.VNI)).thenReturn(tableDaoMock);
 
-        final Eid dstAddr = SourceDestKeyHelper.getDst(EID_SOURCE_DEST_KEY_TYPE);
+        final Eid dstAddr = SourceDestKeyHelper.getDstBinary(EID_SOURCE_DEST_KEY_TYPE);
         final Eid normalizedDstAddr = MaskUtil.normalize(dstAddr);
 
-        final Eid srcAddr = SourceDestKeyHelper.getSrc(EID_SOURCE_DEST_KEY_TYPE);
+        final Eid srcAddr = SourceDestKeyHelper.getSrcBinary(EID_SOURCE_DEST_KEY_TYPE);
         final Eid normalizedSrcAddr = MaskUtil.normalize(srcAddr);
 
         final Map<String, Object> entry = getEntry1();
@@ -199,10 +199,10 @@ public class MultiTableMapCacheTest {
 
         when(daoMock.getSpecific(VNI, SubKeys.VNI)).thenReturn(tableDaoMock);
         when(tableDaoMock.getSpecific(SourceDestKeyHelper
-                .getDst(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST)).thenReturn(dbMock);
+                .getDstBinary(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST)).thenReturn(dbMock);
 
         multiTableMapCache.removeMapping(EID_SOURCE_DEST_KEY_TYPE, true);
-        verify(dbMock).removeSpecific(SourceDestKeyHelper.getSrc(NORMALIZED_SRCDST_EID),
+        verify(dbMock).removeSpecific(SourceDestKeyHelper.getSrcBinary(NORMALIZED_SRCDST_EID),
                 SubKeys.RECORD);
     }
 
@@ -237,11 +237,11 @@ public class MultiTableMapCacheTest {
     @Test
     public void addAuthenticationKeyTest() {
         when(daoMock.getSpecific(VNI, SubKeys.VNI)).thenReturn(tableDaoMock);
-        when(tableDaoMock.putNestedTable(SourceDestKeyHelper.getDst(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST))
+        when(tableDaoMock.putNestedTable(SourceDestKeyHelper.getDstBinary(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST))
                 .thenReturn(srcDstDaoMock);
 
         multiTableMapCache.addAuthenticationKey(EID_SOURCE_DEST_KEY_TYPE, MAPPING_AUTHKEY);
-        verify(srcDstDaoMock).put(SourceDestKeyHelper.getSrc(NORMALIZED_SRCDST_EID),
+        verify(srcDstDaoMock).put(SourceDestKeyHelper.getSrcBinary(NORMALIZED_SRCDST_EID),
                 new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
 
         multiTableMapCache.addAuthenticationKey(EID_TEST, MAPPING_AUTHKEY);
@@ -267,8 +267,8 @@ public class MultiTableMapCacheTest {
      */
     @Test
     public void getAuthenticationKeyTest_withSourceDestKey() {
-        final Eid eidSrc = SourceDestKeyHelper.getSrc(EID_SOURCE_DEST_KEY_TYPE);
-        final Eid eidDst = SourceDestKeyHelper.getDst(EID_SOURCE_DEST_KEY_TYPE);
+        final Eid eidSrc = SourceDestKeyHelper.getSrcBinary(EID_SOURCE_DEST_KEY_TYPE);
+        final Eid eidDst = SourceDestKeyHelper.getDstBinary(EID_SOURCE_DEST_KEY_TYPE);
         final short maskLength = MaskUtil.getMaskForAddress(eidSrc.getAddress());
         final Eid key = MaskUtil.normalize(eidSrc, maskLength);
 
@@ -285,7 +285,7 @@ public class MultiTableMapCacheTest {
      */
     @Test
     public void getAuthenticationKeyTest_withSourceDestKey_nullDao() {
-        final Eid eidDst = SourceDestKeyHelper.getDst(EID_SOURCE_DEST_KEY_TYPE);
+        final Eid eidDst = SourceDestKeyHelper.getDstBinary(EID_SOURCE_DEST_KEY_TYPE);
 
         when(daoMock.getSpecific(VNI, SubKeys.VNI)).thenReturn(tableDaoMock);
         when(tableDaoMock.getSpecific(eidDst, SubKeys.LCAF_SRCDST))
@@ -335,7 +335,7 @@ public class MultiTableMapCacheTest {
     @Test
     public void removeAuthenticationKeyTest_withSourceDestKey() {
         when(daoMock.getSpecific(VNI, SubKeys.VNI)).thenReturn(tableDaoMock);
-        when(tableDaoMock.getSpecific(SourceDestKeyHelper.getDst(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST))
+        when(tableDaoMock.getSpecific(SourceDestKeyHelper.getDstBinary(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST))
                 .thenReturn(srcDstDaoMock);
 
         multiTableMapCache.removeAuthenticationKey(EID_SOURCE_DEST_KEY_TYPE);
@@ -370,11 +370,11 @@ public class MultiTableMapCacheTest {
     @Test
     public void addDataTest_withSourceDestKey() {
         when(daoMock.getSpecific(VNI, SubKeys.VNI)).thenReturn(tableDaoMock);
-        when(tableDaoMock.getSpecific(SourceDestKeyHelper.getDst(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST))
+        when(tableDaoMock.getSpecific(SourceDestKeyHelper.getDstBinary(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST))
                 .thenReturn(srcDstDaoMock);
 
         multiTableMapCache.addData(EID_SOURCE_DEST_KEY_TYPE, SubKeys.RECORD, DUMMY_OBJECT);
-        verify(srcDstDaoMock).put(SourceDestKeyHelper.getSrc(NORMALIZED_SRCDST_EID),
+        verify(srcDstDaoMock).put(SourceDestKeyHelper.getSrcBinary(NORMALIZED_SRCDST_EID),
                 new MappingEntry<>(SubKeys.RECORD, DUMMY_OBJECT));
     }
 
@@ -395,9 +395,9 @@ public class MultiTableMapCacheTest {
     @Test
     public void getDataTest_withSourceDestKey() {
         when(daoMock.getSpecific(VNI, SubKeys.VNI)).thenReturn(tableDaoMock);
-        when(tableDaoMock.getSpecific(SourceDestKeyHelper.getDst(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST))
+        when(tableDaoMock.getSpecific(SourceDestKeyHelper.getDstBinary(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST))
                 .thenReturn(srcDstDaoMock);
-        when(srcDstDaoMock.getSpecific(SourceDestKeyHelper.getSrc(NORMALIZED_SRCDST_EID), SubKeys.RECORD))
+        when(srcDstDaoMock.getSpecific(SourceDestKeyHelper.getSrcBinary(NORMALIZED_SRCDST_EID), SubKeys.RECORD))
                 .thenReturn(DUMMY_OBJECT);
 
         assertEquals(DUMMY_OBJECT, multiTableMapCache.getData(EID_SOURCE_DEST_KEY_TYPE, SubKeys.RECORD));
@@ -429,11 +429,11 @@ public class MultiTableMapCacheTest {
     @Test
     public void removeDataTest_withSourceDestKey() {
         when(daoMock.getSpecific(VNI, SubKeys.VNI)).thenReturn(tableDaoMock);
-        when(tableDaoMock.getSpecific(SourceDestKeyHelper.getDst(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST))
+        when(tableDaoMock.getSpecific(SourceDestKeyHelper.getDstBinary(NORMALIZED_SRCDST_EID), SubKeys.LCAF_SRCDST))
                 .thenReturn(srcDstDaoMock);
 
         multiTableMapCache.removeData(EID_SOURCE_DEST_KEY_TYPE, SubKeys.RECORD);
-        verify(srcDstDaoMock).removeSpecific(SourceDestKeyHelper.getSrc(NORMALIZED_SRCDST_EID), SubKeys.RECORD);
+        verify(srcDstDaoMock).removeSpecific(SourceDestKeyHelper.getSrcBinary(NORMALIZED_SRCDST_EID), SubKeys.RECORD);
     }
 
     /**
index 5cab1364557d10ccaa2ce7d644f683a47b042614..bf46250a02b5693e0bb16aa37c89be246efbf9d4 100644 (file)
@@ -18,10 +18,6 @@ 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;
@@ -99,8 +95,7 @@ public class AuthenticationKeyDataListener implements DataTreeChangeListener<Aut
 
     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) {
+        if (LispAddressUtil.addressNeedsConversionToBinary(originalEid.getAddress())) {
             AuthenticationKeyBuilder akb = new AuthenticationKeyBuilder(authKey);
             akb.setEid(LispAddressUtil.convertToBinary(originalEid));
             return akb.build();
diff --git a/mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/authentication/LispMACAuthenticationTest.java b/mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/authentication/LispMACAuthenticationTest.java
new file mode 100644 (file)
index 0000000..a6b19d4
--- /dev/null
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.lispflowmapping.southbound.authentication;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.lispflowmapping.interfaces.lisp.ILispAuthentication;
+
+public class LispMACAuthenticationTest {
+
+    private static final String KEY = "pass";
+    private static final String ALGORITHM = LispKeyIDEnum.SHA1.getAuthenticationName();
+    private static final String MAP_REQUEST_PACKET =
+            "10 00 00 01 3d 8d 2a cd 39 c8 d6 " +
+            "08 00 01 01 02 03 04 00 01 7f 00 " +
+            "00 02 00 20 00 01 7f 00 00 01";
+
+    private static int macLength;
+    private static byte[] expectedAuthData;
+    private static ByteBuffer byteBuffer;
+    private static LispMACAuthentication lispMACAuthentication;
+
+    @Before
+    public void init() throws InvalidKeyException, NoSuchAlgorithmException {
+        final byte[] bytePacket = toBytePacket(MAP_REQUEST_PACKET);
+        macLength = Mac.getInstance(ALGORITHM).getMacLength();
+        lispMACAuthentication = new LispMACAuthentication(ALGORITHM);
+        byteBuffer = ByteBuffer.allocate(100);
+        byteBuffer.put(bytePacket);
+    }
+
+    /**
+     * Tests {@link LispMACAuthentication#validate} method with key = null.
+     */
+    @Test
+    public void validateTest_withNullKey() throws InvalidKeyException, NoSuchAlgorithmException {
+        expectedAuthData = getExpectedAuthData(ByteBuffer.wrap(byteBuffer.array()));
+        assertEquals(false, lispMACAuthentication.validate(byteBuffer, expectedAuthData, null));
+    }
+
+    /**
+     * Tests {@link LispMACAuthentication#validate} method with mapRegisterBuffer = null.
+     */
+    @Test
+    public void validateTest_withNullBuffer() throws InvalidKeyException, NoSuchAlgorithmException {
+        expectedAuthData = getExpectedAuthData(ByteBuffer.wrap(byteBuffer.array()));
+        assertEquals(true, lispMACAuthentication.validate(null, expectedAuthData, KEY));
+    }
+
+    /**
+     * Tests {@link LispMACAuthentication#validate} method.
+     */
+    @Test
+    public void validateTest() throws NoSuchAlgorithmException, InvalidKeyException {
+        expectedAuthData = getExpectedAuthData(ByteBuffer.wrap(insertEmptyByteArray(byteBuffer).array()));
+        assertEquals(true, lispMACAuthentication.validate(byteBuffer, expectedAuthData, KEY));
+    }
+
+    /**
+     * Tests {@link LispMACAuthentication#getAuthenticationLength} method.
+     */
+    @Test
+    public void getAuthenticationLengthTest() {
+        assertEquals(macLength, lispMACAuthentication.getAuthenticationLength());
+    }
+
+    /**
+     * Tests {@link LispMACAuthentication#getAlgorithm} method.
+     */
+    @Test
+    public void setAlgorithmTest() {
+        final String newAlgorithm = LispKeyIDEnum.SHA256.getAuthenticationName();
+        lispMACAuthentication.setAlgorithm(newAlgorithm);
+        assertEquals(newAlgorithm, lispMACAuthentication.getAlgorithm());
+    }
+
+    /**
+     * Tests {@link LispMACAuthentication#getAuthenticationData} method.
+     */
+    @Test
+    public void getAuthenticationDataTest() throws InvalidKeyException, NoSuchAlgorithmException {
+        final byte[] expectedResutl = getExpectedAuthData(byteBuffer);
+        final byte[] result = lispMACAuthentication.getAuthenticationData(ByteBuffer.wrap(byteBuffer.array()), KEY);
+        assertArrayEquals(expectedResutl, result);
+    }
+
+    /**
+     * Tests {@link LispMACAuthentication#getAuthenticationData} method with no array.
+     */
+    @Test
+    public void getAuthenticationDataTest_withNoArray() throws InvalidKeyException, NoSuchAlgorithmException {
+        byte[] expectedResutl = getExpectedAuthData(ByteBuffer.allocateDirect(0));
+        byte[] result = lispMACAuthentication.getAuthenticationData(ByteBuffer.allocateDirect(0), KEY);
+        assertArrayEquals(expectedResutl, result);
+    }
+
+    private static byte[] toBytePacket(String packetString) {
+        final String[] tokens = packetString.split("\\s+");
+        final ByteBuffer buffer = ByteBuffer.allocate(tokens.length);
+
+        for (String token : tokens) {
+            buffer.put((byte) Integer.parseInt(token, 16));
+        }
+
+        return buffer.array();
+    }
+
+    private static ByteBuffer insertEmptyByteArray(ByteBuffer buffer) {
+        buffer.position(ILispAuthentication.MAP_REGISTER_AND_NOTIFY_AUTHENTICATION_POSITION);
+        buffer.put(new byte[macLength]);
+        buffer.position(0);
+
+        return buffer;
+    }
+
+    private static byte[] getExpectedAuthData(ByteBuffer buffer) throws NoSuchAlgorithmException, InvalidKeyException {
+        final SecretKeySpec secretKey = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
+        final Mac mac = Mac.getInstance(ALGORITHM);
+        mac.init(secretKey);
+
+        return mac.doFinal(buffer.hasArray() ? buffer.array() : new byte[0]);
+    }
+}