/*
- * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
+ * Copyright (c) 2014, 2017 Contextream, 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,
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IetfInetUtil;
-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.rev130715.IetfInetUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
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.InstanceId;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
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.source.dest.key.SourceDestKeyBuilder;
+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.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.Ipv6Binary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6PrefixBinary;
import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class MaskUtil {
private static final Logger LOG = LoggerFactory.getLogger(MaskUtil.class);
- private static final short IPV4_MAX_MASK = 32;
- private static final short IPV6_MAX_MASK = 128;
+ public static final short IPV4_MAX_MASK = 32;
+ public static final short IPV6_MAX_MASK = 128;
// Utility class, should not be instantiated
private MaskUtil() {
}
public static boolean isMaskable(Address address) {
- if (address instanceof Ipv4Prefix || address instanceof Ipv6Prefix || address instanceof SourceDestKey) {
+ if (address instanceof Ipv4Prefix || address instanceof Ipv6Prefix
+ || address instanceof Ipv4PrefixBinary || address instanceof Ipv6PrefixBinary
+ || address instanceof SourceDestKey) {
return true;
} else if (address instanceof InstanceId) {
return isMaskable(((InstanceId)address).getInstanceId().getAddress());
return false;
}
- private static final int slashPosition(final String prefix) {
+ private static int slashPosition(final String prefix) {
final int slash = prefix.lastIndexOf('/');
Preconditions.checkArgument(slash >= 0, "Argument %s does not contain a slash", prefix);
return slash;
}
- private static String getPrefixAddress(final String prefix) {
+ public static String getPrefixAddress(final String prefix) {
return prefix.substring(0, slashPosition(prefix));
}
- private static String getPrefixMask(final String prefix) {
+ public static String getPrefixMask(final String prefix) {
return prefix.substring(slashPosition(prefix) + 1);
}
public static Eid normalize(Eid eid, short mask) {
Address address = eid.getAddress();
try {
- if (address instanceof Ipv4Prefix) {
+ if (address instanceof Ipv4PrefixBinary) {
+ byte[] addr = ((Ipv4PrefixBinary) address).getIpv4AddressBinary().getValue();
+ return LispAddressUtil.asIpv4PrefixBinaryEid(eid, normalizeByteArray(addr, mask), mask);
+ } else if (address instanceof Ipv6PrefixBinary) {
+ byte[] addr = ((Ipv6PrefixBinary) address).getIpv6AddressBinary().getValue();
+ return LispAddressUtil.asIpv6PrefixBinaryEid(eid, normalizeByteArray(addr, mask), mask);
+ } else if (address instanceof Ipv4Prefix) {
final String addr = getPrefixAddress(((Ipv4Prefix)address).getIpv4Prefix().getValue());
InetAddress normalized = normalizeIP(InetAddresses.forString(addr), mask);
return LispAddressUtil.asIpv4PrefixEid(eid, (Inet4Address)normalized, mask);
public static Eid normalize(Eid eid) {
Address address = eid.getAddress();
try {
- if (address instanceof Ipv4Prefix) {
+ if (address instanceof Ipv4PrefixBinary) {
+ byte[] addr = ((Ipv4PrefixBinary) address).getIpv4AddressBinary().getValue();
+ short mask = ((Ipv4PrefixBinary) address).getIpv4MaskLength().toJava();
+ return LispAddressUtil.asIpv4PrefixBinaryEid(eid, normalizeByteArray(addr, mask), mask);
+ } else if (address instanceof Ipv6PrefixBinary) {
+ byte[] addr = ((Ipv6PrefixBinary) address).getIpv6AddressBinary().getValue();
+ short mask = ((Ipv6PrefixBinary) address).getIpv6MaskLength().toJava();
+ return LispAddressUtil.asIpv6PrefixBinaryEid(eid, normalizeByteArray(addr, mask), mask);
+ } else if (address instanceof Ipv4Prefix) {
String[] v4prefix = splitPrefix(((Ipv4Prefix)address).getIpv4Prefix().getValue());
short mask = Short.parseShort(v4prefix[1]);
InetAddress normalized = normalizeIP(InetAddresses.forString(v4prefix[0]), mask);
return normalizeSrcDst(eid);
} else if (address instanceof ServicePath) {
// Build new Service Path eid with service index set to 0
- long spi = ((ServicePath) address).getServicePath().getServicePathId().getValue();
- long vni = eid.getVirtualNetworkId() != null ? eid.getVirtualNetworkId().getValue() : -1;
+ long spi = ((ServicePath) address).getServicePath().getServicePathId().getValue().toJava();
+ long vni = eid.getVirtualNetworkId() != null ? eid.getVirtualNetworkId().getValue().toJava() : -1;
return LispAddressUtil.asServicePathEid(vni, spi, (short)0);
}
} catch (UnknownHostException e) {
}
private static IpPrefix normalizeIpPrefix(IpPrefix address) throws UnknownHostException {
- String[] prefix = splitPrefix(String.valueOf(address.getValue()));
+ String[] prefix = splitPrefix(address.stringValue());
short mask = Short.parseShort(prefix[1]);
InetAddress normalizedAddress = normalizeIP(InetAddresses.forString(prefix[0]), mask);
}
private static InetAddress normalizeIP(InetAddress address, int maskLength) throws UnknownHostException {
- ByteBuffer byteRepresentation = ByteBuffer.wrap(address.getAddress());
- byte b = (byte) 0xff;
+ return InetAddress.getByAddress(normalizeByteArray(address.getAddress(), (short) maskLength));
+ }
+
+ public static byte[] normalizeByteArray(byte[] address, short maskLength) {
+ ByteBuffer byteRepresentation = ByteBuffer.wrap(address);
+ byte byteMask = (byte) 0xff;
int mask = maskLength;
for (int i = 0; i < byteRepresentation.array().length; i++) {
if (mask >= 8) {
- byteRepresentation.put(i, (byte) (b & byteRepresentation.get(i)));
+ byteRepresentation.put(i, (byte) (byteMask & byteRepresentation.get(i)));
} else if (mask > 0) {
- byteRepresentation.put(i, (byte) ((byte) (b << (8 - mask)) & byteRepresentation.get(i)));
+ byteRepresentation.put(i, (byte) ((byte) (byteMask << (8 - mask)) & byteRepresentation.get(i)));
} else {
byteRepresentation.put(i, (byte) (0 & byteRepresentation.get(i)));
}
mask -= 8;
}
- return InetAddress.getByAddress(byteRepresentation.array());
+ return byteRepresentation.array();
}
public static int getMaxMask(Address address) {
- if (address instanceof Ipv4 || address instanceof Ipv4Prefix) {
+ if (address instanceof Ipv4 || address instanceof Ipv4Prefix || address instanceof Ipv4Binary
+ || address instanceof Ipv4PrefixBinary) {
return IPV4_MAX_MASK;
- } else if (address instanceof Ipv6 || address instanceof Ipv6Prefix) {
+ } else if (address instanceof Ipv6 || address instanceof Ipv6Prefix || address instanceof Ipv4Binary
+ || address instanceof Ipv6PrefixBinary) {
return IPV6_MAX_MASK;
} else {
return -1;
}
}
- public static short getMaskForAddress(SimpleAddress address) {
- if (address.getIpPrefix() == null) {
- return -1;
- }
- return getMaskForIpPrefix(address.getIpPrefix());
- }
-
private static String getIpPrefixString(IpPrefix prefix) {
if (prefix.getIpv4Prefix() != null) {
return prefix.getIpv4Prefix().getValue();
return getPrefixAddress(prefix.getIpv6Prefix().getValue());
}
+ public static short getMaskForAddress(SimpleAddress address) {
+ if (address.getIpPrefix() == null) {
+ return -1;
+ }
+ return getMaskForIpPrefix(address.getIpPrefix());
+ }
+
public static short getMaskForAddress(Address address) {
if (address instanceof Ipv4) {
return IPV4_MAX_MASK;
} else if (address instanceof Ipv6) {
return IPV6_MAX_MASK;
+ } else if (address instanceof Ipv4Binary) {
+ return IPV4_MAX_MASK;
+ } else if (address instanceof Ipv6Binary) {
+ return IPV6_MAX_MASK;
} else if (address instanceof Ipv4Prefix) {
- return Short.parseShort(getPrefixMask(((Ipv4Prefix)address).getIpv4Prefix().getValue()));
+ return Short.parseShort(getPrefixMask(((Ipv4Prefix) address).getIpv4Prefix().getValue()));
} else if (address instanceof Ipv6Prefix) {
- return Short.parseShort(getPrefixMask(((Ipv6Prefix)address).getIpv6Prefix().getValue()));
+ return Short.parseShort(getPrefixMask(((Ipv6Prefix) address).getIpv6Prefix().getValue()));
} else if (address instanceof InstanceId) {
return getMaskForAddress(((InstanceId)address).getInstanceId().getAddress());
+ } else if (address instanceof Ipv4PrefixBinary) {
+ return ((Ipv4PrefixBinary) address).getIpv4MaskLength().toJava();
+ } else if (address instanceof Ipv6PrefixBinary) {
+ return ((Ipv6PrefixBinary) address).getIpv6MaskLength().toJava();
}
return -1;
}