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.translate.v3po.interfacesstate;
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
24 import io.fd.honeycomb.translate.read.ReadFailedException;
25 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.honeycomb.vpp.test.read.ReaderCustomizerTest;
28 import io.fd.honeycomb.vpp.test.util.InterfaceDumpHelper;
29 import java.math.BigInteger;
30 import org.junit.Test;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VhostUserRole;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.VhostUser;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.VhostUserBuilder;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
41 import io.fd.vpp.jvpp.core.dto.SwInterfaceVhostUserDetails;
42 import io.fd.vpp.jvpp.core.dto.SwInterfaceVhostUserDetailsReplyDump;
44 public class VhostUserCustomizerTest extends ReaderCustomizerTest<VhostUser, VhostUserBuilder> implements
46 private static final String IFC_CTX_NAME = "ifc-test-instance";
47 private static final String IF_NAME = "VirtualEthernet1";
48 private static final int IF_INDEX = 1;
49 private static final InstanceIdentifier<VhostUser> IID =
50 InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME))
51 .augmentation(VppInterfaceStateAugmentation.class).child(VhostUser.class);
53 private NamingContext interfaceContext;
55 public VhostUserCustomizerTest() {
56 super(VhostUser.class, VppInterfaceStateAugmentationBuilder.class);
60 protected void setUp() throws Exception {
61 interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
62 defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME);
63 whenSwInterfaceDumpThenReturn(api, ifaceDetails());
66 private SwInterfaceDetails ifaceDetails() {
67 final SwInterfaceDetails details = new SwInterfaceDetails();
68 details.swIfIndex = IF_INDEX;
69 details.interfaceName = IF_NAME.getBytes();
74 protected ReaderCustomizer<VhostUser, VhostUserBuilder> initCustomizer() {
75 return new VhostUserCustomizer(api, interfaceContext);
79 public void testRead() throws ReadFailedException {
80 final VhostUserBuilder builder = mock(VhostUserBuilder.class);
81 when(api.swInterfaceVhostUserDump(any())).thenReturn(future(vhostDump()));
82 getCustomizer().readCurrentAttributes(IID, builder, ctx);
83 verifyVhostBuilder(builder);
86 @Test(expected = ReadFailedException.class)
87 public void testReadFailed() throws ReadFailedException {
88 when(api.swInterfaceVhostUserDump(any())).thenReturn(failedFuture());
89 getCustomizer().readCurrentAttributes(IID, mock(VhostUserBuilder.class), ctx);
92 private SwInterfaceVhostUserDetailsReplyDump vhostDump() {
93 final SwInterfaceVhostUserDetailsReplyDump reply = new SwInterfaceVhostUserDetailsReplyDump();
94 final SwInterfaceVhostUserDetails details = new SwInterfaceVhostUserDetails();
95 details.swIfIndex = IF_INDEX;
96 details.interfaceName = IF_NAME.getBytes();
99 details.numRegions = 3;
100 details.sockFilename = "socketName".getBytes();
101 details.virtioNetHdrSz = 4;
102 details.sockErrno = 5;
103 reply.swInterfaceVhostUserDetails.add(details);
107 private void verifyVhostBuilder(final VhostUserBuilder builder) {
108 verify(builder).setRole(VhostUserRole.Server);
109 verify(builder).setFeatures(BigInteger.valueOf(2));
110 verify(builder).setNumMemoryRegions(3L);
111 verify(builder).setSocket("socketName");
112 verify(builder).setVirtioNetHdrSize(4L);
113 verify(builder).setConnectError("5");