2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.v3po.vppclassifier;
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;
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;
60 public class ClassifyTableWriterTest {
62 private static final int TABLE_INDEX = 123;
63 private static final String TABLE_NAME = "table123";
66 private FutureJVpp api;
68 private WriteContext writeContext;
70 private MappingContext mappingContext;
72 private NamingContext classifyTableContext;
73 private ClassifyTableWriter customizer;
76 public void setUp() throws Exception {
78 classifyTableContext = new NamingContext("generatedClassifyTableName", "test-instance");
79 doReturn(mappingContext).when(writeContext).getMappingContext();
80 customizer = new ClassifyTableWriter(api, classifyTableContext);
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();
95 private static InstanceIdentifier<ClassifyTable> getClassifyTableId(final String name) {
96 return InstanceIdentifier.create(VppClassifier.class)
97 .child(ClassifyTable.class, new ClassifyTableKey(name));
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));
108 private void whenClassifyAddDelTableThenFailure() throws ExecutionException, InterruptedException {
109 doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
110 .classifyAddDelTable(any(ClassifyAddDelTable.class));
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);
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);
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;
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};
153 public void testCreate() throws Exception {
154 final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
155 final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
157 whenClassifyAddDelTableThenSuccess();
159 customizer.writeCurrentAttributes(id, classifyTable, writeContext);
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()));
167 public void testCreateFailed() throws Exception {
168 final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
169 final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
171 whenClassifyAddDelTableThenFailure();
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()));
183 fail("WriteFailedException.CreateFailedException was expected");
187 public void testDelete() throws Exception {
188 final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
189 final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
191 final Optional<Mapping> ifcMapping = getMapping(TABLE_NAME, TABLE_INDEX);
192 doReturn(ifcMapping).when(mappingContext).read(any());
194 whenClassifyAddDelTableThenSuccess();
196 customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
198 verifyClassifyAddDelTableDeleteWasInvoked(generateClassifyAddDelTable((byte) 0, TABLE_INDEX));
202 public void testDeleteFailed() throws Exception {
203 final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
204 final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
206 final Optional<Mapping> ifcMapping = getMapping(TABLE_NAME, TABLE_INDEX);
207 doReturn(ifcMapping).when(mappingContext).read(any());
209 whenClassifyAddDelTableThenFailure();
212 customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
213 } catch (WriteFailedException.DeleteFailedException e) {
214 assertTrue(e.getCause() instanceof VppBaseCallException);
215 verifyClassifyAddDelTableDeleteWasInvoked(generateClassifyAddDelTable((byte) 0, TABLE_INDEX));
218 fail("WriteFailedException.DeleteFailedException was expected");
220 customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
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);
229 verifyZeroInteractions(api);