2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.vpp;
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;
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.rev150105.vpp.BridgeDomains;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
42 public class BridgeDomainCustomizerTest extends WriterCustomizerTest implements ByteDataTranslator {
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;
49 public void setUp() throws Exception {
50 customizer = new BridgeDomainCustomizer(api, new NamingContext("generatedBDName", BD_CTX_NAME));
54 private static Boolean intToBoolean(final int value) {
64 private static KeyedInstanceIdentifier<BridgeDomain, BridgeDomainKey> bdIdentifierForName(
65 final String bdName) {
66 return InstanceIdentifier.create(BridgeDomains.class).child(BridgeDomain.class, new BridgeDomainKey(bdName));
69 private BridgeDomain generateBridgeDomain(final String bdName) {
70 final byte arpTerm = 0;
72 final byte forward = 0;
75 return generateBridgeDomain(bdName, arpTerm, flood, forward, learn, uuf);
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()
82 .setArpTermination(intToBoolean(arpTerm))
83 .setFlood(intToBoolean(flood))
84 .setForward(intToBoolean(forward))
85 .setLearn(intToBoolean(learn))
86 .setUnknownUnicastFlood(intToBoolean(uuf))
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;
100 verify(api).bridgeDomainAddDel(expected);
103 private void verifyBridgeDomainDeleteWasInvoked(final int bdId) throws VppInvocationException {
104 final BridgeDomainAddDel expected = new BridgeDomainAddDel();
105 expected.bdId = bdId;
106 verify(api).bridgeDomainAddDel(expected);
109 private void whenBridgeDomainAddDelThenSuccess() {
110 when(api.bridgeDomainAddDel(any(BridgeDomainAddDel.class))).thenReturn(future(new BridgeDomainAddDelReply()));
113 private void whenBridgeDomainAddDelThenFailure() {
114 doReturn(failedFuture()).when(api).bridgeDomainAddDel(any(BridgeDomainAddDel.class));
118 public void testAddBridgeDomain() throws Exception {
120 final String bdName = "bd1";
121 final BridgeDomain bd = generateBridgeDomain(bdName);
122 noMappingDefined(mappingContext, bdName, BD_CTX_NAME);
124 whenBridgeDomainAddDelThenSuccess();
126 customizer.writeCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
128 verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId);
129 verify(mappingContext).put(mappingIid(bdName, BD_CTX_NAME), mapping(bdName, bdId).get());
133 public void testAddBridgeDomainPresentInBdContext() throws Exception {
135 final String bdName = "bd1";
136 final BridgeDomain bd = generateBridgeDomain(bdName);
137 defineMapping(mappingContext, bdName, bdId, BD_CTX_NAME);
139 whenBridgeDomainAddDelThenSuccess();
141 customizer.writeCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
143 verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId);
144 verify(mappingContext).put(mappingIid(bdName, BD_CTX_NAME), mapping(bdName, bdId).get());
148 public void testAddBridgeDomainFailed() throws Exception {
150 final String bdName = "bd1";
151 final BridgeDomain bd = generateBridgeDomain(bdName);
152 noMappingDefined(mappingContext, bdName, BD_CTX_NAME);
154 whenBridgeDomainAddDelThenFailure();
157 customizer.writeCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
158 } catch (WriteFailedException e) {
159 verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId);
162 fail("WriteFailedException.CreateFailedException was expected");
166 public void testDeleteBridgeDomain() throws Exception {
168 final String bdName = "bd1";
169 final BridgeDomain bd = generateBridgeDomain(bdName);
170 defineMapping(mappingContext, bdName, bdId, BD_CTX_NAME);
172 whenBridgeDomainAddDelThenSuccess();
174 customizer.deleteCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
176 verifyBridgeDomainDeleteWasInvoked(bdId);
180 public void testDeleteUnknownBridgeDomain() throws Exception {
181 final String bdName = "bd1";
182 final BridgeDomain bd = generateBridgeDomain("bd1");
183 noMappingDefined(mappingContext, bdName, BD_CTX_NAME);
186 customizer.deleteCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
187 } catch (IllegalArgumentException e) {
188 verify(api, never()).bridgeDomainAddDel(any(BridgeDomainAddDel.class));
191 fail("IllegalArgumentException was expected");
195 public void testDeleteBridgeDomainFailed() throws Exception {
197 final String bdName = "bd1";
198 final BridgeDomain bd = generateBridgeDomain(bdName);
199 defineMapping(mappingContext, bdName, bdId, BD_CTX_NAME);
201 whenBridgeDomainAddDelThenFailure();
204 customizer.deleteCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext);
205 } catch (WriteFailedException e) {
206 verifyBridgeDomainDeleteWasInvoked(bdId);
210 fail("WriteFailedException.DeleteFailedException was expected");
214 public void testUpdateBridgeDomain() throws Exception {
216 final String bdName = "bd1";
217 defineMapping(mappingContext, bdName, bdId, BD_CTX_NAME);
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;
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,
231 whenBridgeDomainAddDelThenSuccess();
234 .updateCurrentAttributes(bdIdentifierForName(bdName), dataBefore, dataAfter,
236 verifyBridgeDomainAddOrUpdateWasInvoked(dataAfter, bdId);
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);
248 .updateCurrentAttributes(bdIdentifierForName(bdName), bdBefore, bdAfter,
250 } catch (IllegalArgumentException e) {
251 verify(api, never()).bridgeDomainAddDel(any(BridgeDomainAddDel.class));
254 fail("IllegalArgumentException was expected");
258 public void testUpdateBridgeDomainFailed() throws Exception {
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);
265 whenBridgeDomainAddDelThenFailure();
268 customizer.updateCurrentAttributes(bdIdentifierForName(bdName), bdBefore, bdAfter, writeContext);
269 } catch (WriteFailedException e) {
270 verifyBridgeDomainAddOrUpdateWasInvoked(bdAfter, bdId);
273 fail("IllegalStateException was expected");