1f15788f0482cedf209964990acdfc028749c1d1
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / vpp / L2FibEntryCustomizerTest.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.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26
27 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
28 import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
29 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
30 import io.fd.honeycomb.translate.v3po.util.NamingContext;
31 import io.fd.honeycomb.translate.write.WriteFailedException;
32 import java.util.concurrent.CompletableFuture;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.L2FibFilter;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.L2FibTable;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntry;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntryBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntryKey;
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.L2FibAddDel;
48 import org.openvpp.jvpp.core.dto.L2FibAddDelReply;
49 import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewriteReply;
50
51 public class L2FibEntryCustomizerTest extends WriterCustomizerTest {
52     private static final String BD_CTX_NAME = "bd-test-instance";
53     private static final String IFC_CTX_NAME = "ifc-test-instance";
54
55     private static final String BD_NAME = "testBD0";
56     private static final int BD_ID = 111;
57     private static final String IFACE_NAME = "eth0";
58     private static final int IFACE_ID = 123;
59
60     private L2FibEntryCustomizer customizer;
61
62     @Override
63     public void setUp() throws Exception {
64         ContextTestUtils.mockMapping(mappingContext, BD_NAME, BD_ID, BD_CTX_NAME);
65         ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
66
67         customizer = new L2FibEntryCustomizer(
68             api,
69             new NamingContext("generatedBdName", BD_CTX_NAME),
70             new NamingContext("generatedIfaceName", IFC_CTX_NAME));
71     }
72
73     private static InstanceIdentifier<L2FibEntry> getL2FibEntryId(final PhysAddress address) {
74         return InstanceIdentifier.create(BridgeDomains.class).child(BridgeDomain.class, new BridgeDomainKey(BD_NAME))
75             .child(L2FibTable.class).child(L2FibEntry.class, new L2FibEntryKey(address));
76     }
77
78     private void whenL2FibAddDelThenSuccess() {
79         final CompletableFuture<L2FibAddDelReply> replyFuture = new CompletableFuture<>();
80         final L2FibAddDelReply reply = new L2FibAddDelReply();
81         replyFuture.complete(reply);
82         doReturn(replyFuture).when(api).l2FibAddDel(any(L2FibAddDel.class));
83     }
84
85     private void whenL2FibAddDelThenFailure() {
86         doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
87             .l2FibAddDel(any(L2FibAddDel.class));
88     }
89
90     private L2FibAddDel generateL2FibAddDelRequest(final long mac, final byte isAdd) {
91         final L2FibAddDel request = new L2FibAddDel();
92         request.mac = mac;
93         request.bdId = BD_ID;
94         request.swIfIndex = IFACE_ID;
95         request.isAdd = isAdd;
96         if (isAdd == 1) {
97             request.staticMac = 1;
98             request.filterMac = 1;
99         }
100         return request;
101     }
102
103     private L2FibEntry generateL2FibEntry(final PhysAddress address) {
104         final L2FibEntryBuilder entry = new L2FibEntryBuilder();
105         entry.setKey(new L2FibEntryKey(address));
106         entry.setPhysAddress(address);
107         entry.setStaticConfig(true);
108         entry.setBridgedVirtualInterface(false);
109         entry.setAction(L2FibFilter.class);
110         entry.setOutgoingInterface(IFACE_NAME);
111         return entry.build();
112     }
113
114     private void verifyL2FibAddDelWasInvoked(final L2FibAddDel expected) throws
115         VppInvocationException {
116         ArgumentCaptor<L2FibAddDel> argumentCaptor = ArgumentCaptor.forClass(L2FibAddDel.class);
117         verify(api).l2FibAddDel(argumentCaptor.capture());
118         final L2FibAddDel actual = argumentCaptor.getValue();
119         assertEquals(expected.mac, actual.mac);
120         assertEquals(expected.bdId, actual.bdId);
121         assertEquals(expected.swIfIndex, actual.swIfIndex);
122         assertEquals(expected.isAdd, actual.isAdd);
123         assertEquals(expected.staticMac, actual.staticMac);
124         assertEquals(expected.filterMac, actual.filterMac);
125     }
126
127     @Test
128     public void testCreate() throws Exception {
129         final long address_vpp = 0x0102030405060000L;
130         final PhysAddress address = new PhysAddress("01:02:03:04:05:06");
131         final L2FibEntry entry = generateL2FibEntry(address);
132         final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
133
134         whenL2FibAddDelThenSuccess();
135
136         customizer.writeCurrentAttributes(id, entry, writeContext);
137
138         verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 1));
139     }
140
141     @Test
142     public void testCreateFailed() throws Exception {
143         final long address_vpp = 0x1122334455660000L;
144         final PhysAddress address = new PhysAddress("11:22:33:44:55:66");
145         final L2FibEntry entry = generateL2FibEntry(address);
146         final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
147
148         whenL2FibAddDelThenFailure();
149
150         try {
151             customizer.writeCurrentAttributes(id, entry, writeContext);
152         } catch (WriteFailedException.CreateFailedException e) {
153             assertTrue(e.getCause() instanceof VppBaseCallException);
154             verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 1));
155             return;
156         }
157         fail("WriteFailedException.CreateFailedException was expected");
158     }
159
160     @Test(expected = UnsupportedOperationException.class)
161     public void testUpdate() throws Exception {
162         customizer.updateCurrentAttributes(InstanceIdentifier.create(L2FibEntry.class), mock(L2FibEntry.class),
163             mock(L2FibEntry.class), writeContext);
164     }
165
166     @Test
167     public void testDelete() throws Exception {
168         final long address_vpp = 0x1122334455660000L;
169         final PhysAddress address = new PhysAddress("11:22:33:44:55:66");
170         final L2FibEntry entry = generateL2FibEntry(address);
171         final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
172
173         whenL2FibAddDelThenSuccess();
174
175         customizer.deleteCurrentAttributes(id, entry, writeContext);
176
177         verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 0));
178     }
179
180     @Test
181     public void testDeleteFailed() throws Exception {
182         final long address_vpp = 0x0102030405060000L;
183         final PhysAddress address = new PhysAddress("01:02:03:04:05:06");
184         final L2FibEntry entry = generateL2FibEntry(address);
185         final InstanceIdentifier<L2FibEntry> id = getL2FibEntryId(address);
186
187         whenL2FibAddDelThenFailure();
188
189         try {
190             customizer.deleteCurrentAttributes(id, entry, writeContext);
191         } catch (WriteFailedException.DeleteFailedException e) {
192             assertTrue(e.getCause() instanceof VppBaseCallException);
193             verifyL2FibAddDelWasInvoked(generateL2FibAddDelRequest(address_vpp, (byte) 0));
194             return;
195         }
196         fail("WriteFailedException.DeleteFailedException was expected");
197     }
198 }