+
+ /**
+ * Tests {@link LispAddressUtil#asSrcDst(String, String, int, int)} method.
+ */
+ @Test
+ public void asSrcDst() {
+ final SourceDestKey sourceDestKey = LispAddressUtil.asSrcDst(IPV4_ADDRESS_VALUE_TEST,
+ IPV6_ADDRESS_VALUE_TEST, MASK_OK_TEST, MASK_OK_TEST);
+ assertNotNull(sourceDestKey);
+ assertEquals(SIMPLE_ADDRESS_WITH_IP_PREFIX_IPV4_TEST, sourceDestKey.getSource());
+ assertEquals(SIMPLE_ADDRESS_WITH_IP_PREFIX_IPV6_TEST, sourceDestKey.getDest());
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#asSrcDstEid(String, String, int, int, int)} method.
+ */
+ @Test
+ public void asSrcDstEid_addressesAsString() {
+ final Eid srcDstEid = LispAddressUtil.asSrcDstEid(IPV4_ADDRESS_VALUE_TEST,
+ IPV6_ADDRESS_VALUE_TEST, MASK_OK_TEST, MASK_OK_TEST, INSTANCE_ID_TYPE_VALUE_SHORT_TEST);
+ assertNotNull(srcDstEid);
+ assertEquals(SourceDestKeyLcaf.class, srcDstEid.getAddressType());
+ final SourceDestKey sourceDestKey = ((org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns
+ .yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey) srcDstEid.getAddress())
+ .getSourceDestKey();
+ assertNotNull(sourceDestKey);
+ assertEquals(SIMPLE_ADDRESS_WITH_IP_PREFIX_IPV4_TEST, sourceDestKey.getSource());
+ assertEquals(SIMPLE_ADDRESS_WITH_IP_PREFIX_IPV6_TEST, sourceDestKey.getDest());
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#asSrcDstEid(SourceDestKey, InstanceIdType)} method.
+ */
+ @Test
+ public void asSrcDstEid_addressesAsSrcDstKey() {
+ final SourceDestKey expectedSourceDestKey = new SourceDestKeyBuilder().setSource
+ (SIMPLE_ADDRESS_WITH_IP_PREFIX_IPV4_TEST).setDest(SIMPLE_ADDRESS_WITH_IP_PREFIX_IPV6_TEST).build();
+ final Eid srcDstEid = LispAddressUtil.asSrcDstEid(expectedSourceDestKey, INSTANCE_ID_TYPE_TEST);
+ assertNotNull(srcDstEid);
+ assertEquals(SourceDestKeyLcaf.class, srcDstEid.getAddressType());
+ final SourceDestKey testedSourceDestKey = ((org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns
+ .yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey) srcDstEid.getAddress())
+ .getSourceDestKey();
+ assertNotNull(testedSourceDestKey);
+ assertEquals(SIMPLE_ADDRESS_WITH_IP_PREFIX_IPV4_TEST, testedSourceDestKey.getSource());
+ assertEquals(SIMPLE_ADDRESS_WITH_IP_PREFIX_IPV6_TEST, testedSourceDestKey.getDest());
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#asSrcDstEid(SourceDestKey, InstanceIdType)} method.
+ */
+ @Test
+ public void asTeLcafRloc() {
+ final List<IpAddress> ipAddresses = Arrays.asList(IP_ADDRESS_OBJECT_WITH_IPV4_TEST, IP_ADDRESS_OBJECT_WITH_IPV6_TEST);
+ final Rloc rloc = LispAddressUtil.asTeLcafRloc(ipAddresses);
+ assertNotNull(rloc);
+ assertEquals(ExplicitLocatorPathLcaf.class, rloc.getAddressType());
+ final List<Hop> hops = ((ExplicitLocatorPath) rloc.getAddress()).getExplicitLocatorPath().getHop();
+ assertEquals(hops.size(), ipAddresses.size());
+
+ for(IpAddress ipAddress : ipAddresses) {
+ assertTrue("Ip address "+ipAddress+"should be part of hops list.",isIpAddressInHops(ipAddress, hops));
+ }
+
+ for(Hop hop : hops) {
+ final Hop.LrsBits lrsBits = hop.getLrsBits();
+ assertFalse(lrsBits.isLookup());
+ assertFalse(lrsBits.isRlocProbe());
+ assertFalse(lrsBits.isStrict());
+
+ final IpAddress ipAddressFromHop = hop.getAddress().getIpAddress();
+ assertNotNull(ipAddressFromHop);
+ }
+ }
+
+ private boolean isIpAddressInHops(final IpAddress ipAddress, final List<Hop> hops) {
+ for (Hop hop : hops) {
+ if (hop.getAddress().getIpAddress().equals(ipAddress)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#asLocatorRecords(List)} method.
+ */
+ @Test
+ public void asLocatorRecords() {
+ final List<Rloc> expectedRlocs = Arrays.asList(LispAddressUtil.toRloc(IPV4_ADDRESS_TEST), LispAddressUtil.toRloc
+ (IPV6_ADDRESS_TEST));
+ final List<LocatorRecord> locatorRecords = LispAddressUtil.asLocatorRecords(expectedRlocs);
+
+ assertEquals(expectedRlocs.size(), locatorRecords.size());
+
+ for (Rloc rloc : expectedRlocs) {
+ assertTrue("Rloc " + rloc + " should be part of " +
+ "locator records list list", isRlocInLocatorRecords(locatorRecords, rloc));
+ }
+
+ for (LocatorRecord locatorRecord : locatorRecords) {
+ assertFalse(locatorRecord.isLocalLocator());
+ assertFalse(locatorRecord.isRlocProbed());
+ assertTrue(locatorRecord.isRouted());
+ assertTrue(1 == locatorRecord.getWeight());
+ assertTrue(1 == locatorRecord.getPriority());
+ assertTrue(1 == locatorRecord.getMulticastWeight());
+ assertTrue(1 == locatorRecord.getMulticastPriority());
+ assertEquals("SFC_LISP", locatorRecord.getLocatorId());
+ }
+ }
+
+ private boolean isRlocInLocatorRecords(final List<LocatorRecord> locatorRecords, final Rloc rloc) {
+ for (LocatorRecord locatorRecord : locatorRecords) {
+ if (locatorRecord.getRloc().equals(rloc)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#getNoAddressEid()} method.
+ */
+ @Test
+ public void getNoAddressEid() {
+ final Eid noAddressEid = LispAddressUtil.getNoAddressEid();
+ assertEquals(NoAddressAfi.class, noAddressEid.getAddressType());
+ assertNull(noAddressEid.getVirtualNetworkId());
+ assertTrue(((NoAddress) noAddressEid.getAddress()).isNoAddress());
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#ipAddressToInet(Address)} method with ipv4 value.
+ */
+ @Test
+ public void ipAddressToInet_ipv4() {
+ final Ipv4 expectedIpv4 = new Ipv4Builder().setIpv4(IPV4_ADDRESS_TEST).build();
+ final InetAddress testedAddress = LispAddressUtil.ipAddressToInet(expectedIpv4);
+ assertEquals(IPV4_ADDRESS_TEST.getValue(), testedAddress.getHostAddress());
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#ipAddressToInet(Address)} method with ipv6 value.
+ */
+ @Test
+ public void ipAddressToInet_ipv6() {
+ final Ipv6 expectedIpv6 = new Ipv6Builder().setIpv6(IPV6_ADDRESS_TEST).build();
+ final InetAddress testedAddress = LispAddressUtil.ipAddressToInet(expectedIpv6);
+ assertEquals(IPV6_ADDRESS_TEST.getValue(), testedAddress.getHostAddress());
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#ipAddressToInet(Address)} method with mac value.
+ */
+ @Test
+ public void ipAddressToInet_other() {
+ final InetAddress testedAddress = LispAddressUtil.ipAddressToInet(new MacBuilder().build());
+ assertNull(testedAddress);
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#compareInetAddresses(InetAddress, InetAddress)} with all possible combination
+ * of input parameters to reach 100 % coverage.
+ */
+ @Test
+ public void compareInetAddresses() throws UnknownHostException {
+ final InetAddress inetIpv4A = Inet4Address.getByAddress(IPV4_ADDRESS_BYTES_A_TEST);
+ final InetAddress inetIpv6A = Inet6Address.getByAddress(IPV6_ADDRESS_BYTES_A_TEST);
+ final InetAddress inetIpv4B = Inet4Address.getByAddress(IPV4_ADDRESS_BYTES_B_TEST);
+ final InetAddress inetIpv6B = Inet6Address.getByAddress(IPV6_ADDRESS_BYTES_B_TEST);
+
+ int comparisonResult = LispAddressUtil.compareInetAddresses(inetIpv4A, inetIpv6A);
+ assertEquals(-1, comparisonResult);
+
+ comparisonResult = LispAddressUtil.compareInetAddresses(inetIpv6A, inetIpv4A);
+ assertEquals(1, comparisonResult);
+
+ comparisonResult = LispAddressUtil.compareInetAddresses(inetIpv4A, inetIpv4B);
+ assertEquals(-1, comparisonResult);
+
+ comparisonResult = LispAddressUtil.compareInetAddresses(inetIpv4B, inetIpv4A);
+ assertEquals(1, comparisonResult);
+
+ comparisonResult = LispAddressUtil.compareInetAddresses(inetIpv4B, inetIpv4B);
+ assertEquals(0, comparisonResult);
+
+ // remove this ignore once https://git.opendaylight.org/gerrit/#/c/35682/1 will be merged */
+/*
+ comparisonResult = LispAddressUtil.compareInetAddresses(inetIpv6A, inetIpv6B);
+ assertEquals(-1, comparisonResult);
+
+ comparisonResult = LispAddressUtil.compareInetAddresses(inetIpv6B, inetIpv6A);
+ assertEquals(1, comparisonResult);
+
+ comparisonResult = LispAddressUtil.compareInetAddresses(inetIpv6B, inetIpv6B);
+ assertEquals(0, comparisonResult);
+*/
+
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#asServicePathEid(long, long, short)} method.
+ */
+ @Test
+ public void asServicePathEid() {
+ final Eid eid = LispAddressUtil.asServicePathEid(INSTANCE_ID_TYPE_VALUE_TEST, SERVICE_PATH_ID_TEST,
+ SERVICE_INDEX_TEST);
+
+ assertNotNull(eid);
+ assertEquals(ServicePathLcaf.class, eid.getAddressType());
+ assertEquals(INSTANCE_ID_TYPE_TEST, eid.getVirtualNetworkId());
+ ServicePath servicePath = ((org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.
+ address.types.rev151105.lisp.address.address.ServicePath) eid.getAddress()).getServicePath();
+ assertNotNull(servicePath);
+ assertEquals(SERVICE_INDEX_TEST, servicePath.getServiceIndex());
+ assertEquals(SERVICE_PATH_ID_TEST, servicePath.getServicePathId().getValue());
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#toIpPrefixEid(IpAddress, int)} method.
+ */
+ @Ignore /* remove when https://git.opendaylight.org/gerrit/#/c/35681/2 will be merged */
+ @Test
+ public void toIpPrefixEid() throws UnknownHostException {
+ final Eid eid = LispAddressUtil.toIpPrefixEid(IP_ADDRESS_OBJECT_WITH_IPV4_TEST, INSTANCE_ID_TYPE_VALUE_SHORT_TEST);
+ verifyEidContainsIpPrefix(eid, MASK_OK_DEFAULT_IPV4_TEST);
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#toEidNoVni(IpPrefix)} method.
+ */
+ @Test
+ public void toEidNoVni() {
+ final Eid eid = LispAddressUtil.toEidNoVni(IP_ADDRESS_PREFIX_WITH_IPV4_TEST);
+ verifyEidContainsIpPrefix(eid, MASK_OK_TEST);
+ }
+
+ public void verifyEidContainsIpPrefix(final Eid eid, final short mask) {
+ final Ipv4Prefix expectedPrefix = new Ipv4Prefix(IPV4_ADDRESS_VALUE_TEST + "/" + mask);
+ assertNotNull(eid);
+ assertTrue(eid.getAddress() instanceof org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp
+ .address.types.rev151105.lisp.address.address.Ipv4Prefix);
+ final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
+ .address.Ipv4Prefix address = (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp
+ .address.types.rev151105.lisp.address.address.Ipv4Prefix) eid.getAddress();
+ assertEquals(expectedPrefix, address.getIpv4Prefix());
+ }
+
+ /**
+ * Tests {@link LispAddressUtil#asEid(SimpleAddress, InstanceIdType)} method.
+ */
+ @Test
+ public void asEid() {
+ final Eid eid = LispAddressUtil.asEid(SIMPLE_ADDRESS_A_TEST, INSTANCE_ID_TYPE_TEST);
+ assertNotNull(eid);
+ assertEquals(Ipv4Afi.class, eid.getAddressType());
+ final Ipv4 address = (Ipv4) eid.getAddress();
+ assertEquals(SIMPLE_ADDRESS_A_TEST.getIpAddress().getIpv4Address(), address.getIpv4());
+ assertEquals(INSTANCE_ID_TYPE_TEST, eid.getVirtualNetworkId());
+ }
+