38ca324d9a26f58ea624bacfd56382d588e9181b
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / vppclassifier / ClassifyTableReaderTest.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.assertEquals;
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Matchers.anyString;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26
27 import com.google.common.base.Optional;
28 import com.google.common.collect.Lists;
29 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
30 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
31 import io.fd.honeycomb.translate.v3po.test.ListReaderCustomizerTest;
32 import io.fd.honeycomb.translate.read.ReadFailedException;
33 import io.fd.honeycomb.translate.v3po.util.NamingContext;
34 import java.util.List;
35 import java.util.concurrent.CompletableFuture;
36 import org.junit.Test;
37 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
38 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
40 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.PacketHandlingAction;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifierState;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifierStateBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNode;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTable;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTableBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTableKey;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
51 import org.openvpp.jvpp.core.dto.ClassifyTableIds;
52 import org.openvpp.jvpp.core.dto.ClassifyTableIdsReply;
53 import org.openvpp.jvpp.core.dto.ClassifyTableInfo;
54 import org.openvpp.jvpp.core.dto.ClassifyTableInfoReply;
55
56 public class ClassifyTableReaderTest extends
57         ListReaderCustomizerTest<ClassifyTable, ClassifyTableKey, ClassifyTableBuilder> {
58
59     private static final int TABLE_INDEX_1 = 1;
60     private static final String TABLE_NAME_1 = "table1";
61     private static final int TABLE_INDEX_2 = 2;
62     private static final String TABLE_NAME_2 = "table2";
63
64     private NamingContext classifyTableContext;
65
66     public ClassifyTableReaderTest() {
67         super(ClassifyTable.class);
68     }
69
70     @Override
71     public void setUpBefore() {
72         classifyTableContext = new NamingContext("classifyTableContext", "test-instance");
73
74         final KeyedInstanceIdentifier<Mapping, MappingKey> t0Id = ContextTestUtils
75                 .getMappingIid(TABLE_NAME_1, "test-instance");
76         final KeyedInstanceIdentifier<Mapping, MappingKey> t1Id = ContextTestUtils
77                 .getMappingIid(TABLE_NAME_2, "test-instance");
78         final Optional<Mapping> t0 = ContextTestUtils.getMapping(TABLE_NAME_1, TABLE_INDEX_1);
79         final Optional<Mapping> t1 = ContextTestUtils.getMapping(TABLE_NAME_2, TABLE_INDEX_2);
80         final List<Mapping> allMappings = Lists.newArrayList(t0.get(), t1.get());
81         final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build();
82         doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(t0Id.firstIdentifierOf(Mappings.class));
83         doReturn(t0).when(mappingContext).read(t0Id);
84         doReturn(t1).when(mappingContext).read(t1Id);
85     }
86
87     @Override
88     protected ReaderCustomizer<ClassifyTable, ClassifyTableBuilder> initCustomizer() {
89         return new ClassifyTableReader(api, classifyTableContext);
90     }
91
92     private static InstanceIdentifier<ClassifyTable> getClassifyTableId(final String name) {
93         return InstanceIdentifier.create(VppClassifierState.class)
94             .child(ClassifyTable.class, new ClassifyTableKey(name));
95     }
96
97     private static ClassifyTableInfoReply generateClassifyTableInfoReply() {
98         final ClassifyTableInfoReply reply = new ClassifyTableInfoReply();
99         reply.tableId = TABLE_INDEX_1;
100         reply.nbuckets = 2;
101         reply.skipNVectors = 0;
102         reply.matchNVectors = 1;
103         reply.nextTableIndex = ~0;
104         reply.missNextIndex = ~0;
105         reply.mask =
106             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
107                 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
108         return reply;
109     }
110
111     private void verifyClasifyTableRead(final ClassifyTableBuilder builder) {
112         verify(builder).setName(TABLE_NAME_1);
113         verify(builder).setNbuckets(2L);
114         verify(builder, times(0)).setNextTable(anyString());
115         verify(builder).setMissNext(new VppNode(PacketHandlingAction.Permit));
116         verify(builder).setMask(new HexString("00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00"));
117         verify(builder).setActiveSessions(0L);
118     }
119
120     @Test
121     public void testMerge() {
122         final VppClassifierStateBuilder builder = mock(VppClassifierStateBuilder.class);
123         final List<ClassifyTable> value = mock(List.class);
124         getCustomizer().merge(builder, value);
125         verify(builder).setClassifyTable(value);
126     }
127
128     @Test
129     public void testRead() throws ReadFailedException {
130         final CompletableFuture<ClassifyTableInfoReply> replyFuture = new CompletableFuture<>();
131         replyFuture.complete(generateClassifyTableInfoReply());
132         doReturn(replyFuture).when(api).classifyTableInfo(any(ClassifyTableInfo.class));
133
134         final ClassifyTableBuilder builder = mock(ClassifyTableBuilder.class);
135         getCustomizer().readCurrentAttributes(getClassifyTableId(TABLE_NAME_1), builder, ctx);
136
137         verifyClasifyTableRead(builder);
138     }
139
140     @Test
141     public void testGetAllIds() throws ReadFailedException {
142         final CompletableFuture<ClassifyTableIdsReply> replyFuture = new CompletableFuture<>();
143         final ClassifyTableIdsReply reply = new ClassifyTableIdsReply();
144         reply.ids = new int[] {1, 2};
145         replyFuture.complete(reply);
146         doReturn(replyFuture).when(api).classifyTableIds(any(ClassifyTableIds.class));
147
148         final List<ClassifyTableKey> allIds = getCustomizer().getAllIds(getClassifyTableId(TABLE_NAME_1), ctx);
149
150         assertEquals(reply.ids.length, allIds.size());
151         assertEquals(TABLE_NAME_1, allIds.get(0).getName());
152         assertEquals(TABLE_NAME_2, allIds.get(1).getName());
153     }
154 }