summaryrefslogtreecommitdiffstats
path: root/lisp/lisp2vpp/src/test/java/io/fd/hc2vpp/lisp/gpe/translate/write/GpeForwardEntryCustomizerTest.java
blob: 11d38cf3d926c871146fc696e6a7d78ecbdf2278 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * 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.lisp.gpe.translate.write;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableSet;
import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
import io.fd.hc2vpp.common.translate.util.NamingContext;
import io.fd.hc2vpp.lisp.gpe.translate.service.GpeStateCheckService;
import io.fd.honeycomb.test.tools.HoneycombTestRunner;
import io.fd.honeycomb.test.tools.annotations.InjectTestData;
import io.fd.honeycomb.test.tools.annotations.InjectablesProcessor;
import io.fd.honeycomb.test.tools.annotations.SchemaContextProvider;
import io.fd.vpp.jvpp.core.dto.GpeAddDelFwdEntry;
import io.fd.vpp.jvpp.core.dto.GpeAddDelFwdEntryReply;
import io.fd.vpp.jvpp.core.types.GpeLocator;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801.gpe.entry.table.grouping.GpeEntryTable;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801.gpe.entry.table.grouping.gpe.entry.table.GpeEntry;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801.gpe.entry.table.grouping.gpe.entry.table.GpeEntryKey;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.lisp.rev171013.$YangModuleInfoImpl;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

@RunWith(HoneycombTestRunner.class)
public class GpeForwardEntryCustomizerTest extends WriterCustomizerTest
        implements InjectablesProcessor, ByteDataTranslator {

    private static final String GPE_ENTRY_ID = "gpe-fwd-entry-1";
    private static final String GPE_ENTRY_PATH = "/gpe:gpe" +
            "/gpe:gpe-feature-data" +
            "/gpe:gpe-entry-table";
    private static final byte[] LOCAL_EID_ADDRESS = {-64, -88, 2, 0};
    private static final byte[] REMOTE_EID_ADDRESS = {-64, -88, 3, 0};
    private static final byte[] PAIR_2_LOCAL_ADDRESS = {-64, -88, 5, 1};
    private static final byte[] PAIR_1_LOCAL_ADDRESS = {-64, -88, 4, 1};
    private static final byte[] PAIR_2_REMOTE_ADDRESS = {-64, -88, 5, 2};
    private static final byte[] PAIR_1_REMOTE_ADDRESS = {-64, -88, 4, 2};

    private static final GpeLocator[] ABAB_LOCATORS = {
            gpeLocator(PAIR_1_LOCAL_ADDRESS, 1, 3),
            gpeLocator(PAIR_2_LOCAL_ADDRESS, 1, 2),
            gpeLocator(PAIR_1_REMOTE_ADDRESS, 1, 0),
            gpeLocator(PAIR_2_REMOTE_ADDRESS, 1, 0)};

    private static final GpeLocator[] BABA_LOCATORS = {
            ABAB_LOCATORS[1], ABAB_LOCATORS[0], ABAB_LOCATORS[3], ABAB_LOCATORS[2]};

    private static final int LOCAL_EID_PREFIX = 24;
    private static final int REMOTE_EID_PREFIX = 16;
    private static final String GPE_ENTRY_CTX = "gpe-entry-ctx";
    private static final int GPE_FWD_ENTRY_INDEX = 4;

    private NamingContext gpeEntryMappingContext;

    @Captor
    private ArgumentCaptor<GpeAddDelFwdEntry> requestCaptor;

    @Mock
    private GpeStateCheckService gpeStateCheckService;

    private InstanceIdentifier<GpeEntry> id;
    private GpeForwardEntryCustomizer customizer;

    @Override
    protected void setUpTest() throws Exception {
        gpeEntryMappingContext = new NamingContext("gpe-entry-", GPE_ENTRY_CTX);
        id = InstanceIdentifier.create(GpeEntryTable.class)
                .child(GpeEntry.class, new GpeEntryKey(GPE_ENTRY_ID));
        customizer = new GpeForwardEntryCustomizer(api, gpeStateCheckService, gpeEntryMappingContext);
    }

    @SchemaContextProvider
    public ModuleInfoBackedContext schemaContext() {
        return provideSchemaContextFor(ImmutableSet.of($YangModuleInfoImpl.getInstance(),
                org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.$YangModuleInfoImpl
                        .getInstance(),
                org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801.$YangModuleInfoImpl
                        .getInstance()));
    }

    @Test
    public void testWriteCurrentAttributesFull(@InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-full.json",
            id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
        when(api.gpeAddDelFwdEntry(any())).thenReturn(future(entryReply()));
        final GpeEntry entry = entryTable.getGpeEntry().get(0);
        customizer.writeCurrentAttributes(id, entry, writeContext);
        verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
        assertFullRequest(true, requestCaptor.getValue());
        verify(mappingContext, times(1))
                .put(mappingIid(entry.getId(), GPE_ENTRY_CTX), mapping(entry.getId(), GPE_FWD_ENTRY_INDEX).get());
    }

    private static GpeAddDelFwdEntryReply entryReply() {
        final GpeAddDelFwdEntryReply reply = new GpeAddDelFwdEntryReply();
        reply.fwdEntryIndex = GPE_FWD_ENTRY_INDEX;
        return reply;
    }

    @Test
    public void testWriteCurrentAttributesWithoutLocators(
            @InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-without-locators.json",
                    id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
        when(api.gpeAddDelFwdEntry(any())).thenReturn(future(entryReply()));
        final GpeEntry entry = entryTable.getGpeEntry().get(0);
        customizer.writeCurrentAttributes(id, entry, writeContext);
        verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
        assertLocatorLessRequest(true, requestCaptor.getValue());
        verify(mappingContext, times(1))
                .put(mappingIid(entry.getId(), GPE_ENTRY_CTX), mapping(entry.getId(), GPE_FWD_ENTRY_INDEX).get());
    }

    @Test
    public void testWriteCurrentAttributesWithoutAction(
            @InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-without-action.json",
                    id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
        when(api.gpeAddDelFwdEntry(any())).thenReturn(future(entryReply()));
        final GpeEntry entry = entryTable.getGpeEntry().get(0);
        customizer.writeCurrentAttributes(id, entry, writeContext);
        verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
        assertActionLessRequest(true, requestCaptor.getValue());
        verify(mappingContext, times(1))
                .put(mappingIid(entry.getId(), GPE_ENTRY_CTX), mapping(entry.getId(), GPE_FWD_ENTRY_INDEX).get());
    }

    /**
     * Gpe entry allows no local eid
     */
    @Test
    public void testWriteCurrentAttributesNoLocalEid(
            @InjectTestData(resourcePath = "/gpe/invalid/invalid-gpe-fwd-entry-no-local-eid.json",
                    id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
        when(api.gpeAddDelFwdEntry(any())).thenReturn(future(entryReply()));
        final GpeEntry entry = entryTable.getGpeEntry().get(0);
        customizer.writeCurrentAttributes(id, entry, writeContext);
        verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
        assertActionLessNoLeidRequest(true, requestCaptor.getValue());
        verify(mappingContext, times(1))
                .put(mappingIid(entry.getId(), GPE_ENTRY_CTX), mapping(entry.getId(), GPE_FWD_ENTRY_INDEX).get());
    }

    @Test
    public void testWriteCurrentAttributesFailNoRemoteEid(
            @InjectTestData(resourcePath = "/gpe/invalid/invalid-gpe-fwd-entry-no-remote-eid.json",
                    id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
        try {
            customizer.writeCurrentAttributes(id, entryTable.getGpeEntry().get(0), writeContext);
        } catch (IllegalArgumentException e) {
            verifyZeroInteractions(api);
            return;
        }
        fail("Test should have failed");
    }

    @Test
    public void testDeleteCurrentAttributesFull(@InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-full.json",
            id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
        when(api.gpeAddDelFwdEntry(any())).thenReturn(future(new GpeAddDelFwdEntryReply()));
        final GpeEntry entry = entryTable.getGpeEntry().get(0);
        customizer.deleteCurrentAttributes(id, entry, writeContext);
        verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
        assertFullRequest(false, requestCaptor.getValue());
        verify(mappingContext, times(1)).delete(mappingIid(entry.getId(), GPE_ENTRY_CTX));
    }

    @Test
    public void testDeleteCurrentAttributesWithoutLocators(
            @InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-without-locators.json",
                    id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
        when(api.gpeAddDelFwdEntry(any())).thenReturn(future(new GpeAddDelFwdEntryReply()));
        final GpeEntry entry = entryTable.getGpeEntry().get(0);
        customizer.deleteCurrentAttributes(id, entry, writeContext);
        verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
        assertLocatorLessRequest(false, requestCaptor.getValue());
        verify(mappingContext, times(1)).delete(mappingIid(entry.getId(), GPE_ENTRY_CTX));
    }

    @Test
    public void testDeleteCurrentAttributesWithoutAction(
            @InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-without-action.json",
                    id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
        when(api.gpeAddDelFwdEntry(any())).thenReturn(future(new GpeAddDelFwdEntryReply()));
        final GpeEntry entry = entryTable.getGpeEntry().get(0);
        customizer.deleteCurrentAttributes(id, entry, writeContext);
        verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
        assertActionLessRequest(false, requestCaptor.getValue());
        verify(mappingContext, times(1)).delete(mappingIid(entry.getId(), GPE_ENTRY_CTX));
    }

    @Test
    public void testDeleteCurrentAttributesNoLocalEid(
            @InjectTestData(resourcePath = "/gpe/invalid/invalid-gpe-fwd-entry-no-local-eid.json",
                    id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
        when(api.gpeAddDelFwdEntry(any())).thenReturn(future(new GpeAddDelFwdEntryReply()));
        final GpeEntry entry = entryTable.getGpeEntry().get(0);
        customizer.deleteCurrentAttributes(id, entry, writeContext);
        verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
        assertActionLessNoLeidRequest(false, requestCaptor.getValue());
        verify(mappingContext, times(1)).delete(mappingIid(entry.getId(), GPE_ENTRY_CTX));
    }

    @Test
    public void testDeleteCurrentAttributesFailNoRemoteEid(
            @InjectTestData(resourcePath = "/gpe/invalid/invalid-gpe-fwd-entry-no-remote-eid.json",
                    id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
        try {
            customizer.deleteCurrentAttributes(id, entryTable.getGpeEntry().get(0), writeContext);
        } catch (IllegalArgumentException e) {
            verifyZeroInteractions(api);
            return;
        }
        fail("Test should have failed");
    }

    private void assertActionLessNoLeidRequest(final boolean add, final GpeAddDelFwdEntry actual) {

        assertEquals(booleanToByte(add), actual.isAdd);
        assertEquals(10, actual.dpTable);
        assertEquals(12, actual.vni);
        assertEquals(0, actual.eidType);
        assertEquals(4, actual.locNum);
        assertTrue(Arrays.equals(REMOTE_EID_ADDRESS, actual.rmtEid));
        assertEquals(REMOTE_EID_PREFIX, actual.rmtLen);
        assertTrue(Arrays.equals(ABAB_LOCATORS, actual.locs) || Arrays.equals(BABA_LOCATORS, actual.locs));
    }

    private void assertActionLessRequest(final boolean add, final GpeAddDelFwdEntry actual) {

        assertEquals(booleanToByte(add), actual.isAdd);
        assertEquals(10, actual.dpTable);
        assertEquals(12, actual.vni);
        assertEquals(0, actual.eidType);
        assertEquals(0, actual.action);
        assertEquals(0, actual.locNum);
    }

    private void assertLocatorLessRequest(final boolean add, final GpeAddDelFwdEntry actual) {

        assertEquals(booleanToByte(add), actual.isAdd);
        assertEquals(10, actual.dpTable);
        assertEquals(12, actual.vni);
        assertEquals(0, actual.eidType);
        assertEquals(1, actual.action);
        assertEquals(0, actual.locNum);
        assertTrue(Arrays.equals(LOCAL_EID_ADDRESS, actual.lclEid));
        assertEquals(LOCAL_EID_PREFIX, actual.lclLen);
        assertTrue(Arrays.equals(REMOTE_EID_ADDRESS, actual.rmtEid));
        assertEquals(REMOTE_EID_PREFIX, actual.rmtLen);
    }


    private void assertFullRequest(final boolean add, final GpeAddDelFwdEntry actual) {

        assertEquals(booleanToByte(add), actual.isAdd);
        assertEquals(10, actual.dpTable);
        assertEquals(12, actual.vni);
        assertEquals(0, actual.eidType);
        assertEquals(4, actual.locNum);
        assertTrue(Arrays.equals(LOCAL_EID_ADDRESS, actual.lclEid));
        assertEquals(LOCAL_EID_PREFIX, actual.lclLen);
        assertTrue(Arrays.equals(REMOTE_EID_ADDRESS, actual.rmtEid));
        assertEquals(REMOTE_EID_PREFIX, actual.rmtLen);
        assertTrue(Arrays.equals(ABAB_LOCATORS, actual.locs) || Arrays.equals(BABA_LOCATORS, actual.locs));
    }

    private static GpeLocator gpeLocator(final byte[] address, final int isIpv4, final int weight) {
        GpeLocator locator = new GpeLocator();
        locator.isIp4 = (byte) isIpv4;
        locator.weight = (byte) weight;
        locator.addr = address;

        return locator;
    }
}