+ private static Ipv4PrefixBinary convertToBinary(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+ .lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix prefix) {
+ Ipv4PrefixBinaryBuilder pb = new Ipv4PrefixBinaryBuilder();
+ byte[] address = InetAddresses.forString(MaskUtil.getAddressStringForIpv4Prefix(prefix)).getAddress();
+ pb.setIpv4AddressBinary(new Ipv4AddressBinary(address));
+ pb.setIpv4MaskLength(fromJava(MaskUtil.getMaskForAddress(prefix)));
+ return pb.build();
+ }
+
+ private static Ipv6PrefixBinary convertToBinary(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+ .lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix prefix) {
+ Ipv6PrefixBinaryBuilder pb = new Ipv6PrefixBinaryBuilder();
+ byte[] address = InetAddresses.forString(MaskUtil.getAddressStringForIpv6Prefix(prefix)).getAddress();
+ pb.setIpv6AddressBinary(new Ipv6AddressBinary(address));
+ pb.setIpv6MaskLength(fromJava(MaskUtil.getMaskForAddress(prefix)));
+ return pb.build();
+ }
+
+ private static Ipv4Binary convertToBinary(Ipv4 address) {
+ byte[] addr = InetAddresses.forString(address.getIpv4().getValue()).getAddress();
+ return new Ipv4BinaryBuilder().setIpv4Binary(new Ipv4AddressBinary(addr)).build();
+ }
+
+ private static Ipv6Binary convertToBinary(Ipv6 address) {
+ byte[] addr = InetAddresses.forString(address.getIpv6().getValue()).getAddress();
+ return new Ipv6BinaryBuilder().setIpv6Binary(new Ipv6AddressBinary(addr)).build();
+ }
+
+ private static Pair<LispAddressFamily, Address> convertToBinary(Address addr) {
+ final Address convAddr;
+ final LispAddressFamily convType;
+ 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.VALUE;
+ } 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.VALUE;
+ } else if (addr instanceof Ipv4) {
+ convAddr = convertToBinary((Ipv4) addr);
+ convType = Ipv4BinaryAfi.VALUE;
+ } else if (addr instanceof Ipv6) {
+ convAddr = convertToBinary((Ipv6) addr);
+ convType = Ipv6BinaryAfi.VALUE;
+ } else {
+ convAddr = null;
+ convType = null;
+ }
+ return new ImmutablePair<>(convType, convAddr);
+ }
+
+ public static Eid convertToBinary(Eid eid) {
+ Pair<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 Rloc convertToBinary(Rloc rloc) {
+ Pair<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();
+ }
+
+ /**
+ * Converts the {@link InetAddress} into Ipv4 or Ipv6 {@link IpAddressBinary}. If null parameter is passed, method
+ * returns the Ipv4 loopback address (127.0.0.1).
+ *
+ * @param inetAddress Any Ipv4 or Ipv6 InetAddress.
+ * @return The converted Ipv4 or Ipv6 IpAddressBinary, or Ipv4 loopback address (127.0.0.1) if null is passed.
+ */
+ public static IpAddressBinary addressBinaryFromInet(InetAddress inetAddress) {
+ if (inetAddress == null) {
+ inetAddress = Inet4Address.getLoopbackAddress();
+ }
+
+ if (inetAddress instanceof Inet4Address) {
+ return new IpAddressBinary(new Ipv4AddressBinary(inetAddress.getAddress()));
+ } else if (inetAddress instanceof Inet6Address) {
+ return new IpAddressBinary(new Ipv6AddressBinary(inetAddress.getAddress()));
+ }
+ return null;
+ }
+
+ public static IpAddressBinary addressBinaryFromAddress(Address address) {
+ if (address instanceof Ipv4Binary) {
+ return new IpAddressBinary(((Ipv4Binary) address).getIpv4Binary());
+ } else if (address instanceof Ipv6Binary) {
+ return new IpAddressBinary(((Ipv6Binary) address).getIpv6Binary());
+ }
+ return null;
+ }
+
+ 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().toJava())).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().toJava())).build();
+ }
+
+ private static Ipv4 convertFromBinary(Ipv4Binary address) {
+ return new Ipv4Builder().setIpv4(IetfInetUtil.INSTANCE.ipv4AddressFor(address.getIpv4Binary().getValue()))
+ .build();
+ }
+
+ private static Ipv6 convertFromBinary(Ipv6Binary address) {
+ return new Ipv6Builder().setIpv6(IetfInetUtil.INSTANCE.ipv6AddressFor(address.getIpv6Binary().getValue()))
+ .build();
+ }
+
+ private static Pair<LispAddressFamily, Address> convertFromBinary(Address addr) {
+ final Address convAddr;
+ final LispAddressFamily convType;
+ if (addr instanceof Ipv4PrefixBinary) {
+ convAddr = convertFromBinary((Ipv4PrefixBinary) addr);
+ convType = Ipv4PrefixAfi.VALUE;
+ } else if (addr instanceof Ipv6PrefixBinary) {
+ convAddr = convertFromBinary((Ipv6PrefixBinary) addr);
+ convType = Ipv6PrefixAfi.VALUE;
+ } else if (addr instanceof Ipv4Binary) {
+ convAddr = convertFromBinary((Ipv4Binary) addr);
+ convType = Ipv4Afi.VALUE;
+ } else if (addr instanceof Ipv6Binary) {
+ convAddr = convertFromBinary((Ipv6Binary) addr);
+ convType = Ipv6Afi.VALUE;
+ } else {
+ convAddr = null;
+ convType = null;
+ }
+ return new ImmutablePair<>(convType, convAddr);
+ }
+
+ public static Eid convertFromBinary(Eid eid) {
+ Pair<LispAddressFamily, Address> converted = convertFromBinary(eid.getAddress());
+ if (converted.getRight() == null) {
+ return eid;
+ }
+ EidBuilder eb = new EidBuilder(eid);
+ eb.setAddressType(converted.getLeft());
+ eb.setAddress(converted.getRight());
+ return eb.build();
+ }
+
+ public static Rloc convertFromBinary(Rloc rloc) {
+ Pair<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[] one, byte[] two) {
+ if (one.length < two.length) {