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