HONEYCOMB-116: utility for stubbing jvpp methods
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / vpp / L2FibEntryCustomizerTest.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.vpp;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26
27 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
28 import io.fd.honeycomb.translate.v3po.util.NamingContext;
29 import io.fd.honeycomb.translate.write.WriteFailedException;
30 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
31 import org.junit.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.L2FibFilter;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.L2FibTable;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntry;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntryBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntryKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomains;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.openvpp.jvpp.VppBaseCallException;
44 import org.openvpp.jvpp.VppInvocationException;
45 import org.openvpp.jvpp.core.dto.L2FibAddDel;
46 import org.openvpp.jvpp.core.dto.L2FibAddDelReply;
47
48 public class L2FibEntryCustomizerTest extends WriterCustomizerTest {
49     private static final String BD_CTX_NAME = "bd-test-instance";
50     private static final String IFC_CTX_NAME = "ifc-test-instance";
51
52     private static final String BD_NAME = "testBD0";
53     private static final int BD_ID = 111;
54     private static final String IFACE_NAME = "eth0";
55     private static final int IFACE_ID = 123;
56
57     private L2FibEntryCustomizer customizer;
58
59     @Override
60     public void setUp() throws Exception {
61         ContextTestUtils.mockMapping(mappingContext, BD_NAME, BD_ID, BD_CTX_NAME);
62         ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
63
64         customizer = new L2FibEntryCustomizer(
65             api,
66             new NamingContext("generatedBdName", BD_CTX_NAME),
67             new NamingContext("generatedIfaceName", IFC_CTX_NAME));
68     }
69
70     private static InstanceIdentifier<L2FibEntry> getL2FibEntryId(final PhysAddress address) {
71         return InstanceIdentifier.create(BridgeDomains.class).child(BridgeDomain.class, new BridgeDomainKey(BD_NAME))
72             .child(L2FibTable.class).child(L2FibEntry.class, new L2FibEntryKey(address));
73     }
74
75     private void whenL2FibAddDelThenSuccess() {
76         doReturn(future(new L2FibAddDelReply())).when(api).l2FibAddDel(any(L2FibAddDel.class));
77     }
78
79     private void whenL2FibAddDelThenFailure() {
80         doReturn(failedFuture()).when(api).l2FibAddDel(any(L2FibAddDel.class));
81     }
82
83     private L2FibAddDel generateL2FibAddDelRequest(final long mac, final byte isAdd) {
84         final L2FibAddDel request = new L2FibAddDel();
85         request.mac = mac;
86         request.bdId = BD_ID;
87         request.swIfIndex = IFACE_ID;
88         request.isAdd = isAdd;
89         if (isAdd == 1) {
90             request.staticMac = 1;
91             request.filterMac = 1;
92         }
93         return request;
94     }
95
96     private L2FibEntry generateL2FibEntry(final PhysAddress address) {
97         final L2FibEntryBuilder entry = new L2FibEntryBuilder();
98         entry.setKey(new L2FibEntryKey(address));
99         entry.setPhysAddress(address);
100         entry.setStaticConfig(true);
101         entry.setBridgedVirtualInterface(false);
102         entry.setAction(L2FibFilter.class);
103         entry.setOutgoingInterface(IFACE_NAME);
104         return entry.build();
105     }
106
107     private void verifyL2FibAddDelWasInvoked(final L2FibAddDel expected) throws
108         VppInvocationException {
109         ArgumentCaptor<L2FibAddDel> argumentCaptor = ArgumentCaptor.forClass(L2FibAddDel.class);
110         verify(api).l2FibAddDel(argumentCaptor.capture());
111         final L2FibAddDel actual = argumentCaptor.getValue();
112         assertEquals(expected.mac, actual.mac);
113         assertEquals(expected.bdId, actual.bdId);
114         assertEquals(expected.swIfIndex, actual.swIfIndex);
115         assertEquals(expected.isAdd, actual.isAdd);
116         assertEquals(expected.staticMac, actual.staticMac);
117         assertEquals(expected.filterMac, actual.filterMac);
118     }
119
120     @Test
121     public void testCreate() throws Exception {
122         final long address_vpp = 0x0102030405060000L;
123         final PhysAddress address = new PhysAddress("01:02:03:04:05:06");
124         final L2FibEntry entry = generateL2FibEntry(address);
125         final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
126
127         whenL2FibAddDelThenSuccess();
128
129         customizer.writeCurrentAttributes(id, entry, writeContext);
130
131         verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 1));
132     }
133
134     @Test
135     public void testCreateFailed() throws Exception {
136         final long address_vpp = 0x1122334455660000L;
137         final PhysAddress address = new PhysAddress("11:22:33:44:55:66");
138         final L2FibEntry entry = generateL2FibEntry(address);
139         final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
140
141         whenL2FibAddDelThenFailure();
142
143         try {
144             customizer.writeCurrentAttributes(id, entry, writeContext);
145         } catch (WriteFailedException.CreateFailedException e) {
146             assertTrue(e.getCause() instanceof VppBaseCallException);
147             verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 1));
148             return;
149         }
150         fail("WriteFailedException.CreateFailedException was expected");
151     }
152
153     @Test(expected = UnsupportedOperationException.class)
154     public void testUpdate() throws Exception {
155         customizer.updateCurrentAttributes(InstanceIdentifier.create(L2FibEntry.class), mock(L2FibEntry.class),
156             mock(L2FibEntry.class), writeContext);
157     }
158
159     @Test
160     public void testDelete() throws Exception {
161         final long address_vpp = 0x1122334455660000L;
162         final PhysAddress address = new PhysAddress("11:22:33:44:55:66");
163         final L2FibEntry entry = generateL2FibEntry(address);
164         final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
165
166         whenL2FibAddDelThenSuccess();
167
168         customizer.deleteCurrentAttributes(id, entry, writeContext);
169
170         verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 0));
171     }
172
173     @Test
174     public void testDeleteFailed() throws Exception {
175         final long address_vpp = 0x0102030405060000L;
176         final PhysAddress address = new PhysAddress("01:02:03:04:05:06");
177         final L2FibEntry entry = generateL2FibEntry(address);
178         final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
179
180         whenL2FibAddDelThenFailure();
181
182         try {
183             customizer.deleteCurrentAttributes(id, entry, writeContext);
184         } catch (WriteFailedException.DeleteFailedException e) {
185             assertTrue(e.getCause() instanceof VppBaseCallException);
186             verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 0));
187             return;
188         }
189         fail("WriteFailedException.DeleteFailedException was expected");
190     }
191 }