HONEYCOMB-118: extend classifer model to support node names.
[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 import static org.mockito.Mockito.when;
27
28 import com.google.common.base.Optional;
29 import io.fd.honeycomb.translate.read.ReadFailedException;
30 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
31 import io.fd.honeycomb.translate.v3po.test.ListReaderCustomizerTest;
32 import java.util.List;
33 import java.util.concurrent.CompletableFuture;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.PacketHandlingAction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifierState;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifierStateBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNode;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTable;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTableBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTableKey;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.openvpp.jvpp.core.dto.ClassifyTableIds;
46 import org.openvpp.jvpp.core.dto.ClassifyTableIdsReply;
47 import org.openvpp.jvpp.core.dto.ClassifyTableInfo;
48 import org.openvpp.jvpp.core.dto.ClassifyTableInfoReply;
49
50 public class ClassifyTableReaderTest extends
51         ListReaderCustomizerTest<ClassifyTable, ClassifyTableKey, ClassifyTableBuilder> {
52
53     private static final int TABLE_INDEX_1 = 1;
54     private static final String TABLE_NAME_1 = "table1";
55     private static final int TABLE_INDEX_2 = 2;
56     private static final String TABLE_NAME_2 = "table2";
57
58     @Mock
59     private VppClassifierContextManager classifierContext;
60
61     public ClassifyTableReaderTest() {
62         super(ClassifyTable.class);
63     }
64
65     @Override
66     protected ReaderCustomizer<ClassifyTable, ClassifyTableBuilder> initCustomizer() {
67         return new ClassifyTableReader(api, classifierContext);
68     }
69
70     private static InstanceIdentifier<ClassifyTable> getClassifyTableId(final String name) {
71         return InstanceIdentifier.create(VppClassifierState.class)
72             .child(ClassifyTable.class, new ClassifyTableKey(name));
73     }
74
75     private static ClassifyTableInfoReply generateClassifyTableInfoReply() {
76         final ClassifyTableInfoReply reply = new ClassifyTableInfoReply();
77         reply.tableId = TABLE_INDEX_1;
78         reply.nbuckets = 2;
79         reply.skipNVectors = 0;
80         reply.matchNVectors = 1;
81         reply.nextTableIndex = ~0;
82         reply.missNextIndex = ~0;
83         reply.mask =
84             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
85                 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
86         return reply;
87     }
88
89     private void verifyClasifyTableRead(final ClassifyTableBuilder builder) {
90         verify(builder).setName(TABLE_NAME_1);
91         verify(builder).setNbuckets(2L);
92         verify(builder, times(0)).setNextTable(anyString());
93         verify(builder).setMissNext(new VppNode(PacketHandlingAction.Permit));
94         verify(builder).setMask(new HexString("00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00"));
95         verify(builder).setActiveSessions(0L);
96     }
97
98     @Test
99     public void testMerge() {
100         final VppClassifierStateBuilder builder = mock(VppClassifierStateBuilder.class);
101         final List<ClassifyTable> value = mock(List.class);
102         getCustomizer().merge(builder, value);
103         verify(builder).setClassifyTable(value);
104     }
105
106     @Test
107     public void testRead() throws ReadFailedException {
108         final CompletableFuture<ClassifyTableInfoReply> replyFuture = new CompletableFuture<>();
109         replyFuture.complete(generateClassifyTableInfoReply());
110         doReturn(replyFuture).when(api).classifyTableInfo(any(ClassifyTableInfo.class));
111
112         when(classifierContext.containsTable(TABLE_NAME_1, mappingContext)).thenReturn(true);
113         when(classifierContext.getTableIndex(TABLE_NAME_1, mappingContext)).thenReturn(TABLE_INDEX_1);
114         when(classifierContext.getTableBaseNode(TABLE_NAME_1, mappingContext)).thenReturn(Optional.absent());
115
116         final ClassifyTableBuilder builder = mock(ClassifyTableBuilder.class);
117         getCustomizer().readCurrentAttributes(getClassifyTableId(TABLE_NAME_1), builder, ctx);
118
119         verifyClasifyTableRead(builder);
120     }
121
122     @Test
123     public void testGetAllIds() throws ReadFailedException {
124         final CompletableFuture<ClassifyTableIdsReply> replyFuture = new CompletableFuture<>();
125         final ClassifyTableIdsReply reply = new ClassifyTableIdsReply();
126         reply.ids = new int[] {1, 2};
127         replyFuture.complete(reply);
128         doReturn(replyFuture).when(api).classifyTableIds(any(ClassifyTableIds.class));
129
130         when(classifierContext.getTableName(TABLE_INDEX_1, mappingContext)).thenReturn(TABLE_NAME_1);
131         when(classifierContext.getTableName(TABLE_INDEX_2, mappingContext)).thenReturn(TABLE_NAME_2);
132
133         final List<ClassifyTableKey> allIds = getCustomizer().getAllIds(getClassifyTableId(TABLE_NAME_1), ctx);
134
135         assertEquals(reply.ids.length, allIds.size());
136         assertEquals(TABLE_NAME_1, allIds.get(0).getName());
137         assertEquals(TABLE_NAME_2, allIds.get(1).getName());
138     }
139 }