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 static io.fd.honeycomb.v3po.translate.v3po.interfacesstate.InterfaceUtils.yangIfIndexToVpp;
20 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
21 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid;
22 import static io.fd.honeycomb.v3po.translate.v3po.test.InterfaceTestUtils.whenSwInterfaceDumpThenReturn;
23 import static org.junit.Assert.assertArrayEquals;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.verifyZeroInteractions;
32 import com.google.common.base.Optional;
33 import com.google.common.collect.Lists;
34 import io.fd.honeycomb.v3po.translate.spi.read.RootReaderCustomizer;
35 import io.fd.honeycomb.v3po.translate.v3po.test.ListReaderCustomizerTest;
36 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.List;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
43 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
45 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
53 import org.openvpp.jvpp.VppInvocationException;
54 import org.openvpp.jvpp.dto.SwInterfaceDetails;
55 import org.openvpp.jvpp.dto.SwInterfaceDump;
57 public class InterfaceCustomizerTest extends
58 ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> {
60 private NamingContext interfacesContext;
62 public InterfaceCustomizerTest() {
63 super(Interface.class);
67 public void setUpBefore() {
68 interfacesContext = new NamingContext("generatedIfaceName", "test-instance");
72 protected RootReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
73 final KeyedInstanceIdentifier<Mapping, MappingKey> eth0Id = getMappingIid("eth0", "test-instance");
74 final KeyedInstanceIdentifier<Mapping, MappingKey> eth1Id = getMappingIid("eth1", "test-instance");
75 final KeyedInstanceIdentifier<Mapping, MappingKey> subEth1Id = getMappingIid("eth1.1", "test-instance");
76 final Optional<Mapping> eth0 = getMapping("eth0", 0);
77 final Optional<Mapping> eth1 = getMapping("eth1", 1);
78 final Optional<Mapping> subEth1 = getMapping("eth1.1", 2);
80 final List<Mapping> allMappings =
81 Lists.newArrayList(getMapping("eth0", 0).get(), getMapping("eth1", 1).get(), getMapping("eth1.1", 2).get());
82 final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build();
83 doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(eth0Id.firstIdentifierOf(Mappings.class));
85 doReturn(eth0).when(mappingContext).read(eth0Id);
86 doReturn(eth1).when(mappingContext).read(eth1Id);
87 doReturn(subEth1).when(mappingContext).read(subEth1Id);
89 return new InterfaceCustomizer(api, interfacesContext);
92 // TODO use reflexion and move to ListReaderCustomizerTest
94 public void testMerge() throws Exception {
95 final InterfacesStateBuilder builder = mock(InterfacesStateBuilder.class);
96 final List<Interface> value = Collections.emptyList();
97 getCustomizer().merge(builder, value);
98 verify(builder).setInterface(value);
101 private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName,
102 final int dumpIfcsInvocationCount)
103 throws VppInvocationException {
104 // TODO adding equals methods for jvpp DTOs would make ArgumentCaptor usage obsolete
105 ArgumentCaptor<SwInterfaceDump> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceDump.class);
106 verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(argumentCaptor.capture());
107 final SwInterfaceDump actual = argumentCaptor.getValue();
108 assertEquals(nameFilterValid, actual.nameFilterValid);
109 assertArrayEquals(ifaceName.getBytes(), actual.nameFilter);
112 private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
113 assertEquals(iface.getName(), new String(details.interfaceName));
114 assertEquals(yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
115 assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address));
119 public void testReadCurrentAttributes() throws Exception {
120 final String ifaceName = "eth0";
121 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
122 .child(Interface.class, new InterfaceKey(ifaceName));
123 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
125 final SwInterfaceDetails iface = new SwInterfaceDetails();
126 iface.interfaceName = ifaceName.getBytes();
129 iface.l2AddressLength = 6;
130 iface.l2Address = new byte[iface.l2AddressLength];
131 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
132 whenSwInterfaceDumpThenReturn(api, interfaceList);
134 getCustomizer().readCurrentAttributes(id, builder, ctx);
136 verifySwInterfaceDumpWasInvoked(1, ifaceName, 1);
137 assertIfacesAreEqual(builder.build(), iface);
141 public void testReadCurrentAttributesFailed() throws Exception {
142 final String ifaceName = "eth0";
143 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
144 .child(Interface.class, new InterfaceKey(ifaceName));
145 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
147 whenSwInterfaceDumpThenReturn(api, Collections.emptyList());
150 getCustomizer().readCurrentAttributes(id, builder, ctx);
151 } catch (IllegalArgumentException e) {
152 verifySwInterfaceDumpWasInvoked(0, ifaceName, 2);
156 fail("ReadFailedException was expected");
160 public void testReadSubInterface() throws Exception {
161 final String ifaceName = "eth1.1";
162 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
163 .child(Interface.class, new InterfaceKey(ifaceName));
164 final InterfaceBuilder builder = mock(InterfaceBuilder.class);
166 final SwInterfaceDetails iface = new SwInterfaceDetails();
167 iface.interfaceName = ifaceName.getBytes();
169 iface.supSwIfIndex = 1;
171 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
172 whenSwInterfaceDumpThenReturn(api, interfaceList);
174 getCustomizer().readCurrentAttributes(id, builder, ctx);
176 verifySwInterfaceDumpWasInvoked(1, ifaceName, 1);
177 verifyZeroInteractions(builder);
181 public void testGetAllIds() throws Exception {
182 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
183 .child(Interface.class);
185 final String swIf0Name = "eth0";
186 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
188 swIf0.interfaceName = swIf0Name.getBytes();
189 final String swIf1Name = "eth1";
190 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
192 swIf1.interfaceName = swIf1Name.getBytes();
193 final String swSubIf1Name = "eth1.1";
194 final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails();
195 swSubIf1.swIfIndex = 2;
197 swSubIf1.supSwIfIndex = 1;
198 swSubIf1.interfaceName = swSubIf1Name.getBytes();
199 whenSwInterfaceDumpThenReturn(api, Arrays.asList(swIf0, swIf1, swSubIf1));
201 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(swIf0Name), new InterfaceKey(swIf1Name));
202 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
204 verifySwInterfaceDumpWasInvoked(0, "", 1);
206 // sub-interface should not be on the list
207 assertEquals(expectedIds, actualIds);