summaryrefslogtreecommitdiffstats
path: root/dhcp/dhcp-impl/src/test/java/io/fd/hc2vpp/dhcp/read/DhcpRelayCustomizerTest.java
blob: 193758bc8427451ab7062e9959326d7786c422cf (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
/*
 * Copyright (c) 2017 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.dhcp.read;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import io.fd.hc2vpp.common.test.read.InitializingListReaderCustomizerTest;
import io.fd.honeycomb.translate.read.ReadFailedException;
import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
import io.fd.vpp.jvpp.core.dto.DhcpProxyDetails;
import io.fd.vpp.jvpp.core.dto.DhcpProxyDetailsReplyDump;
import io.fd.vpp.jvpp.core.dto.DhcpProxyDump;
import io.fd.vpp.jvpp.core.types.DhcpServer;
import java.util.List;
import org.junit.Test;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.fib.table.management.rev180521.Ipv4;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.fib.table.management.rev180521.Ipv6;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.fib.table.management.rev180521.VniReference;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev180629.Dhcp;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev180629.dhcp.attributes.Relays;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev180629.dhcp.attributes.RelaysBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev180629.dhcp.attributes.relays.Relay;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev180629.dhcp.attributes.relays.RelayBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev180629.dhcp.attributes.relays.RelayKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev180629.relay.attributes.Server;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;

public class DhcpRelayCustomizerTest extends InitializingListReaderCustomizerTest<Relay, RelayKey, RelayBuilder> {
    private static InstanceIdentifier<Relays> RELAYS = InstanceIdentifier.create(Dhcp.class).child(Relays.class);
    private KeyedInstanceIdentifier<Relay, RelayKey> IP4_IID =
        RELAYS.child(Relay.class, new RelayKey(Ipv4.class, new VniReference(123L)));
    private KeyedInstanceIdentifier<Relay, RelayKey> IP6_IID =
        RELAYS.child(Relay.class, new RelayKey(Ipv6.class, new VniReference(321L)));
    public DhcpRelayCustomizerTest() {
        super(Relay.class, RelaysBuilder.class);
    }

    @Override
    protected ReaderCustomizer<Relay, RelayBuilder> initCustomizer() {
        return new DhcpRelayCustomizer(api);
    }

    @Override
    protected void setUp() throws Exception {
        final DhcpProxyDetailsReplyDump ip4 = new DhcpProxyDetailsReplyDump();
        final DhcpProxyDetails ip4Proxy = new DhcpProxyDetails();
        ip4Proxy.rxVrfId = 123;
        ip4Proxy.dhcpSrcAddress = new byte[] {1, 2, 3, 4};
        final DhcpServer ip4server1 = new DhcpServer();
        ip4server1.serverVrfId = 11;
        ip4server1.dhcpServer = new byte[] {8, 8, 8, 8};
        final DhcpServer ip4server2 = new DhcpServer();
        ip4server2.serverVrfId = 12;
        ip4server2.dhcpServer = new byte[] {8, 8, 8, 4};
        ip4Proxy.servers = new DhcpServer[] {ip4server1, ip4server2};
        ip4.dhcpProxyDetails.add(ip4Proxy);
        when(api.dhcpProxyDump(new DhcpProxyDump())).thenReturn(future(ip4));

        final DhcpProxyDetailsReplyDump ip6 = new DhcpProxyDetailsReplyDump();
        final DhcpProxyDetails ip6Proxy = new DhcpProxyDetails();
        ip6Proxy.rxVrfId = 321;
        // 2001:0db8:0a0b:12f0:0000:0000:0000:0001
        ip6Proxy.dhcpSrcAddress =
            new byte[] {0x20, 0x01, 0x0d, (byte) 0xb8, 0x0a, 0x0b, 0x12, (byte) 0xf0, 0, 0, 0, 0, 0, 0, 0, 0x01};
        final DhcpServer ip6server = new DhcpServer();
        ip6server.serverVrfId = 22;
        // 2001:0db8:0a0b:12f0:0000:0000:0000:0002
        ip6server.dhcpServer =
            new byte[] {0x20, 0x01, 0x0d, (byte) 0xb8, 0x0a, 0x0b, 0x12, (byte) 0xf0, 0, 0, 0, 0, 0, 0, 0, 0x02};
        ip6Proxy.servers = new DhcpServer[] {ip6server};
        ip6Proxy.isIpv6 = 1;
        final DhcpProxyDump ip6Dump = new DhcpProxyDump();
        ip6Dump.isIp6 = 1;
        ip6.dhcpProxyDetails.add(ip6Proxy);
        when(api.dhcpProxyDump(ip6Dump)).thenReturn(future(ip6));
    }

    @Test
    public void testGetAllIds() throws ReadFailedException {
        final List<RelayKey> allIds = getCustomizer().getAllIds(RELAYS.child(Relay.class), ctx);
        assertEquals(2, allIds.size());
        assertThat(allIds, containsInAnyOrder(IP4_IID.getKey(), IP6_IID.getKey()));
    }

    @Test
    public void testReadIp4() throws ReadFailedException {
        final RelayBuilder builder = new RelayBuilder();
        getCustomizer().readCurrentAttributes(IP4_IID, builder, ctx);
        assertEquals(IP4_IID.getKey().getAddressFamily(), builder.getAddressFamily());
        assertEquals(IP4_IID.getKey().getRxVrfId(), builder.getRxVrfId());
        assertEquals("1.2.3.4", builder.getGatewayAddress().stringValue());
        final List<Server> server = builder.getServer();
        assertEquals(2, server.size());
        assertEquals(11L, server.get(0).getVrfId().longValue());
        assertEquals("8.8.8.8", server.get(0).getAddress().stringValue());
        assertEquals(12L, server.get(1).getVrfId().longValue());
        assertEquals("8.8.8.4", server.get(1).getAddress().stringValue());
    }

    @Test
    public void testReadIp6() throws ReadFailedException {
        final RelayBuilder builder = new RelayBuilder();
        getCustomizer().readCurrentAttributes(IP6_IID, builder, ctx);
        assertEquals(IP6_IID.getKey().getAddressFamily(), builder.getAddressFamily());
        assertEquals(IP6_IID.getKey().getRxVrfId(), builder.getRxVrfId());
        assertEquals(22L, builder.getServer().get(0).getVrfId().longValue());
        assertEquals("2001:db8:a0b:12f0::1", builder.getGatewayAddress().stringValue());
        assertEquals("2001:db8:a0b:12f0::2", builder.getServer().get(0).getAddress().stringValue());
    }

    @Test
    public void testInit() {
        final Relay data = new RelayBuilder().build();
        invokeInitTest(IP4_IID, data, IP4_IID, data);
    }
}