2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.v3po.interfacesstate;
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;
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.List;
55 import static org.junit.Assert.assertTrue;
56 import static org.powermock.api.mockito.PowerMockito.mock;
58 @RunWith(PowerMockRunner.class)
59 @SuppressStaticInitializationFor("org.openvpp.vppjapi.vppConn")
60 @PrepareForTest(vppApi.class)
61 public class InterfaceCustomizerTest {
63 public static final vppVersion VERSION = new vppVersion("test", "1", "2", "33");
66 private CompositeRootReader<InterfacesState, InterfacesStateBuilder> interfacesStateReader;
67 private DelegatingReaderRegistry readerRegistry;
68 private ReadContext ctx;
70 private CompositeRootReader<InterfacesState, InterfacesStateBuilder> getInterfacesStateReader(
71 final vppApi vppApi) {
73 final CompositeListReader<Interface, InterfaceKey, InterfaceBuilder> interfacesReader =
74 new CompositeListReader<>(Interface.class, new InterfaceCustomizer(vppApi));
76 final List<ChildReader<? extends ChildOf<InterfacesState>>> childReaders = new ArrayList<>();
77 childReaders.add(interfacesReader);
79 return new CompositeRootReader<>(InterfacesState.class, childReaders,
80 RWUtils.<InterfacesState>emptyAugReaderList(),
81 new ReflexiveRootReaderCustomizer<>(InterfacesStateBuilder.class));
84 public static vppInterfaceDetails createVppInterfaceDetails(int ifIndex, String name) {
85 return new vppInterfaceDetails(
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);
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()]);
101 PowerMockito.when(api.swInterfaceDump((byte) 0, new byte[]{})).
103 PowerMockito.when(api.swInterfaceDump((byte) 1, ifArr[0].interfaceName.getBytes())).thenReturn(ifArr);
105 interfacesStateReader = getInterfacesStateReader(api);
106 readerRegistry = new DelegatingReaderRegistry(
107 Collections.<Reader<? extends DataObject>>singletonList(interfacesStateReader));
111 public void testReadAll() throws ReadFailedException {
112 final Multimap<InstanceIdentifier<? extends DataObject>, ? extends DataObject> dataObjects =
113 readerRegistry.readAll(ctx);
115 System.out.println(dataObjects.keys());
116 final DataObject obj = Iterables.getOnlyElement(
117 dataObjects.get(Iterables.getOnlyElement(dataObjects.keySet())));
118 assertTrue(obj instanceof InterfacesState);
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);