HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / vpp / BridgeDomainCustomizerTest.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.fail;
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25
26 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
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.VppInvocationException;
31 import io.fd.vpp.jvpp.core.dto.BridgeDomainAddDel;
32 import io.fd.vpp.jvpp.core.dto.BridgeDomainAddDelReply;
33 import javax.annotation.Nullable;
34 import org.junit.Test;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.BridgeDomains;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.bridge.domains.BridgeDomain;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.bridge.domains.BridgeDomainBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.bridge.domains.BridgeDomainKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
41
42 public class BridgeDomainCustomizerTest extends WriterCustomizerTest implements ByteDataTranslator {
43
44     private static final String BD_CTX_NAME = "bd-test-instance";
45     private static final byte ADD_OR_UPDATE_BD = (byte) 1;
46     private BridgeDomainCustomizer customizer;
47
48     @Override
49     public void setUp() throws Exception {
50         customizer = new BridgeDomainCustomizer(api, new NamingContext("generatedBDName", BD_CTX_NAME));
51     }
52
53     @Nullable
54     private static Boolean intToBoolean(final int value) {
55         if (value == 0) {
56             return Boolean.FALSE;
57         }
58         if (value == 1) {
59             return Boolean.TRUE;
60         }
61         return null;
62     }
63
64     private static KeyedInstanceIdentifier<BridgeDomain, BridgeDomainKey> bdIdentifierForName(
65             final String bdName) {
66         return InstanceIdentifier.create(BridgeDomains.class).child(BridgeDomain.class, new BridgeDomainKey(bdName));
67     }
68
69     private BridgeDomain generateBridgeDomain(final String bdName) {
70         final byte arpTerm = 0;
71         final byte flood = 1;
72         final byte forward = 0;
73         final byte learn = 1;
74         final byte uuf = 0;
75         return generateBridgeDomain(bdName, arpTerm, flood, forward, learn, uuf);
76     }
77
78     private BridgeDomain generateBridgeDomain(final String bdName, final int arpTerm, final int flood,
79                                               final int forward, final int learn, final int uuf) {
80         return new BridgeDomainBuilder()
81                 .setName(bdName)
82                 .setArpTermination(intToBoolean(arpTerm))
83                 .setFlood(intToBoolean(flood))
84                 .setForward(intToBoolean(forward))
85                 .setLearn(intToBoolean(learn))
86                 .setUnknownUnicastFlood(intToBoolean(uuf))
87                 .build();
88     }
89
90     private void verifyBridgeDomainAddOrUpdateWasInvoked(final BridgeDomain bd, final int bdId)
91             throws VppInvocationException {
92         final BridgeDomainAddDel expected = new BridgeDomainAddDel();
93         expected.arpTerm = booleanToByte(bd.isArpTermination());
94         expected.flood = booleanToByte(bd.isFlood());
95         expected.forward = booleanToByte(bd.isForward());
96         expected.learn = booleanToByte(bd.isLearn());
97         expected.uuFlood = booleanToByte(bd.isUnknownUnicastFlood());
98         expected.isAdd = ADD_OR_UPDATE_BD;
99         expected.bdId = bdId;
100         verify(api).bridgeDomainAddDel(expected);
101     }
102
103     private void verifyBridgeDomainDeleteWasInvoked(final int bdId) throws VppInvocationException {
104         final BridgeDomainAddDel expected = new BridgeDomainAddDel();
105         expected.bdId = bdId;
106         verify(api).bridgeDomainAddDel(expected);
107     }
108
109     private void whenBridgeDomainAddDelThenSuccess() {
110         when(api.bridgeDomainAddDel(any(BridgeDomainAddDel.class))).thenReturn(future(new BridgeDomainAddDelReply()));
111     }
112
113     private void whenBridgeDomainAddDelThenFailure() {
114         doReturn(failedFuture()).when(api).bridgeDomainAddDel(any(BridgeDomainAddDel.class));
115     }
116
117     @Test
118     public void testAddBridgeDomain() throws Exception {
119         final int bdId = 1;
120         final String bdName = "bd1";
121         final BridgeDomain bd = generateBridgeDomain(bdName);
122         noMappingDefined(mappingContext, bdName, BD_CTX_NAME);
123
124         whenBridgeDomainAddDelThenSuccess();
125
126         customizer.writeCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
127
128         verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId);
129         verify(mappingContext).put(mappingIid(bdName, BD_CTX_NAME), mapping(bdName, bdId).get());
130     }
131
132     @Test
133     public void testAddBridgeDomainPresentInBdContext() throws Exception {
134         final int bdId = 1;
135         final String bdName = "bd1";
136         final BridgeDomain bd = generateBridgeDomain(bdName);
137         defineMapping(mappingContext, bdName, bdId, BD_CTX_NAME);
138
139         whenBridgeDomainAddDelThenSuccess();
140
141         customizer.writeCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
142
143         verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId);
144         verify(mappingContext).put(mappingIid(bdName, BD_CTX_NAME), mapping(bdName, bdId).get());
145     }
146
147     @Test
148     public void testAddBridgeDomainFailed() throws Exception {
149         final int bdId = 1;
150         final String bdName = "bd1";
151         final BridgeDomain bd = generateBridgeDomain(bdName);
152         noMappingDefined(mappingContext, bdName, BD_CTX_NAME);
153
154         whenBridgeDomainAddDelThenFailure();
155
156         try {
157             customizer.writeCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
158         } catch (WriteFailedException e) {
159             verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId);
160             return;
161         }
162         fail("WriteFailedException.CreateFailedException  was expected");
163     }
164
165     @Test
166     public void testDeleteBridgeDomain() throws Exception {
167         final int bdId = 1;
168         final String bdName = "bd1";
169         final BridgeDomain bd = generateBridgeDomain(bdName);
170         defineMapping(mappingContext, bdName, bdId, BD_CTX_NAME);
171
172         whenBridgeDomainAddDelThenSuccess();
173
174         customizer.deleteCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
175
176         verifyBridgeDomainDeleteWasInvoked(bdId);
177     }
178
179     @Test
180     public void testDeleteUnknownBridgeDomain() throws Exception {
181         final String bdName = "bd1";
182         final BridgeDomain bd = generateBridgeDomain("bd1");
183         noMappingDefined(mappingContext, bdName, BD_CTX_NAME);
184
185         try {
186             customizer.deleteCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
187         } catch (IllegalArgumentException e) {
188             verify(api, never()).bridgeDomainAddDel(any(BridgeDomainAddDel.class));
189             return;
190         }
191         fail("IllegalArgumentException was expected");
192     }
193
194     @Test
195     public void testDeleteBridgeDomainFailed() throws Exception {
196         final int bdId = 1;
197         final String bdName = "bd1";
198         final BridgeDomain bd = generateBridgeDomain(bdName);
199         defineMapping(mappingContext, bdName, bdId, BD_CTX_NAME);
200
201         whenBridgeDomainAddDelThenFailure();
202
203         try {
204             customizer.deleteCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
205         } catch (WriteFailedException e) {
206             verifyBridgeDomainDeleteWasInvoked(bdId);
207             return;
208         }
209
210         fail("WriteFailedException.DeleteFailedException was expected");
211     }
212
213     @Test
214     public void testUpdateBridgeDomain() throws Exception {
215         final int bdId = 1;
216         final String bdName = "bd1";
217         defineMapping(mappingContext, bdName, bdId, BD_CTX_NAME);
218
219         final byte arpTermBefore = 1;
220         final byte floodBefore = 1;
221         final byte forwardBefore = 0;
222         final byte learnBefore = 1;
223         final byte uufBefore = 0;
224
225         final BridgeDomain dataBefore =
226                 generateBridgeDomain(bdName, arpTermBefore, floodBefore, forwardBefore, learnBefore, uufBefore);
227         final BridgeDomain dataAfter =
228                 generateBridgeDomain(bdName, arpTermBefore ^ 1, floodBefore ^ 1, forwardBefore ^ 1, learnBefore ^ 1,
229                         uufBefore ^ 1);
230
231         whenBridgeDomainAddDelThenSuccess();
232
233         customizer
234                 .updateCurrentAttributes(bdIdentifierForName(bdName), dataBefore, dataAfter,
235                         writeContext);
236         verifyBridgeDomainAddOrUpdateWasInvoked(dataAfter, bdId);
237     }
238
239     @Test
240     public void testUpdateUnknownBridgeDomain() throws Exception {
241         final String bdName = "bd1";
242         final BridgeDomain bdBefore = generateBridgeDomain(bdName, 0, 1, 0, 1, 0);
243         final BridgeDomain bdAfter = generateBridgeDomain(bdName, 1, 1, 0, 1, 0);
244         noMappingDefined(mappingContext, bdName, BD_CTX_NAME);
245
246         try {
247             customizer
248                     .updateCurrentAttributes(bdIdentifierForName(bdName), bdBefore, bdAfter,
249                             writeContext);
250         } catch (IllegalArgumentException e) {
251             verify(api, never()).bridgeDomainAddDel(any(BridgeDomainAddDel.class));
252             return;
253         }
254         fail("IllegalArgumentException was expected");
255     }
256
257     @Test
258     public void testUpdateBridgeDomainFailed() throws Exception {
259         final int bdId = 1;
260         final String bdName = "bd1";
261         final BridgeDomain bdBefore = generateBridgeDomain(bdName, 0, 1, 0, 1, 0);
262         final BridgeDomain bdAfter = generateBridgeDomain(bdName, 1, 1, 0, 1, 0);
263         defineMapping(mappingContext, bdName, bdId, BD_CTX_NAME);
264
265         whenBridgeDomainAddDelThenFailure();
266
267         try {
268             customizer.updateCurrentAttributes(bdIdentifierForName(bdName), bdBefore, bdAfter, writeContext);
269         } catch (WriteFailedException e) {
270             verifyBridgeDomainAddOrUpdateWasInvoked(bdAfter, bdId);
271             return;
272         }
273         fail("IllegalStateException was expected");
274     }
275
276 }