efd485f1076e73e234344e12a58c028f15d4399d
[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
28 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
29 import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
30 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
31 import io.fd.honeycomb.translate.v3po.util.NamingContext;
32 import io.fd.honeycomb.translate.write.WriteFailedException;
33 import java.util.concurrent.CompletableFuture;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.bridge.domain.attributes.ArpTerminationTable;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.bridge.domain.attributes.arp.termination.table.ArpTerminationTableEntry;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.bridge.domain.attributes.arp.termination.table.ArpTerminationTableEntryBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.bridge.domain.attributes.arp.termination.table.ArpTerminationTableEntryKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomains;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainKey;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.openvpp.jvpp.VppBaseCallException;
48 import org.openvpp.jvpp.VppInvocationException;
49 import org.openvpp.jvpp.core.dto.BdIpMacAddDel;
50 import org.openvpp.jvpp.core.dto.BdIpMacAddDelReply;
51
52 public class ArpTerminationTableEntryCustomizerTest extends WriterCustomizerTest {
53     private static final String BD_CTX_NAME = "bd-test-instance";
54     private static final String IFC_CTX_NAME = "ifc-test-instance";
55
56     private static final String BD_NAME = "testBD0";
57     private static final int BD_ID = 111;
58     private static final String IFACE_NAME = "eth0";
59     private static final int IFACE_ID = 123;
60     private ArpTerminationTableEntryCustomizer customizer;
61     private byte[] ipAddressRaw;
62     private byte[] physAddressRaw;
63     private PhysAddress physAddress;
64     private IpAddress ipAddress;
65     private ArpTerminationTableEntry entry;
66     private InstanceIdentifier<ArpTerminationTableEntry> id;
67
68     @Override
69     public void setUp() throws Exception {
70         customizer = new ArpTerminationTableEntryCustomizer(api, new NamingContext("generatedBdName", BD_CTX_NAME));
71
72         ipAddressRaw = new byte[] {1, 2, 3, 4};
73         physAddressRaw = new byte[] {1, 2, 3, 4, 5, 6};
74         physAddress = new PhysAddress("01:02:03:04:05:06");
75
76         ipAddress = new IpAddress(Ipv4AddressNoZone.getDefaultInstance("1.2.3.4"));
77         entry = generateArpEntry(ipAddress, physAddress);
78         id = getArpEntryId(ipAddress, physAddress);
79
80         ContextTestUtils.mockMapping(mappingContext, BD_NAME, BD_ID, BD_CTX_NAME);
81         ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
82     }
83
84     private static InstanceIdentifier<ArpTerminationTableEntry> getArpEntryId(final IpAddress ipAddress,
85                                                                               final PhysAddress physAddress) {
86         return InstanceIdentifier.create(BridgeDomains.class).child(BridgeDomain.class, new BridgeDomainKey(BD_NAME))
87             .child(ArpTerminationTable.class)
88             .child(ArpTerminationTableEntry.class, new ArpTerminationTableEntryKey(ipAddress, physAddress));
89     }
90
91     private void whenBdIpMacAddDelThenSuccess() {
92         final CompletableFuture<BdIpMacAddDelReply> replyFuture = new CompletableFuture<>();
93         final BdIpMacAddDelReply reply = new BdIpMacAddDelReply();
94         replyFuture.complete(reply);
95         doReturn(replyFuture).when(api).bdIpMacAddDel(any(BdIpMacAddDel.class));
96     }
97
98     private void whenBdIpMacAddDelThenFailure() {
99         doReturn(TestHelperUtils.<BdIpMacAddDelReply>createFutureException()).when(api)
100             .bdIpMacAddDel(any(BdIpMacAddDel.class));
101     }
102
103     private BdIpMacAddDel generateBdIpMacAddDelRequest(final byte[] ipAddress, final byte[] macAddress,
104                                                        final byte isAdd) {
105         final BdIpMacAddDel request = new BdIpMacAddDel();
106         request.ipAddress = ipAddress;
107         request.macAddress = macAddress;
108         request.bdId = BD_ID;
109         request.isAdd = isAdd;
110         return request;
111     }
112
113     private ArpTerminationTableEntry generateArpEntry(final IpAddress ipAddress, final PhysAddress physAddress) {
114         final ArpTerminationTableEntryBuilder entry = new ArpTerminationTableEntryBuilder();
115         entry.setKey(new ArpTerminationTableEntryKey(ipAddress, physAddress));
116         entry.setPhysAddress(physAddress);
117         entry.setIpAddress(ipAddress);
118         return entry.build();
119     }
120
121     private void verifyBdIpMacAddDelWasInvoked(final BdIpMacAddDel expected) throws
122         VppInvocationException {
123         ArgumentCaptor<BdIpMacAddDel> argumentCaptor = ArgumentCaptor.forClass(BdIpMacAddDel.class);
124         verify(api).bdIpMacAddDel(argumentCaptor.capture());
125         final BdIpMacAddDel actual = argumentCaptor.getValue();
126         assertArrayEquals(expected.macAddress, actual.macAddress);
127         assertArrayEquals(expected.ipAddress, actual.ipAddress);
128         assertEquals(expected.bdId, actual.bdId);
129         assertEquals(expected.isAdd, actual.isAdd);
130     }
131
132     @Test
133     public void testCreate() throws Exception {
134         whenBdIpMacAddDelThenSuccess();
135         customizer.writeCurrentAttributes(id, entry, writeContext);
136         verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 1));
137     }
138
139     @Test
140     public void testCreateFailed() throws Exception {
141         whenBdIpMacAddDelThenFailure();
142         try {
143             customizer.writeCurrentAttributes(id, entry, writeContext);
144         } catch (WriteFailedException.CreateFailedException e) {
145             assertTrue(e.getCause() instanceof VppBaseCallException);
146             verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 1));
147             return;
148         }
149         fail("WriteFailedException.CreateFailedException was expected");
150     }
151
152     @Test(expected = UnsupportedOperationException.class)
153     public void testUpdate() throws Exception {
154         customizer.updateCurrentAttributes(InstanceIdentifier.create(ArpTerminationTableEntry.class),
155             mock(ArpTerminationTableEntry.class),
156             mock(ArpTerminationTableEntry.class), writeContext);
157     }
158
159     @Test
160     public void testDelete() throws Exception {
161         whenBdIpMacAddDelThenSuccess();
162         customizer.deleteCurrentAttributes(id, entry, writeContext);
163         verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 0));
164     }
165
166     @Test
167     public void testDeleteFailed() throws Exception {
168         whenBdIpMacAddDelThenFailure();
169         try {
170             customizer.deleteCurrentAttributes(id, entry, writeContext);
171         } catch (WriteFailedException.DeleteFailedException e) {
172             assertTrue(e.getCause() instanceof VppBaseCallException);
173             verifyBdIpMacAddDelWasInvoked(generateBdIpMacAddDelRequest(ipAddressRaw, physAddressRaw, (byte) 0));
174             return;
175         }
176         fail("WriteFailedException.DeleteFailedException was expected");
177     }
178 }