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.DisabledInterfacesManager;
32 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
33 import io.fd.honeycomb.translate.v3po.test.InterfaceTestUtils;
34 import io.fd.honeycomb.translate.v3po.test.ListReaderCustomizerTest;
35 import io.fd.honeycomb.translate.v3po.util.NamingContext;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
39 import org.junit.Assert;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Mock;
43 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
44 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
46 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
54 import org.openvpp.jvpp.VppInvocationException;
55 import org.openvpp.jvpp.core.dto.SwInterfaceDetails;
56 import org.openvpp.jvpp.core.dto.SwInterfaceDump;
58 public class InterfaceCustomizerTest extends
59 ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> {
61 private NamingContext interfacesContext;
63 private DisabledInterfacesManager interfaceDisableContext;
65 public InterfaceCustomizerTest() {
66 super(Interface.class);
70 public void setUpBefore() {
71 interfacesContext = new NamingContext("generatedIfaceName", "test-instance");
75 protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
76 final KeyedInstanceIdentifier<Mapping, MappingKey> eth0Id = ContextTestUtils
77 .getMappingIid("eth0", "test-instance");
78 final KeyedInstanceIdentifier<Mapping, MappingKey> eth1Id = ContextTestUtils
79 .getMappingIid("eth1", "test-instance");
80 final KeyedInstanceIdentifier<Mapping, MappingKey> subEth1Id = ContextTestUtils
81 .getMappingIid("eth1.1", "test-instance");
82 final Optional<Mapping> eth0 = ContextTestUtils.getMapping("eth0", 0);
83 final Optional<Mapping> eth1 = ContextTestUtils.getMapping("eth1", 1);
84 final Optional<Mapping> subEth1 = ContextTestUtils.getMapping("eth1.1", 2);
86 final List<Mapping> allMappings =
87 Lists.newArrayList(ContextTestUtils.getMapping("eth0", 0).get(), ContextTestUtils.getMapping("eth1", 1).get(), ContextTestUtils
88 .getMapping("eth1.1", 2).get());
89 final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build();
90 doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(eth0Id.firstIdentifierOf(Mappings.class));
92 doReturn(eth0).when(mappingContext).read(eth0Id);
93 doReturn(eth1).when(mappingContext).read(eth1Id);
94 doReturn(subEth1).when(mappingContext).read(subEth1Id);
96 return new InterfaceCustomizer(api, interfacesContext, interfaceDisableContext);
100 public void testMerge() throws Exception {
101 final InterfacesStateBuilder builder = mock(InterfacesStateBuilder.class);
102 final List<Interface> value = Collections.emptyList();
103 getCustomizer().merge(builder, value);
104 verify(builder).setInterface(value);
107 private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName,
108 final int dumpIfcsInvocationCount)
109 throws VppInvocationException {
110 // TODO HONEYCOMB-185 adding equals methods for jvpp DTOs would make ArgumentCaptor usage obsolete
111 ArgumentCaptor<SwInterfaceDump> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceDump.class);
112 verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(argumentCaptor.capture());
113 final SwInterfaceDump actual = argumentCaptor.getValue();
114 assertEquals(nameFilterValid, actual.nameFilterValid);
115 assertArrayEquals(ifaceName.getBytes(), actual.nameFilter);
118 private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
119 assertEquals(iface.getName(), new String(details.interfaceName));
120 Assert.assertEquals(InterfaceUtils.yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
121 assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address));
125 public void testReadCurrentAttributes() throws Exception {
126 final String ifaceName = "eth0";
127 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
128 .child(Interface.class, new InterfaceKey(ifaceName));
129 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
131 final SwInterfaceDetails iface = new SwInterfaceDetails();
132 iface.interfaceName = ifaceName.getBytes();
135 iface.l2AddressLength = 6;
136 iface.l2Address = new byte[iface.l2AddressLength];
137 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
138 InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, interfaceList);
140 getCustomizer().readCurrentAttributes(id, builder, ctx);
142 verifySwInterfaceDumpWasInvoked(1, ifaceName, 1);
143 assertIfacesAreEqual(builder.build(), iface);
147 public void testReadCurrentAttributesFailed() throws Exception {
148 final String ifaceName = "eth0";
149 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
150 .child(Interface.class, new InterfaceKey(ifaceName));
151 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
153 InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Collections.emptyList());
156 getCustomizer().readCurrentAttributes(id, builder, ctx);
157 } catch (IllegalArgumentException e) {
158 verifySwInterfaceDumpWasInvoked(0, ifaceName, 2);
162 fail("ReadFailedException was expected");
166 public void testReadSubInterface() throws Exception {
167 final String ifaceName = "eth1.1";
168 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
169 .child(Interface.class, new InterfaceKey(ifaceName));
170 final InterfaceBuilder builder = mock(InterfaceBuilder.class);
172 final SwInterfaceDetails iface = new SwInterfaceDetails();
173 iface.interfaceName = ifaceName.getBytes();
175 iface.supSwIfIndex = 1;
177 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
178 InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, interfaceList);
180 getCustomizer().readCurrentAttributes(id, builder, ctx);
182 verifySwInterfaceDumpWasInvoked(1, ifaceName, 1);
183 verifyZeroInteractions(builder);
187 public void testGetAllIds() throws Exception {
188 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
189 .child(Interface.class);
191 final String swIf0Name = "eth0";
192 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
194 swIf0.interfaceName = swIf0Name.getBytes();
195 final String swIf1Name = "eth1";
196 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
198 swIf1.interfaceName = swIf1Name.getBytes();
199 final String swSubIf1Name = "eth1.1";
200 final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails();
201 swSubIf1.swIfIndex = 2;
203 swSubIf1.supSwIfIndex = 1;
204 swSubIf1.interfaceName = swSubIf1Name.getBytes();
205 InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Arrays.asList(swIf0, swIf1, swSubIf1));
207 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(swIf0Name), new InterfaceKey(swIf1Name));
208 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
210 verifySwInterfaceDumpWasInvoked(0, "", 1);
212 // sub-interface should not be on the list
213 assertEquals(expectedIds, actualIds);
217 public void testGetAllIdsWithDisabled() throws Exception {
218 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
219 .child(Interface.class);
221 doReturn(true).when(interfaceDisableContext).isInterfaceDisabled(1, mappingContext);
223 final String swIf0Name = "eth0";
224 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
226 swIf0.interfaceName = swIf0Name.getBytes();
227 final String swIf1Name = "eth1";
228 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
230 swIf1.interfaceName = swIf1Name.getBytes();
231 InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Arrays.asList(swIf0, swIf1));
233 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(swIf0Name));
234 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
236 // disabled interface should not be on the list
237 assertEquals(expectedIds, actualIds);