0869986ee75257be0368187492b445e5c23ff938
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / vppclassifier / ClassifyTableWriterTest.java
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.translate.v3po.vppclassifier;
18
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;
26
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;
43
44 public class ClassifyTableWriterTest extends WriterCustomizerTest {
45
46     private static final int TABLE_INDEX = 123;
47     private static final String TABLE_NAME = "table123";
48
49     @Mock
50     private VppClassifierContextManager classifierContext;
51
52     private ClassifyTableWriter customizer;
53
54     @Override
55     public void setUp() throws Exception {
56         customizer = new ClassifyTableWriter(api, classifierContext);
57     }
58
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();
70     }
71
72     private static InstanceIdentifier<ClassifyTable> getClassifyTableId(final String name) {
73         return InstanceIdentifier.create(VppClassifier.class)
74                 .child(ClassifyTable.class, new ClassifyTableKey(name));
75     }
76
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));
81     }
82
83     private void whenClassifyAddDelTableThenFailure() {
84         doReturn(failedFuture()).when(api).classifyAddDelTable(any(ClassifyAddDelTable.class));
85     }
86
87     private static ClassifyAddDelTable generateClassifyAddDelTable(final byte isAdd) {
88         return generateClassifyAddDelTable(isAdd, -1);
89     }
90
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;
95         request.nbuckets = 2;
96         request.memorySize = 2 << 20;
97         request.skipNVectors = 0;
98         request.matchNVectors = 1;
99         request.nextTableIndex = ~0;
100         request.missNextIndex = ~0;
101         request.mask =
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};
104         return request;
105     }
106
107     @Test
108     public void testCreate() throws Exception {
109         final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
110         final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
111
112         whenClassifyAddDelTableThenSuccess();
113
114         customizer.writeCurrentAttributes(id, classifyTable, writeContext);
115
116         verify(api).classifyAddDelTable(generateClassifyAddDelTable((byte) 1));
117         verify(classifierContext)
118                 .addTable(TABLE_INDEX, classifyTable.getName(), classifyTable.getClassifierNode(), mappingContext);
119     }
120
121     @Test
122     public void testCreateFailed() throws Exception {
123         final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
124         final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
125
126         whenClassifyAddDelTableThenFailure();
127
128         try {
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);
135             return;
136         }
137         fail("WriteFailedException.CreateFailedException was expected");
138     }
139
140     @Test
141     public void testDelete() throws Exception {
142         final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
143         final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
144
145         when(classifierContext.containsTable(TABLE_NAME, mappingContext)).thenReturn(true);
146         when(classifierContext.getTableIndex(TABLE_NAME, mappingContext)).thenReturn(TABLE_INDEX);
147         whenClassifyAddDelTableThenSuccess();
148
149         customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
150
151         verify(api).classifyAddDelTable(generateClassifyAddDelTable((byte) 0, TABLE_INDEX));
152     }
153
154     @Test
155     public void testDeleteFailed() throws Exception {
156         final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
157         final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
158
159         when(classifierContext.containsTable(TABLE_NAME, mappingContext)).thenReturn(true);
160         when(classifierContext.getTableIndex(TABLE_NAME, mappingContext)).thenReturn(TABLE_INDEX);
161         whenClassifyAddDelTableThenFailure();
162
163         try {
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));
168             return;
169         }
170         fail("WriteFailedException.DeleteFailedException was expected");
171
172         customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
173     }
174
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);
180     }
181 }