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.test.ContextTestUtils;
32 import io.fd.honeycomb.translate.v3po.util.NamingContext;
33 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
37 import org.junit.Assert;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.openvpp.jvpp.VppInvocationException;
47 import org.openvpp.jvpp.core.dto.SwInterfaceDetails;
48 import org.openvpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
49 import org.openvpp.jvpp.core.dto.SwInterfaceDump;
51 public class InterfaceCustomizerTest extends
52 ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> {
54 private static final String IFC_CTX_NAME = "ifc-test-instance";
55 private static final String IFACE0_NAME = "eth0";
56 private static final String IFACE1_NAME = "eth1";
57 private static final String SUB_IFACE_NAME = "eth1.1";
58 private static final int IFACE0_ID = 0;
59 private static final int IFACE1_ID = 1;
60 private static final int SUB_IFACE_ID = 2;
62 private NamingContext interfacesContext;
64 private DisabledInterfacesManager interfaceDisableContext;
66 public InterfaceCustomizerTest() {
67 super(Interface.class);
72 interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
73 ContextTestUtils.mockMapping(mappingContext, IFACE0_NAME, IFACE0_ID, IFC_CTX_NAME);
74 ContextTestUtils.mockMapping(mappingContext, IFACE1_NAME, IFACE1_ID, IFC_CTX_NAME);
75 ContextTestUtils.mockMapping(mappingContext, SUB_IFACE_NAME, SUB_IFACE_ID, IFC_CTX_NAME);
79 protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
80 return new InterfaceCustomizer(api, interfacesContext, interfaceDisableContext);
84 public void testMerge() throws Exception {
85 final InterfacesStateBuilder builder = mock(InterfacesStateBuilder.class);
86 final List<Interface> value = Collections.emptyList();
87 getCustomizer().merge(builder, value);
88 verify(builder).setInterface(value);
91 public void whenSwInterfaceDumpThenReturn(final List<SwInterfaceDetails> interfaceList) {
92 final SwInterfaceDetailsReplyDump reply = new SwInterfaceDetailsReplyDump();
93 reply.swInterfaceDetails = interfaceList;
94 when(api.swInterfaceDump(any(SwInterfaceDump.class))).thenReturn(future(reply));
97 private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName,
98 final int dumpIfcsInvocationCount)
99 throws VppInvocationException {
100 final SwInterfaceDump expected = new SwInterfaceDump();
101 expected.nameFilterValid = (byte)nameFilterValid;
102 expected.nameFilter = ifaceName.getBytes();
103 verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(expected);
106 private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
107 assertEquals(iface.getName(), new String(details.interfaceName));
108 Assert.assertEquals(InterfaceUtils.yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
109 assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address));
113 public void testReadCurrentAttributes() throws Exception {
114 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
115 .child(Interface.class, new InterfaceKey(IFACE0_NAME));
116 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
118 final SwInterfaceDetails iface = new SwInterfaceDetails();
119 iface.interfaceName = IFACE0_NAME.getBytes();
122 iface.l2AddressLength = 6;
123 iface.l2Address = new byte[iface.l2AddressLength];
124 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
125 whenSwInterfaceDumpThenReturn(interfaceList);
127 getCustomizer().readCurrentAttributes(id, builder, ctx);
129 verifySwInterfaceDumpWasInvoked(1, IFACE0_NAME, 1);
130 assertIfacesAreEqual(builder.build(), iface);
134 public void testReadCurrentAttributesFailed() throws Exception {
135 final String ifaceName = IFACE0_NAME;
136 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
137 .child(Interface.class, new InterfaceKey(ifaceName));
138 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
140 whenSwInterfaceDumpThenReturn(Collections.emptyList());
143 getCustomizer().readCurrentAttributes(id, builder, ctx);
144 } catch (IllegalArgumentException e) {
145 verifySwInterfaceDumpWasInvoked(0, ifaceName, 2);
149 fail("ReadFailedException was expected");
153 public void testReadSubInterface() throws Exception {
154 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
155 .child(Interface.class, new InterfaceKey(SUB_IFACE_NAME));
156 final InterfaceBuilder builder = mock(InterfaceBuilder.class);
158 final SwInterfaceDetails iface = new SwInterfaceDetails();
159 iface.interfaceName = SUB_IFACE_NAME.getBytes();
161 iface.supSwIfIndex = 1;
163 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
164 whenSwInterfaceDumpThenReturn(interfaceList);
166 getCustomizer().readCurrentAttributes(id, builder, ctx);
168 verifySwInterfaceDumpWasInvoked(1, SUB_IFACE_NAME, 1);
169 verifyZeroInteractions(builder);
173 public void testGetAllIds() throws Exception {
174 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
175 .child(Interface.class);
177 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
179 swIf0.interfaceName = IFACE0_NAME.getBytes();
180 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
182 swIf1.interfaceName = IFACE1_NAME.getBytes();
183 final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails();
184 swSubIf1.swIfIndex = 2;
186 swSubIf1.supSwIfIndex = 1;
187 swSubIf1.interfaceName = SUB_IFACE_NAME.getBytes();
188 whenSwInterfaceDumpThenReturn(Arrays.asList(swIf0, swIf1, swSubIf1));
190 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME), new InterfaceKey(
192 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
194 verifySwInterfaceDumpWasInvoked(0, "", 1);
196 // sub-interface should not be on the list
197 assertEquals(expectedIds, actualIds);
201 public void testGetAllIdsWithDisabled() throws Exception {
202 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
203 .child(Interface.class);
205 doReturn(true).when(interfaceDisableContext).isInterfaceDisabled(1, mappingContext);
207 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
209 swIf0.interfaceName = IFACE0_NAME.getBytes();
210 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
212 swIf1.interfaceName = IFACE1_NAME.getBytes();
213 whenSwInterfaceDumpThenReturn(Arrays.asList(swIf0, swIf1));
215 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME));
216 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
218 // disabled interface should not be on the list
219 assertEquals(expectedIds, actualIds);