b24d936623325d76eef4893b41f4e604d9f15a5c
[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.translate.v3po.vppclassifier;
18
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import com.google.common.base.Optional;
30 import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
31 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
32 import io.fd.honeycomb.translate.write.WriteFailedException;
33 import java.util.concurrent.CompletableFuture;
34 import java.util.concurrent.ExecutionException;
35 import org.junit.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Mock;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.OpaqueIndex;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.PacketHandlingAction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifier;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNode;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNodeName;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySession;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySessionBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySessionKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableKey;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.openvpp.jvpp.VppBaseCallException;
51 import org.openvpp.jvpp.core.dto.ClassifyAddDelSession;
52 import org.openvpp.jvpp.core.dto.ClassifyAddDelSessionReply;
53 import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewriteReply;
54
55 public class ClassifySessionWriterTest extends WriterCustomizerTest {
56
57     private static final int TABLE_INDEX = 123;
58     private static final String TABLE_NAME = "table123";
59
60     @Mock
61     private VppClassifierContextManager classfierContext;
62
63     private ClassifySessionWriter customizer;
64     private static final int SESSION_INDEX = 456;
65
66     @Override
67     public void setUp() throws Exception {
68         customizer = new ClassifySessionWriter(api, classfierContext);
69
70         when(classfierContext.containsTable(TABLE_NAME, mappingContext)).thenReturn(true);
71         when(classfierContext.getTableIndex(TABLE_NAME, mappingContext)).thenReturn(TABLE_INDEX);
72
73         final ClassifyTable table = mock(ClassifyTable.class);
74         when(table.getClassifierNode()).thenReturn(new VppNodeName("ip4-classifier"));
75         when(writeContext.readAfter(any())).thenReturn(Optional.of(table));
76         when(writeContext.readBefore(any())).thenReturn(Optional.of(table));
77     }
78
79     private static ClassifySession generateClassifySession(final long opaqueIndex, final String match) {
80         final ClassifySessionBuilder builder = new ClassifySessionBuilder();
81         builder.setOpaqueIndex(new OpaqueIndex(opaqueIndex));
82         builder.setHitNext(new VppNode(PacketHandlingAction.Deny));
83         builder.setAdvance(123);
84         builder.setMatch(new HexString(match));
85         return builder.build();
86     }
87
88     private static InstanceIdentifier<ClassifySession> getClassifySessionId(final String tableName,
89                                                                             final String match) {
90         return InstanceIdentifier.create(VppClassifier.class)
91             .child(ClassifyTable.class, new ClassifyTableKey(tableName))
92             .child(ClassifySession.class, new ClassifySessionKey(new HexString(match)));
93     }
94
95     private void whenClassifyAddDelSessionThenSuccess() throws ExecutionException, InterruptedException {
96         final CompletableFuture<ClassifyAddDelSessionReply> replyFuture = new CompletableFuture<>();
97         replyFuture.complete(new ClassifyAddDelSessionReply());
98         doReturn(replyFuture).when(api).classifyAddDelSession(any(ClassifyAddDelSession.class));
99     }
100
101     private void whenClassifyAddDelSessionThenFailure() throws ExecutionException, InterruptedException {
102         doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
103             .classifyAddDelSession(any(ClassifyAddDelSession.class));
104     }
105
106     private void verifyClassifyAddDelSessionWasInvoked(final ClassifyAddDelSession expected) {
107         ArgumentCaptor<ClassifyAddDelSession> argumentCaptor = ArgumentCaptor.forClass(ClassifyAddDelSession.class);
108         verify(api).classifyAddDelSession(argumentCaptor.capture());
109         final ClassifyAddDelSession actual = argumentCaptor.getValue();
110         assertEquals(expected.opaqueIndex, actual.opaqueIndex);
111         assertEquals(expected.isAdd, actual.isAdd);
112         assertEquals(expected.tableIndex, actual.tableIndex);
113         assertEquals(expected.hitNextIndex, actual.hitNextIndex);
114         assertArrayEquals(expected.match, actual.match);
115         assertEquals(expected.advance, actual.advance);
116     }
117
118     private void verifyClassifyAddDelSessionDeleteWasInvoked(final ClassifyAddDelSession expected) {
119         ArgumentCaptor<ClassifyAddDelSession> argumentCaptor = ArgumentCaptor.forClass(ClassifyAddDelSession.class);
120         verify(api).classifyAddDelSession(argumentCaptor.capture());
121         final ClassifyAddDelSession actual = argumentCaptor.getValue();
122         assertEquals(expected.opaqueIndex, actual.opaqueIndex);
123         assertEquals(expected.isAdd, actual.isAdd);
124         assertEquals(expected.tableIndex, actual.tableIndex);
125     }
126
127     private static ClassifyAddDelSession generateClassifyAddDelSession(final byte isAdd, final int tableIndex,
128                                                                        final int sessionIndex) {
129         final ClassifyAddDelSession request = new ClassifyAddDelSession();
130         request.isAdd = isAdd;
131         request.tableIndex = tableIndex;
132         request.opaqueIndex = sessionIndex;
133         request.hitNextIndex = 0;
134         request.advance = 123;
135         request.match =
136             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
137                 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
138         return request;
139     }
140
141     @Test
142     public void testCreate() throws Exception {
143         final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
144         final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
145         final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
146
147         whenClassifyAddDelSessionThenSuccess();
148
149         customizer.writeCurrentAttributes(id, classifySession, writeContext);
150
151         verifyClassifyAddDelSessionWasInvoked(generateClassifyAddDelSession((byte) 1, TABLE_INDEX, SESSION_INDEX));
152     }
153
154     @Test
155     public void testCreateFailed() throws Exception {
156         final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
157         final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
158         final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
159
160         whenClassifyAddDelSessionThenFailure();
161
162         try {
163             customizer.writeCurrentAttributes(id, classifySession, writeContext);
164         } catch (WriteFailedException.CreateFailedException e) {
165             assertTrue(e.getCause() instanceof VppBaseCallException);
166             verifyClassifyAddDelSessionWasInvoked(generateClassifyAddDelSession((byte) 1, TABLE_INDEX, SESSION_INDEX));
167             return;
168         }
169         fail("WriteFailedException.CreateFailedException was expected");
170     }
171
172     @Test(expected = UnsupportedOperationException.class)
173     public void testUpdate() throws Exception {
174         customizer.updateCurrentAttributes(null, null, null, writeContext);
175     }
176
177     @Test
178     public void testDelete() throws Exception {
179         final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
180         final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
181         final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
182
183         whenClassifyAddDelSessionThenSuccess();
184
185         customizer.deleteCurrentAttributes(id, classifySession, writeContext);
186
187         verifyClassifyAddDelSessionDeleteWasInvoked(
188             generateClassifyAddDelSession((byte) 0, TABLE_INDEX, SESSION_INDEX));
189     }
190
191     @Test
192     public void testDeleteFailed() throws Exception {
193         final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
194         final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
195         final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
196
197         whenClassifyAddDelSessionThenFailure();
198
199         try {
200             customizer.deleteCurrentAttributes(id, classifySession, writeContext);
201         } catch (WriteFailedException.DeleteFailedException e) {
202             assertTrue(e.getCause() instanceof VppBaseCallException);
203             verifyClassifyAddDelSessionDeleteWasInvoked(
204                 generateClassifyAddDelSession((byte) 0, TABLE_INDEX, SESSION_INDEX));
205             return;
206         }
207         fail("WriteFailedException.DeleteFailedException was expected");
208
209         customizer.deleteCurrentAttributes(id, classifySession, writeContext);
210     }
211 }