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