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;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.net.InetAddresses;
/**
* Contains methods for:
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) {
}
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);
}
}
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);
}
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);
}
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()));
}
}
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;
}
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;
}
*/
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;
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;
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.";
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";
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());
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.";
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;
+ }
}
// 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);
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;
}
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;
boolean authFailed = false;
boolean mappingUpdated = false;
boolean merge = ConfigIni.getInstance().mappingMergeIsSet() && mapRegister.isMergeEnabled();
- MappingAuthkey authkey = null;
Set<SubscriberRLOC> subscribers = null;
MappingRecord oldMapping;
// 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);
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;
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();
*/
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;
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;
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!");
}
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!");
}
}
}
}
+
+ 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;
+ }
}
import com.google.common.collect.Lists;
+import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
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;
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);
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.
*/
}
/**
- * 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() {
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;
return TRANSPORT_ADDRESS_1.equals(notifyTransportAddress)
|| TRANSPORT_ADDRESS_2.equals(notifyTransportAddress);
}
-
}
}
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);
}
/**
.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 ------------------------
// 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);
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;
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;
// 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));
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;
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;
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();
}
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;
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();
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) {
}
}
+ 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)) {
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());
--- /dev/null
+/*
+ * 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();
+ }
+}
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));
// 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);
}
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 {
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));
}
// 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 {
// 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
// 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() {
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));
}
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);
}
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);
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);
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();
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);
}
@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);
*/
@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);
*/
@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))
@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);
@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));
}
@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));
@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);
}
/**
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;
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();
--- /dev/null
+/*
+ * 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]);
+ }
+}