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.Matchers.any;
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;
27 import static org.mockito.Mockito.when;
29 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
30 import io.fd.honeycomb.translate.v3po.DisabledInterfacesManager;
31 import io.fd.honeycomb.translate.v3po.util.NamingContext;
32 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import org.junit.Assert;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.openvpp.jvpp.VppInvocationException;
46 import org.openvpp.jvpp.core.dto.SwInterfaceDetails;
47 import org.openvpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
48 import org.openvpp.jvpp.core.dto.SwInterfaceDump;
50 public class InterfaceCustomizerTest extends
51 ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> implements InterfaceDataTranslator {
53 private static final String IFC_CTX_NAME = "ifc-test-instance";
54 private static final String IFACE0_NAME = "eth0";
55 private static final String IFACE1_NAME = "eth1";
56 private static final String SUB_IFACE_NAME = "eth1.1";
57 private static final int IFACE0_ID = 0;
58 private static final int IFACE1_ID = 1;
59 private static final int SUB_IFACE_ID = 2;
61 private NamingContext interfacesContext;
63 private DisabledInterfacesManager interfaceDisableContext;
65 public InterfaceCustomizerTest() {
66 super(Interface.class);
71 interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
72 defineMapping(mappingContext, IFACE0_NAME, IFACE0_ID, IFC_CTX_NAME);
73 defineMapping(mappingContext, IFACE1_NAME, IFACE1_ID, IFC_CTX_NAME);
74 defineMapping(mappingContext, SUB_IFACE_NAME, SUB_IFACE_ID, IFC_CTX_NAME);
78 protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
79 return new InterfaceCustomizer(api, interfacesContext, interfaceDisableContext);
83 public void testMerge() throws Exception {
84 final InterfacesStateBuilder builder = mock(InterfacesStateBuilder.class);
85 final List<Interface> value = Collections.emptyList();
86 getCustomizer().merge(builder, value);
87 verify(builder).setInterface(value);
90 public void whenSwInterfaceDumpThenReturn(final List<SwInterfaceDetails> interfaceList) {
91 final SwInterfaceDetailsReplyDump reply = new SwInterfaceDetailsReplyDump();
92 reply.swInterfaceDetails = interfaceList;
93 when(api.swInterfaceDump(any(SwInterfaceDump.class))).thenReturn(future(reply));
96 private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName,
97 final int dumpIfcsInvocationCount)
98 throws VppInvocationException {
99 final SwInterfaceDump expected = new SwInterfaceDump();
100 expected.nameFilterValid = (byte) nameFilterValid;
101 expected.nameFilter = ifaceName.getBytes();
102 verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(expected);
105 private void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
106 assertEquals(iface.getName(), new String(details.interfaceName));
107 Assert.assertEquals(yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
108 assertEquals(iface.getPhysAddress().getValue(), vppPhysAddrToYang(details.l2Address));
112 public void testReadCurrentAttributes() throws Exception {
113 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
114 .child(Interface.class, new InterfaceKey(IFACE0_NAME));
115 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
117 final SwInterfaceDetails iface = new SwInterfaceDetails();
118 iface.interfaceName = IFACE0_NAME.getBytes();
121 iface.l2AddressLength = 6;
122 iface.l2Address = new byte[iface.l2AddressLength];
123 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
124 whenSwInterfaceDumpThenReturn(interfaceList);
126 getCustomizer().readCurrentAttributes(id, builder, ctx);
128 verifySwInterfaceDumpWasInvoked(1, IFACE0_NAME, 1);
129 assertIfacesAreEqual(builder.build(), iface);
133 public void testReadCurrentAttributesFailed() throws Exception {
134 final String ifaceName = IFACE0_NAME;
135 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
136 .child(Interface.class, new InterfaceKey(ifaceName));
137 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
139 whenSwInterfaceDumpThenReturn(Collections.emptyList());
142 getCustomizer().readCurrentAttributes(id, builder, ctx);
143 } catch (IllegalArgumentException e) {
144 verifySwInterfaceDumpWasInvoked(0, ifaceName, 2);
148 fail("ReadFailedException was expected");
152 public void testReadSubInterface() throws Exception {
153 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
154 .child(Interface.class, new InterfaceKey(SUB_IFACE_NAME));
155 final InterfaceBuilder builder = mock(InterfaceBuilder.class);
157 final SwInterfaceDetails iface = new SwInterfaceDetails();
158 iface.interfaceName = SUB_IFACE_NAME.getBytes();
160 iface.supSwIfIndex = 1;
162 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
163 whenSwInterfaceDumpThenReturn(interfaceList);
165 getCustomizer().readCurrentAttributes(id, builder, ctx);
167 verifySwInterfaceDumpWasInvoked(1, SUB_IFACE_NAME, 1);
168 verifyZeroInteractions(builder);
172 public void testGetAllIds() throws Exception {
173 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
174 .child(Interface.class);
176 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
178 swIf0.interfaceName = IFACE0_NAME.getBytes();
179 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
181 swIf1.interfaceName = IFACE1_NAME.getBytes();
182 final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails();
183 swSubIf1.swIfIndex = 2;
185 swSubIf1.supSwIfIndex = 1;
186 swSubIf1.interfaceName = SUB_IFACE_NAME.getBytes();
187 whenSwInterfaceDumpThenReturn(Arrays.asList(swIf0, swIf1, swSubIf1));
189 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME), new InterfaceKey(
191 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
193 verifySwInterfaceDumpWasInvoked(0, "", 1);
195 // sub-interface should not be on the list
196 assertEquals(expectedIds, actualIds);
200 public void testGetAllIdsWithDisabled() throws Exception {
201 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
202 .child(Interface.class);
204 doReturn(true).when(interfaceDisableContext).isInterfaceDisabled(1, mappingContext);
206 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
208 swIf0.interfaceName = IFACE0_NAME.getBytes();
209 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
211 swIf1.interfaceName = IFACE1_NAME.getBytes();
212 whenSwInterfaceDumpThenReturn(Arrays.asList(swIf0, swIf1));
214 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME));
215 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
217 // disabled interface should not be on the list
218 assertEquals(expectedIds, actualIds);