summaryrefslogtreecommitdiffstats
path: root/lisp/lisp2vpp/src/test/java/io/fd/hc2vpp/lisp/translate/read/AdjacencyCustomizerTest.java
blob: 2135a469b79b9d1493da6dff57ee2d499e2b7553 (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
195
196
197
198
/*
 * 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.read;

import static io.fd.hc2vpp.lisp.translate.AdjacencyData.ADDRESS_ONE;
import static io.fd.hc2vpp.lisp.translate.AdjacencyData.ADDRESS_THREE;
import static io.fd.hc2vpp.lisp.translate.AdjacencyData.LOCAL_EID_ONE;
import static io.fd.hc2vpp.lisp.translate.AdjacencyData.LOCAL_EID_PREFIX_ONE;
import static io.fd.hc2vpp.lisp.translate.AdjacencyData.LOCAL_EID_PREFIX_TWO;
import static io.fd.hc2vpp.lisp.translate.AdjacencyData.LOCAL_EID_TWO;
import static io.fd.hc2vpp.lisp.translate.AdjacencyData.REMOTE_EID_ONE;
import static io.fd.hc2vpp.lisp.translate.AdjacencyData.REMOTE_EID_PREFIX_ONE;
import static io.fd.hc2vpp.lisp.translate.AdjacencyData.REMOTE_EID_PREFIX_TWO;
import static io.fd.hc2vpp.lisp.translate.AdjacencyData.REMOTE_EID_TWO;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest;
import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
import io.fd.hc2vpp.lisp.context.util.AdjacenciesMappingContext;
import io.fd.hc2vpp.lisp.context.util.EidMappingContext;
import io.fd.hc2vpp.lisp.translate.util.EidMetadataProvider;
import io.fd.hc2vpp.lisp.util.AdjacencyMappingContextTestHelper;
import io.fd.hc2vpp.lisp.util.EidMappingContextHelper;
import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
import io.fd.vpp.jvpp.core.dto.OneAdjacenciesGetReply;
import io.fd.vpp.jvpp.core.types.OneAdjacency;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.MappingId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.adjacencies.grouping.Adjacencies;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.adjacencies.grouping.AdjacenciesBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.adjacencies.grouping.adjacencies.Adjacency;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.adjacencies.grouping.adjacencies.AdjacencyBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.adjacencies.grouping.adjacencies.AdjacencyKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.dp.subtable.grouping.RemoteMappings;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.dp.subtable.grouping.remote.mappings.RemoteMapping;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.dp.subtable.grouping.remote.mappings.RemoteMappingKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.eid.table.grouping.EidTable;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.eid.table.grouping.eid.table.VniTable;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.eid.table.grouping.eid.table.VniTableKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev171013.eid.table.grouping.eid.table.vni.table.BridgeDomainSubtable;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;


public class AdjacencyCustomizerTest
        extends ListReaderCustomizerTest<Adjacency, AdjacencyKey, AdjacencyBuilder>
        implements ByteDataTranslator, EidMetadataProvider, EidMappingContextHelper, AdjacencyMappingContextTestHelper {

    public static final String NORMALIZED_PREFIX_ONE = "192.168.2.0/24";
    public static final String NORMALIZED_PREFIX_THREE = "192.168.0.0/16";
    private InstanceIdentifier<Adjacency> identifier;

    public AdjacencyCustomizerTest() {
        super(Adjacency.class, AdjacenciesBuilder.class);
    }

    @Before
    public void init() {
        identifier = InstanceIdentifier.create(EidTable.class)
                .child(VniTable.class, new VniTableKey(2L))
                .child(BridgeDomainSubtable.class)
                .child(RemoteMappings.class)
                .child(RemoteMapping.class, new RemoteMappingKey(new MappingId("remote-mapping")))
                .child(Adjacencies.class)
                .child(Adjacency.class, new AdjacencyKey("adj-one"));
        defineAdjacencyMapping(mappingContext, "local-eid-one", "remote-eid-one", "adj-one",
                "adjacencies-mapping-context");
        defineAdjacencyMapping(mappingContext, "local-eid-two", "remote-eid-two", "adj-two",
                "adjacencies-mapping-context");
    }

    private void defineEidAddressMapping() {
        defineEidMapping(mappingContext, LOCAL_EID_ONE, new MappingId("local-eid-one"), "local-mapping-context");
        defineEidMapping(mappingContext, LOCAL_EID_TWO, new MappingId("local-eid-two"), "local-mapping-context");
        defineEidMapping(mappingContext, REMOTE_EID_ONE, new MappingId("remote-eid-one"), "remote-mapping-context");
        defineEidMapping(mappingContext, REMOTE_EID_TWO, new MappingId("remote-eid-two"), "remote-mapping-context");
    }

    private void defineEidPrefixMapping() {
        defineEidMapping(mappingContext, LOCAL_EID_PREFIX_ONE, new MappingId("local-eid-one"), "local-mapping-context");
        defineEidMapping(mappingContext, LOCAL_EID_PREFIX_TWO, new MappingId("local-eid-two"), "local-mapping-context");
        defineEidMapping(mappingContext, REMOTE_EID_PREFIX_ONE, new MappingId("remote-eid-one"), "remote-mapping-context");
        defineEidMapping(mappingContext, REMOTE_EID_PREFIX_TWO, new MappingId("remote-eid-two"), "remote-mapping-context");
    }

    @Test
    public void getAllIds() throws Exception {
        mockAddressDump();
        defineEidAddressMapping();
        final List<AdjacencyKey> keys = getCustomizer().getAllIds(identifier, ctx);

        assertThat(keys, hasSize(2));
        assertThat(keys, contains(new AdjacencyKey("adj-one"), new AdjacencyKey("adj-two")));
    }

    @Test
    public void readCurrentAttributes() throws Exception {
        mockAddressDump();
        defineEidAddressMapping();
        final AdjacencyBuilder builder = new AdjacencyBuilder();
        getCustomizer().readCurrentAttributes(identifier, builder, ctx);

        assertEquals("adj-one", builder.getId());
        assertEquals(new AdjacencyKey("adj-one"), builder.getKey());
        assertEquals(ADDRESS_ONE.getValue(), Ipv4.class.cast(builder.getLocalEid().getAddress()).getIpv4().getValue());
        assertEquals(ADDRESS_THREE.getValue(),
                Ipv4.class.cast(builder.getRemoteEid().getAddress()).getIpv4().getValue());
    }

    @Test
    public void readCurrentAttributesPrefixBased() throws Exception {
        mockPrefixDump();
        defineEidPrefixMapping();
        final AdjacencyBuilder builder = new AdjacencyBuilder();
        getCustomizer().readCurrentAttributes(identifier, builder, ctx);

        assertEquals("adj-one", builder.getId());
        assertEquals(new AdjacencyKey("adj-one"), builder.getKey());
        assertEquals(NORMALIZED_PREFIX_ONE,
                Ipv4Prefix.class.cast(builder.getLocalEid().getAddress()).getIpv4Prefix().getValue());
        assertEquals(NORMALIZED_PREFIX_THREE,
                Ipv4Prefix.class.cast(builder.getRemoteEid().getAddress()).getIpv4Prefix().getValue());
    }

    @Override
    protected ReaderCustomizer<Adjacency, AdjacencyBuilder> initCustomizer() {
        return new AdjacencyCustomizer(api, new EidMappingContext("local-mapping-context", "local-mapping-"),
                new EidMappingContext("remote-mapping-context", "remote-mapping-"),
                new AdjacenciesMappingContext("adjacencies-mapping-context"));
    }


    private void mockAddressDump() {
        OneAdjacency adjacencyOne = new OneAdjacency();
        adjacencyOne.eidType = 0;
        adjacencyOne.leid = new byte[]{-64, -88, 2, 1};
        adjacencyOne.leidPrefixLen = 32;
        adjacencyOne.reid = new byte[]{-64, -88, 2, 3};
        adjacencyOne.reidPrefixLen = 32;


        OneAdjacency adjacencyTwo = new OneAdjacency();
        adjacencyTwo.eidType = 0;
        adjacencyTwo.leid = new byte[]{-64, -88, 2, 2};
        adjacencyTwo.leidPrefixLen = 32;
        adjacencyTwo.reid = new byte[]{-64, -88, 2, 4};
        adjacencyTwo.reidPrefixLen = 32;

        OneAdjacenciesGetReply reply = new OneAdjacenciesGetReply();
        reply.adjacencies = new OneAdjacency[]{adjacencyOne, adjacencyTwo};

        when(api.oneAdjacenciesGet(any())).thenReturn(future(reply));
    }

    private void mockPrefixDump() {
        OneAdjacency adjacencyOne = new OneAdjacency();
        adjacencyOne.eidType = 0;
        adjacencyOne.leid = new byte[]{-64, -88, 2, 1};
        adjacencyOne.leidPrefixLen = 24;
        adjacencyOne.reid = new byte[]{-64, -88, 2, 3};
        adjacencyOne.reidPrefixLen = 16;


        OneAdjacency adjacencyTwo = new OneAdjacency();
        adjacencyTwo.eidType = 0;
        adjacencyTwo.leid = new byte[]{-64, -88, 2, 2};
        adjacencyTwo.leidPrefixLen = 28;
        adjacencyTwo.reid = new byte[]{-64, -88, 2, 4};
        adjacencyTwo.reidPrefixLen = 8;

        OneAdjacenciesGetReply reply = new OneAdjacenciesGetReply();
        reply.adjacencies = new OneAdjacency[]{adjacencyOne, adjacencyTwo};

        when(api.oneAdjacenciesGet(any())).thenReturn(future(reply));
    }
}