summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/test/java
diff options
context:
space:
mode:
authorMarek Gradzki <mgradzki@cisco.com>2016-07-13 08:48:14 +0200
committerMarek Gradzki <mgradzki@cisco.com>2016-07-14 10:53:40 +0200
commitb8190b726c63b2a188a4cffac28ab15977f1d4bc (patch)
treef1450c89590664f992a45ca3a20fe6d521818740 /v3po/v3po2vpp/src/test/java
parent5fc7ae574e71ac326521d3fe8860f38604bedf53 (diff)
HONEYCOMB-49: VPP classifier model. Translation layer implementation
Change-Id: I7d91f29f621fbb7ede9b3dd5d49a9b5c3ba58565 Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'v3po/v3po2vpp/src/test/java')
-rw-r--r--v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifySessionReaderTest.java136
-rw-r--r--v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifySessionWriterTest.java218
-rw-r--r--v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifyTableReaderTest.java153
-rw-r--r--v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifyTableWriterTest.java231
4 files changed, 738 insertions, 0 deletions
diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifySessionReaderTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifySessionReaderTest.java
new file mode 100644
index 000000000..ce0d3f62e
--- /dev/null
+++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifySessionReaderTest.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.v3po.translate.v3po.vppclassifier;
+
+import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.base.Optional;
+import io.fd.honeycomb.v3po.translate.ModificationCache;
+import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
+import io.fd.honeycomb.v3po.translate.spi.read.RootReaderCustomizer;
+import io.fd.honeycomb.v3po.translate.v3po.test.ListReaderCustomizerTest;
+import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.attributes.ClassifySession;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.attributes.ClassifySessionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.attributes.ClassifySessionKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.openvpp.jvpp.dto.ClassifySessionDetails;
+import org.openvpp.jvpp.dto.ClassifySessionDetailsReplyDump;
+import org.openvpp.jvpp.dto.ClassifySessionDump;
+
+public class ClassifySessionReaderTest extends
+ ListReaderCustomizerTest<ClassifySession, ClassifySessionKey, ClassifySessionBuilder> {
+
+ private static final String MATCH_1 = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
+ private static final String MATCH_2 = "00:00:00:00:00:00:01:02:03:04:05:07:00:00:00:00";
+
+ private static final int TABLE_INDEX = 1;
+ private static final String TABLE_NAME = "table1";
+
+ private NamingContext classifyTableContext;
+
+ public ClassifySessionReaderTest() {
+ super(ClassifySession.class);
+ }
+
+ @Override
+ public void setUpBefore() {
+ classifyTableContext = new NamingContext("classifyTableContext", "test-instance");
+
+ final Optional<Mapping> ifcMapping = getMapping(TABLE_NAME, TABLE_INDEX);
+ doReturn(ifcMapping).when(mappingContext).read(any());
+ }
+
+ @Override
+ protected RootReaderCustomizer<ClassifySession, ClassifySessionBuilder> initCustomizer() {
+ return new ClassifySessionReader(api, classifyTableContext);
+ }
+
+ private static InstanceIdentifier<ClassifySession> getClassifySessionId(final String tableName,
+ final String match) {
+ return InstanceIdentifier.create(VppClassifier.class)
+ .child(ClassifyTable.class, new ClassifyTableKey(tableName))
+ .child(ClassifySession.class, new ClassifySessionKey(new HexString(match)));
+ }
+
+ @Test
+ public void testMerge() {
+ final ClassifyTableBuilder builder = mock(ClassifyTableBuilder.class);
+ final List<ClassifySession> value = mock(List.class);
+ getCustomizer().merge(builder, value);
+ verify(builder).setClassifySession(value);
+ }
+
+ @Test
+ public void testReadWithCache() throws ReadFailedException {
+ final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, MATCH_1);
+ final ClassifySessionBuilder builder = mock(ClassifySessionBuilder.class);
+ final ModificationCache cache = new ModificationCache();
+ final ClassifySessionDetailsReplyDump dump = new ClassifySessionDetailsReplyDump();
+ final ClassifySessionDetails details = new ClassifySessionDetails();
+ details.match =
+ new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
+ (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
+ dump.classifySessionDetails = Collections.singletonList(details);
+ cache.put(ClassifySessionReader.CACHE_KEY + id.firstKeyOf(ClassifyTable.class), dump);
+ when(ctx.getModificationCache()).thenReturn(cache);
+
+ getCustomizer().readCurrentAttributes(id, builder, ctx);
+ }
+
+ @Test
+ public void testGetAllIds() throws ReadFailedException {
+ final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, MATCH_1);
+ final ClassifySessionDetailsReplyDump dump = new ClassifySessionDetailsReplyDump();
+ final ClassifySessionDetails details1 = new ClassifySessionDetails();
+ details1.match =
+ new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
+ (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
+ final ClassifySessionDetails details2 = new ClassifySessionDetails();
+ details2.match =
+ new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
+ (byte) 0x05, (byte) 0x07, 0x00, 0x00, 0x00, 0x00};
+ dump.classifySessionDetails = Arrays.asList(details1, details2);
+
+ final CompletableFuture<ClassifySessionDetailsReplyDump> replyFuture = new CompletableFuture<>();
+ replyFuture.complete(dump);
+ doReturn(replyFuture).when(api).classifySessionDump(any(ClassifySessionDump.class));
+
+ final List<ClassifySessionKey> allIds = getCustomizer().getAllIds(id, ctx);
+ assertEquals(2, allIds.size());
+ assertEquals(MATCH_1, allIds.get(0).getMatch().getValue());
+ assertEquals(MATCH_2, allIds.get(1).getMatch().getValue());
+ }
+
+} \ No newline at end of file
diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifySessionWriterTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifySessionWriterTest.java
new file mode 100644
index 000000000..04a766fa3
--- /dev/null
+++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifySessionWriterTest.java
@@ -0,0 +1,218 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.v3po.translate.v3po.vppclassifier;
+
+import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
+import static junit.framework.TestCase.assertTrue;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+import static org.mockito.MockitoAnnotations.initMocks;
+
+import com.google.common.base.Optional;
+import io.fd.honeycomb.v3po.translate.MappingContext;
+import io.fd.honeycomb.v3po.translate.v3po.test.TestHelperUtils;
+import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
+import io.fd.honeycomb.v3po.translate.write.WriteContext;
+import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.OpaqueIndex;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.PacketHandlingAction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.attributes.ClassifySession;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.attributes.ClassifySessionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.attributes.ClassifySessionKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.openvpp.jvpp.VppBaseCallException;
+import org.openvpp.jvpp.dto.ClassifyAddDelSession;
+import org.openvpp.jvpp.dto.ClassifyAddDelSessionReply;
+import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
+import org.openvpp.jvpp.future.FutureJVpp;
+
+public class ClassifySessionWriterTest {
+
+ private static final int TABLE_INDEX = 123;
+ private static final String TABLE_NAME = "table123";
+
+ @Mock
+ private FutureJVpp api;
+ @Mock
+ private WriteContext writeContext;
+ @Mock
+ private MappingContext mappingContext;
+
+ private NamingContext classifyTableContext;
+ private ClassifySessionWriter customizer;
+ private static final int SESSION_INDEX = 456;
+
+ @Before
+ public void setUp() throws Exception {
+ initMocks(this);
+ classifyTableContext = new NamingContext("generatedClassifyTableName", "test-instance");
+ doReturn(mappingContext).when(writeContext).getMappingContext();
+ customizer = new ClassifySessionWriter(api, classifyTableContext);
+
+ final Optional<Mapping> ifcMapping = getMapping(TABLE_NAME, TABLE_INDEX);
+ doReturn(ifcMapping).when(mappingContext).read(any());
+ }
+
+ private static ClassifySession generateClassifySession(final long opaqueIndex, final String match) {
+ final ClassifySessionBuilder builder = new ClassifySessionBuilder();
+ builder.setOpaqueIndex(new OpaqueIndex(opaqueIndex));
+ builder.setHitNextIndex(new VppNode(PacketHandlingAction.Deny));
+ builder.setAdvance(123);
+ builder.setMatch(new HexString(match));
+ return builder.build();
+ }
+
+ private static InstanceIdentifier<ClassifySession> getClassifySessionId(final String tableName,
+ final String match) {
+ return InstanceIdentifier.create(VppClassifier.class)
+ .child(ClassifyTable.class, new ClassifyTableKey(tableName))
+ .child(ClassifySession.class, new ClassifySessionKey(new HexString(match)));
+ }
+
+ private void whenClassifyAddDelSessionThenSuccess() throws ExecutionException, InterruptedException {
+ final CompletableFuture<ClassifyAddDelSessionReply> replyFuture = new CompletableFuture<>();
+ replyFuture.complete(new ClassifyAddDelSessionReply());
+ doReturn(replyFuture).when(api).classifyAddDelSession(any(ClassifyAddDelSession.class));
+ }
+
+ private void whenClassifyAddDelSessionThenFailure() throws ExecutionException, InterruptedException {
+ doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
+ .classifyAddDelSession(any(ClassifyAddDelSession.class));
+ }
+
+ private void verifyClassifyAddDelSessionWasInvoked(final ClassifyAddDelSession expected) {
+ ArgumentCaptor<ClassifyAddDelSession> argumentCaptor = ArgumentCaptor.forClass(ClassifyAddDelSession.class);
+ verify(api).classifyAddDelSession(argumentCaptor.capture());
+ final ClassifyAddDelSession actual = argumentCaptor.getValue();
+ assertEquals(expected.opaqueIndex, actual.opaqueIndex);
+ assertEquals(expected.isAdd, actual.isAdd);
+ assertEquals(expected.tableIndex, actual.tableIndex);
+ assertEquals(expected.hitNextIndex, actual.hitNextIndex);
+ assertArrayEquals(expected.match, actual.match);
+ assertEquals(expected.advance, actual.advance);
+ }
+
+ private void verifyClassifyAddDelSessionDeleteWasInvoked(final ClassifyAddDelSession expected) {
+ ArgumentCaptor<ClassifyAddDelSession> argumentCaptor = ArgumentCaptor.forClass(ClassifyAddDelSession.class);
+ verify(api).classifyAddDelSession(argumentCaptor.capture());
+ final ClassifyAddDelSession actual = argumentCaptor.getValue();
+ assertEquals(expected.opaqueIndex, actual.opaqueIndex);
+ assertEquals(expected.isAdd, actual.isAdd);
+ assertEquals(expected.tableIndex, actual.tableIndex);
+ }
+
+ private static ClassifyAddDelSession generateClassifyAddDelSession(final byte isAdd, final int tableIndex,
+ final int sessionIndex) {
+ final ClassifyAddDelSession request = new ClassifyAddDelSession();
+ request.isAdd = isAdd;
+ request.tableIndex = tableIndex;
+ request.opaqueIndex = sessionIndex;
+ request.hitNextIndex = 0;
+ request.advance = 123;
+ request.match =
+ new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
+ (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
+ return request;
+ }
+
+ @Test
+ public void testCreate() throws Exception {
+ final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
+ final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
+ final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
+
+ whenClassifyAddDelSessionThenSuccess();
+
+ customizer.writeCurrentAttributes(id, classifySession, writeContext);
+
+ verifyClassifyAddDelSessionWasInvoked(generateClassifyAddDelSession((byte) 1, TABLE_INDEX, SESSION_INDEX));
+ }
+
+ @Test
+ public void testCreateFailed() throws Exception {
+ final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
+ final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
+ final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
+
+ whenClassifyAddDelSessionThenFailure();
+
+ try {
+ customizer.writeCurrentAttributes(id, classifySession, writeContext);
+ } catch (WriteFailedException.CreateFailedException e) {
+ assertTrue(e.getCause() instanceof VppBaseCallException);
+ verifyClassifyAddDelSessionWasInvoked(generateClassifyAddDelSession((byte) 1, TABLE_INDEX, SESSION_INDEX));
+ return;
+ }
+ fail("WriteFailedException.CreateFailedException was expected");
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testUpdate() throws Exception {
+ customizer.updateCurrentAttributes(null, null, null, writeContext);
+ }
+
+ @Test
+ public void testDelete() throws Exception {
+ final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
+ final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
+ final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
+
+ whenClassifyAddDelSessionThenSuccess();
+
+ customizer.deleteCurrentAttributes(id, classifySession, writeContext);
+
+ verifyClassifyAddDelSessionDeleteWasInvoked(
+ generateClassifyAddDelSession((byte) 0, TABLE_INDEX, SESSION_INDEX));
+ }
+
+ @Test
+ public void testDeleteFailed() throws Exception {
+ final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
+ final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
+ final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
+
+ whenClassifyAddDelSessionThenFailure();
+
+ try {
+ customizer.deleteCurrentAttributes(id, classifySession, writeContext);
+ } catch (WriteFailedException.DeleteFailedException e) {
+ assertTrue(e.getCause() instanceof VppBaseCallException);
+ verifyClassifyAddDelSessionDeleteWasInvoked(
+ generateClassifyAddDelSession((byte) 0, TABLE_INDEX, SESSION_INDEX));
+ return;
+ }
+ fail("WriteFailedException.DeleteFailedException was expected");
+
+ customizer.deleteCurrentAttributes(id, classifySession, writeContext);
+ }
+} \ No newline at end of file
diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifyTableReaderTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifyTableReaderTest.java
new file mode 100644
index 000000000..14d6c5544
--- /dev/null
+++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifyTableReaderTest.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.v3po.translate.v3po.vppclassifier;
+
+import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
+import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
+import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
+import io.fd.honeycomb.v3po.translate.spi.read.RootReaderCustomizer;
+import io.fd.honeycomb.v3po.translate.v3po.test.ListReaderCustomizerTest;
+import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.PacketHandlingAction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifierBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+import org.openvpp.jvpp.dto.ClassifyTableIds;
+import org.openvpp.jvpp.dto.ClassifyTableIdsReply;
+import org.openvpp.jvpp.dto.ClassifyTableInfo;
+import org.openvpp.jvpp.dto.ClassifyTableInfoReply;
+
+public class ClassifyTableReaderTest extends
+ ListReaderCustomizerTest<ClassifyTable, ClassifyTableKey, ClassifyTableBuilder> {
+
+ private static final int TABLE_INDEX_1 = 1;
+ private static final String TABLE_NAME_1 = "table1";
+ private static final int TABLE_INDEX_2 = 2;
+ private static final String TABLE_NAME_2 = "table2";
+
+ private NamingContext classifyTableContext;
+
+ public ClassifyTableReaderTest() {
+ super(ClassifyTable.class);
+ }
+
+ @Override
+ public void setUpBefore() {
+ classifyTableContext = new NamingContext("classifyTableContext", "test-instance");
+
+ final KeyedInstanceIdentifier<Mapping, MappingKey> t0Id = getMappingIid(TABLE_NAME_1, "test-instance");
+ final KeyedInstanceIdentifier<Mapping, MappingKey> t1Id = getMappingIid(TABLE_NAME_2, "test-instance");
+ final Optional<Mapping> t0 = getMapping(TABLE_NAME_1, TABLE_INDEX_1);
+ final Optional<Mapping> t1 = getMapping(TABLE_NAME_2, TABLE_INDEX_2);
+ final List<Mapping> allMappings = Lists.newArrayList(t0.get(), t1.get());
+ final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build();
+ doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(t0Id.firstIdentifierOf(Mappings.class));
+ doReturn(t0).when(mappingContext).read(t0Id);
+ doReturn(t1).when(mappingContext).read(t1Id);
+ }
+
+ @Override
+ protected RootReaderCustomizer<ClassifyTable, ClassifyTableBuilder> initCustomizer() {
+ return new ClassifyTableReader(api, classifyTableContext);
+ }
+
+ private static InstanceIdentifier<ClassifyTable> getClassifyTableId(final String name) {
+ return InstanceIdentifier.create(VppClassifier.class)
+ .child(ClassifyTable.class, new ClassifyTableKey(name));
+ }
+
+ private static ClassifyTableInfoReply generateClassifyTableInfoReply() {
+ final ClassifyTableInfoReply reply = new ClassifyTableInfoReply();
+ reply.tableId = TABLE_INDEX_1;
+ reply.nbuckets = 2;
+ reply.skipNVectors = 0;
+ reply.matchNVectors = 1;
+ reply.nextTableIndex = ~0;
+ reply.missNextIndex = ~0;
+ reply.mask =
+ new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
+ (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
+ return reply;
+ }
+
+ private void verifyClasifyTableRead(final ClassifyTableBuilder builder) {
+ verify(builder).setName(TABLE_NAME_1);
+ verify(builder).setNbuckets(2L);
+ verify(builder, times(0)).setNextTable(anyString());
+ verify(builder).setMissNextIndex(new VppNode(PacketHandlingAction.Permit));
+ verify(builder).setMask(new HexString("00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00"));
+ verify(builder).setActiveSessions(0L);
+ }
+
+ @Test
+ public void testMerge() {
+ final VppClassifierBuilder builder = mock(VppClassifierBuilder.class);
+ final List<ClassifyTable> value = mock(List.class);
+ getCustomizer().merge(builder, value);
+ verify(builder).setClassifyTable(value);
+ }
+
+ @Test
+ public void testRead() throws ReadFailedException {
+ final CompletableFuture<ClassifyTableInfoReply> replyFuture = new CompletableFuture<>();
+ replyFuture.complete(generateClassifyTableInfoReply());
+ doReturn(replyFuture).when(api).classifyTableInfo(any(ClassifyTableInfo.class));
+
+ final ClassifyTableBuilder builder = mock(ClassifyTableBuilder.class);
+ getCustomizer().readCurrentAttributes(getClassifyTableId(TABLE_NAME_1), builder, ctx);
+
+ verifyClasifyTableRead(builder);
+ }
+
+ @Test
+ public void testGetAllIds() throws ReadFailedException {
+ final CompletableFuture<ClassifyTableIdsReply> replyFuture = new CompletableFuture<>();
+ final ClassifyTableIdsReply reply = new ClassifyTableIdsReply();
+ reply.ids = new int[] {1, 2};
+ replyFuture.complete(reply);
+ doReturn(replyFuture).when(api).classifyTableIds(any(ClassifyTableIds.class));
+
+ final List<ClassifyTableKey> allIds = getCustomizer().getAllIds(getClassifyTableId(TABLE_NAME_1), ctx);
+
+ assertEquals(reply.ids.length, allIds.size());
+ assertEquals(TABLE_NAME_1, allIds.get(0).getName());
+ assertEquals(TABLE_NAME_2, allIds.get(1).getName());
+ }
+} \ No newline at end of file
diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifyTableWriterTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifyTableWriterTest.java
new file mode 100644
index 000000000..683bd69c3
--- /dev/null
+++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vppclassifier/ClassifyTableWriterTest.java
@@ -0,0 +1,231 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.v3po.translate.v3po.vppclassifier;
+
+import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
+import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid;
+import static junit.framework.TestCase.assertTrue;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.MockitoAnnotations.initMocks;
+
+import com.google.common.base.Optional;
+import io.fd.honeycomb.v3po.translate.MappingContext;
+import io.fd.honeycomb.v3po.translate.v3po.test.TestHelperUtils;
+import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
+import io.fd.honeycomb.v3po.translate.write.WriteContext;
+import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.PacketHandlingAction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.openvpp.jvpp.VppBaseCallException;
+import org.openvpp.jvpp.dto.ClassifyAddDelTable;
+import org.openvpp.jvpp.dto.ClassifyAddDelTableReply;
+import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
+import org.openvpp.jvpp.future.FutureJVpp;
+
+public class ClassifyTableWriterTest {
+
+ private static final int TABLE_INDEX = 123;
+ private static final String TABLE_NAME = "table123";
+
+ @Mock
+ private FutureJVpp api;
+ @Mock
+ private WriteContext writeContext;
+ @Mock
+ private MappingContext mappingContext;
+
+ private NamingContext classifyTableContext;
+ private ClassifyTableWriter customizer;
+
+ @Before
+ public void setUp() throws Exception {
+ initMocks(this);
+ classifyTableContext = new NamingContext("generatedClassifyTableName", "test-instance");
+ doReturn(mappingContext).when(writeContext).getMappingContext();
+ customizer = new ClassifyTableWriter(api, classifyTableContext);
+ }
+
+ private static ClassifyTable generateClassifyTable(final String name) {
+ final ClassifyTableBuilder builder = new ClassifyTableBuilder();
+ builder.setName(name);
+ builder.setKey(new ClassifyTableKey(name));
+ builder.setSkipNVectors(0L);
+ builder.setNbuckets(2L);
+ builder.setMemorySize(2L << 20);
+ builder.setMissNextIndex(new VppNode(PacketHandlingAction.Permit));
+ builder.setMask(new HexString("00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00"));
+ return builder.build();
+ }
+
+ private static InstanceIdentifier<ClassifyTable> getClassifyTableId(final String name) {
+ return InstanceIdentifier.create(VppClassifier.class)
+ .child(ClassifyTable.class, new ClassifyTableKey(name));
+ }
+
+ private void whenClassifyAddDelTableThenSuccess() throws ExecutionException, InterruptedException {
+ final CompletableFuture<ClassifyAddDelTableReply> replyFuture = new CompletableFuture<>();
+ final ClassifyAddDelTableReply reply = new ClassifyAddDelTableReply();
+ reply.newTableIndex = TABLE_INDEX;
+ replyFuture.complete(reply);
+ doReturn(replyFuture).when(api).classifyAddDelTable(any(ClassifyAddDelTable.class));
+ }
+
+ private void whenClassifyAddDelTableThenFailure() throws ExecutionException, InterruptedException {
+ doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
+ .classifyAddDelTable(any(ClassifyAddDelTable.class));
+ }
+
+ private void verifyClassifyAddDelTableAddWasInvoked(final ClassifyAddDelTable expected) {
+ ArgumentCaptor<ClassifyAddDelTable> argumentCaptor = ArgumentCaptor.forClass(ClassifyAddDelTable.class);
+ verify(api).classifyAddDelTable(argumentCaptor.capture());
+ final ClassifyAddDelTable actual = argumentCaptor.getValue();
+ assertEquals(expected.isAdd, actual.isAdd);
+ assertEquals(~0, actual.tableIndex);
+ assertEquals(expected.nbuckets, actual.nbuckets);
+ assertEquals(expected.memorySize, actual.memorySize);
+ assertEquals(expected.skipNVectors, actual.skipNVectors);
+ assertEquals(expected.matchNVectors, actual.matchNVectors);
+ assertEquals(expected.nextTableIndex, actual.nextTableIndex);
+ assertEquals(expected.missNextIndex, actual.missNextIndex);
+ assertArrayEquals(expected.mask, actual.mask);
+ }
+
+ private void verifyClassifyAddDelTableDeleteWasInvoked(final ClassifyAddDelTable expected) {
+ ArgumentCaptor<ClassifyAddDelTable> argumentCaptor = ArgumentCaptor.forClass(ClassifyAddDelTable.class);
+ verify(api).classifyAddDelTable(argumentCaptor.capture());
+ final ClassifyAddDelTable actual = argumentCaptor.getValue();
+ assertEquals(expected.isAdd, actual.isAdd);
+ assertEquals(expected.tableIndex, actual.tableIndex);
+ }
+
+ private static ClassifyAddDelTable generateClassifyAddDelTable(final byte isAdd, final int tableIndex) {
+ final ClassifyAddDelTable request = new ClassifyAddDelTable();
+ request.isAdd = isAdd;
+ request.tableIndex = tableIndex;
+ request.nbuckets = 2;
+ request.memorySize = 2 << 20;
+ request.skipNVectors = 0;
+ request.matchNVectors = 1;
+ request.nextTableIndex = ~0;
+ request.missNextIndex = ~0;
+ request.mask =
+ new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
+ (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
+ return request;
+ }
+
+ @Test
+ public void testCreate() throws Exception {
+ final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
+ final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
+
+ whenClassifyAddDelTableThenSuccess();
+
+ customizer.writeCurrentAttributes(id, classifyTable, writeContext);
+
+ verifyClassifyAddDelTableAddWasInvoked(generateClassifyAddDelTable((byte) 1, TABLE_INDEX));
+ verify(mappingContext)
+ .put(eq(getMappingIid(TABLE_NAME, "test-instance")), eq(getMapping(TABLE_NAME, TABLE_INDEX).get()));
+ }
+
+ @Test
+ public void testCreateFailed() throws Exception {
+ final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
+ final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
+
+ whenClassifyAddDelTableThenFailure();
+
+ try {
+ customizer.writeCurrentAttributes(id, classifyTable, writeContext);
+ } catch (WriteFailedException.CreateFailedException e) {
+ assertTrue(e.getCause() instanceof VppBaseCallException);
+ verifyClassifyAddDelTableAddWasInvoked(generateClassifyAddDelTable((byte) 1, TABLE_INDEX));
+ verify(mappingContext, times(0)).put(
+ eq(getMappingIid(TABLE_NAME, "test-instance")),
+ eq(getMapping(TABLE_NAME, TABLE_INDEX).get()));
+ return;
+ }
+ fail("WriteFailedException.CreateFailedException was expected");
+ }
+
+ @Test
+ public void testDelete() throws Exception {
+ final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
+ final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
+
+ final Optional<Mapping> ifcMapping = getMapping(TABLE_NAME, TABLE_INDEX);
+ doReturn(ifcMapping).when(mappingContext).read(any());
+
+ whenClassifyAddDelTableThenSuccess();
+
+ customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
+
+ verifyClassifyAddDelTableDeleteWasInvoked(generateClassifyAddDelTable((byte) 0, TABLE_INDEX));
+ }
+
+ @Test
+ public void testDeleteFailed() throws Exception {
+ final ClassifyTable classifyTable = generateClassifyTable(TABLE_NAME);
+ final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
+
+ final Optional<Mapping> ifcMapping = getMapping(TABLE_NAME, TABLE_INDEX);
+ doReturn(ifcMapping).when(mappingContext).read(any());
+
+ whenClassifyAddDelTableThenFailure();
+
+ try {
+ customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
+ } catch (WriteFailedException.DeleteFailedException e) {
+ assertTrue(e.getCause() instanceof VppBaseCallException);
+ verifyClassifyAddDelTableDeleteWasInvoked(generateClassifyAddDelTable((byte) 0, TABLE_INDEX));
+ return;
+ }
+ fail("WriteFailedException.DeleteFailedException was expected");
+
+ customizer.deleteCurrentAttributes(id, classifyTable, writeContext);
+ }
+
+ @Test
+ public void testUpdate() throws Exception {
+ final ClassifyTable classifyTableBefore = generateClassifyTable(TABLE_NAME);
+ final InstanceIdentifier<ClassifyTable> id = getClassifyTableId(TABLE_NAME);
+ customizer.updateCurrentAttributes(id, classifyTableBefore, new ClassifyTableBuilder().build(), writeContext);
+
+ verifyZeroInteractions(api);
+ }
+} \ No newline at end of file