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.assertEquals;
20 import static org.junit.Assert.fail;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.verifyZeroInteractions;
27 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
28 import io.fd.honeycomb.translate.v3po.DisabledInterfacesManager;
29 import io.fd.honeycomb.translate.vpp.util.NamingContext;
30 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
31 import io.fd.honeycomb.vpp.test.util.InterfaceDumpHelper;
32 import java.util.Arrays;
33 import java.util.List;
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import io.fd.vpp.jvpp.VppInvocationException;
44 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
45 import io.fd.vpp.jvpp.core.dto.SwInterfaceDump;
47 public class InterfaceCustomizerTest extends ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder>
48 implements InterfaceDataTranslator, InterfaceDumpHelper {
50 private static final String IFC_CTX_NAME = "ifc-test-instance";
51 private static final String IFACE0_NAME = "eth0";
52 private static final String IFACE1_NAME = "eth1";
53 private static final String SUB_IFACE_NAME = "eth1.1";
54 private static final int IFACE0_ID = 0;
55 private static final int IFACE1_ID = 1;
56 private static final int SUB_IFACE_ID = 2;
58 private NamingContext interfacesContext;
60 private DisabledInterfacesManager interfaceDisableContext;
62 public InterfaceCustomizerTest() {
63 super(Interface.class, InterfacesStateBuilder.class);
68 interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
69 defineMapping(mappingContext, IFACE0_NAME, IFACE0_ID, IFC_CTX_NAME);
70 defineMapping(mappingContext, IFACE1_NAME, IFACE1_ID, IFC_CTX_NAME);
71 defineMapping(mappingContext, SUB_IFACE_NAME, SUB_IFACE_ID, IFC_CTX_NAME);
75 protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
76 return new InterfaceCustomizer(api, interfacesContext, interfaceDisableContext);
79 private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName,
80 final int dumpIfcsInvocationCount)
81 throws VppInvocationException {
82 final SwInterfaceDump expected = new SwInterfaceDump();
83 expected.nameFilterValid = (byte) nameFilterValid;
84 expected.nameFilter = ifaceName.getBytes();
85 verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(expected);
88 private void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
89 assertEquals(iface.getName(), new String(details.interfaceName));
90 Assert.assertEquals(yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
91 assertEquals(iface.getPhysAddress().getValue(), vppPhysAddrToYang(details.l2Address));
95 public void testReadCurrentAttributes() throws Exception {
96 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
97 .child(Interface.class, new InterfaceKey(IFACE0_NAME));
98 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
100 final SwInterfaceDetails iface = new SwInterfaceDetails();
101 iface.interfaceName = IFACE0_NAME.getBytes();
104 iface.l2AddressLength = 6;
105 iface.l2Address = new byte[iface.l2AddressLength];
106 whenSwInterfaceDumpThenReturn(api, iface);
108 getCustomizer().readCurrentAttributes(id, builder, ctx);
110 verifySwInterfaceDumpWasInvoked(1, IFACE0_NAME, 1);
111 assertIfacesAreEqual(builder.build(), iface);
115 public void testReadCurrentAttributesFailed() throws Exception {
116 final String ifaceName = IFACE0_NAME;
117 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
118 .child(Interface.class, new InterfaceKey(ifaceName));
119 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
121 whenSwInterfaceDumpThenReturn(api);
124 getCustomizer().readCurrentAttributes(id, builder, ctx);
125 } catch (IllegalArgumentException e) {
126 verifySwInterfaceDumpWasInvoked(0, ifaceName, 2);
130 fail("ReadFailedException was expected");
134 public void testReadSubInterface() throws Exception {
135 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
136 .child(Interface.class, new InterfaceKey(SUB_IFACE_NAME));
137 final InterfaceBuilder builder = mock(InterfaceBuilder.class);
139 final SwInterfaceDetails iface = new SwInterfaceDetails();
140 iface.interfaceName = SUB_IFACE_NAME.getBytes();
142 iface.supSwIfIndex = 1;
144 whenSwInterfaceDumpThenReturn(api, iface);
146 getCustomizer().readCurrentAttributes(id, builder, ctx);
148 verifySwInterfaceDumpWasInvoked(1, SUB_IFACE_NAME, 1);
149 verifyZeroInteractions(builder);
153 public void testGetAllIds() throws Exception {
154 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
155 .child(Interface.class);
157 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
159 swIf0.interfaceName = IFACE0_NAME.getBytes();
160 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
162 swIf1.interfaceName = IFACE1_NAME.getBytes();
163 final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails();
164 swSubIf1.swIfIndex = 2;
166 swSubIf1.supSwIfIndex = 1;
167 swSubIf1.interfaceName = SUB_IFACE_NAME.getBytes();
168 whenSwInterfaceDumpThenReturn(api, swIf0, swIf1, swSubIf1);
170 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME), new InterfaceKey(
172 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
174 verifySwInterfaceDumpWasInvoked(0, "", 1);
176 // sub-interface should not be on the list
177 assertEquals(expectedIds, actualIds);
181 public void testGetAllIdsWithDisabled() throws Exception {
182 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
183 .child(Interface.class);
185 doReturn(true).when(interfaceDisableContext).isInterfaceDisabled(1, mappingContext);
187 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
189 swIf0.interfaceName = IFACE0_NAME.getBytes();
190 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
192 swIf1.interfaceName = IFACE1_NAME.getBytes();
193 whenSwInterfaceDumpThenReturn(api, swIf0, swIf1);
195 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME));
196 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
198 // disabled interface should not be on the list
199 assertEquals(expectedIds, actualIds);