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.translate.v3po.vppclassifier;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
29 import io.fd.vpp.jvpp.VppBaseCallException;
30 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
31 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTableReply;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.PacketHandlingAction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.VppClassifier;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.VppNode;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.VppNodeName;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.vpp.classifier.ClassifyTable;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.vpp.classifier.ClassifyTableBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.vpp.classifier.ClassifyTableKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 public class ClassifyTableWriterTest extends WriterCustomizerTest {
46 private static final int TABLE_INDEX = 123;
47 private static final String TABLE_NAME = "table123";
50 private VppClassifierContextManager classifierContext;
52 private ClassifyTableWriter customizer;
55 public void setUp() throws Exception {
56 customizer = new ClassifyTableWriter(api, classifierContext);
59 private static ClassifyTable generateClassifyTable(final String name) {
60 final ClassifyTableBuilder builder = new ClassifyTableBuilder();
61 builder.setName(name);
62 builder.setClassifierNode(new VppNodeName("ip4-classifier"));
63 builder.setKey(new ClassifyTableKey(name));
64 builder.setSkipNVectors(0L);
65 builder.setNbuckets(2L);
66 builder.setMemorySize(2L << 20);
67 builder.setMissNext(new VppNode(PacketHandlingAction.Permit));
68 builder.setMask(new HexString("00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00"));
69 return builder.build();
72 private static InstanceIdentifier<ClassifyTable> getClassifyTableId(final String name) {
73 return InstanceIdentifier.create(VppClassifier.class)
74 .child(ClassifyTable.class, new ClassifyTableKey(name));
77 private void whenClassifyAddDelTableThenSuccess() {
78 final ClassifyAddDelTableReply reply = new ClassifyAddDelTableReply();
79 reply.newTableIndex = TABLE_INDEX;
80 doReturn(future(reply)).when(api).classifyAddDelTable(any(ClassifyAddDelTable.class));
83 private void whenClassifyAddDelTableThenFailure() {
84 doReturn(failedFuture()).when(api).classifyAddDelTable(any(ClassifyAddDelTable.class));
87 private static ClassifyAddDelTable generateClassifyAddDelTable(final byte isAdd) {
88 return generateClassifyAddDelTable(isAdd, -1);
91 private static ClassifyAddDelTable generateClassifyAddDelTable(final byte isAdd, final int tableIndex) {
92 final ClassifyAddDelTable request = new ClassifyAddDelTable();
93 request.isAdd = isAdd;
94 request.tableIndex = tableIndex;
96 request.memorySize = 2 << 20;
97 request.skipNVectors = 0;
98 request.matchNVectors = 1;
99 request.nextTableIndex = ~0;
100 request.missNextIndex = ~0;
102 new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
103 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
108 public void testCreate() throws Exception {
109 final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
110 final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
112 whenClassifyAddDelTableThenSuccess();
114 customizer.writeCurrentAttributes(id, classifyTable, writeContext);
116 verify(api).classifyAddDelTable(generateClassifyAddDelTable((byte) 1));
117 verify(classifierContext)
118 .addTable(TABLE_INDEX, classifyTable.getName(), classifyTable.getClassifierNode(), mappingContext);
122 public void testCreateFailed() throws Exception {
123 final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
124 final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
126 whenClassifyAddDelTableThenFailure();
129 customizer.writeCurrentAttributes(id, classifyTable, writeContext);
130 } catch (WriteFailedException e) {
131 assertTrue(e.getCause() instanceof VppBaseCallException);
132 verify(api).classifyAddDelTable(generateClassifyAddDelTable((byte) 1));
133 verify(classifierContext, times(0))
134 .addTable(TABLE_INDEX, classifyTable.getName(), classifyTable.getClassifierNode(), mappingContext);
137 fail("WriteFailedException.CreateFailedException was expected");
141 public void testDelete() throws Exception {
142 final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
143 final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
145 when(classifierContext.containsTable(TABLE_NAME, mappingContext)).thenReturn(true);
146 when(classifierContext.getTableIndex(TABLE_NAME, mappingContext)).thenReturn(TABLE_INDEX);
147 whenClassifyAddDelTableThenSuccess();
149 customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
151 verify(api).classifyAddDelTable(generateClassifyAddDelTable((byte) 0, TABLE_INDEX));
155 public void testDeleteFailed() throws Exception {
156 final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
157 final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
159 when(classifierContext.containsTable(TABLE_NAME, mappingContext)).thenReturn(true);
160 when(classifierContext.getTableIndex(TABLE_NAME, mappingContext)).thenReturn(TABLE_INDEX);
161 whenClassifyAddDelTableThenFailure();
164 customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
165 } catch (WriteFailedException e) {
166 assertTrue(e.getCause() instanceof VppBaseCallException);
167 verify(api).classifyAddDelTable(generateClassifyAddDelTable((byte) 0, TABLE_INDEX));
170 fail("WriteFailedException.DeleteFailedException was expected");
172 customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
175 @Test(expected = UnsupportedOperationException.class)
176 public void testUpdate() throws Exception {
177 final ClassifyTable classifyTableBefore = generateClassifyTable(TABLE_NAME);
178 final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
179 customizer.updateCurrentAttributes(id, classifyTableBefore, new ClassifyTableBuilder().build(), writeContext);