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