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 org.junit.Assert.assertArrayEquals;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.fail;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
29 import io.fd.honeycomb.v3po.translate.spi.read.RootReaderCustomizer;
30 import io.fd.honeycomb.v3po.translate.v3po.test.ListReaderCustomizerTest;
31 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.CompletionStage;
37 import java.util.concurrent.ExecutionException;
38 import org.junit.Test;
39 import org.mockito.ArgumentCaptor;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.openvpp.jvpp.dto.SwInterfaceDetails;
47 import org.openvpp.jvpp.dto.SwInterfaceDetailsReplyDump;
48 import org.openvpp.jvpp.dto.SwInterfaceDump;
50 public class InterfaceCustomizerTest extends
51 ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> {
53 private NamingContext interfacesContext;
55 public InterfaceCustomizerTest() {
56 super(Interface.class);
60 public void setUpBefore() {
61 interfacesContext = new NamingContext("generatedIfaceName");
65 protected RootReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
66 return new InterfaceCustomizer(api, interfacesContext);
69 // TODO use reflexion and move to ListReaderCustomizerTest
71 public void testMerge() throws Exception {
72 final InterfacesStateBuilder builder = mock(InterfacesStateBuilder.class);
73 final List<Interface> value = Collections.emptyList();
74 getCustomizer().merge(builder, value);
75 verify(builder).setInterface(value);
78 private void verifyBridgeDomainDumpUpdateWasInvoked(final int nameFilterValid, final String ifaceName) {
79 // TODO adding equals methods for jvpp DTOs would make ArgumentCaptor usage obsolete
80 ArgumentCaptor<SwInterfaceDump> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceDump.class);
81 verify(api).swInterfaceDump(argumentCaptor.capture());
82 final SwInterfaceDump actual = argumentCaptor.getValue();
83 assertEquals(nameFilterValid, actual.nameFilterValid);
84 assertArrayEquals(ifaceName.getBytes(), actual.nameFilter);
87 private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
88 assertEquals(iface.getName(), new String(details.interfaceName));
89 assertEquals(YangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
90 assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address));
93 private void whenSwInterfaceDumpThenReturn(final List<SwInterfaceDetails> interfaceList)
94 throws ExecutionException, InterruptedException {
95 final CompletionStage<SwInterfaceDetailsReplyDump> replyCS = mock(CompletionStage.class);
96 final CompletableFuture<SwInterfaceDetailsReplyDump> replyFuture = mock(CompletableFuture.class);
97 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
98 final SwInterfaceDetailsReplyDump reply = new SwInterfaceDetailsReplyDump();
99 reply.swInterfaceDetails = interfaceList;
100 when(replyFuture.get()).thenReturn(reply);
101 when(api.swInterfaceDump(any(SwInterfaceDump.class))).thenReturn(replyCS);
105 public void testReadCurrentAttributes() throws Exception {
106 final String ifaceName = "eth0";
107 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
108 .child(Interface.class, new InterfaceKey(ifaceName));
109 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
111 final SwInterfaceDetails iface = new SwInterfaceDetails();
112 iface.interfaceName = ifaceName.getBytes();
115 iface.l2AddressLength = 6;
116 iface.l2Address = new byte[iface.l2AddressLength];
117 final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
118 whenSwInterfaceDumpThenReturn(interfaceList);
120 getCustomizer().readCurrentAttributes(id, builder, ctx);
122 verifyBridgeDomainDumpUpdateWasInvoked(1, ifaceName);
123 assertIfacesAreEqual(builder.build(), iface);
127 public void testReadCurrentAttributesFailed() throws Exception {
128 final String ifaceName = "eth0";
129 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
130 .child(Interface.class, new InterfaceKey(ifaceName));
131 final InterfaceBuilder builder = getCustomizer().getBuilder(id);
133 whenSwInterfaceDumpThenReturn(Collections.emptyList());
136 getCustomizer().readCurrentAttributes(id, builder, ctx);
137 } catch (ReadFailedException e) {
138 verifyBridgeDomainDumpUpdateWasInvoked(1, ifaceName);
142 fail("ReadFailedException was expected");
146 public void testGetAllIds() throws Exception {
147 final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
148 .child(Interface.class);
150 final String swIf0Name = "eth0";
151 final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
152 swIf0.interfaceName = swIf0Name.getBytes();
153 final String swIf1Name = "eth0";
154 final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
155 swIf1.interfaceName = swIf1Name.getBytes();
156 whenSwInterfaceDumpThenReturn(Arrays.asList(swIf0, swIf1));
158 final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(swIf0Name), new InterfaceKey(swIf1Name));
159 final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
161 verifyBridgeDomainDumpUpdateWasInvoked(0, "");
163 assertEquals(expectedIds, actualIds);