4a1ca91b30abafe6211b5b73faba748c223816e6
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfacesstate / VhostUserCustomizerTest.java
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.translate.v3po.interfacesstate;
18
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;
23
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.rev150105.VhostUserRole;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VhostUser;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.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;
43
44 public class VhostUserCustomizerTest extends ReaderCustomizerTest<VhostUser, VhostUserBuilder> implements
45     InterfaceDumpHelper {
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);
52
53     private NamingContext interfaceContext;
54
55     public VhostUserCustomizerTest() {
56         super(VhostUser.class, VppInterfaceStateAugmentationBuilder.class);
57     }
58
59     @Override
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());
64     }
65
66     private SwInterfaceDetails ifaceDetails() {
67         final SwInterfaceDetails details = new SwInterfaceDetails();
68         details.swIfIndex = IF_INDEX;
69         details.interfaceName = IF_NAME.getBytes();
70         return details;
71     }
72
73     @Override
74     protected ReaderCustomizer<VhostUser, VhostUserBuilder> initCustomizer() {
75         return new VhostUserCustomizer(api, interfaceContext);
76     }
77
78     @Test
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);
84     }
85
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);
90     }
91
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();
97         details.isServer = 1;
98         details.features = 2;
99         details.numRegions = 3;
100         details.sockFilename = "socketName".getBytes();
101         details.virtioNetHdrSz = 4;
102         details.sockErrno = 5;
103         reply.swInterfaceVhostUserDetails.add(details);
104         return reply;
105     }
106
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");
114     }
115 }