683bd69c3ebb56eda6c24964890a8cb1638cf732
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 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.honeycomb.v3po.translate.v3po.vppclassifier;
18
19 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid;
21 import static junit.framework.TestCase.assertTrue;
22 import static org.junit.Assert.assertArrayEquals;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.verifyZeroInteractions;
31 import static org.mockito.MockitoAnnotations.initMocks;
32
33 import com.google.common.base.Optional;
34 import io.fd.honeycomb.v3po.translate.MappingContext;
35 import io.fd.honeycomb.v3po.translate.v3po.test.TestHelperUtils;
36 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
37 import io.fd.honeycomb.v3po.translate.write.WriteContext;
38 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
39 import java.util.concurrent.CompletableFuture;
40 import java.util.concurrent.ExecutionException;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.ArgumentCaptor;
44 import org.mockito.Mock;
45 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.PacketHandlingAction;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifier;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNode;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableKey;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.openvpp.jvpp.VppBaseCallException;
55 import org.openvpp.jvpp.dto.ClassifyAddDelTable;
56 import org.openvpp.jvpp.dto.ClassifyAddDelTableReply;
57 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
58 import org.openvpp.jvpp.future.FutureJVpp;
59
60 public class ClassifyTableWriterTest {
61
62     private static final int TABLE_INDEX = 123;
63     private static final String TABLE_NAME = "table123";
64
65     @Mock
66     private FutureJVpp api;
67     @Mock
68     private WriteContext writeContext;
69     @Mock
70     private MappingContext mappingContext;
71
72     private NamingContext classifyTableContext;
73     private ClassifyTableWriter customizer;
74
75     @Before
76     public void setUp() throws Exception {
77         initMocks(this);
78         classifyTableContext = new NamingContext("generatedClassifyTableName", "test-instance");
79         doReturn(mappingContext).when(writeContext).getMappingContext();
80         customizer = new ClassifyTableWriter(api, classifyTableContext);
81     }
82
83     private static ClassifyTable generateClassifyTable(final String name) {
84         final ClassifyTableBuilder builder = new ClassifyTableBuilder();
85         builder.setName(name);
86         builder.setKey(new ClassifyTableKey(name));
87         builder.setSkipNVectors(0L);
88         builder.setNbuckets(2L);
89         builder.setMemorySize(2L << 20);
90         builder.setMissNextIndex(new VppNode(PacketHandlingAction.Permit));
91         builder.setMask(new HexString("00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00"));
92         return builder.build();
93     }
94
95     private static InstanceIdentifier<ClassifyTable> getClassifyTableId(final String name) {
96         return InstanceIdentifier.create(VppClassifier.class)
97             .child(ClassifyTable.class, new ClassifyTableKey(name));
98     }
99
100     private void whenClassifyAddDelTableThenSuccess() throws ExecutionException, InterruptedException {
101         final CompletableFuture<ClassifyAddDelTableReply> replyFuture = new CompletableFuture<>();
102         final ClassifyAddDelTableReply reply = new ClassifyAddDelTableReply();
103         reply.newTableIndex = TABLE_INDEX;
104         replyFuture.complete(reply);
105         doReturn(replyFuture).when(api).classifyAddDelTable(any(ClassifyAddDelTable.class));
106     }
107
108     private void whenClassifyAddDelTableThenFailure() throws ExecutionException, InterruptedException {
109         doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
110             .classifyAddDelTable(any(ClassifyAddDelTable.class));
111     }
112
113     private void verifyClassifyAddDelTableAddWasInvoked(final ClassifyAddDelTable expected) {
114         ArgumentCaptor<ClassifyAddDelTable> argumentCaptor = ArgumentCaptor.forClass(ClassifyAddDelTable.class);
115         verify(api).classifyAddDelTable(argumentCaptor.capture());
116         final ClassifyAddDelTable actual = argumentCaptor.getValue();
117         assertEquals(expected.isAdd, actual.isAdd);
118         assertEquals(~0, actual.tableIndex);
119         assertEquals(expected.nbuckets, actual.nbuckets);
120         assertEquals(expected.memorySize, actual.memorySize);
121         assertEquals(expected.skipNVectors, actual.skipNVectors);
122         assertEquals(expected.matchNVectors, actual.matchNVectors);
123         assertEquals(expected.nextTableIndex, actual.nextTableIndex);
124         assertEquals(expected.missNextIndex, actual.missNextIndex);
125         assertArrayEquals(expected.mask, actual.mask);
126     }
127
128     private void verifyClassifyAddDelTableDeleteWasInvoked(final ClassifyAddDelTable expected) {
129         ArgumentCaptor<ClassifyAddDelTable> argumentCaptor = ArgumentCaptor.forClass(ClassifyAddDelTable.class);
130         verify(api).classifyAddDelTable(argumentCaptor.capture());
131         final ClassifyAddDelTable actual = argumentCaptor.getValue();
132         assertEquals(expected.isAdd, actual.isAdd);
133         assertEquals(expected.tableIndex, actual.tableIndex);
134     }
135
136     private static ClassifyAddDelTable generateClassifyAddDelTable(final byte isAdd, final int tableIndex) {
137         final ClassifyAddDelTable request = new ClassifyAddDelTable();
138         request.isAdd = isAdd;
139         request.tableIndex = tableIndex;
140         request.nbuckets = 2;
141         request.memorySize = 2 << 20;
142         request.skipNVectors = 0;
143         request.matchNVectors = 1;
144         request.nextTableIndex = ~0;
145         request.missNextIndex = ~0;
146         request.mask =
147             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
148                 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
149         return request;
150     }
151
152     @Test
153     public void testCreate() throws Exception {
154         final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
155         final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
156
157         whenClassifyAddDelTableThenSuccess();
158
159         customizer.writeCurrentAttributes(id, classifyTable, writeContext);
160
161         verifyClassifyAddDelTableAddWasInvoked(generateClassifyAddDelTable((byte) 1, TABLE_INDEX));
162         verify(mappingContext)
163             .put(eq(getMappingIid(TABLE_NAME, "test-instance")), eq(getMapping(TABLE_NAME, TABLE_INDEX).get()));
164     }
165
166     @Test
167     public void testCreateFailed() throws Exception {
168         final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
169         final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
170
171         whenClassifyAddDelTableThenFailure();
172
173         try {
174             customizer.writeCurrentAttributes(id, classifyTable, writeContext);
175         } catch (WriteFailedException.CreateFailedException e) {
176             assertTrue(e.getCause() instanceof VppBaseCallException);
177             verifyClassifyAddDelTableAddWasInvoked(generateClassifyAddDelTable((byte) 1, TABLE_INDEX));
178             verify(mappingContext, times(0)).put(
179                 eq(getMappingIid(TABLE_NAME, "test-instance")),
180                 eq(getMapping(TABLE_NAME, TABLE_INDEX).get()));
181             return;
182         }
183         fail("WriteFailedException.CreateFailedException was expected");
184     }
185
186     @Test
187     public void testDelete() throws Exception {
188         final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
189         final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
190
191         final Optional<Mapping> ifcMapping = getMapping(TABLE_NAME, TABLE_INDEX);
192         doReturn(ifcMapping).when(mappingContext).read(any());
193
194         whenClassifyAddDelTableThenSuccess();
195
196         customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
197
198         verifyClassifyAddDelTableDeleteWasInvoked(generateClassifyAddDelTable((byte) 0, TABLE_INDEX));
199     }
200
201     @Test
202     public void testDeleteFailed() throws Exception {
203         final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
204         final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
205
206         final Optional<Mapping> ifcMapping = getMapping(TABLE_NAME, TABLE_INDEX);
207         doReturn(ifcMapping).when(mappingContext).read(any());
208
209         whenClassifyAddDelTableThenFailure();
210
211         try {
212             customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
213         } catch (WriteFailedException.DeleteFailedException e) {
214             assertTrue(e.getCause() instanceof VppBaseCallException);
215             verifyClassifyAddDelTableDeleteWasInvoked(generateClassifyAddDelTable((byte) 0, TABLE_INDEX));
216             return;
217         }
218         fail("WriteFailedException.DeleteFailedException was expected");
219
220         customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
221     }
222
223     @Test
224     public void testUpdate() throws Exception {
225         final ClassifyTable classifyTableBefore = generateClassifyTable(TABLE_NAME);
226         final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
227         customizer.updateCurrentAttributes(id, classifyTableBefore, new ClassifyTableBuilder().build(), writeContext);
228
229         verifyZeroInteractions(api);
230     }
231 }