summaryrefslogtreecommitdiffstats
path: root/lisp/lisp2vpp/src/test/java/io/fd/hc2vpp/lisp/translate/util/EidTranslatorTest.java
blob: 8884c7c1434351b448c08448beb2f48195d78a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.hc2vpp.lisp.translate.util;

import org.junit.Test;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6Afi;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.MacAfi;
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.yang.types.rev130715.MacAddress;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.adjacencies.grouping.adjacencies.adjacency.LocalEid;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.adjacencies.grouping.adjacencies.adjacency.RemoteEid;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.Eid;

import java.util.Arrays;

import static io.fd.hc2vpp.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.*;
import static org.junit.Assert.*;

public class EidTranslatorTest implements EidTranslator {

    private static final String MAC_STRING = "bf:bf:bf:bf:bf:bf";
    private static final Mac MAC_ADDRES = new MacBuilder().setMac(
            new MacAddress(MAC_STRING))
            .build();
    private static final byte[] MAC_ADDRESS_BYTES = {-65, -65, -65, -65, -65, -65};
    private static final byte[] IPV6_ADDRESS_BYTES = {32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 0, 1};
    private static final String IPV6_STRING = "2001:db8:a0b:12f0::1";
    private static final Ipv6 IPV6_ADDRESS = new Ipv6Builder().setIpv6(
            new Ipv6Address(IPV6_STRING))
            .build();
    private static final String IPV4_STRING = "192.168.2.1";
    private static final Ipv4 IPV4_ADDRESS = new Ipv4Builder().setIpv4(
            new Ipv4Address(IPV4_STRING))
            .build();
    private static final byte[] IPV_ADDRESS_BYTES = {-64, -88, 2, 1};

    @Test
    public void testGetEidType() {
        assertEquals(IPV4, getEidType(
                new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.EidBuilder()
                        .setAddress(IPV4_ADDRESS).build()));

        assertEquals(IPV6, getEidType(
                new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.EidBuilder()
                        .setAddress(IPV6_ADDRESS).build()));

        assertEquals(MAC, getEidType(
                new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.EidBuilder()
                        .setAddress(MAC_ADDRES).build()));
    }

    @Test
    public void testGetPrefixLength() {
        assertEquals(32, getPrefixLength(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.local.mapping.EidBuilder()
                .setAddress(IPV4_ADDRESS).build()));
        assertEquals(-128, getPrefixLength(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.local.mapping.EidBuilder()
                .setAddress(IPV6_ADDRESS).build()));
        assertEquals(0, getPrefixLength(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.local.mapping.EidBuilder()
                .setAddress(MAC_ADDRES).build()));
    }

    @Test
    public void testGetArrayAsEidRemoteIpv4() {
        final Eid eid = getArrayAsEidRemote(IPV4, IPV_ADDRESS_BYTES, 10);
        assertEquals(IPV4_STRING, ((Ipv4) eid.getAddress()).getIpv4().getValue());
        assertEquals(10, eid.getVirtualNetworkId().getValue().intValue());
        assertEquals(Ipv4Afi.class, eid.getAddressType());
    }

    @Test
    public void testGetArrayAsEidRemoteIpv6() {
        final Eid eid = getArrayAsEidRemote(IPV6, IPV6_ADDRESS_BYTES, 12);
        assertEquals(IPV6_STRING, ((Ipv6) eid.getAddress()).getIpv6().getValue());
        assertEquals(12, eid.getVirtualNetworkId().getValue().intValue());
        assertEquals(Ipv6Afi.class, eid.getAddressType());
    }

    @Test
    public void testGetArrayAsEidRemoteMac() {
        final Eid eid = getArrayAsEidRemote(MAC, MAC_ADDRESS_BYTES, 13);
        assertEquals(MAC_STRING, ((Mac) eid.getAddress()).getMac().getValue());
        assertEquals(13, eid.getVirtualNetworkId().getValue().intValue());
        assertEquals(MacAfi.class, eid.getAddressType());
    }

    @Test
    public void testGetArrayAsLocalEidIpv4() {
        final LocalEid eid = getArrayAsLocalEid(IPV4, IPV_ADDRESS_BYTES, 10);
        assertEquals(IPV4_STRING, ((Ipv4) eid.getAddress()).getIpv4().getValue());
        assertEquals(10, eid.getVirtualNetworkId().getValue().intValue());
        assertEquals(Ipv4Afi.class, eid.getAddressType());
    }

    @Test
    public void testGetArrayAsLocalEidIpv6() {
        final LocalEid eid = getArrayAsLocalEid(IPV6, IPV6_ADDRESS_BYTES, 12);
        assertEquals(IPV6_STRING, ((Ipv6) eid.getAddress()).getIpv6().getValue());
        assertEquals(12, eid.getVirtualNetworkId().getValue().intValue());
        assertEquals(Ipv6Afi.class, eid.getAddressType());
    }

    @Test
    public void testGetArrayAsLocalEidMac() {
        final LocalEid eid = getArrayAsLocalEid(MAC, MAC_ADDRESS_BYTES, 13);
        assertEquals(MAC_STRING, ((Mac) eid.getAddress()).getMac().getValue());
        assertEquals(13, eid.getVirtualNetworkId().getValue().intValue());
        assertEquals(MacAfi.class, eid.getAddressType());
    }

    @Test
    public void testGetArrayAsRemoteEidIpv4() {
        final RemoteEid eid = getArrayAsRemoteEid(IPV4, IPV_ADDRESS_BYTES, 10);
        assertEquals(IPV4_STRING, ((Ipv4) eid.getAddress()).getIpv4().getValue());
        assertEquals(10, eid.getVirtualNetworkId().getValue().intValue());
        assertEquals(Ipv4Afi.class, eid.getAddressType());
    }

    @Test
    public void testGetArrayAsRemoteEidIpv6() {
        final RemoteEid eid = getArrayAsRemoteEid(IPV6, IPV6_ADDRESS_BYTES, 12);
        assertEquals(IPV6_STRING, ((Ipv6) eid.getAddress()).getIpv6().getValue());
        assertEquals(12, eid.getVirtualNetworkId().getValue().intValue());
        assertEquals(Ipv6Afi.class, eid.getAddressType());
    }

    @Test
    public void testGetArrayAsRemoteEidMac() {
        final RemoteEid eid = getArrayAsRemoteEid(MAC, MAC_ADDRESS_BYTES, 13);
        assertEquals(MAC_STRING, ((Mac) eid.getAddress()).getMac().getValue());
        assertEquals(13, eid.getVirtualNetworkId().getValue().intValue());
        assertEquals(MacAfi.class, eid.getAddressType());
    }

    @Test
    public void testGetArrayAsEidStringIpv4() {
        assertEquals(IPV4_STRING, getArrayAsEidString(IPV4, IPV_ADDRESS_BYTES));
    }

    @Test
    public void testGetArrayAsEidStringIpv6() {
        assertEquals(IPV6_STRING, getArrayAsEidString(IPV6, IPV6_ADDRESS_BYTES));
    }

    @Test
    public void testGetArrayAsEidStringMac() {
        assertEquals(MAC_STRING, getArrayAsEidString(MAC, MAC_ADDRESS_BYTES));
    }

    @Test
    public void testResolveByteArrayIpv4() {
        assertTrue(Arrays.equals(IPV_ADDRESS_BYTES, resolveByteArray(IPV4, IPV4_ADDRESS)));
    }

    @Test
    public void testResolveByteArrayIpv6() {
        assertTrue(Arrays.equals(IPV6_ADDRESS_BYTES, resolveByteArray(IPV6, IPV6_ADDRESS)));
    }

    @Test
    public void testResolveByteArrayMac() {
        assertTrue(Arrays.equals(MAC_ADDRESS_BYTES, resolveByteArray(MAC, MAC_ADDRES)));
    }

    @Test
    public void testCompareAddressesPositive() {
        assertTrue(compareAddresses(MAC_ADDRES, MAC_ADDRES));
        assertTrue(compareAddresses(IPV4_ADDRESS, IPV4_ADDRESS));
        assertTrue(compareAddresses(IPV6_ADDRESS, IPV6_ADDRESS));
    }

    @Test
    public void testCompareAddressesNegative() {
        assertFalse(compareAddresses(MAC_ADDRES, IPV6_ADDRESS));
        assertFalse(compareAddresses(IPV4_ADDRESS, IPV6_ADDRESS));
        assertFalse(compareAddresses(IPV4_ADDRESS, MAC_ADDRES));
    }
}