3f33d141f913a26f2cde95d79fe691fee86f6687
[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 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.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
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;
49
50 public class InterfaceCustomizerTest extends
51         ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> {
52
53     private NamingContext interfacesContext;
54
55     public InterfaceCustomizerTest() {
56         super(Interface.class);
57     }
58
59     @Override
60     public void setUpBefore() {
61         interfacesContext = new NamingContext("generatedIfaceName");
62     }
63
64     @Override
65     protected RootReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
66         interfacesContext.addName(0, "eth0");
67         interfacesContext.addName(1, "eth1");
68         return new InterfaceCustomizer(api, interfacesContext);
69     }
70
71     // TODO use reflexion and move to ListReaderCustomizerTest
72     @Test
73     public void testMerge() throws Exception {
74         final InterfacesStateBuilder builder = mock(InterfacesStateBuilder.class);
75         final List<Interface> value = Collections.emptyList();
76         getCustomizer().merge(builder, value);
77         verify(builder).setInterface(value);
78     }
79
80     private void verifyBridgeDomainDumpUpdateWasInvoked(final int nameFilterValid, final String ifaceName,
81                                                         final int dumpIfcsInvocationCount) {
82         // TODO adding equals methods for jvpp DTOs would make ArgumentCaptor usage obsolete
83         ArgumentCaptor<SwInterfaceDump> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceDump.class);
84         verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(argumentCaptor.capture());
85         final SwInterfaceDump actual = argumentCaptor.getValue();
86         assertEquals(nameFilterValid, actual.nameFilterValid);
87         assertArrayEquals(ifaceName.getBytes(), actual.nameFilter);
88     }
89
90     private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) {
91         assertEquals(iface.getName(), new String(details.interfaceName));
92         assertEquals(yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex);
93         assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address));
94     }
95
96     private void whenSwInterfaceDumpThenReturn(final List<SwInterfaceDetails> interfaceList)
97             throws ExecutionException, InterruptedException {
98         final CompletionStage<SwInterfaceDetailsReplyDump> replyCS = mock(CompletionStage.class);
99         final CompletableFuture<SwInterfaceDetailsReplyDump> replyFuture = mock(CompletableFuture.class);
100         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
101         final SwInterfaceDetailsReplyDump reply = new SwInterfaceDetailsReplyDump();
102         reply.swInterfaceDetails = interfaceList;
103         when(replyFuture.get()).thenReturn(reply);
104         when(api.swInterfaceDump(any(SwInterfaceDump.class))).thenReturn(replyCS);
105     }
106
107     @Test
108     public void testReadCurrentAttributes() throws Exception {
109         final String ifaceName = "eth0";
110         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
111                 .child(Interface.class, new InterfaceKey(ifaceName));
112         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
113
114         final SwInterfaceDetails iface = new SwInterfaceDetails();
115         iface.interfaceName = ifaceName.getBytes();
116         iface.swIfIndex = 0;
117         iface.linkSpeed = 1;
118         iface.l2AddressLength = 6;
119         iface.l2Address = new byte[iface.l2AddressLength];
120         final List<SwInterfaceDetails> interfaceList = Collections.singletonList(iface);
121         whenSwInterfaceDumpThenReturn(interfaceList);
122
123         getCustomizer().readCurrentAttributes(id, builder, ctx);
124
125         verifyBridgeDomainDumpUpdateWasInvoked(1, ifaceName, 1);
126         assertIfacesAreEqual(builder.build(), iface);
127     }
128
129     @Test
130     public void testReadCurrentAttributesFailed() throws Exception {
131         final String ifaceName = "eth0";
132         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
133                 .child(Interface.class, new InterfaceKey(ifaceName));
134         final InterfaceBuilder builder = getCustomizer().getBuilder(id);
135
136         whenSwInterfaceDumpThenReturn(Collections.emptyList());
137
138         try {
139             getCustomizer().readCurrentAttributes(id, builder, ctx);
140         } catch (IllegalArgumentException e) {
141             verifyBridgeDomainDumpUpdateWasInvoked(0, ifaceName, 2);
142             return;
143         }
144
145         fail("ReadFailedException was expected");
146     }
147
148     @Test
149     public void testGetAllIds() throws Exception {
150         final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class)
151                 .child(Interface.class);
152
153         final String swIf0Name = "eth0";
154         final SwInterfaceDetails swIf0 = new SwInterfaceDetails();
155         swIf0.swIfIndex = 0;
156         swIf0.interfaceName = swIf0Name.getBytes();
157         final String swIf1Name = "eth1";
158         final SwInterfaceDetails swIf1 = new SwInterfaceDetails();
159         swIf1.swIfIndex = 1;
160         swIf1.interfaceName = swIf1Name.getBytes();
161         whenSwInterfaceDumpThenReturn(Arrays.asList(swIf0, swIf1));
162
163         final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(swIf0Name), new InterfaceKey(swIf1Name));
164         final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx);
165
166         verifyBridgeDomainDumpUpdateWasInvoked(0, "", 1);
167
168         assertEquals(expectedIds, actualIds);
169     }
170 }