b6ef0ece5805253b7750a78eff7c622b4dc0b446
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.translate.v3po.interfacesstate;
18
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;
26
27 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
28 import io.fd.honeycomb.translate.v3po.DisabledInterfacesManager;
29 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
30 import io.fd.honeycomb.translate.v3po.test.InterfaceTestUtils;
31 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
32 import io.fd.honeycomb.translate.v3po.util.NamingContext;
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.SwInterfaceDump;
48
49 public class InterfaceCustomizerTest extends
50         ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> {
51
52     private static final String IFC_CTX_NAME = "ifc-test-instance";
53     private static final String IFACE0_NAME = "eth0";
54     private static final String IFACE1_NAME = "eth1";
55     private static final String SUB_IFACE_NAME = "eth1.1";
56     private static final int IFACE0_ID = 0;
57     private static final int IFACE1_ID = 1;
58     private static final int SUB_IFACE_ID = 2;
59
60     private NamingContext interfacesContext;
61     @Mock
62     private DisabledInterfacesManager interfaceDisableContext;
63
64     public InterfaceCustomizerTest() {
65         super(Interface.class);
66     }
67
68     @Override
69     public void setUp() {
70         interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
71         ContextTestUtils.mockMapping(mappingContext, IFACE0_NAME, IFACE0_ID, IFC_CTX_NAME);
72         ContextTestUtils.mockMapping(mappingContext, IFACE1_NAME, IFACE1_ID, IFC_CTX_NAME);
73         ContextTestUtils.mockMapping(mappingContext, SUB_IFACE_NAME, SUB_IFACE_ID, IFC_CTX_NAME);
74     }
75
76     @Override
77     protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
78         return new InterfaceCustomizer(api, interfacesContext, interfaceDisableContext);
79     }
80
81     @Test
82     public void testMerge() throws Exception {
83         final InterfacesStateBuilder builder = mock(InterfacesStateBuilder.class);
84         final List<Interface> value = Collections.emptyList();
85         getCustomizer().merge(builder, value);
86         verify(builder).setInterface(value);
87     }
88
89     private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName,
90                                                  final int dumpIfcsInvocationCount)
91         throws VppInvocationException {
92         final SwInterfaceDump expected = new SwInterfaceDump();
93         expected.nameFilterValid = (byte)nameFilterValid;
94         expected.nameFilter = ifaceName.getBytes();
95         verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(expected);
96     }
97
98     private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
99         assertEquals(iface.getName(), new String(details.interfaceName));
100         Assert.assertEquals(InterfaceUtils.yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
101         assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address));
102     }
103
104     @Test
105     public void testReadCurrentAttributes() throws Exception {
106         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
107             .child(Interface.class, new InterfaceKey(IFACE0_NAME));
108         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
109
110         final SwInterfaceDetails iface = new SwInterfaceDetails();
111         iface.interfaceName = IFACE0_NAME.getBytes();
112         iface.swIfIndex = 0;
113         iface.linkSpeed = 1;
114         iface.l2AddressLength = 6;
115         iface.l2Address = new byte[iface.l2AddressLength];
116         final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
117         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, interfaceList);
118
119         getCustomizer().readCurrentAttributes(id, builder, ctx);
120
121         verifySwInterfaceDumpWasInvoked(1, IFACE0_NAME, 1);
122         assertIfacesAreEqual(builder.build(), iface);
123     }
124
125     @Test
126     public void testReadCurrentAttributesFailed() throws Exception {
127         final String ifaceName = IFACE0_NAME;
128         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
129             .child(Interface.class, new InterfaceKey(ifaceName));
130         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
131
132         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Collections.emptyList());
133
134         try {
135             getCustomizer().readCurrentAttributes(id, builder, ctx);
136         } catch (IllegalArgumentException e) {
137             verifySwInterfaceDumpWasInvoked(0, ifaceName, 2);
138             return;
139         }
140
141         fail("ReadFailedException was expected");
142     }
143
144     @Test
145     public void testReadSubInterface() throws Exception {
146         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
147             .child(Interface.class, new InterfaceKey(SUB_IFACE_NAME));
148         final InterfaceBuilder builder = mock(InterfaceBuilder.class);
149
150         final SwInterfaceDetails iface = new SwInterfaceDetails();
151         iface.interfaceName = SUB_IFACE_NAME.getBytes();
152         iface.swIfIndex = 2;
153         iface.supSwIfIndex = 1;
154         iface.subId = 1;
155         final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
156         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, interfaceList);
157
158         getCustomizer().readCurrentAttributes(id, builder, ctx);
159
160         verifySwInterfaceDumpWasInvoked(1, SUB_IFACE_NAME, 1);
161         verifyZeroInteractions(builder);
162     }
163
164     @Test
165     public void testGetAllIds() throws Exception {
166         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
167             .child(Interface.class);
168
169         final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
170         swIf0.swIfIndex = 0;
171         swIf0.interfaceName = IFACE0_NAME.getBytes();
172         final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
173         swIf1.swIfIndex = 1;
174         swIf1.interfaceName = IFACE1_NAME.getBytes();
175         final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails();
176         swSubIf1.swIfIndex = 2;
177         swSubIf1.subId = 1;
178         swSubIf1.supSwIfIndex = 1;
179         swSubIf1.interfaceName = SUB_IFACE_NAME.getBytes();
180         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Arrays.asList(swIf0, swIf1, swSubIf1));
181
182         final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME), new InterfaceKey(
183             IFACE1_NAME));
184         final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
185
186         verifySwInterfaceDumpWasInvoked(0, "", 1);
187
188         // sub-interface should not be on the list
189         assertEquals(expectedIds, actualIds);
190     }
191
192     @Test
193     public void testGetAllIdsWithDisabled() throws Exception {
194         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
195             .child(Interface.class);
196
197         doReturn(true).when(interfaceDisableContext).isInterfaceDisabled(1, mappingContext);
198
199         final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
200         swIf0.swIfIndex = 0;
201         swIf0.interfaceName = IFACE0_NAME.getBytes();
202         final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
203         swIf1.swIfIndex = 1;
204         swIf1.interfaceName = IFACE1_NAME.getBytes();
205         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Arrays.asList(swIf0, swIf1));
206
207         final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME));
208         final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
209
210         // disabled interface should not be on the list
211         assertEquals(expectedIds, actualIds);
212     }
213 }