a10468ee832614b6bc7d9d093df50411a3366f0d
[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.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;
28
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;
49
50 public class InterfaceCustomizerTest extends
51         ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> implements InterfaceDataTranslator {
52
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;
60
61     private NamingContext interfacesContext;
62     @Mock
63     private DisabledInterfacesManager interfaceDisableContext;
64
65     public InterfaceCustomizerTest() {
66         super(Interface.class, InterfacesStateBuilder.class);
67     }
68
69     @Override
70     public void setUp() {
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);
75     }
76
77     @Override
78     protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
79         return new InterfaceCustomizer(api, interfacesContext, interfaceDisableContext);
80     }
81
82     private void whenSwInterfaceDumpThenReturn(final List<SwInterfaceDetails> interfaceList) {
83         final SwInterfaceDetailsReplyDump reply = new SwInterfaceDetailsReplyDump();
84         reply.swInterfaceDetails = interfaceList;
85         when(api.swInterfaceDump(any(SwInterfaceDump.class))).thenReturn(future(reply));
86     }
87
88     private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName,
89                                                  final int dumpIfcsInvocationCount)
90             throws VppInvocationException {
91         final SwInterfaceDump expected = new SwInterfaceDump();
92         expected.nameFilterValid = (byte) nameFilterValid;
93         expected.nameFilter = ifaceName.getBytes();
94         verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(expected);
95     }
96
97     private void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
98         assertEquals(iface.getName(), new String(details.interfaceName));
99         Assert.assertEquals(yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
100         assertEquals(iface.getPhysAddress().getValue(), vppPhysAddrToYang(details.l2Address));
101     }
102
103     @Test
104     public void testReadCurrentAttributes() throws Exception {
105         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
106                 .child(Interface.class, new InterfaceKey(IFACE0_NAME));
107         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
108
109         final SwInterfaceDetails iface = new SwInterfaceDetails();
110         iface.interfaceName = IFACE0_NAME.getBytes();
111         iface.swIfIndex = 0;
112         iface.linkSpeed = 1;
113         iface.l2AddressLength = 6;
114         iface.l2Address = new byte[iface.l2AddressLength];
115         final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
116         whenSwInterfaceDumpThenReturn(interfaceList);
117
118         getCustomizer().readCurrentAttributes(id, builder, ctx);
119
120         verifySwInterfaceDumpWasInvoked(1, IFACE0_NAME, 1);
121         assertIfacesAreEqual(builder.build(), iface);
122     }
123
124     @Test
125     public void testReadCurrentAttributesFailed() throws Exception {
126         final String ifaceName = IFACE0_NAME;
127         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
128                 .child(Interface.class, new InterfaceKey(ifaceName));
129         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
130
131         whenSwInterfaceDumpThenReturn(Collections.emptyList());
132
133         try {
134             getCustomizer().readCurrentAttributes(id, builder, ctx);
135         } catch (IllegalArgumentException e) {
136             verifySwInterfaceDumpWasInvoked(0, ifaceName, 2);
137             return;
138         }
139
140         fail("ReadFailedException was expected");
141     }
142
143     @Test
144     public void testReadSubInterface() throws Exception {
145         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
146                 .child(Interface.class, new InterfaceKey(SUB_IFACE_NAME));
147         final InterfaceBuilder builder = mock(InterfaceBuilder.class);
148
149         final SwInterfaceDetails iface = new SwInterfaceDetails();
150         iface.interfaceName = SUB_IFACE_NAME.getBytes();
151         iface.swIfIndex = 2;
152         iface.supSwIfIndex = 1;
153         iface.subId = 1;
154         final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
155         whenSwInterfaceDumpThenReturn(interfaceList);
156
157         getCustomizer().readCurrentAttributes(id, builder, ctx);
158
159         verifySwInterfaceDumpWasInvoked(1, SUB_IFACE_NAME, 1);
160         verifyZeroInteractions(builder);
161     }
162
163     @Test
164     public void testGetAllIds() throws Exception {
165         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
166                 .child(Interface.class);
167
168         final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
169         swIf0.swIfIndex = 0;
170         swIf0.interfaceName = IFACE0_NAME.getBytes();
171         final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
172         swIf1.swIfIndex = 1;
173         swIf1.interfaceName = IFACE1_NAME.getBytes();
174         final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails();
175         swSubIf1.swIfIndex = 2;
176         swSubIf1.subId = 1;
177         swSubIf1.supSwIfIndex = 1;
178         swSubIf1.interfaceName = SUB_IFACE_NAME.getBytes();
179         whenSwInterfaceDumpThenReturn(Arrays.asList(swIf0, swIf1, swSubIf1));
180
181         final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME), new InterfaceKey(
182                 IFACE1_NAME));
183         final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
184
185         verifySwInterfaceDumpWasInvoked(0, "", 1);
186
187         // sub-interface should not be on the list
188         assertEquals(expectedIds, actualIds);
189     }
190
191     @Test
192     public void testGetAllIdsWithDisabled() throws Exception {
193         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
194                 .child(Interface.class);
195
196         doReturn(true).when(interfaceDisableContext).isInterfaceDisabled(1, mappingContext);
197
198         final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
199         swIf0.swIfIndex = 0;
200         swIf0.interfaceName = IFACE0_NAME.getBytes();
201         final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
202         swIf1.swIfIndex = 1;
203         swIf1.interfaceName = IFACE1_NAME.getBytes();
204         whenSwInterfaceDumpThenReturn(Arrays.asList(swIf0, swIf1));
205
206         final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME));
207         final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
208
209         // disabled interface should not be on the list
210         assertEquals(expectedIds, actualIds);
211     }
212 }