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.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.MockitoAnnotations.initMocks;
29 import io.fd.honeycomb.translate.MappingContext;
30 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
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.WriteContext;
34 import io.fd.honeycomb.translate.write.WriteFailedException;
35 import java.util.concurrent.CompletableFuture;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Mock;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.bridge.domain.attributes.ArpTerminationTable;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.bridge.domain.attributes.arp.termination.table.ArpTerminationTableEntry;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.bridge.domain.attributes.arp.termination.table.ArpTerminationTableEntryBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.bridge.domain.attributes.arp.termination.table.ArpTerminationTableEntryKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomains;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainKey;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.openvpp.jvpp.VppBaseCallException;
52 import org.openvpp.jvpp.VppInvocationException;
53 import org.openvpp.jvpp.core.dto.BdIpMacAddDel;
54 import org.openvpp.jvpp.core.dto.BdIpMacAddDelReply;
55 import org.openvpp.jvpp.core.future.FutureJVppCore;
57 public class ArpTerminationTableEntryCustomizerTest {
58 private static final String BD_CTX_NAME = "bd-test-instance";
59 private static final String IFC_CTX_NAME = "ifc-test-instance";
61 private static final String BD_NAME = "testBD0";
62 private static final int BD_ID = 111;
63 private static final String IFACE_NAME = "eth0";
64 private static final int IFACE_ID = 123;
67 private FutureJVppCore api;
69 private WriteContext writeContext;
71 private MappingContext mappingContext;
73 private NamingContext bdContext;
75 private ArpTerminationTableEntryCustomizer customizer;
76 private byte[] ipAddressRaw;
77 private byte[] physAddressRaw;
78 private PhysAddress physAddress;
79 private IpAddress ipAddress;
80 private ArpTerminationTableEntry entry;
81 private InstanceIdentifier<ArpTerminationTableEntry> id;
84 public void setUp() throws Exception {
86 doReturn(mappingContext).when(writeContext).getMappingContext();
87 bdContext = new NamingContext("generatedBdName", BD_CTX_NAME);
89 customizer = new ArpTerminationTableEntryCustomizer(api, bdContext);
91 ipAddressRaw = new byte[] {1, 2, 3, 4};
92 physAddressRaw = new byte[] {1, 2, 3, 4, 5, 6};
93 physAddress = new PhysAddress("01:02:03:04:05:06");
95 ipAddress = new IpAddress(Ipv4AddressNoZone.getDefaultInstance("1.2.3.4"));
96 entry = generateArpEntry(ipAddress, physAddress);
97 id = getArpEntryId(ipAddress, physAddress);
99 ContextTestUtils.mockMapping(mappingContext, BD_NAME, BD_ID, BD_CTX_NAME);
100 ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
103 private static InstanceIdentifier<ArpTerminationTableEntry> getArpEntryId(final IpAddress ipAddress,
104 final PhysAddress physAddress) {
105 return InstanceIdentifier.create(BridgeDomains.class).child(BridgeDomain.class, new BridgeDomainKey(BD_NAME))
106 .child(ArpTerminationTable.class)
107 .child(ArpTerminationTableEntry.class, new ArpTerminationTableEntryKey(ipAddress, physAddress));
110 private void whenBdIpMacAddDelThenSuccess() {
111 final CompletableFuture<BdIpMacAddDelReply> replyFuture = new CompletableFuture<>();
112 final BdIpMacAddDelReply reply = new BdIpMacAddDelReply();
113 replyFuture.complete(reply);
114 doReturn(replyFuture).when(api).bdIpMacAddDel(any(BdIpMacAddDel.class));
117 private void whenBdIpMacAddDelThenFailure() {
118 doReturn(TestHelperUtils.<BdIpMacAddDelReply>createFutureException()).when(api)
119 .bdIpMacAddDel(any(BdIpMacAddDel.class));
122 private BdIpMacAddDel generateBdIpMacAddDelRequest(final byte[] ipAddress, final byte[] macAddress,
124 final BdIpMacAddDel request = new BdIpMacAddDel();
125 request.ipAddress = ipAddress;
126 request.macAddress = macAddress;
127 request.bdId = BD_ID;
128 request.isAdd = isAdd;
132 private ArpTerminationTableEntry generateArpEntry(final IpAddress ipAddress, final PhysAddress physAddress) {
133 final ArpTerminationTableEntryBuilder entry = new ArpTerminationTableEntryBuilder();
134 entry.setKey(new ArpTerminationTableEntryKey(ipAddress, physAddress));
135 entry.setPhysAddress(physAddress);
136 entry.setIpAddress(ipAddress);
137 return entry.build();
140 private void verifyBdIpMacAddDelWasInvoked(final BdIpMacAddDel expected) throws
141 VppInvocationException {
142 ArgumentCaptor<BdIpMacAddDel> argumentCaptor = ArgumentCaptor.forClass(BdIpMacAddDel.class);
143 verify(api).bdIpMacAddDel(argumentCaptor.capture());
144 final BdIpMacAddDel actual = argumentCaptor.getValue();
145 assertArrayEquals(expected.macAddress, actual.macAddress);
146 assertArrayEquals(expected.ipAddress, actual.ipAddress);
147 assertEquals(expected.bdId, actual.bdId);
148 assertEquals(expected.isAdd, actual.isAdd);
152 public void testCreate() throws Exception {
153 whenBdIpMacAddDelThenSuccess();
154 customizer.writeCurrentAttributes(id, entry, writeContext);
155 verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 1));
159 public void testCreateFailed() throws Exception {
160 whenBdIpMacAddDelThenFailure();
162 customizer.writeCurrentAttributes(id, entry, writeContext);
163 } catch (WriteFailedException.CreateFailedException e) {
164 assertTrue(e.getCause() instanceof VppBaseCallException);
165 verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 1));
168 fail("WriteFailedException.CreateFailedException was expected");
171 @Test(expected = UnsupportedOperationException.class)
172 public void testUpdate() throws Exception {
173 customizer.updateCurrentAttributes(InstanceIdentifier.create(ArpTerminationTableEntry.class),
174 mock(ArpTerminationTableEntry.class),
175 mock(ArpTerminationTableEntry.class), writeContext);
179 public void testDelete() throws Exception {
180 whenBdIpMacAddDelThenSuccess();
181 customizer.deleteCurrentAttributes(id, entry, writeContext);
182 verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 0));
186 public void testDeleteFailed() throws Exception {
187 whenBdIpMacAddDelThenFailure();
189 customizer.deleteCurrentAttributes(id, entry, writeContext);
190 } catch (WriteFailedException.DeleteFailedException e) {
191 assertTrue(e.getCause() instanceof VppBaseCallException);
192 verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 0));
195 fail("WriteFailedException.DeleteFailedException was expected");