HONEYCOMB-118: extend classifer model to support node names.
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / vppclassifier / ClassifySessionReaderTest.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.Mockito.doReturn;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25
26 import io.fd.honeycomb.translate.ModificationCache;
27 import io.fd.honeycomb.translate.read.ReadFailedException;
28 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
29 import io.fd.honeycomb.translate.v3po.test.ListReaderCustomizerTest;
30 import java.util.Arrays;
31 import java.util.Collections;
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.VppClassifierState;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySession;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySessionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySessionKey;
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.ClassifySessionDetails;
46 import org.openvpp.jvpp.core.dto.ClassifySessionDetailsReplyDump;
47 import org.openvpp.jvpp.core.dto.ClassifySessionDump;
48
49 public class ClassifySessionReaderTest extends
50         ListReaderCustomizerTest<ClassifySession, ClassifySessionKey, ClassifySessionBuilder> {
51
52     private static final String MATCH_1 = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
53     private static final String MATCH_2 = "00:00:00:00:00:00:01:02:03:04:05:07:00:00:00:00";
54
55     private static final int TABLE_INDEX = 1;
56     private static final String TABLE_NAME = "table1";
57
58     @Mock
59     private VppClassifierContextManager classifierContext;
60
61     public ClassifySessionReaderTest() {
62         super(ClassifySession.class);
63     }
64
65     @Override
66     protected ReaderCustomizer<ClassifySession, ClassifySessionBuilder> initCustomizer() {
67         return new ClassifySessionReader(api, classifierContext);
68     }
69
70     private static InstanceIdentifier<ClassifySession> getClassifySessionId(final String tableName,
71                                                                             final String match) {
72         return InstanceIdentifier.create(VppClassifierState.class)
73             .child(ClassifyTable.class, new ClassifyTableKey(tableName))
74             .child(ClassifySession.class, new ClassifySessionKey(new HexString(match)));
75     }
76
77     @Test
78     public void testMerge() {
79         final ClassifyTableBuilder builder = mock(ClassifyTableBuilder.class);
80         final List<ClassifySession> value = mock(List.class);
81         getCustomizer().merge(builder, value);
82         verify(builder).setClassifySession(value);
83     }
84
85     @Test
86     public void testReadWithCache() throws ReadFailedException {
87         final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, MATCH_1);
88         final ClassifySessionBuilder builder = mock(ClassifySessionBuilder.class);
89         final ModificationCache cache = new ModificationCache();
90         final ClassifySessionDetailsReplyDump dump = new ClassifySessionDetailsReplyDump();
91         final ClassifySessionDetails details = new ClassifySessionDetails();
92         details.match =
93             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
94                 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
95         dump.classifySessionDetails = Collections.singletonList(details);
96         cache.put(ClassifySessionReader.CACHE_KEY + id.firstKeyOf(ClassifyTable.class), dump);
97         when(ctx.getModificationCache()).thenReturn(cache);
98
99         getCustomizer().readCurrentAttributes(id, builder, ctx);
100     }
101
102     @Test
103     public void testGetAllIds() throws ReadFailedException {
104         final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, MATCH_1);
105         final ClassifySessionDetailsReplyDump dump = new ClassifySessionDetailsReplyDump();
106         final ClassifySessionDetails details1 = new ClassifySessionDetails();
107         details1.match =
108             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
109                 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
110         final ClassifySessionDetails details2 = new ClassifySessionDetails();
111         details2.match =
112             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
113                 (byte) 0x05, (byte) 0x07, 0x00, 0x00, 0x00, 0x00};
114         dump.classifySessionDetails = Arrays.asList(details1, details2);
115
116         final CompletableFuture<ClassifySessionDetailsReplyDump> replyFuture = new CompletableFuture<>();
117         replyFuture.complete(dump);
118         doReturn(replyFuture).when(api).classifySessionDump(any(ClassifySessionDump.class));
119
120         when(classifierContext.containsTable(TABLE_NAME, mappingContext)).thenReturn(true);
121         when(classifierContext.getTableIndex(TABLE_NAME, mappingContext)).thenReturn(TABLE_INDEX);
122
123         final List<ClassifySessionKey> allIds = getCustomizer().getAllIds(id, ctx);
124         assertEquals(2, allIds.size());
125         assertEquals(MATCH_1, allIds.get(0).getMatch().getValue());
126         assertEquals(MATCH_2, allIds.get(1).getMatch().getValue());
127     }
128
129 }