86c1df2fd33540193b574827768cd28ed2dfdc63
[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.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;
27
28 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
29 import io.fd.honeycomb.translate.v3po.DisabledInterfacesManager;
30 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
31 import io.fd.honeycomb.translate.v3po.test.InterfaceTestUtils;
32 import io.fd.honeycomb.translate.v3po.test.ListReaderCustomizerTest;
33 import io.fd.honeycomb.translate.v3po.util.NamingContext;
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.ArgumentCaptor;
40 import org.mockito.Mock;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.openvpp.jvpp.VppInvocationException;
48 import org.openvpp.jvpp.core.dto.SwInterfaceDetails;
49 import org.openvpp.jvpp.core.dto.SwInterfaceDump;
50
51 public class InterfaceCustomizerTest extends
52         ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> {
53
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;
61
62     private NamingContext interfacesContext;
63     @Mock
64     private DisabledInterfacesManager interfaceDisableContext;
65
66     public InterfaceCustomizerTest() {
67         super(Interface.class);
68     }
69
70     @Override
71     public void setUpBefore() {
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);
76     }
77
78     @Override
79     protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
80         return new InterfaceCustomizer(api, interfacesContext, interfaceDisableContext);
81     }
82
83     @Test
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);
89     }
90
91     private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName,
92                                                  final int dumpIfcsInvocationCount)
93         throws VppInvocationException {
94         // TODO HONEYCOMB-185 adding equals methods for jvpp DTOs would make ArgumentCaptor usage obsolete
95         ArgumentCaptor<SwInterfaceDump> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceDump.class);
96         verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(argumentCaptor.capture());
97         final SwInterfaceDump actual = argumentCaptor.getValue();
98         assertEquals(nameFilterValid, actual.nameFilterValid);
99         assertArrayEquals(ifaceName.getBytes(), actual.nameFilter);
100     }
101
102     private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
103         assertEquals(iface.getName(), new String(details.interfaceName));
104         Assert.assertEquals(InterfaceUtils.yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
105         assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address));
106     }
107
108     @Test
109     public void testReadCurrentAttributes() throws Exception {
110         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
111             .child(Interface.class, new InterfaceKey(IFACE0_NAME));
112         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
113
114         final SwInterfaceDetails iface = new SwInterfaceDetails();
115         iface.interfaceName = IFACE0_NAME.getBytes();
116         iface.swIfIndex = 0;
117         iface.linkSpeed = 1;
118         iface.l2AddressLength = 6;
119         iface.l2Address = new byte[iface.l2AddressLength];
120         final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
121         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, interfaceList);
122
123         getCustomizer().readCurrentAttributes(id, builder, ctx);
124
125         verifySwInterfaceDumpWasInvoked(1, IFACE0_NAME, 1);
126         assertIfacesAreEqual(builder.build(), iface);
127     }
128
129     @Test
130     public void testReadCurrentAttributesFailed() throws Exception {
131         final String ifaceName = IFACE0_NAME;
132         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
133             .child(Interface.class, new InterfaceKey(ifaceName));
134         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
135
136         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Collections.emptyList());
137
138         try {
139             getCustomizer().readCurrentAttributes(id, builder, ctx);
140         } catch (IllegalArgumentException e) {
141             verifySwInterfaceDumpWasInvoked(0, ifaceName, 2);
142             return;
143         }
144
145         fail("ReadFailedException was expected");
146     }
147
148     @Test
149     public void testReadSubInterface() throws Exception {
150         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
151             .child(Interface.class, new InterfaceKey(SUB_IFACE_NAME));
152         final InterfaceBuilder builder = mock(InterfaceBuilder.class);
153
154         final SwInterfaceDetails iface = new SwInterfaceDetails();
155         iface.interfaceName = SUB_IFACE_NAME.getBytes();
156         iface.swIfIndex = 2;
157         iface.supSwIfIndex = 1;
158         iface.subId = 1;
159         final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
160         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, interfaceList);
161
162         getCustomizer().readCurrentAttributes(id, builder, ctx);
163
164         verifySwInterfaceDumpWasInvoked(1, SUB_IFACE_NAME, 1);
165         verifyZeroInteractions(builder);
166     }
167
168     @Test
169     public void testGetAllIds() throws Exception {
170         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
171             .child(Interface.class);
172
173         final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
174         swIf0.swIfIndex = 0;
175         swIf0.interfaceName = IFACE0_NAME.getBytes();
176         final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
177         swIf1.swIfIndex = 1;
178         swIf1.interfaceName = IFACE1_NAME.getBytes();
179         final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails();
180         swSubIf1.swIfIndex = 2;
181         swSubIf1.subId = 1;
182         swSubIf1.supSwIfIndex = 1;
183         swSubIf1.interfaceName = SUB_IFACE_NAME.getBytes();
184         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Arrays.asList(swIf0, swIf1, swSubIf1));
185
186         final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME), new InterfaceKey(
187             IFACE1_NAME));
188         final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
189
190         verifySwInterfaceDumpWasInvoked(0, "", 1);
191
192         // sub-interface should not be on the list
193         assertEquals(expectedIds, actualIds);
194     }
195
196     @Test
197     public void testGetAllIdsWithDisabled() throws Exception {
198         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
199             .child(Interface.class);
200
201         doReturn(true).when(interfaceDisableContext).isInterfaceDisabled(1, mappingContext);
202
203         final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
204         swIf0.swIfIndex = 0;
205         swIf0.interfaceName = IFACE0_NAME.getBytes();
206         final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
207         swIf1.swIfIndex = 1;
208         swIf1.interfaceName = IFACE1_NAME.getBytes();
209         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Arrays.asList(swIf0, swIf1));
210
211         final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME));
212         final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
213
214         // disabled interface should not be on the list
215         assertEquals(expectedIds, actualIds);
216     }
217 }