summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vpp/BridgeDomainCustomizerTest.java
blob: f504918a8d3b8d8670ec0b77a37e7dfe9f4f470b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.v3po.translate.v3po.vpp;

import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.initMocks;

import io.fd.honeycomb.v3po.translate.Context;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainKey;
import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
import org.openvpp.vppjapi.vppApi;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("org.openvpp.vppjapi.vppConn")
@PrepareForTest(vppApi.class)
public class BridgeDomainCustomizerTest {

    private static final int RESPONSE_NOT_READY = -77;
    private static final byte ADD_OR_UPDATE_BD = (byte) 1;
    private static final byte ZERO = 0;
    private vppApi api;

    @Mock
    private Context ctx;

    private BridgeDomainCustomizer customizer;

    @Before
    public void setUp() throws Exception {
        // TODO create base class for tests using vppApi
        api = PowerMockito.mock(vppApi.class);
        initMocks(this);
        customizer = new BridgeDomainCustomizer(api);

        PowerMockito.doAnswer(BridgeDomainTestUtils.BD_NAME_TO_ID_ANSWER).when(api).findOrAddBridgeDomainId(anyString());
        PowerMockito.doAnswer(BridgeDomainTestUtils.BD_NAME_TO_ID_ANSWER).when(api).bridgeDomainIdFromName(anyString());
        PowerMockito.when(api.getRetval(anyInt(), anyInt())).thenReturn(RESPONSE_NOT_READY).thenReturn(0);
        PowerMockito.doReturn(0).when(api).getRetval(anyInt(), anyInt());
    }

    private BridgeDomain generateBridgeDomain(final String bdName) {
        final byte arpTerm = 0;
        final byte flood = 1;
        final byte forward = 0;
        final byte learn = 1;
        final byte uuf = 0;
        return generateBridgeDomain(bdName, arpTerm, flood, forward, learn, uuf);
    }

    private BridgeDomain generateBridgeDomain(final String bdName, final int arpTerm, final int flood,
                                              final int forward, final int learn, final int uuf) {
        return new BridgeDomainBuilder()
                .setName(bdName)
                .setArpTermination(BridgeDomainTestUtils.intToBoolean(arpTerm))
                .setFlood(BridgeDomainTestUtils.intToBoolean(flood))
                .setForward(BridgeDomainTestUtils.intToBoolean(forward))
                .setLearn(BridgeDomainTestUtils.intToBoolean(learn))
                .setUnknownUnicastFlood(BridgeDomainTestUtils.intToBoolean(uuf))
                .build();
    }

    private final int verifyBridgeDomainAddOrUpdateWasInvoked(final BridgeDomain bd) {
        final int bdn1Id = BridgeDomainTestUtils.bdNameToID(bd.getName());
        final byte arpTerm = BridgeDomainTestUtils.booleanToByte(bd.isArpTermination());
        final byte flood = BridgeDomainTestUtils.booleanToByte(bd.isFlood());
        final byte forward = BridgeDomainTestUtils.booleanToByte(bd.isForward());
        final byte learn = BridgeDomainTestUtils.booleanToByte(bd.isLearn());
        final byte uuf = BridgeDomainTestUtils.booleanToByte(bd.isUnknownUnicastFlood());
        return verify(api).bridgeDomainAddDel(bdn1Id, flood, forward, learn, uuf, arpTerm, ADD_OR_UPDATE_BD);
    }

    private int verifyBridgeDomainAddOrUpdateWasNotInvoked(final BridgeDomain bd) {
        final int bdn1Id = BridgeDomainTestUtils.bdNameToID(bd.getName());
        final byte arpTerm = BridgeDomainTestUtils.booleanToByte(bd.isArpTermination());
        final byte flood = BridgeDomainTestUtils.booleanToByte(bd.isFlood());
        final byte forward = BridgeDomainTestUtils.booleanToByte(bd.isForward());
        final byte learn = BridgeDomainTestUtils.booleanToByte(bd.isLearn());
        final byte uuf = BridgeDomainTestUtils.booleanToByte(bd.isUnknownUnicastFlood());
        return verify(api, never()).bridgeDomainAddDel(bdn1Id, flood, forward, learn, uuf, arpTerm, ADD_OR_UPDATE_BD);
    }

    private int verifyBridgeDomainDeletedWasInvoked(final BridgeDomain bd) {
        final int bdn1Id = BridgeDomainTestUtils.bdNameToID(bd.getName());
        return verify(api).bridgeDomainAddDel(bdn1Id, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO);
    }

    private int verifyBridgeDomainDeletedWasNotInvoked(final BridgeDomain bd) {
        final int bdn1Id = BridgeDomainTestUtils.bdNameToID(bd.getName());
        return verify(api, never()).bridgeDomainAddDel(bdn1Id, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO);
    }

    @Test
    public void testAddBridgeDomain() {
        final String bdName = "bd1";
        final BridgeDomain bd = generateBridgeDomain("bd1");

        customizer.writeCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);

        verifyBridgeDomainAddOrUpdateWasInvoked(bd);
    }

    @Test
    public void testBridgeDomainNameCreateFailed() {
        final String bdName = "bd1";
        final BridgeDomain bd = generateBridgeDomain("bd1");

        // make vpp api fail to create id for our bd name
        PowerMockito.doReturn(-1).when(api).findOrAddBridgeDomainId(bdName);

        try {
            customizer.writeCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
        } catch (IllegalStateException e) {
            verifyBridgeDomainAddOrUpdateWasNotInvoked(bd);
            return;
        }
        fail("IllegalStateException was expected");
    }

    @Test
    public void testAddBridgeDomainFailed() {
        // make any call to vpp fail
        PowerMockito.doReturn(-1).when(api).getRetval(anyInt(), anyInt());

        final String bdName = "bd1";
        final BridgeDomain bd = generateBridgeDomain(bdName);

        try {
            customizer.writeCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
        } catch (IllegalStateException e) {
            verifyBridgeDomainAddOrUpdateWasInvoked(bd);
            return;
        }
        fail("IllegalStateException was expected");
    }

    @Test
    public void testDeleteBridgeDomain() {
        final String bdName = "bd1";
        final BridgeDomain bd = generateBridgeDomain("bd1");

        customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);

        verifyBridgeDomainDeletedWasInvoked(bd);
    }

    @Test
    public void testDeleteUnknownBridgeDomain() {
        final String bdName = "bd1";
        final BridgeDomain bd = generateBridgeDomain("bd1");

        // make vpp api not find our bd
        PowerMockito.doReturn(-1).when(api).bridgeDomainIdFromName(bdName);

        try {
            customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
        } catch (IllegalStateException e) {
            verifyBridgeDomainDeletedWasNotInvoked(bd);
            return;
        }
        fail("IllegalStateException was expected");
    }

    @Test
    public void testDeleteBridgeDomainFailed() {
        // make any call to vpp fail
        PowerMockito.doReturn(-1).when(api).getRetval(anyInt(), anyInt());

        final String bdName = "bd1";
        final BridgeDomain bd = generateBridgeDomain(bdName);

        try {
            customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
        } catch (IllegalStateException e) {
            verifyBridgeDomainDeletedWasInvoked(bd);
            return;
        }
        fail("IllegalStateException was expected");
    }

    @Test
    public void testUpdateBridgeDomain() throws Exception {
        final String bdName = "bd1";
        final byte arpTermBefore = 1;
        final byte floodBefore = 1;
        final byte forwardBefore = 0;
        final byte learnBefore = 1;
        final byte uufBefore = 0;

        final BridgeDomain dataBefore =
                generateBridgeDomain(bdName, arpTermBefore, floodBefore, forwardBefore, learnBefore, uufBefore);
        final BridgeDomain dataAfter =
                generateBridgeDomain(bdName, arpTermBefore ^ 1, floodBefore ^ 1, forwardBefore ^ 1, learnBefore ^ 1,
                        uufBefore ^ 1);

        final KeyedInstanceIdentifier<BridgeDomain, BridgeDomainKey> id = BridgeDomainTestUtils.bdIdentifierForName(bdName);

        customizer.updateCurrentAttributes(id, dataBefore, dataAfter, ctx);

        verifyBridgeDomainAddOrUpdateWasInvoked(dataAfter);
    }

    @Test
    public void testUpdateUnknownBridgeDomain() throws Exception {
        final String bdName = "bd1";
        final BridgeDomain bd = generateBridgeDomain("bd1");

        // make vpp api not find our bd
        PowerMockito.doReturn(-1).when(api).bridgeDomainIdFromName(bdName);

        try {
            customizer.updateCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, bd, ctx);
        } catch (IllegalStateException e) {
            verifyBridgeDomainAddOrUpdateWasNotInvoked(bd);
            return;
        }
        fail("IllegalStateException was expected");
    }

    @Test
    public void testUpdateBridgeDomainFailed() {
        // make any call to vpp fail
        PowerMockito.doReturn(-1).when(api).getRetval(anyInt(), anyInt());

        final String bdName = "bd1";
        final BridgeDomain bd = generateBridgeDomain(bdName);

        try {
            customizer.updateCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, bd, ctx);
        } catch (IllegalStateException e) {
            verifyBridgeDomainAddOrUpdateWasInvoked(bd);
            return;
        }
        fail("IllegalStateException was expected");
    }

}