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 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;
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.dto.ClassifyAddDelSession;
55 import org.openvpp.jvpp.dto.ClassifyAddDelSessionReply;
56 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
57 import org.openvpp.jvpp.future.FutureJVpp;
59 public class ClassifySessionWriterTest {
61 private static final int TABLE_INDEX = 123;
62 private static final String TABLE_NAME = "table123";
65 private FutureJVpp api;
67 private WriteContext writeContext;
69 private MappingContext mappingContext;
71 private NamingContext classifyTableContext;
72 private ClassifySessionWriter customizer;
73 private static final int SESSION_INDEX = 456;
76 public void setUp() throws Exception {
78 classifyTableContext = new NamingContext("generatedClassifyTableName", "test-instance");
79 doReturn(mappingContext).when(writeContext).getMappingContext();
80 customizer = new ClassifySessionWriter(api, classifyTableContext);
82 final Optional<Mapping> ifcMapping = getMapping(TABLE_NAME, TABLE_INDEX);
83 doReturn(ifcMapping).when(mappingContext).read(any());
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();
95 private static InstanceIdentifier<ClassifySession> getClassifySessionId(final String tableName,
97 return InstanceIdentifier.create(VppClassifier.class)
98 .child(ClassifyTable.class, new ClassifyTableKey(tableName))
99 .child(ClassifySession.class, new ClassifySessionKey(new HexString(match)));
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));
108 private void whenClassifyAddDelSessionThenFailure() throws ExecutionException, InterruptedException {
109 doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
110 .classifyAddDelSession(any(ClassifyAddDelSession.class));
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);
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);
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;
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};
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);
154 whenClassifyAddDelSessionThenSuccess();
156 customizer.writeCurrentAttributes(id, classifySession, writeContext);
158 verifyClassifyAddDelSessionWasInvoked(generateClassifyAddDelSession((byte) 1, TABLE_INDEX, SESSION_INDEX));
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);
167 whenClassifyAddDelSessionThenFailure();
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));
176 fail("WriteFailedException.CreateFailedException was expected");
179 @Test(expected = UnsupportedOperationException.class)
180 public void testUpdate() throws Exception {
181 customizer.updateCurrentAttributes(null, null, null, writeContext);
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);
190 whenClassifyAddDelSessionThenSuccess();
192 customizer.deleteCurrentAttributes(id, classifySession, writeContext);
194 verifyClassifyAddDelSessionDeleteWasInvoked(
195 generateClassifyAddDelSession((byte) 0, TABLE_INDEX, SESSION_INDEX));
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);
204 whenClassifyAddDelSessionThenFailure();
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));
214 fail("WriteFailedException.DeleteFailedException was expected");
216 customizer.deleteCurrentAttributes(id, classifySession, writeContext);