summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/vpp/VppTest.java
blob: 0ff3ba16da61f83aeacc0b333b69f47ad08e96ef (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
/*
 * 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.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;

import com.google.common.collect.Lists;
import io.fd.honeycomb.v3po.translate.impl.write.CompositeRootWriter;
import io.fd.honeycomb.v3po.translate.write.WriteContext;
import io.fd.honeycomb.v3po.translate.util.write.DelegatingWriterRegistry;
import io.fd.honeycomb.v3po.translate.write.Writer;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.Vpp;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomains;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomainsBuilder;
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.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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 VppTest {

    private vppApi api;
    private DelegatingWriterRegistry rootRegistry;
    private CompositeRootWriter<Vpp> vppWriter;
    private WriteContext ctx;

    final byte zero = (byte) 0;
    final byte flood = (byte) 1;
    final byte forward = (byte) 0;
    final byte learn = (byte) 1;
    final byte uuf = (byte) 0;
    final byte arpTerm = (byte) 0;
    final byte add = (byte) 1;

    @Before
    public void setUp() throws Exception {
        api = PowerMockito.mock(vppApi.class);
        ctx = mock(WriteContext.class);
        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.doReturn(1).when(api).getRetval(anyInt(), anyInt());
        vppWriter = VppUtils.getVppWriter(api);
        rootRegistry = new DelegatingWriterRegistry(
            Collections.<Writer<? extends DataObject>>singletonList(vppWriter));
    }

    @Test
    public void writeVpp() throws Exception {
        rootRegistry.update(
            InstanceIdentifier.create(Vpp.class),
            null,
            new VppBuilder().setBridgeDomains(getBridgeDomains("bdn1")).build(),
            ctx);

        verify(api).bridgeDomainAddDel(1, flood, forward, learn, uuf, arpTerm, add);

        vppWriter.update(InstanceIdentifier.create(Vpp.class),
            null,
            new VppBuilder().setBridgeDomains(getBridgeDomains("bdn1")).build(),
            ctx);

        verify(api, times(2)).bridgeDomainAddDel(1, flood, forward, learn, uuf, arpTerm, add);
    }

    @Test
    public void writeVppFromRoot() throws Exception {
        final Vpp vpp = new VppBuilder().setBridgeDomains(getBridgeDomains("bdn1")).build();

        rootRegistry.update(Collections.<InstanceIdentifier<?>, DataObject>emptyMap(),
            Collections.<InstanceIdentifier<?>, DataObject>singletonMap(InstanceIdentifier.create(Vpp.class),
                vpp), ctx);

        verify(api).bridgeDomainAddDel(1, flood, forward, learn, uuf, arpTerm, add);
    }

    private BridgeDomains getBridgeDomains(String... name) {
        final List<BridgeDomain> bdmns = Lists.newArrayList();
        for (String s : name) {
            bdmns.add(new BridgeDomainBuilder()
                .setName(s)
                .setArpTermination(false)
                .setFlood(true)
                .setForward(false)
                .setLearn(true)
                .build());
        }
        return new BridgeDomainsBuilder()
                .setBridgeDomain(bdmns)
                .build();
    }

    @Test
    public void deleteVpp() throws Exception {
        rootRegistry.update(
            InstanceIdentifier.create(Vpp.class),
            new VppBuilder().setBridgeDomains(getBridgeDomains("bdn1")).build(),
            null,
            ctx);

        final byte zero = (byte) 0;

        verify(api).bridgeDomainAddDel(1, zero, zero, zero, zero, zero, zero);
    }

    @Test
    public void updateVppNoActualChange() throws Exception {
        rootRegistry.update(
            InstanceIdentifier.create(Vpp.class),
            new VppBuilder().setBridgeDomains(getBridgeDomains("bdn1")).build(),
            new VppBuilder().setBridgeDomains(getBridgeDomains("bdn1")).build(),
            ctx);

        verifyZeroInteractions(api);
    }

    @Test
    public void writeUpdate() throws Exception {
        final BridgeDomains domainsBefore = getBridgeDomains("bdn1");
        final BridgeDomain bdn1Before = domainsBefore.getBridgeDomain().get(0);

        final BridgeDomain bdn1After = new BridgeDomainBuilder(bdn1Before).setFlood(!bdn1Before.isFlood()).build();
        final BridgeDomains domainsAfter = new BridgeDomainsBuilder()
            .setBridgeDomain(Collections.singletonList(bdn1After))
            .build();

        rootRegistry.update(
            InstanceIdentifier.create(Vpp.class),
            new VppBuilder().setBridgeDomains(domainsBefore).build(),
            new VppBuilder().setBridgeDomains(domainsAfter).build(),
            ctx);

        final int bdn1Id = 1;

        // bdn1 is created with negated flood value
        verify(api).bridgeDomainAddDel(bdn1Id, (byte) (flood ^ 1), forward, learn, uuf, arpTerm, add);
    }

    // TODO test unkeyed list
    // TODO test update of a child without dedicated writer
}