24fb70be7602b0cf3a0b33500fe9f822ae2a79b1
[hc2vpp.git] /
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.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;
28
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.dto.BdIpMacAddDel;
54 import org.openvpp.jvpp.dto.BdIpMacAddDelReply;
55 import org.openvpp.jvpp.future.FutureJVpp;
56
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";
60
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;
65
66     @Mock
67     private FutureJVpp api;
68     @Mock
69     private WriteContext writeContext;
70     @Mock
71     private MappingContext mappingContext;
72
73     private NamingContext bdContext;
74
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;
82
83     @Before
84     public void setUp() throws Exception {
85         initMocks(this);
86         doReturn(mappingContext).when(writeContext).getMappingContext();
87         bdContext = new NamingContext("generatedBdName", BD_CTX_NAME);
88
89         customizer = new ArpTerminationTableEntryCustomizer(api, bdContext);
90
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");
94
95         ipAddress = new IpAddress(Ipv4AddressNoZone.getDefaultInstance("1.2.3.4"));
96         entry = generateArpEntry(ipAddress, physAddress);
97         id = getArpEntryId(ipAddress, physAddress);
98
99         ContextTestUtils.mockMapping(mappingContext, BD_NAME, BD_ID, BD_CTX_NAME);
100         ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
101     }
102
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));
108     }
109
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));
115     }
116
117     private void whenBdIpMacAddDelThenFailure() {
118         doReturn(TestHelperUtils.<BdIpMacAddDelReply>createFutureException()).when(api)
119             .bdIpMacAddDel(any(BdIpMacAddDel.class));
120     }
121
122     private BdIpMacAddDel generateBdIpMacAddDelRequest(final byte[] ipAddress, final byte[] macAddress,
123                                                        final byte isAdd) {
124         final BdIpMacAddDel request = new BdIpMacAddDel();
125         request.ipAddress = ipAddress;
126         request.macAddress = macAddress;
127         request.bdId = BD_ID;
128         request.isAdd = isAdd;
129         return request;
130     }
131
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();
138     }
139
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);
149     }
150
151     @Test
152     public void testCreate() throws Exception {
153         whenBdIpMacAddDelThenSuccess();
154         customizer.writeCurrentAttributes(id, entry, writeContext);
155         verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 1));
156     }
157
158     @Test
159     public void testCreateFailed() throws Exception {
160         whenBdIpMacAddDelThenFailure();
161         try {
162             customizer.writeCurrentAttributes(id, entry, writeContext);
163         } catch (WriteFailedException.CreateFailedException e) {
164             assertTrue(e.getCause() instanceof VppBaseCallException);
165             verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 1));
166             return;
167         }
168         fail("WriteFailedException.CreateFailedException was expected");
169     }
170
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);
176     }
177
178     @Test
179     public void testDelete() throws Exception {
180         whenBdIpMacAddDelThenSuccess();
181         customizer.deleteCurrentAttributes(id, entry, writeContext);
182         verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 0));
183     }
184
185     @Test
186     public void testDeleteFailed() throws Exception {
187         whenBdIpMacAddDelThenFailure();
188         try {
189             customizer.deleteCurrentAttributes(id, entry, writeContext);
190         } catch (WriteFailedException.DeleteFailedException e) {
191             assertTrue(e.getCause() instanceof VppBaseCallException);
192             verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 0));
193             return;
194         }
195         fail("WriteFailedException.DeleteFailedException was expected");
196     }
197 }