2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.v3po.interfacesstate;
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;
31 import com.google.common.base.Optional;
32 import com.google.common.collect.Lists;
33 import io.fd.honeycomb.v3po.translate.spi.read.RootReaderCustomizer;
34 import io.fd.honeycomb.v3po.translate.v3po.test.ListReaderCustomizerTest;
35 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
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;
56 public class InterfaceCustomizerTest extends
57 ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> {
59 private NamingContext interfacesContext;
61 public InterfaceCustomizerTest() {
62 super(Interface.class);
66 public void setUpBefore() {
67 interfacesContext = new NamingContext("generatedIfaceName", "test-instance");
71 protected RootReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
72 final KeyedInstanceIdentifier<Mapping, MappingKey> eth0Id = getMappingIid("eth0", "test-instance");
73 final KeyedInstanceIdentifier<Mapping, MappingKey> eth1Id = getMappingIid("eth1", "test-instance");
74 final Optional<Mapping> eth0 = getMapping("eth0", 0);
75 final Optional<Mapping> eth1 = getMapping("eth1", 1);
77 final List<Mapping> allMappings = Lists.newArrayList(getMapping("eth0", 0).get(), getMapping("eth1", 1).get());
78 final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build();
79 doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(eth0Id.firstIdentifierOf(Mappings.class));
81 doReturn(eth0).when(mappingContext).read(eth0Id);
82 doReturn(eth1).when(mappingContext).read(eth1Id);
84 return new InterfaceCustomizer(api, interfacesContext);
87 // TODO use reflexion and move to ListReaderCustomizerTest
89 public void testMerge() throws Exception {
90 final InterfacesStateBuilder builder = mock(InterfacesStateBuilder.class);
91 final List<Interface> value = Collections.emptyList();
92 getCustomizer().merge(builder, value);
93 verify(builder).setInterface(value);
96 private void verifyBridgeDomainDumpUpdateWasInvoked(final int nameFilterValid, final String ifaceName,
97 final int dumpIfcsInvocationCount) throws VppInvocationException {
98 // TODO adding equals methods for jvpp DTOs would make ArgumentCaptor usage obsolete
99 ArgumentCaptor<SwInterfaceDump> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceDump.class);
100 verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(argumentCaptor.capture());
101 final SwInterfaceDump actual = argumentCaptor.getValue();
102 assertEquals(nameFilterValid, actual.nameFilterValid);
103 assertArrayEquals(ifaceName.getBytes(), actual.nameFilter);
106 private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
107 assertEquals(iface.getName(), new String(details.interfaceName));
108 assertEquals(yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
109 assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address));
115 public void testReadCurrentAttributes() throws Exception {
116 final String ifaceName = "eth0";
117 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
118 .child(Interface.class, new InterfaceKey(ifaceName));
119 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
121 final SwInterfaceDetails iface = new SwInterfaceDetails();
122 iface.interfaceName = ifaceName.getBytes();
125 iface.l2AddressLength = 6;
126 iface.l2Address = new byte[iface.l2AddressLength];
127 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
128 whenSwInterfaceDumpThenReturn(api, interfaceList);
130 getCustomizer().readCurrentAttributes(id, builder, ctx);
132 verifyBridgeDomainDumpUpdateWasInvoked(1, ifaceName, 1);
133 assertIfacesAreEqual(builder.build(), iface);
137 public void testReadCurrentAttributesFailed() throws Exception {
138 final String ifaceName = "eth0";
139 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
140 .child(Interface.class, new InterfaceKey(ifaceName));
141 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
143 whenSwInterfaceDumpThenReturn(api, Collections.emptyList());
146 getCustomizer().readCurrentAttributes(id, builder, ctx);
147 } catch (IllegalArgumentException e) {
148 verifyBridgeDomainDumpUpdateWasInvoked(0, ifaceName, 2);
152 fail("ReadFailedException was expected");
156 public void testGetAllIds() throws Exception {
157 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
158 .child(Interface.class);
160 final String swIf0Name = "eth0";
161 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
163 swIf0.interfaceName = swIf0Name.getBytes();
164 final String swIf1Name = "eth1";
165 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
167 swIf1.interfaceName = swIf1Name.getBytes();
168 whenSwInterfaceDumpThenReturn(api, Arrays.asList(swIf0, swIf1));
170 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(swIf0Name), new InterfaceKey(swIf1Name));
171 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
173 verifyBridgeDomainDumpUpdateWasInvoked(0, "", 1);
175 assertEquals(expectedIds, actualIds);