581da29d77bdbc6f453e43602b91966f3ddb60bb
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfacesstate / InterfaceCustomizerTest.java
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.vpp.util.NamingContext;
30 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
31 import io.fd.honeycomb.vpp.test.util.InterfaceDumpHelper;
32 import java.util.Arrays;
33 import java.util.List;
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import io.fd.vpp.jvpp.VppInvocationException;
44 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
45 import io.fd.vpp.jvpp.core.dto.SwInterfaceDump;
46
47 public class InterfaceCustomizerTest extends ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder>
48     implements InterfaceDataTranslator, InterfaceDumpHelper {
49
50     private static final String IFC_CTX_NAME = "ifc-test-instance";
51     private static final String IFACE0_NAME = "eth0";
52     private static final String IFACE1_NAME = "eth1";
53     private static final String SUB_IFACE_NAME = "eth1.1";
54     private static final int IFACE0_ID = 0;
55     private static final int IFACE1_ID = 1;
56     private static final int SUB_IFACE_ID = 2;
57
58     private NamingContext interfacesContext;
59     @Mock
60     private DisabledInterfacesManager interfaceDisableContext;
61
62     public InterfaceCustomizerTest() {
63         super(Interface.class, InterfacesStateBuilder.class);
64     }
65
66     @Override
67     public void setUp() {
68         interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
69         defineMapping(mappingContext, IFACE0_NAME, IFACE0_ID, IFC_CTX_NAME);
70         defineMapping(mappingContext, IFACE1_NAME, IFACE1_ID, IFC_CTX_NAME);
71         defineMapping(mappingContext, SUB_IFACE_NAME, SUB_IFACE_ID, IFC_CTX_NAME);
72     }
73
74     @Override
75     protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
76         return new InterfaceCustomizer(api, interfacesContext, interfaceDisableContext);
77     }
78
79     private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName,
80                                                  final int dumpIfcsInvocationCount)
81             throws VppInvocationException {
82         final SwInterfaceDump expected = new SwInterfaceDump();
83         expected.nameFilterValid = (byte) nameFilterValid;
84         expected.nameFilter = ifaceName.getBytes();
85         verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(expected);
86     }
87
88     private void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
89         assertEquals(iface.getName(), new String(details.interfaceName));
90         Assert.assertEquals(yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
91         assertEquals(iface.getPhysAddress().getValue(), vppPhysAddrToYang(details.l2Address));
92     }
93
94     @Test
95     public void testReadCurrentAttributes() throws Exception {
96         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
97                 .child(Interface.class, new InterfaceKey(IFACE0_NAME));
98         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
99
100         final SwInterfaceDetails iface = new SwInterfaceDetails();
101         iface.interfaceName = IFACE0_NAME.getBytes();
102         iface.swIfIndex = 0;
103         iface.linkSpeed = 1;
104         iface.l2AddressLength = 6;
105         iface.l2Address = new byte[iface.l2AddressLength];
106         whenSwInterfaceDumpThenReturn(api, iface);
107
108         getCustomizer().readCurrentAttributes(id, builder, ctx);
109
110         verifySwInterfaceDumpWasInvoked(1, IFACE0_NAME, 1);
111         assertIfacesAreEqual(builder.build(), iface);
112     }
113
114     @Test
115     public void testReadCurrentAttributesFailed() throws Exception {
116         final String ifaceName = IFACE0_NAME;
117         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
118                 .child(Interface.class, new InterfaceKey(ifaceName));
119         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
120
121         whenSwInterfaceDumpThenReturn(api);
122
123         try {
124             getCustomizer().readCurrentAttributes(id, builder, ctx);
125         } catch (IllegalArgumentException e) {
126             verifySwInterfaceDumpWasInvoked(0, ifaceName, 2);
127             return;
128         }
129
130         fail("ReadFailedException was expected");
131     }
132
133     @Test
134     public void testReadSubInterface() throws Exception {
135         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
136                 .child(Interface.class, new InterfaceKey(SUB_IFACE_NAME));
137         final InterfaceBuilder builder = mock(InterfaceBuilder.class);
138
139         final SwInterfaceDetails iface = new SwInterfaceDetails();
140         iface.interfaceName = SUB_IFACE_NAME.getBytes();
141         iface.swIfIndex = 2;
142         iface.supSwIfIndex = 1;
143         iface.subId = 1;
144         whenSwInterfaceDumpThenReturn(api, iface);
145
146         getCustomizer().readCurrentAttributes(id, builder, ctx);
147
148         verifySwInterfaceDumpWasInvoked(1, SUB_IFACE_NAME, 1);
149         verifyZeroInteractions(builder);
150     }
151
152     @Test
153     public void testGetAllIds() throws Exception {
154         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
155                 .child(Interface.class);
156
157         final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
158         swIf0.swIfIndex = 0;
159         swIf0.interfaceName = IFACE0_NAME.getBytes();
160         final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
161         swIf1.swIfIndex = 1;
162         swIf1.interfaceName = IFACE1_NAME.getBytes();
163         final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails();
164         swSubIf1.swIfIndex = 2;
165         swSubIf1.subId = 1;
166         swSubIf1.supSwIfIndex = 1;
167         swSubIf1.interfaceName = SUB_IFACE_NAME.getBytes();
168         whenSwInterfaceDumpThenReturn(api, swIf0, swIf1, swSubIf1);
169
170         final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME), new InterfaceKey(
171                 IFACE1_NAME));
172         final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
173
174         verifySwInterfaceDumpWasInvoked(0, "", 1);
175
176         // sub-interface should not be on the list
177         assertEquals(expectedIds, actualIds);
178     }
179
180     @Test
181     public void testGetAllIdsWithDisabled() throws Exception {
182         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
183                 .child(Interface.class);
184
185         doReturn(true).when(interfaceDisableContext).isInterfaceDisabled(1, mappingContext);
186
187         final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
188         swIf0.swIfIndex = 0;
189         swIf0.interfaceName = IFACE0_NAME.getBytes();
190         final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
191         swIf1.swIfIndex = 1;
192         swIf1.interfaceName = IFACE1_NAME.getBytes();
193         whenSwInterfaceDumpThenReturn(api, swIf0, swIf1);
194
195         final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME));
196         final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
197
198         // disabled interface should not be on the list
199         assertEquals(expectedIds, actualIds);
200     }
201 }