summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/write/SubInterfaceCustomizerTest.java
blob: 923db9b88203d009862b8d3245d3fc744f75a405 (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
/*
 * 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.hc2vpp.v3po.write;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
import io.fd.hc2vpp.common.translate.util.NamingContext;
import io.fd.honeycomb.translate.write.WriteFailedException;
import io.fd.jvpp.VppBaseCallException;
import io.fd.jvpp.core.dto.CreateSubif;
import io.fd.jvpp.core.dto.CreateSubifReply;
import io.fd.jvpp.core.dto.SwInterfaceSetFlags;
import io.fd.jvpp.core.dto.SwInterfaceSetFlagsReply;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.SubinterfaceAugmentation;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319._802dot1ad;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.SubInterfaces;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterface;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceBuilder;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceKey;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.match.attributes.match.type.vlan.tagged.VlanTaggedBuilder;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.base.attributes.Match;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.base.attributes.MatchBuilder;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.base.attributes.TagsBuilder;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.base.attributes.tags.Tag;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.base.attributes.tags.TagBuilder;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.base.attributes.tags.TagKey;
import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qTagVlanType;
import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTagBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

public class SubInterfaceCustomizerTest extends WriterCustomizerTest {

    private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
    private static final String SUPER_IF_NAME = "local0";
    private static final int SUPER_IF_ID = 1;
    private static final String SUB_IFACE_NAME = "local0.11";
    private static final int SUBIF_INDEX = 11;
    private static final short STAG_ID = 100;
    private static final short CTAG_ID = 200;
    private static final short CTAG_ANY_ID = 0; // only the *IdAny flag is set
    private final Tag STAG_100;
    private final Tag CTAG_200;
    private final Tag CTAG_ANY;
    private NamingContext namingContext;
    private SubInterfaceCustomizer customizer;

    public SubInterfaceCustomizerTest() {
        STAG_100 = generateTag((short) 0, SVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId((int) STAG_ID)));
        CTAG_200 = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId(200)));
        CTAG_ANY = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(Dot1qTag.VlanId.Enumeration.Any));
    }

    private static Tag generateTag(final short index, final Class<? extends Dot1qTagVlanType> tagType,
                                   final Dot1qTag.VlanId vlanId) {
        TagBuilder tag = new TagBuilder();
        tag.setIndex(index);
        tag.withKey(new TagKey(index));
        final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
        dtag.setTagType(tagType);
        dtag.setVlanId(vlanId);
        tag.setDot1qTag(dtag.build());
        return tag.build();
    }

    static Match generateMatch() {
        final MatchBuilder match = new MatchBuilder();
        final VlanTaggedBuilder tagged = new VlanTaggedBuilder();
        tagged.setMatchExactTags(true);
        match.setMatchType(
                new org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.match.attributes.match.type.VlanTaggedBuilder()
                        .setVlanTagged(tagged.build()).build());
        return match.build();
    }

    @Override
    public void setUpTest() throws Exception {
        namingContext = new NamingContext("generatedSubInterfaceName", IFC_TEST_INSTANCE);
        customizer = new SubInterfaceCustomizer(api, namingContext);
        defineMapping(mappingContext, SUB_IFACE_NAME, SUBIF_INDEX, IFC_TEST_INSTANCE);
        defineMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_ID, IFC_TEST_INSTANCE);
    }

    private SubInterface generateSubInterface(final boolean enabled, final List<Tag> tagList) {
        SubInterfaceBuilder builder = new SubInterfaceBuilder();
        builder.setVlanType(_802dot1ad.class);
        builder.setIdentifier(11L);
        final TagsBuilder tags = new TagsBuilder();

        tags.setTag(tagList);

        builder.setTags(tags.build());

        builder.setMatch(generateMatch());
        builder.setEnabled(enabled);
        return builder.build();
    }

    private CreateSubif generateSubInterfaceRequest(final int superIfId, final short innerVlanId,
                                                    final boolean isInnerAny) {
        CreateSubif request = new CreateSubif();
        request.subId = 11;
        request.swIfIndex = superIfId;
        request.twoTags = 1;
        request.innerVlanId = innerVlanId;
        request.innerVlanIdAny = (byte) (isInnerAny
                ? 1
                : 0);
        request.dot1Ad = 1;
        request.outerVlanId = STAG_ID;
        request.exactMatch = 1;
        return request;
    }

    private SwInterfaceSetFlags generateSwInterfaceEnableRequest(final int swIfIndex) {
        SwInterfaceSetFlags request = new SwInterfaceSetFlags();
        request.swIfIndex = swIfIndex;
        request.adminUpDown = 1;
        return request;
    }

    private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long index) {
        return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
                SubinterfaceAugmentation.class).child(SubInterfaces.class)
                .child(SubInterface.class, new SubInterfaceKey(index));
    }

    private void whenCreateSubifThenSuccess() {
        doReturn(future(new CreateSubifReply())).when(api).createSubif(any(CreateSubif.class));
    }

    /**
     * Failure response send
     */
    private void whenCreateSubifThenFailure() {
        doReturn(failedFuture()).when(api).createSubif(any(CreateSubif.class));
    }

    private void whenSwInterfaceSetFlagsThenSuccess() {
        doReturn(future(new SwInterfaceSetFlagsReply())).when(api).swInterfaceSetFlags(any(SwInterfaceSetFlags.class));
    }

    private SwInterfaceSetFlags verifySwInterfaceSetFlagsWasInvoked(final SwInterfaceSetFlags expected)
            throws VppBaseCallException {
        ArgumentCaptor<SwInterfaceSetFlags> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlags.class);
        verify(api).swInterfaceSetFlags(argumentCaptor.capture());
        final SwInterfaceSetFlags actual = argumentCaptor.getValue();

        assertEquals(expected.swIfIndex, actual.swIfIndex);
        assertEquals(expected.adminUpDown, actual.adminUpDown);
        return actual;
    }

    @Test
    public void testCreateTwoTags() throws Exception {
        final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
        final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);

        whenCreateSubifThenSuccess();
        whenSwInterfaceSetFlagsThenSuccess();

        customizer.writeCurrentAttributes(id, subInterface, writeContext);

        verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
        verify(mappingContext)
                .put(eq(mappingIid(SUB_IFACE_NAME, IFC_TEST_INSTANCE)), eq(
                        mapping(SUB_IFACE_NAME, 0).get()));
    }

    @Test
    public void testCreateDot1qAnyTag() throws Exception {
        final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_ANY));
        final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);

        whenCreateSubifThenSuccess();
        whenSwInterfaceSetFlagsThenSuccess();

        customizer.writeCurrentAttributes(id, subInterface, writeContext);

        verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ANY_ID, true));
        verify(mappingContext)
                .put(eq(mappingIid(SUB_IFACE_NAME, IFC_TEST_INSTANCE)), eq(
                        mapping(SUB_IFACE_NAME, 0).get()));
    }

    @Test
    public void testCreateFailed() throws Exception {
        final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
        final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);

        whenCreateSubifThenFailure();

        try {
            customizer.writeCurrentAttributes(id, subInterface, writeContext);
        } catch (WriteFailedException.CreateFailedException e) {
            assertTrue(e.getCause() instanceof VppBaseCallException);
            verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
            verify(mappingContext, times(0)).put(
                    eq(mappingIid(SUPER_IF_NAME, IFC_TEST_INSTANCE)),
                    eq(mapping(SUPER_IF_NAME, 0).get()));
            return;
        }
        fail("WriteFailedException.CreateFailedException was expected");
    }

    @Test
    public void testUpdate() throws Exception {
        final List<Tag> tags = Arrays.asList(STAG_100, CTAG_200);
        final SubInterface before = generateSubInterface(false, tags);
        final SubInterface after = generateSubInterface(true, tags);
        final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);

        whenSwInterfaceSetFlagsThenSuccess();
        customizer.updateCurrentAttributes(id, before, after, writeContext);

        verifySwInterfaceSetFlagsWasInvoked(generateSwInterfaceEnableRequest(SUBIF_INDEX));
    }

    @Test(expected = UnsupportedOperationException.class)
    public void testDelete() throws Exception {
        final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
        customizer.deleteCurrentAttributes(null, subInterface, writeContext);
    }
}