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