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.interfaces;
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;
28 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
29 import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
30 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
31 import io.fd.honeycomb.translate.v3po.util.NamingContext;
32 import io.fd.honeycomb.translate.write.WriteFailedException;
33 import java.util.Arrays;
34 import java.util.List;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.ExecutionException;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
40 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qTagVlanType;
41 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
42 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
43 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
44 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTagBuilder;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceAugmentation;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1ad;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.vlan.tagged.VlanTaggedBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Match;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.MatchBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.TagsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.Tag;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagKey;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.openvpp.jvpp.VppBaseCallException;
63 import org.openvpp.jvpp.core.dto.CreateSubif;
64 import org.openvpp.jvpp.core.dto.CreateSubifReply;
65 import org.openvpp.jvpp.core.dto.SwInterfaceSetFlags;
66 import org.openvpp.jvpp.core.dto.SwInterfaceSetFlagsReply;
68 public class SubInterfaceCustomizerTest extends WriterCustomizerTest {
70 private NamingContext namingContext;
71 private SubInterfaceCustomizer customizer;
73 private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
74 private static final String SUPER_IF_NAME = "local0";
75 private static final int SUPER_IF_ID = 1;
76 private static final String SUB_IFACE_NAME = "local0.11";
77 private static final int SUBIF_INDEX = 11;
79 private static final short STAG_ID = 100;
80 private static final short CTAG_ID = 200;
81 private static final short CTAG_ANY_ID = 0; // only the *IdAny flag is set
83 private final Tag STAG_100;
84 private final Tag CTAG_200;
85 private final Tag CTAG_ANY;
87 public SubInterfaceCustomizerTest() {
88 STAG_100 = generateTag((short) 0, SVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId((int) STAG_ID)));
89 CTAG_200 = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId(200)));
90 CTAG_ANY = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(Dot1qTag.VlanId.Enumeration.Any));
94 public void setUp() throws Exception {
95 namingContext = new NamingContext("generatedSubInterfaceName", IFC_TEST_INSTANCE);
96 customizer = new SubInterfaceCustomizer(api, namingContext);
97 ContextTestUtils.mockMapping(mappingContext, SUB_IFACE_NAME, SUBIF_INDEX, IFC_TEST_INSTANCE);
98 ContextTestUtils.mockMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_ID, IFC_TEST_INSTANCE);
101 private SubInterface generateSubInterface(final boolean enabled, final List<Tag> tagList) {
102 SubInterfaceBuilder builder = new SubInterfaceBuilder();
103 builder.setVlanType(_802dot1ad.class);
104 builder.setIdentifier(11L);
105 final TagsBuilder tags = new TagsBuilder();
107 tags.setTag(tagList);
109 builder.setTags(tags.build());
111 builder.setMatch(generateMatch());
112 builder.setEnabled(enabled);
113 return builder.build();
116 private static Tag generateTag(final short index, final Class<? extends Dot1qTagVlanType> tagType,
117 final Dot1qTag.VlanId vlanId) {
118 TagBuilder tag = new TagBuilder();
120 tag.setKey(new TagKey(index));
121 final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
122 dtag.setTagType(tagType);
123 dtag.setVlanId(vlanId);
124 tag.setDot1qTag(dtag.build());
128 private static Match generateMatch() {
129 final MatchBuilder match = new MatchBuilder();
130 final VlanTaggedBuilder tagged = new VlanTaggedBuilder();
131 tagged.setMatchExactTags(true);
133 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.VlanTaggedBuilder()
134 .setVlanTagged(tagged.build()).build());
135 return match.build();
138 private CreateSubif generateSubInterfaceRequest(final int superIfId, final short innerVlanId,
139 final boolean isInnerAny) {
140 CreateSubif request = new CreateSubif();
142 request.swIfIndex = superIfId;
144 request.innerVlanId = innerVlanId;
145 request.innerVlanIdAny = (byte) (isInnerAny
149 request.outerVlanId = STAG_ID;
153 private SwInterfaceSetFlags generateSwInterfaceEnableRequest(final int swIfIndex) {
154 SwInterfaceSetFlags request = new SwInterfaceSetFlags();
155 request.swIfIndex = swIfIndex;
156 request.adminUpDown = 1;
160 private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long index) {
161 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
162 SubinterfaceAugmentation.class).child(SubInterfaces.class)
163 .child(SubInterface.class, new SubInterfaceKey(index));
166 private void whenCreateSubifThenSuccess() throws ExecutionException, InterruptedException, VppBaseCallException {
167 final CompletableFuture<CreateSubifReply> replyFuture = new CompletableFuture<>();
168 final CreateSubifReply reply = new CreateSubifReply();
169 replyFuture.complete(reply);
170 doReturn(replyFuture).when(api).createSubif(any(CreateSubif.class));
174 * Failure response send
176 private void whenCreateSubifThenFailure() throws ExecutionException, InterruptedException, VppBaseCallException {
177 doReturn(TestHelperUtils.<CreateSubifReply>createFutureException()).when(api)
178 .createSubif(any(CreateSubif.class));
181 private void whenSwInterfaceSetFlagsThenSuccess()
182 throws ExecutionException, InterruptedException, VppBaseCallException {
183 final CompletableFuture<SwInterfaceSetFlagsReply> replyFuture = new CompletableFuture<>();
184 final SwInterfaceSetFlagsReply reply = new SwInterfaceSetFlagsReply();
185 replyFuture.complete(reply);
186 doReturn(replyFuture).when(api).swInterfaceSetFlags(any(SwInterfaceSetFlags.class));
189 private CreateSubif verifyCreateSubifWasInvoked(final CreateSubif expected) throws VppBaseCallException {
190 ArgumentCaptor<CreateSubif> argumentCaptor = ArgumentCaptor.forClass(CreateSubif.class);
191 verify(api).createSubif(argumentCaptor.capture());
192 final CreateSubif actual = argumentCaptor.getValue();
194 assertEquals(expected.swIfIndex, actual.swIfIndex);
195 assertEquals(expected.subId, actual.subId);
196 assertEquals(expected.noTags, actual.noTags);
197 assertEquals(expected.oneTag, actual.oneTag);
198 assertEquals(expected.twoTags, actual.twoTags);
199 assertEquals(expected.dot1Ad, actual.dot1Ad);
200 assertEquals(expected.exactMatch, actual.exactMatch);
201 assertEquals(expected.defaultSub, actual.defaultSub);
202 assertEquals(expected.outerVlanIdAny, actual.outerVlanIdAny);
203 assertEquals(expected.innerVlanIdAny, actual.innerVlanIdAny);
204 assertEquals(expected.outerVlanId, actual.outerVlanId);
205 assertEquals(expected.innerVlanId, actual.innerVlanId);
209 private SwInterfaceSetFlags verifySwInterfaceSetFlagsWasInvoked(final SwInterfaceSetFlags expected)
210 throws VppBaseCallException {
211 ArgumentCaptor<SwInterfaceSetFlags> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlags.class);
212 verify(api).swInterfaceSetFlags(argumentCaptor.capture());
213 final SwInterfaceSetFlags actual = argumentCaptor.getValue();
215 assertEquals(expected.swIfIndex, actual.swIfIndex);
216 assertEquals(expected.adminUpDown, actual.adminUpDown);
221 public void testCreateTwoTags() throws Exception {
222 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
223 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
225 whenCreateSubifThenSuccess();
226 whenSwInterfaceSetFlagsThenSuccess();
228 customizer.writeCurrentAttributes(id, subInterface, writeContext);
230 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
231 verify(mappingContext)
232 .put(eq(ContextTestUtils.getMappingIid(SUB_IFACE_NAME, IFC_TEST_INSTANCE)), eq(
233 ContextTestUtils.getMapping(SUB_IFACE_NAME, 0).get()));
237 public void testCreateDot1qAnyTag() throws Exception {
238 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_ANY));
239 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
241 whenCreateSubifThenSuccess();
242 whenSwInterfaceSetFlagsThenSuccess();
244 customizer.writeCurrentAttributes(id, subInterface, writeContext);
246 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ANY_ID, true));
247 verify(mappingContext)
248 .put(eq(ContextTestUtils.getMappingIid(SUB_IFACE_NAME, IFC_TEST_INSTANCE)), eq(
249 ContextTestUtils.getMapping(SUB_IFACE_NAME, 0).get()));
253 public void testCreateFailed() throws Exception {
254 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
255 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
257 whenCreateSubifThenFailure();
260 customizer.writeCurrentAttributes(id, subInterface, writeContext);
261 } catch (WriteFailedException.CreateFailedException e) {
262 assertTrue(e.getCause() instanceof VppBaseCallException);
263 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
264 verify(mappingContext, times(0)).put(
265 eq(ContextTestUtils.getMappingIid(SUPER_IF_NAME, IFC_TEST_INSTANCE)),
266 eq(ContextTestUtils.getMapping(SUPER_IF_NAME, 0).get()));
269 fail("WriteFailedException.CreateFailedException was expected");
273 public void testUpdate() throws Exception {
274 final List<Tag> tags = Arrays.asList(STAG_100, CTAG_200);
275 final SubInterface before = generateSubInterface(false, tags);
276 final SubInterface after = generateSubInterface(true, tags);
277 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
279 whenSwInterfaceSetFlagsThenSuccess();
280 customizer.updateCurrentAttributes(id, before, after, writeContext);
282 verifySwInterfaceSetFlagsWasInvoked(generateSwInterfaceEnableRequest(SUBIF_INDEX));
285 @Test(expected = UnsupportedOperationException.class)
286 public void testDelete() throws Exception {
287 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
288 customizer.deleteCurrentAttributes(null, subInterface, writeContext);