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