f5159919cef9e29a2e1c1a790c6e5737020ff93f
[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 com.google.common.base.Optional;
27 import io.fd.honeycomb.translate.ModificationCache;
28 import io.fd.honeycomb.translate.read.ReadFailedException;
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.v3po.util.NamingContext;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.concurrent.CompletableFuture;
37 import org.junit.Test;
38 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifierState;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySession;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySessionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySessionKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTable;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTableBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTableKey;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.openvpp.jvpp.core.dto.ClassifySessionDetails;
49 import org.openvpp.jvpp.core.dto.ClassifySessionDetailsReplyDump;
50 import org.openvpp.jvpp.core.dto.ClassifySessionDump;
51
52 public class ClassifySessionReaderTest extends
53         ListReaderCustomizerTest<ClassifySession, ClassifySessionKey, ClassifySessionBuilder> {
54
55     private static final String MATCH_1 = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
56     private static final String MATCH_2 = "00:00:00:00:00:00:01:02:03:04:05:07:00:00:00:00";
57
58     private static final int TABLE_INDEX = 1;
59     private static final String TABLE_NAME = "table1";
60
61     private NamingContext classifyTableContext;
62
63     public ClassifySessionReaderTest() {
64         super(ClassifySession.class);
65     }
66
67     @Override
68     public void setUpBefore() {
69         classifyTableContext = new NamingContext("classifyTableContext", "test-instance");
70
71         final Optional<Mapping> ifcMapping = ContextTestUtils.getMapping(TABLE_NAME, TABLE_INDEX);
72         doReturn(ifcMapping).when(mappingContext).read(any());
73     }
74
75     @Override
76     protected ReaderCustomizer<ClassifySession, ClassifySessionBuilder> initCustomizer() {
77         return new ClassifySessionReader(api, classifyTableContext);
78     }
79
80     private static InstanceIdentifier<ClassifySession> getClassifySessionId(final String tableName,
81                                                                             final String match) {
82         return InstanceIdentifier.create(VppClassifierState.class)
83             .child(ClassifyTable.class, new ClassifyTableKey(tableName))
84             .child(ClassifySession.class, new ClassifySessionKey(new HexString(match)));
85     }
86
87     @Test
88     public void testMerge() {
89         final ClassifyTableBuilder builder = mock(ClassifyTableBuilder.class);
90         final List<ClassifySession> value = mock(List.class);
91         getCustomizer().merge(builder, value);
92         verify(builder).setClassifySession(value);
93     }
94
95     @Test
96     public void testReadWithCache() throws ReadFailedException {
97         final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, MATCH_1);
98         final ClassifySessionBuilder builder = mock(ClassifySessionBuilder.class);
99         final ModificationCache cache = new ModificationCache();
100         final ClassifySessionDetailsReplyDump dump = new ClassifySessionDetailsReplyDump();
101         final ClassifySessionDetails details = new ClassifySessionDetails();
102         details.match =
103             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
104                 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
105         dump.classifySessionDetails = Collections.singletonList(details);
106         cache.put(ClassifySessionReader.CACHE_KEY + id.firstKeyOf(ClassifyTable.class), dump);
107         when(ctx.getModificationCache()).thenReturn(cache);
108
109         getCustomizer().readCurrentAttributes(id, builder, ctx);
110     }
111
112     @Test
113     public void testGetAllIds() throws ReadFailedException {
114         final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, MATCH_1);
115         final ClassifySessionDetailsReplyDump dump = new ClassifySessionDetailsReplyDump();
116         final ClassifySessionDetails details1 = new ClassifySessionDetails();
117         details1.match =
118             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
119                 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
120         final ClassifySessionDetails details2 = new ClassifySessionDetails();
121         details2.match =
122             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
123                 (byte) 0x05, (byte) 0x07, 0x00, 0x00, 0x00, 0x00};
124         dump.classifySessionDetails = Arrays.asList(details1, details2);
125
126         final CompletableFuture<ClassifySessionDetailsReplyDump> replyFuture = new CompletableFuture<>();
127         replyFuture.complete(dump);
128         doReturn(replyFuture).when(api).classifySessionDump(any(ClassifySessionDump.class));
129
130         final List<ClassifySessionKey> allIds = getCustomizer().getAllIds(id, ctx);
131         assertEquals(2, allIds.size());
132         assertEquals(MATCH_1, allIds.get(0).getMatch().getValue());
133         assertEquals(MATCH_2, allIds.get(1).getMatch().getValue());
134     }
135
136 }