1fee0b3cf211b32a01bfcd7548b18e9c0e0a5b32
[hc2vpp.git] /
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.interfaces;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27
28 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
29 import io.fd.honeycomb.translate.v3po.util.NamingContext;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
32 import java.util.Arrays;
33 import java.util.List;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
37 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qTagVlanType;
38 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
39 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
40 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
41 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTagBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceAugmentation;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1ad;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.vlan.tagged.VlanTaggedBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Match;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.MatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.TagsBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.Tag;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagKey;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.openvpp.jvpp.VppBaseCallException;
60 import org.openvpp.jvpp.core.dto.CreateSubif;
61 import org.openvpp.jvpp.core.dto.CreateSubifReply;
62 import org.openvpp.jvpp.core.dto.SwInterfaceSetFlags;
63 import org.openvpp.jvpp.core.dto.SwInterfaceSetFlagsReply;
64
65 public class SubInterfaceCustomizerTest extends WriterCustomizerTest {
66
67     private NamingContext namingContext;
68     private SubInterfaceCustomizer customizer;
69
70     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
71     private static final String SUPER_IF_NAME = "local0";
72     private static final int SUPER_IF_ID = 1;
73     private static final String SUB_IFACE_NAME = "local0.11";
74     private static final int SUBIF_INDEX = 11;
75
76     private static final short STAG_ID = 100;
77     private static final short CTAG_ID = 200;
78     private static final short CTAG_ANY_ID = 0; // only the *IdAny flag is set
79
80     private final Tag STAG_100;
81     private final Tag CTAG_200;
82     private final Tag CTAG_ANY;
83
84     public SubInterfaceCustomizerTest() {
85         STAG_100 = generateTag((short) 0, SVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId((int) STAG_ID)));
86         CTAG_200 = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId(200)));
87         CTAG_ANY = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(Dot1qTag.VlanId.Enumeration.Any));
88     }
89
90     @Override
91     public void setUp() throws Exception {
92         namingContext = new NamingContext("generatedSubInterfaceName", IFC_TEST_INSTANCE);
93         customizer = new SubInterfaceCustomizer(api, namingContext);
94         ContextTestUtils.mockMapping(mappingContext, SUB_IFACE_NAME, SUBIF_INDEX, IFC_TEST_INSTANCE);
95         ContextTestUtils.mockMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_ID, IFC_TEST_INSTANCE);
96     }
97
98     private SubInterface generateSubInterface(final boolean enabled, final List<Tag> tagList) {
99         SubInterfaceBuilder builder = new SubInterfaceBuilder();
100         builder.setVlanType(_802dot1ad.class);
101         builder.setIdentifier(11L);
102         final TagsBuilder tags = new TagsBuilder();
103
104         tags.setTag(tagList);
105
106         builder.setTags(tags.build());
107
108         builder.setMatch(generateMatch());
109         builder.setEnabled(enabled);
110         return builder.build();
111     }
112
113     private static Tag generateTag(final short index, final Class<? extends Dot1qTagVlanType> tagType,
114                                    final Dot1qTag.VlanId vlanId) {
115         TagBuilder tag = new TagBuilder();
116         tag.setIndex(index);
117         tag.setKey(new TagKey(index));
118         final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
119         dtag.setTagType(tagType);
120         dtag.setVlanId(vlanId);
121         tag.setDot1qTag(dtag.build());
122         return tag.build();
123     }
124
125     private static Match generateMatch() {
126         final MatchBuilder match = new MatchBuilder();
127         final VlanTaggedBuilder tagged = new VlanTaggedBuilder();
128         tagged.setMatchExactTags(true);
129         match.setMatchType(
130             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.VlanTaggedBuilder()
131                 .setVlanTagged(tagged.build()).build());
132         return match.build();
133     }
134
135     private CreateSubif generateSubInterfaceRequest(final int superIfId, final short innerVlanId,
136                                                     final boolean isInnerAny) {
137         CreateSubif request = new CreateSubif();
138         request.subId = 11;
139         request.swIfIndex = superIfId;
140         request.twoTags = 1;
141         request.innerVlanId = innerVlanId;
142         request.innerVlanIdAny = (byte) (isInnerAny
143             ? 1
144             : 0);
145         request.dot1Ad = 1;
146         request.outerVlanId = STAG_ID;
147         return request;
148     }
149
150     private SwInterfaceSetFlags generateSwInterfaceEnableRequest(final int swIfIndex) {
151         SwInterfaceSetFlags request = new SwInterfaceSetFlags();
152         request.swIfIndex = swIfIndex;
153         request.adminUpDown = 1;
154         return request;
155     }
156
157     private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long index) {
158         return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
159             SubinterfaceAugmentation.class).child(SubInterfaces.class)
160             .child(SubInterface.class, new SubInterfaceKey(index));
161     }
162
163     private void whenCreateSubifThenSuccess() {
164         doReturn(future(new CreateSubifReply())).when(api).createSubif(any(CreateSubif.class));
165     }
166
167     /**
168      * Failure response send
169      */
170     private void whenCreateSubifThenFailure() {
171         doReturn(failedFuture()).when(api).createSubif(any(CreateSubif.class));
172     }
173
174     private void whenSwInterfaceSetFlagsThenSuccess() {
175         doReturn(future(new SwInterfaceSetFlagsReply())).when(api).swInterfaceSetFlags(any(SwInterfaceSetFlags.class));
176     }
177
178     private SwInterfaceSetFlags verifySwInterfaceSetFlagsWasInvoked(final SwInterfaceSetFlags expected)
179         throws VppBaseCallException {
180         ArgumentCaptor<SwInterfaceSetFlags> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlags.class);
181         verify(api).swInterfaceSetFlags(argumentCaptor.capture());
182         final SwInterfaceSetFlags actual = argumentCaptor.getValue();
183
184         assertEquals(expected.swIfIndex, actual.swIfIndex);
185         assertEquals(expected.adminUpDown, actual.adminUpDown);
186         return actual;
187     }
188
189     @Test
190     public void testCreateTwoTags() throws Exception {
191         final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
192         final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
193
194         whenCreateSubifThenSuccess();
195         whenSwInterfaceSetFlagsThenSuccess();
196
197         customizer.writeCurrentAttributes(id, subInterface, writeContext);
198
199         verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
200         verify(mappingContext)
201             .put(eq(ContextTestUtils.getMappingIid(SUB_IFACE_NAME, IFC_TEST_INSTANCE)), eq(
202                 ContextTestUtils.getMapping(SUB_IFACE_NAME, 0).get()));
203     }
204
205     @Test
206     public void testCreateDot1qAnyTag() throws Exception {
207         final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_ANY));
208         final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
209
210         whenCreateSubifThenSuccess();
211         whenSwInterfaceSetFlagsThenSuccess();
212
213         customizer.writeCurrentAttributes(id, subInterface, writeContext);
214
215         verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ANY_ID, true));
216         verify(mappingContext)
217             .put(eq(ContextTestUtils.getMappingIid(SUB_IFACE_NAME, IFC_TEST_INSTANCE)), eq(
218                 ContextTestUtils.getMapping(SUB_IFACE_NAME, 0).get()));
219     }
220
221     @Test
222     public void testCreateFailed() throws Exception {
223         final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
224         final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
225
226         whenCreateSubifThenFailure();
227
228         try {
229             customizer.writeCurrentAttributes(id, subInterface, writeContext);
230         } catch (WriteFailedException.CreateFailedException e) {
231             assertTrue(e.getCause() instanceof VppBaseCallException);
232             verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
233             verify(mappingContext, times(0)).put(
234                 eq(ContextTestUtils.getMappingIid(SUPER_IF_NAME, IFC_TEST_INSTANCE)),
235                 eq(ContextTestUtils.getMapping(SUPER_IF_NAME, 0).get()));
236             return;
237         }
238         fail("WriteFailedException.CreateFailedException was expected");
239     }
240
241     @Test
242     public void testUpdate() throws Exception {
243         final List<Tag> tags = Arrays.asList(STAG_100, CTAG_200);
244         final SubInterface before = generateSubInterface(false, tags);
245         final SubInterface after = generateSubInterface(true, tags);
246         final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
247
248         whenSwInterfaceSetFlagsThenSuccess();
249         customizer.updateCurrentAttributes(id, before, after, writeContext);
250
251         verifySwInterfaceSetFlagsWasInvoked(generateSwInterfaceEnableRequest(SUBIF_INDEX));
252     }
253
254     @Test(expected = UnsupportedOperationException.class)
255     public void testDelete() throws Exception {
256         final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
257         customizer.deleteCurrentAttributes(null, subInterface, writeContext);
258     }
259 }