HONEYCOMB-58 - Routing Api
[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.when;
24
25 import io.fd.honeycomb.translate.ModificationCache;
26 import io.fd.honeycomb.translate.read.ReadFailedException;
27 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
28 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.VppClassifierState;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.classify.table.base.attributes.ClassifySession;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.classify.table.base.attributes.ClassifySessionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.classify.table.base.attributes.ClassifySessionKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.vpp.classifier.state.ClassifyTable;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.vpp.classifier.state.ClassifyTableBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.vpp.classifier.state.ClassifyTableKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import io.fd.vpp.jvpp.core.dto.ClassifySessionDetails;
44 import io.fd.vpp.jvpp.core.dto.ClassifySessionDetailsReplyDump;
45 import io.fd.vpp.jvpp.core.dto.ClassifySessionDump;
46
47 public class ClassifySessionReaderTest extends
48     ListReaderCustomizerTest<ClassifySession, ClassifySessionKey, ClassifySessionBuilder> {
49
50     private static final String MATCH_1 = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
51     private static final String MATCH_2 = "00:00:00:00:00:00:01:02:03:04:05:07:00:00:00:00";
52
53     private static final int TABLE_INDEX = 1;
54     private static final String TABLE_NAME = "table1";
55
56     @Mock
57     private VppClassifierContextManager classifierContext;
58
59     public ClassifySessionReaderTest() {
60         super(ClassifySession.class, ClassifyTableBuilder.class);
61     }
62
63     @Override
64     protected ReaderCustomizer<ClassifySession, ClassifySessionBuilder> initCustomizer() {
65         return new ClassifySessionReader(api, classifierContext);
66     }
67
68     private static InstanceIdentifier<ClassifySession> getClassifySessionId(final String tableName,
69                                                                             final String match) {
70         return InstanceIdentifier.create(VppClassifierState.class)
71             .child(ClassifyTable.class, new ClassifyTableKey(tableName))
72             .child(ClassifySession.class, new ClassifySessionKey(new HexString(match)));
73     }
74
75     @Test
76     public void testReadWithCache() throws ReadFailedException {
77         final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, MATCH_1);
78         final ClassifySessionBuilder builder = mock(ClassifySessionBuilder.class);
79         final ModificationCache cache = new ModificationCache();
80         final ClassifySessionDetailsReplyDump dump = new ClassifySessionDetailsReplyDump();
81         final ClassifySessionDetails details = new ClassifySessionDetails();
82         details.match =
83             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
84                 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
85         dump.classifySessionDetails = Collections.singletonList(details);
86         cache.put(ClassifySessionReader.CACHE_KEY + id.firstKeyOf(ClassifyTable.class), dump);
87         when(ctx.getModificationCache()).thenReturn(cache);
88
89         getCustomizer().readCurrentAttributes(id, builder, ctx);
90     }
91
92     @Test
93     public void testGetAllIds() throws ReadFailedException {
94         final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, MATCH_1);
95         final ClassifySessionDetailsReplyDump dump = new ClassifySessionDetailsReplyDump();
96         final ClassifySessionDetails details1 = new ClassifySessionDetails();
97         details1.match =
98             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
99                 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
100         final ClassifySessionDetails details2 = new ClassifySessionDetails();
101         details2.match =
102             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
103                 (byte) 0x05, (byte) 0x07, 0x00, 0x00, 0x00, 0x00};
104         dump.classifySessionDetails = Arrays.asList(details1, details2);
105         doReturn(future(dump)).when(api).classifySessionDump(any(ClassifySessionDump.class));
106
107         when(classifierContext.containsTable(TABLE_NAME, mappingContext)).thenReturn(true);
108         when(classifierContext.getTableIndex(TABLE_NAME, mappingContext)).thenReturn(TABLE_INDEX);
109
110         final List<ClassifySessionKey> allIds = getCustomizer().getAllIds(id, ctx);
111         assertEquals(2, allIds.size());
112         assertEquals(MATCH_1, allIds.get(0).getMatch().getValue());
113         assertEquals(MATCH_2, allIds.get(1).getMatch().getValue());
114     }
115
116 }