111d813aaa987250cc40d6dd3102f13ac5e8feda
[hc2vpp.git] /
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.v3po.translate.v3po.interfacesstate;
18
19 import com.google.common.base.Optional;
20 import com.google.common.collect.Iterables;
21 import com.google.common.collect.Multimap;
22 import io.fd.honeycomb.v3po.translate.impl.read.CompositeListReader;
23 import io.fd.honeycomb.v3po.translate.impl.read.CompositeRootReader;
24 import io.fd.honeycomb.v3po.translate.read.ChildReader;
25 import io.fd.honeycomb.v3po.translate.read.ReadContext;
26 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
27 import io.fd.honeycomb.v3po.translate.read.Reader;
28 import io.fd.honeycomb.v3po.translate.util.RWUtils;
29 import io.fd.honeycomb.v3po.translate.util.read.DelegatingReaderRegistry;
30 import io.fd.honeycomb.v3po.translate.util.read.ReflexiveRootReaderCustomizer;
31 import io.fd.honeycomb.v3po.translate.v3po.interfacesstate.InterfaceCustomizer;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
40 import org.opendaylight.yangtools.yang.binding.ChildOf;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.openvpp.vppjapi.vppApi;
44 import org.openvpp.vppjapi.vppInterfaceDetails;
45 import org.openvpp.vppjapi.vppVersion;
46 import org.powermock.api.mockito.PowerMockito;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
49 import org.powermock.modules.junit4.PowerMockRunner;
50
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.List;
54
55 import static org.junit.Assert.assertTrue;
56 import static org.powermock.api.mockito.PowerMockito.mock;
57
58 @RunWith(PowerMockRunner.class)
59 @SuppressStaticInitializationFor("org.openvpp.vppjapi.vppConn")
60 @PrepareForTest(vppApi.class)
61 public class InterfaceCustomizerTest {
62
63     public static final vppVersion VERSION = new vppVersion("test", "1", "2", "33");
64
65     private vppApi api;
66     private CompositeRootReader<InterfacesState, InterfacesStateBuilder> interfacesStateReader;
67     private DelegatingReaderRegistry readerRegistry;
68     private ReadContext ctx;
69
70     private CompositeRootReader<InterfacesState, InterfacesStateBuilder> getInterfacesStateReader(
71             final vppApi vppApi) {
72
73         final CompositeListReader<Interface, InterfaceKey, InterfaceBuilder> interfacesReader =
74                 new CompositeListReader<>(Interface.class, new InterfaceCustomizer(vppApi));
75
76         final List<ChildReader<? extends ChildOf<InterfacesState>>> childReaders = new ArrayList<>();
77         childReaders.add(interfacesReader);
78
79         return new CompositeRootReader<>(InterfacesState.class, childReaders,
80                 RWUtils.<InterfacesState>emptyAugReaderList(),
81                 new ReflexiveRootReaderCustomizer<>(InterfacesStateBuilder.class));
82     }
83
84     public static vppInterfaceDetails createVppInterfaceDetails(int ifIndex, String name) {
85         return new vppInterfaceDetails(
86                 ifIndex, name, 0,
87                 new byte[]{ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00},
88                 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, 0, 0,
89                 (byte) 0, (byte) 0, (byte) 0, (byte) 0, 0, 0, 0, 0, 0);
90     }
91
92     @Before
93     public void setUp() throws Exception {
94         api = mock(vppApi.class);
95         // PowerMockito.doReturn(VERSION).when(api).getVppVersion();
96         ctx = mock(ReadContext.class);
97         List<vppInterfaceDetails> ifaces = new ArrayList<>();
98         ifaces.add(createVppInterfaceDetails(0, "loop0"));
99         vppInterfaceDetails[] ifArr = ifaces.toArray(new vppInterfaceDetails[ifaces.size()]);
100
101         PowerMockito.when(api.swInterfaceDump((byte) 0, new byte[]{})).
102                 thenReturn(ifArr);
103         PowerMockito.when(api.swInterfaceDump((byte) 1, ifArr[0].interfaceName.getBytes())).thenReturn(ifArr);
104
105         interfacesStateReader = getInterfacesStateReader(api);
106         readerRegistry = new DelegatingReaderRegistry(
107                 Collections.<Reader<? extends DataObject>>singletonList(interfacesStateReader));
108     }
109
110     @Test
111     public void testReadAll() throws ReadFailedException {
112         final Multimap<InstanceIdentifier<? extends DataObject>, ? extends DataObject> dataObjects =
113                 readerRegistry.readAll(ctx);
114
115         System.out.println(dataObjects.keys());
116         final DataObject obj = Iterables.getOnlyElement(
117                 dataObjects.get(Iterables.getOnlyElement(dataObjects.keySet())));
118         assertTrue(obj instanceof InterfacesState);
119     }
120
121     @Test
122     public void testReadId() throws ReadFailedException {
123         Optional<? extends DataObject> read =
124                 readerRegistry.read(InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey("Loofdsafdsap0")), ctx);
125         System.err.println(read);
126     }
127 }