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.translate.v3po.interfacesstate;
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.fail;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.verifyZeroInteractions;
28 import com.google.common.base.Optional;
29 import com.google.common.collect.Lists;
30 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
31 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
32 import io.fd.honeycomb.translate.v3po.test.InterfaceTestUtils;
33 import io.fd.honeycomb.translate.v3po.test.ListReaderCustomizerTest;
34 import io.fd.honeycomb.translate.v3po.util.NamingContext;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38 import org.junit.Assert;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
42 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
44 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
52 import org.openvpp.jvpp.VppInvocationException;
53 import org.openvpp.jvpp.core.dto.SwInterfaceDetails;
54 import org.openvpp.jvpp.core.dto.SwInterfaceDump;
56 public class InterfaceCustomizerTest extends
57 ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> {
59 private NamingContext interfacesContext;
61 public InterfaceCustomizerTest() {
62 super(Interface.class);
66 public void setUpBefore() {
67 interfacesContext = new NamingContext("generatedIfaceName", "test-instance");
71 protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
72 final KeyedInstanceIdentifier<Mapping, MappingKey> eth0Id = ContextTestUtils
73 .getMappingIid("eth0", "test-instance");
74 final KeyedInstanceIdentifier<Mapping, MappingKey> eth1Id = ContextTestUtils
75 .getMappingIid("eth1", "test-instance");
76 final KeyedInstanceIdentifier<Mapping, MappingKey> subEth1Id = ContextTestUtils
77 .getMappingIid("eth1.1", "test-instance");
78 final Optional<Mapping> eth0 = ContextTestUtils.getMapping("eth0", 0);
79 final Optional<Mapping> eth1 = ContextTestUtils.getMapping("eth1", 1);
80 final Optional<Mapping> subEth1 = ContextTestUtils.getMapping("eth1.1", 2);
82 final List<Mapping> allMappings =
83 Lists.newArrayList(ContextTestUtils.getMapping("eth0", 0).get(), ContextTestUtils.getMapping("eth1", 1).get(), ContextTestUtils
84 .getMapping("eth1.1", 2).get());
85 final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build();
86 doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(eth0Id.firstIdentifierOf(Mappings.class));
88 doReturn(eth0).when(mappingContext).read(eth0Id);
89 doReturn(eth1).when(mappingContext).read(eth1Id);
90 doReturn(subEth1).when(mappingContext).read(subEth1Id);
92 return new InterfaceCustomizer(api, interfacesContext);
96 public void testMerge() throws Exception {
97 final InterfacesStateBuilder builder = mock(InterfacesStateBuilder.class);
98 final List<Interface> value = Collections.emptyList();
99 getCustomizer().merge(builder, value);
100 verify(builder).setInterface(value);
103 private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName,
104 final int dumpIfcsInvocationCount)
105 throws VppInvocationException {
106 // TODO HONEYCOMB-185 adding equals methods for jvpp DTOs would make ArgumentCaptor usage obsolete
107 ArgumentCaptor<SwInterfaceDump> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceDump.class);
108 verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(argumentCaptor.capture());
109 final SwInterfaceDump actual = argumentCaptor.getValue();
110 assertEquals(nameFilterValid, actual.nameFilterValid);
111 assertArrayEquals(ifaceName.getBytes(), actual.nameFilter);
114 private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
115 assertEquals(iface.getName(), new String(details.interfaceName));
116 Assert.assertEquals(InterfaceUtils.yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
117 assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address));
121 public void testReadCurrentAttributes() throws Exception {
122 final String ifaceName = "eth0";
123 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
124 .child(Interface.class, new InterfaceKey(ifaceName));
125 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
127 final SwInterfaceDetails iface = new SwInterfaceDetails();
128 iface.interfaceName = ifaceName.getBytes();
131 iface.l2AddressLength = 6;
132 iface.l2Address = new byte[iface.l2AddressLength];
133 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
134 InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, interfaceList);
136 getCustomizer().readCurrentAttributes(id, builder, ctx);
138 verifySwInterfaceDumpWasInvoked(1, ifaceName, 1);
139 assertIfacesAreEqual(builder.build(), iface);
143 public void testReadCurrentAttributesFailed() throws Exception {
144 final String ifaceName = "eth0";
145 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
146 .child(Interface.class, new InterfaceKey(ifaceName));
147 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
149 InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Collections.emptyList());
152 getCustomizer().readCurrentAttributes(id, builder, ctx);
153 } catch (IllegalArgumentException e) {
154 verifySwInterfaceDumpWasInvoked(0, ifaceName, 2);
158 fail("ReadFailedException was expected");
162 public void testReadSubInterface() throws Exception {
163 final String ifaceName = "eth1.1";
164 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
165 .child(Interface.class, new InterfaceKey(ifaceName));
166 final InterfaceBuilder builder = mock(InterfaceBuilder.class);
168 final SwInterfaceDetails iface = new SwInterfaceDetails();
169 iface.interfaceName = ifaceName.getBytes();
171 iface.supSwIfIndex = 1;
173 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
174 InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, interfaceList);
176 getCustomizer().readCurrentAttributes(id, builder, ctx);
178 verifySwInterfaceDumpWasInvoked(1, ifaceName, 1);
179 verifyZeroInteractions(builder);
183 public void testGetAllIds() throws Exception {
184 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
185 .child(Interface.class);
187 final String swIf0Name = "eth0";
188 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
190 swIf0.interfaceName = swIf0Name.getBytes();
191 final String swIf1Name = "eth1";
192 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
194 swIf1.interfaceName = swIf1Name.getBytes();
195 final String swSubIf1Name = "eth1.1";
196 final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails();
197 swSubIf1.swIfIndex = 2;
199 swSubIf1.supSwIfIndex = 1;
200 swSubIf1.interfaceName = swSubIf1Name.getBytes();
201 InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Arrays.asList(swIf0, swIf1, swSubIf1));
203 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(swIf0Name), new InterfaceKey(swIf1Name));
204 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
206 verifySwInterfaceDumpWasInvoked(0, "", 1);
208 // sub-interface should not be on the list
209 assertEquals(expectedIds, actualIds);