74c9bdeba4cdc2d1e7299320c9fabd0b70d24459
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.hc2vpp.lisp.gpe.translate.write;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.fail;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.verifyZeroInteractions;
25 import static org.mockito.Mockito.when;
26
27 import com.google.common.collect.ImmutableSet;
28 import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
29 import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
30 import io.fd.hc2vpp.lisp.gpe.translate.ctx.GpeEntryIdentifier;
31 import io.fd.hc2vpp.lisp.gpe.translate.ctx.GpeEntryMappingContext;
32 import io.fd.hc2vpp.lisp.gpe.translate.ctx.GpeLocatorPair;
33 import io.fd.hc2vpp.lisp.gpe.translate.ctx.GpeLocatorPairMappingContext;
34 import io.fd.hc2vpp.lisp.gpe.translate.service.GpeStateCheckService;
35 import io.fd.honeycomb.test.tools.HoneycombTestRunner;
36 import io.fd.honeycomb.test.tools.annotations.InjectTestData;
37 import io.fd.honeycomb.test.tools.annotations.InjectablesProcessor;
38 import io.fd.honeycomb.test.tools.annotations.SchemaContextProvider;
39 import io.fd.vpp.jvpp.core.dto.GpeAddDelFwdEntry;
40 import io.fd.vpp.jvpp.core.dto.GpeAddDelFwdEntryReply;
41 import io.fd.vpp.jvpp.core.types.GpeLocator;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.ArgumentCaptor;
45 import org.mockito.Captor;
46 import org.mockito.Mock;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170518.gpe.entry.table.grouping.GpeEntryTable;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170518.gpe.entry.table.grouping.gpe.entry.table.GpeEntry;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170518.gpe.entry.table.grouping.gpe.entry.table.GpeEntryKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170518.gpe.entry.table.grouping.gpe.entry.table.gpe.entry.LocatorPairs;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.$YangModuleInfoImpl;
52 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54
55 @RunWith(HoneycombTestRunner.class)
56 public class GpeForwardEntryCustomizerTest extends WriterCustomizerTest
57         implements InjectablesProcessor, ByteDataTranslator {
58
59     private static final String GPE_ENTRY_ID = "gpe-fwd-entry-1";
60     private static final String GPE_ENTRY_PATH = "/gpe:gpe" +
61             "/gpe:gpe-feature-data" +
62             "/gpe:gpe-entry-table";
63     private static final byte[] LOCAL_EID_ADDRESS = {-64, -88, 2, 0};
64     private static final byte[] REMOTE_EID_ADDRESS = {-64, -88, 3, 0};
65     private static final byte[] PAIR_2_LOCAL_ADDRESS = {-64, -88, 5, 1};
66     private static final byte[] PAIR_1_LOCAL_ADDRESS = {-64, -88, 4, 1};
67     private static final byte[] PAIR_2_REMOTE_ADDRESS = {-64, -88, 5, 2};
68     private static final byte[] PAIR_1_REMOTE_ADDRESS = {-64, -88, 4, 2};
69     private static final int LOCAL_EID_PREFIX = 24;
70     private static final int REMOTE_EID_PREFIX = 16;
71
72     @Captor
73     private ArgumentCaptor<GpeAddDelFwdEntry> requestCaptor;
74
75     @Mock
76     private GpeEntryMappingContext gpeEntryMappingContext;
77
78     @Mock
79     private GpeLocatorPairMappingContext gpeLocatorPairMappingContext;
80
81     @Mock
82     private GpeStateCheckService gpeStateCheckService;
83
84     private InstanceIdentifier<GpeEntry> id;
85     private GpeForwardEntryCustomizer customizer;
86
87     @Override
88     protected void setUpTest() throws Exception {
89         id = InstanceIdentifier.create(GpeEntryTable.class)
90                 .child(GpeEntry.class, new GpeEntryKey(GPE_ENTRY_ID));
91         customizer = new GpeForwardEntryCustomizer(api, gpeStateCheckService, gpeEntryMappingContext,
92                 gpeLocatorPairMappingContext);
93     }
94
95     @SchemaContextProvider
96     public ModuleInfoBackedContext schemaContext() {
97         return provideSchemaContextFor(ImmutableSet.of($YangModuleInfoImpl.getInstance(),
98                 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.$YangModuleInfoImpl
99                         .getInstance(),
100                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170518.$YangModuleInfoImpl
101                         .getInstance()));
102     }
103
104     @Test
105     public void testWriteCurrentAttributesFull(@InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-full.json",
106             id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
107         when(api.gpeAddDelFwdEntry(any())).thenReturn(future(new GpeAddDelFwdEntryReply()));
108         final GpeEntry entry = entryTable.getGpeEntry().get(0);
109         customizer.writeCurrentAttributes(id, entry, writeContext);
110         verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
111         assertEquals(expectedFullRequest(true), requestCaptor.getValue());
112         verify(gpeEntryMappingContext, times(1))
113                 .addMapping(entry.getId(), GpeEntryIdentifier.fromEntry(entry), mappingContext);
114
115         final LocatorPairs locatorPairFirst = entry.getLocatorPairs().get(0);
116         final LocatorPairs locatorPairSecond = entry.getLocatorPairs().get(1);
117         verify(gpeLocatorPairMappingContext, times(1))
118                 .addMapping(entry.getId(), locatorPairFirst.getId(),
119                         GpeLocatorPair.fromLocatorPair(locatorPairFirst), mappingContext);
120         verify(gpeLocatorPairMappingContext, times(1))
121                 .addMapping(entry.getId(), locatorPairSecond.getId(),
122                         GpeLocatorPair.fromLocatorPair(locatorPairSecond), mappingContext);
123     }
124
125     @Test
126     public void testWriteCurrentAttributesWithoutLocators(
127             @InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-without-locators.json",
128                     id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
129         when(api.gpeAddDelFwdEntry(any())).thenReturn(future(new GpeAddDelFwdEntryReply()));
130         final GpeEntry entry = entryTable.getGpeEntry().get(0);
131         customizer.writeCurrentAttributes(id, entry, writeContext);
132         verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
133         assertEquals(expectedLocatorLessRequest(true), requestCaptor.getValue());
134         verify(gpeEntryMappingContext, times(1))
135                 .addMapping(entry.getId(), GpeEntryIdentifier.fromEntry(entry), mappingContext);
136         verifyZeroInteractions(gpeLocatorPairMappingContext);
137     }
138
139     @Test
140     public void testWriteCurrentAttributesWithoutAction(
141             @InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-without-action.json",
142                     id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
143         when(api.gpeAddDelFwdEntry(any())).thenReturn(future(new GpeAddDelFwdEntryReply()));
144         final GpeEntry entry = entryTable.getGpeEntry().get(0);
145         customizer.writeCurrentAttributes(id, entry, writeContext);
146         verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
147         assertEquals(expectedActionLessRequest(true), requestCaptor.getValue());
148         verify(gpeEntryMappingContext, times(1))
149                 .addMapping(entry.getId(), GpeEntryIdentifier.fromEntry(entry), mappingContext);
150         verifyZeroInteractions(gpeLocatorPairMappingContext);
151     }
152
153     @Test
154     public void testWriteCurrentAttributesFailNoLocalEid(
155             @InjectTestData(resourcePath = "/gpe/invalid/invalid-gpe-fwd-entry-no-local-eid.json",
156                     id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
157         try {
158             customizer.writeCurrentAttributes(id, entryTable.getGpeEntry().get(0), writeContext);
159         } catch (IllegalArgumentException e) {
160             verifyZeroInteractions(api);
161             return;
162         }
163         fail("Test should have failed");
164     }
165
166     @Test
167     public void testWriteCurrentAttributesFailNoRemoteEid(
168             @InjectTestData(resourcePath = "/gpe/invalid/invalid-gpe-fwd-entry-no-remote-eid.json",
169                     id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
170         try {
171             customizer.writeCurrentAttributes(id, entryTable.getGpeEntry().get(0), writeContext);
172         } catch (IllegalArgumentException e) {
173             verifyZeroInteractions(api);
174             return;
175         }
176         fail("Test should have failed");
177     }
178
179     @Test
180     public void testDeleteCurrentAttributesFull(@InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-full.json",
181             id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
182         when(api.gpeAddDelFwdEntry(any())).thenReturn(future(new GpeAddDelFwdEntryReply()));
183         final GpeEntry entry = entryTable.getGpeEntry().get(0);
184         customizer.deleteCurrentAttributes(id, entry, writeContext);
185         verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
186         assertEquals(expectedFullRequest(false), requestCaptor.getValue());
187         verify(gpeEntryMappingContext, times(1))
188                 .removeMapping(entry.getId(), mappingContext);
189         verify(gpeLocatorPairMappingContext, times(1))
190                 .removeMapping(entry.getId(), mappingContext);
191     }
192
193     @Test
194     public void testDeleteCurrentAttributesWithoutLocators(
195             @InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-without-locators.json",
196                     id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
197         when(api.gpeAddDelFwdEntry(any())).thenReturn(future(new GpeAddDelFwdEntryReply()));
198         final GpeEntry entry = entryTable.getGpeEntry().get(0);
199         customizer.deleteCurrentAttributes(id, entry, writeContext);
200         verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
201         assertEquals(expectedLocatorLessRequest(false), requestCaptor.getValue());
202         verify(gpeEntryMappingContext, times(1))
203                 .removeMapping(entry.getId(), mappingContext);
204         verify(gpeLocatorPairMappingContext, times(1))
205                 .removeMapping(entry.getId(), mappingContext);
206     }
207
208     @Test
209     public void testDeleteCurrentAttributesWithoutAction(
210             @InjectTestData(resourcePath = "/gpe/gpe-fwd-entry-without-action.json",
211                     id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
212         when(api.gpeAddDelFwdEntry(any())).thenReturn(future(new GpeAddDelFwdEntryReply()));
213         final GpeEntry entry = entryTable.getGpeEntry().get(0);
214         customizer.deleteCurrentAttributes(id, entry, writeContext);
215         verify(api, times(1)).gpeAddDelFwdEntry(requestCaptor.capture());
216         assertEquals(expectedActionLessRequest(false), requestCaptor.getValue());
217         verify(gpeEntryMappingContext, times(1))
218                 .removeMapping(entry.getId(), mappingContext);
219         verify(gpeLocatorPairMappingContext, times(1))
220                 .removeMapping(entry.getId(), mappingContext);
221     }
222
223     @Test
224     public void testDeleteCurrentAttributesFailNoLocalEid(
225             @InjectTestData(resourcePath = "/gpe/invalid/invalid-gpe-fwd-entry-no-local-eid.json",
226                     id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
227         try {
228             customizer.deleteCurrentAttributes(id, entryTable.getGpeEntry().get(0), writeContext);
229         } catch (IllegalArgumentException e) {
230             verifyZeroInteractions(api);
231             return;
232         }
233         fail("Test should have failed");
234     }
235
236     @Test
237     public void testDeleteCurrentAttributesFailNoRemoteEid(
238             @InjectTestData(resourcePath = "/gpe/invalid/invalid-gpe-fwd-entry-no-remote-eid.json",
239                     id = GPE_ENTRY_PATH) GpeEntryTable entryTable) throws Exception {
240         try {
241             customizer.deleteCurrentAttributes(id, entryTable.getGpeEntry().get(0), writeContext);
242         } catch (IllegalArgumentException e) {
243             verifyZeroInteractions(api);
244             return;
245         }
246         fail("Test should have failed");
247     }
248
249     private GpeAddDelFwdEntry expectedActionLessRequest(final boolean add) {
250         final GpeAddDelFwdEntry request = new GpeAddDelFwdEntry();
251
252         request.isAdd = booleanToByte(add);
253         request.dpTable = 10;
254         request.vni = 12;
255         request.eidType = 0;
256         request.action = 0;
257         request.lclEid = LOCAL_EID_ADDRESS;
258         request.lclLen = LOCAL_EID_PREFIX;
259         request.rmtEid = REMOTE_EID_ADDRESS;
260         request.rmtLen = REMOTE_EID_PREFIX;
261         request.locNum = 0;
262         return request;
263     }
264
265     private GpeAddDelFwdEntry expectedLocatorLessRequest(final boolean add) {
266         final GpeAddDelFwdEntry request = new GpeAddDelFwdEntry();
267
268         request.isAdd = booleanToByte(add);
269         request.dpTable = 10;
270         request.vni = 12;
271         request.eidType = 0;
272         request.action = 1;
273         request.lclEid = LOCAL_EID_ADDRESS;
274         request.lclLen = LOCAL_EID_PREFIX;
275         request.rmtEid = REMOTE_EID_ADDRESS;
276         request.rmtLen = REMOTE_EID_PREFIX;
277         request.locNum = 0;
278         return request;
279     }
280
281
282     private GpeAddDelFwdEntry expectedFullRequest(final boolean add) {
283         final GpeAddDelFwdEntry request = new GpeAddDelFwdEntry();
284
285         request.isAdd = booleanToByte(add);
286         request.dpTable = 10;
287         request.vni = 12;
288         request.eidType = 0;
289         request.action = 1;
290         request.lclEid = LOCAL_EID_ADDRESS;
291         request.lclLen = LOCAL_EID_PREFIX;
292         request.rmtEid = REMOTE_EID_ADDRESS;
293         request.rmtLen = REMOTE_EID_PREFIX;
294         request.locNum = 4;
295         request.locs = new GpeLocator[]{
296                 gpeLocator(PAIR_2_LOCAL_ADDRESS, 1, 2),
297                 gpeLocator(PAIR_1_LOCAL_ADDRESS, 1, 3),
298                 gpeLocator(PAIR_1_REMOTE_ADDRESS, 1, 0),
299                 gpeLocator(PAIR_2_REMOTE_ADDRESS, 1, 0)
300         };
301
302         return request;
303     }
304
305     private GpeLocator gpeLocator(final byte[] address, final int isIpv4, final int weight) {
306         GpeLocator locator = new GpeLocator();
307         locator.isIp4 = (byte) isIpv4;
308         locator.weight = (byte) weight;
309         locator.addr = address;
310
311         return locator;
312     }
313 }