8bde1ac320c1036d6adf29cc5dc9064392cb37c1
[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 com.google.common.base.Optional;
29 import com.google.common.collect.Lists;
30 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
31 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
32 import io.fd.honeycomb.translate.v3po.test.InterfaceTestUtils;
33 import io.fd.honeycomb.translate.v3po.test.ListReaderCustomizerTest;
34 import io.fd.honeycomb.translate.v3po.util.NamingContext;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38 import org.junit.Assert;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
42 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
44 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
52 import org.openvpp.jvpp.VppInvocationException;
53 import org.openvpp.jvpp.dto.SwInterfaceDetails;
54 import org.openvpp.jvpp.dto.SwInterfaceDump;
55
56 public class InterfaceCustomizerTest extends
57         ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> {
58
59     private NamingContext interfacesContext;
60
61     public InterfaceCustomizerTest() {
62         super(Interface.class);
63     }
64
65     @Override
66     public void setUpBefore() {
67         interfacesContext = new NamingContext("generatedIfaceName", "test-instance");
68     }
69
70     @Override
71     protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
72         final KeyedInstanceIdentifier<Mapping, MappingKey> eth0Id = ContextTestUtils
73                 .getMappingIid("eth0", "test-instance");
74         final KeyedInstanceIdentifier<Mapping, MappingKey> eth1Id = ContextTestUtils
75                 .getMappingIid("eth1", "test-instance");
76         final KeyedInstanceIdentifier<Mapping, MappingKey> subEth1Id = ContextTestUtils
77                 .getMappingIid("eth1.1", "test-instance");
78         final Optional<Mapping> eth0 = ContextTestUtils.getMapping("eth0", 0);
79         final Optional<Mapping> eth1 = ContextTestUtils.getMapping("eth1", 1);
80         final Optional<Mapping> subEth1 = ContextTestUtils.getMapping("eth1.1", 2);
81
82         final List<Mapping> allMappings =
83             Lists.newArrayList(ContextTestUtils.getMapping("eth0", 0).get(), ContextTestUtils.getMapping("eth1", 1).get(), ContextTestUtils
84                     .getMapping("eth1.1", 2).get());
85         final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build();
86         doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(eth0Id.firstIdentifierOf(Mappings.class));
87
88         doReturn(eth0).when(mappingContext).read(eth0Id);
89         doReturn(eth1).when(mappingContext).read(eth1Id);
90         doReturn(subEth1).when(mappingContext).read(subEth1Id);
91
92         return new InterfaceCustomizer(api, interfacesContext);
93     }
94
95     // TODO use reflexion and move to ListReaderCustomizerTest
96     @Test
97     public void testMerge() throws Exception {
98         final InterfacesStateBuilder builder = mock(InterfacesStateBuilder.class);
99         final List<Interface> value = Collections.emptyList();
100         getCustomizer().merge(builder, value);
101         verify(builder).setInterface(value);
102     }
103
104     private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName,
105                                                  final int dumpIfcsInvocationCount)
106         throws VppInvocationException {
107         // TODO adding equals methods for jvpp DTOs would make ArgumentCaptor usage obsolete
108         ArgumentCaptor<SwInterfaceDump> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceDump.class);
109         verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(argumentCaptor.capture());
110         final SwInterfaceDump actual = argumentCaptor.getValue();
111         assertEquals(nameFilterValid, actual.nameFilterValid);
112         assertArrayEquals(ifaceName.getBytes(), actual.nameFilter);
113     }
114
115     private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
116         assertEquals(iface.getName(), new String(details.interfaceName));
117         Assert.assertEquals(InterfaceUtils.yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
118         assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address));
119     }
120
121     @Test
122     public void testReadCurrentAttributes() throws Exception {
123         final String ifaceName = "eth0";
124         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
125             .child(Interface.class, new InterfaceKey(ifaceName));
126         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
127
128         final SwInterfaceDetails iface = new SwInterfaceDetails();
129         iface.interfaceName = ifaceName.getBytes();
130         iface.swIfIndex = 0;
131         iface.linkSpeed = 1;
132         iface.l2AddressLength = 6;
133         iface.l2Address = new byte[iface.l2AddressLength];
134         final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
135         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, interfaceList);
136
137         getCustomizer().readCurrentAttributes(id, builder, ctx);
138
139         verifySwInterfaceDumpWasInvoked(1, ifaceName, 1);
140         assertIfacesAreEqual(builder.build(), iface);
141     }
142
143     @Test
144     public void testReadCurrentAttributesFailed() throws Exception {
145         final String ifaceName = "eth0";
146         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
147             .child(Interface.class, new InterfaceKey(ifaceName));
148         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
149
150         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Collections.emptyList());
151
152         try {
153             getCustomizer().readCurrentAttributes(id, builder, ctx);
154         } catch (IllegalArgumentException e) {
155             verifySwInterfaceDumpWasInvoked(0, ifaceName, 2);
156             return;
157         }
158
159         fail("ReadFailedException was expected");
160     }
161
162     @Test
163     public void testReadSubInterface() throws Exception {
164         final String ifaceName = "eth1.1";
165         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
166             .child(Interface.class, new InterfaceKey(ifaceName));
167         final InterfaceBuilder builder = mock(InterfaceBuilder.class);
168
169         final SwInterfaceDetails iface = new SwInterfaceDetails();
170         iface.interfaceName = ifaceName.getBytes();
171         iface.swIfIndex = 2;
172         iface.supSwIfIndex = 1;
173         iface.subId = 1;
174         final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
175         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, interfaceList);
176
177         getCustomizer().readCurrentAttributes(id, builder, ctx);
178
179         verifySwInterfaceDumpWasInvoked(1, ifaceName, 1);
180         verifyZeroInteractions(builder);
181     }
182
183     @Test
184     public void testGetAllIds() throws Exception {
185         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
186             .child(Interface.class);
187
188         final String swIf0Name = "eth0";
189         final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
190         swIf0.swIfIndex = 0;
191         swIf0.interfaceName = swIf0Name.getBytes();
192         final String swIf1Name = "eth1";
193         final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
194         swIf1.swIfIndex = 1;
195         swIf1.interfaceName = swIf1Name.getBytes();
196         final String swSubIf1Name = "eth1.1";
197         final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails();
198         swSubIf1.swIfIndex = 2;
199         swSubIf1.subId = 1;
200         swSubIf1.supSwIfIndex = 1;
201         swSubIf1.interfaceName = swSubIf1Name.getBytes();
202         InterfaceTestUtils.whenSwInterfaceDumpThenReturn(api, Arrays.asList(swIf0, swIf1, swSubIf1));
203
204         final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(swIf0Name), new InterfaceKey(swIf1Name));
205         final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
206
207         verifySwInterfaceDumpWasInvoked(0, "", 1);
208
209         // sub-interface should not be on the list
210         assertEquals(expectedIds, actualIds);
211     }
212 }