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.vpp;
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 import static org.mockito.MockitoAnnotations.initMocks;
28 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
29 import io.fd.honeycomb.translate.write.WriteContext;
30 import io.fd.honeycomb.translate.MappingContext;
31 import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
32 import io.fd.honeycomb.translate.v3po.util.NamingContext;
33 import io.fd.honeycomb.translate.write.WriteFailedException;
34 import java.util.concurrent.CompletableFuture;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Mock;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.L2FibFilter;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.L2FibTable;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntryBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntryKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomains;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainKey;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.openvpp.jvpp.VppBaseCallException;
50 import org.openvpp.jvpp.VppInvocationException;
51 import org.openvpp.jvpp.core.dto.L2FibAddDel;
52 import org.openvpp.jvpp.core.dto.L2FibAddDelReply;
53 import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewriteReply;
54 import org.openvpp.jvpp.core.future.FutureJVppCore;
56 public class L2FibEntryCustomizerTest {
57 private static final String BD_CTX_NAME = "bd-test-instance";
58 private static final String IFC_CTX_NAME = "ifc-test-instance";
60 private static final String BD_NAME = "testBD0";
61 private static final int BD_ID = 111;
62 private static final String IFACE_NAME = "eth0";
63 private static final int IFACE_ID = 123;
66 private FutureJVppCore api;
68 private WriteContext writeContext;
70 private MappingContext mappingContext;
72 private NamingContext bdContext;
73 private NamingContext interfaceContext;
75 private L2FibEntryCustomizer customizer;
78 public void setUp() throws Exception {
80 doReturn(mappingContext).when(writeContext).getMappingContext();
81 bdContext = new NamingContext("generatedBdName", BD_CTX_NAME);
82 ContextTestUtils.mockMapping(mappingContext, BD_NAME, BD_ID, BD_CTX_NAME);
83 interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
84 ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
86 customizer = new L2FibEntryCustomizer(api, bdContext, interfaceContext);
89 private static InstanceIdentifier<L2FibEntry> getL2FibEntryId(final PhysAddress address) {
90 return InstanceIdentifier.create(BridgeDomains.class).child(BridgeDomain.class, new BridgeDomainKey(BD_NAME))
91 .child(L2FibTable.class).child(L2FibEntry.class, new L2FibEntryKey(address));
94 private void whenL2FibAddDelThenSuccess() {
95 final CompletableFuture<L2FibAddDelReply> replyFuture = new CompletableFuture<>();
96 final L2FibAddDelReply reply = new L2FibAddDelReply();
97 replyFuture.complete(reply);
98 doReturn(replyFuture).when(api).l2FibAddDel(any(L2FibAddDel.class));
101 private void whenL2FibAddDelThenFailure() {
102 doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
103 .l2FibAddDel(any(L2FibAddDel.class));
106 private L2FibAddDel generateL2FibAddDelRequest(final long mac, final byte isAdd) {
107 final L2FibAddDel request = new L2FibAddDel();
109 request.bdId = BD_ID;
110 request.swIfIndex = IFACE_ID;
111 request.isAdd = isAdd;
113 request.staticMac = 1;
114 request.filterMac = 1;
119 private L2FibEntry generateL2FibEntry(final PhysAddress address) {
120 final L2FibEntryBuilder entry = new L2FibEntryBuilder();
121 entry.setKey(new L2FibEntryKey(address));
122 entry.setPhysAddress(address);
123 entry.setStaticConfig(true);
124 entry.setBridgedVirtualInterface(false);
125 entry.setAction(L2FibFilter.class);
126 entry.setOutgoingInterface(IFACE_NAME);
127 return entry.build();
130 private void verifyL2FibAddDelWasInvoked(final L2FibAddDel expected) throws
131 VppInvocationException {
132 ArgumentCaptor<L2FibAddDel> argumentCaptor = ArgumentCaptor.forClass(L2FibAddDel.class);
133 verify(api).l2FibAddDel(argumentCaptor.capture());
134 final L2FibAddDel actual = argumentCaptor.getValue();
135 assertEquals(expected.mac, actual.mac);
136 assertEquals(expected.bdId, actual.bdId);
137 assertEquals(expected.swIfIndex, actual.swIfIndex);
138 assertEquals(expected.isAdd, actual.isAdd);
139 assertEquals(expected.staticMac, actual.staticMac);
140 assertEquals(expected.filterMac, actual.filterMac);
144 public void testCreate() throws Exception {
145 final long address_vpp = 0x0102030405060000L;
146 final PhysAddress address = new PhysAddress("01:02:03:04:05:06");
147 final L2FibEntry entry = generateL2FibEntry(address);
148 final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
150 whenL2FibAddDelThenSuccess();
152 customizer.writeCurrentAttributes(id, entry, writeContext);
154 verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 1));
158 public void testCreateFailed() throws Exception {
159 final long address_vpp = 0x1122334455660000L;
160 final PhysAddress address = new PhysAddress("11:22:33:44:55:66");
161 final L2FibEntry entry = generateL2FibEntry(address);
162 final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
164 whenL2FibAddDelThenFailure();
167 customizer.writeCurrentAttributes(id, entry, writeContext);
168 } catch (WriteFailedException.CreateFailedException e) {
169 assertTrue(e.getCause() instanceof VppBaseCallException);
170 verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 1));
173 fail("WriteFailedException.CreateFailedException was expected");
176 @Test(expected = UnsupportedOperationException.class)
177 public void testUpdate() throws Exception {
178 customizer.updateCurrentAttributes(InstanceIdentifier.create(L2FibEntry.class), mock(L2FibEntry.class),
179 mock(L2FibEntry.class), writeContext);
183 public void testDelete() throws Exception {
184 final long address_vpp = 0x1122334455660000L;
185 final PhysAddress address = new PhysAddress("11:22:33:44:55:66");
186 final L2FibEntry entry = generateL2FibEntry(address);
187 final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
189 whenL2FibAddDelThenSuccess();
191 customizer.deleteCurrentAttributes(id, entry, writeContext);
193 verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 0));
197 public void testDeleteFailed() throws Exception {
198 final long address_vpp = 0x0102030405060000L;
199 final PhysAddress address = new PhysAddress("01:02:03:04:05:06");
200 final L2FibEntry entry = generateL2FibEntry(address);
201 final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
203 whenL2FibAddDelThenFailure();
206 customizer.deleteCurrentAttributes(id, entry, writeContext);
207 } catch (WriteFailedException.DeleteFailedException e) {
208 assertTrue(e.getCause() instanceof VppBaseCallException);
209 verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 0));
212 fail("WriteFailedException.DeleteFailedException was expected");