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;
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;
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";
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;
57 private L2FibEntryCustomizer customizer;
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);
64 customizer = new L2FibEntryCustomizer(
66 new NamingContext("generatedBdName", BD_CTX_NAME),
67 new NamingContext("generatedIfaceName", IFC_CTX_NAME));
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));
75 private void whenL2FibAddDelThenSuccess() {
76 doReturn(future(new L2FibAddDelReply())).when(api).l2FibAddDel(any(L2FibAddDel.class));
79 private void whenL2FibAddDelThenFailure() {
80 doReturn(failedFuture()).when(api).l2FibAddDel(any(L2FibAddDel.class));
83 private L2FibAddDel generateL2FibAddDelRequest(final long mac, final byte isAdd) {
84 final L2FibAddDel request = new L2FibAddDel();
87 request.swIfIndex = IFACE_ID;
88 request.isAdd = isAdd;
90 request.staticMac = 1;
91 request.filterMac = 1;
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();
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);
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);
127 whenL2FibAddDelThenSuccess();
129 customizer.writeCurrentAttributes(id, entry, writeContext);
131 verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 1));
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);
141 whenL2FibAddDelThenFailure();
144 customizer.writeCurrentAttributes(id, entry, writeContext);
145 } catch (WriteFailedException.CreateFailedException e) {
146 assertTrue(e.getCause() instanceof VppBaseCallException);
147 verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 1));
150 fail("WriteFailedException.CreateFailedException was expected");
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);
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);
166 whenL2FibAddDelThenSuccess();
168 customizer.deleteCurrentAttributes(id, entry, writeContext);
170 verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 0));
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);
180 whenL2FibAddDelThenFailure();
183 customizer.deleteCurrentAttributes(id, entry, writeContext);
184 } catch (WriteFailedException.DeleteFailedException e) {
185 assertTrue(e.getCause() instanceof VppBaseCallException);
186 verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 0));
189 fail("WriteFailedException.DeleteFailedException was expected");