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.vpp.util.NamingContext;
29 import io.fd.honeycomb.translate.write.WriteFailedException;
30 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
31 import java.util.Arrays;
32 import java.util.List;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
36 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qTagVlanType;
37 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
38 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
39 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
40 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTagBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1ad;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.vlan.tagged.VlanTaggedBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Match;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.MatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.TagsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.Tag;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagKey;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import io.fd.vpp.jvpp.VppBaseCallException;
59 import io.fd.vpp.jvpp.core.dto.CreateSubif;
60 import io.fd.vpp.jvpp.core.dto.CreateSubifReply;
61 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlags;
62 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlagsReply;
64 public class SubInterfaceCustomizerTest extends WriterCustomizerTest {
66 private NamingContext namingContext;
67 private SubInterfaceCustomizer customizer;
69 private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
70 private static final String SUPER_IF_NAME = "local0";
71 private static final int SUPER_IF_ID = 1;
72 private static final String SUB_IFACE_NAME = "local0.11";
73 private static final int SUBIF_INDEX = 11;
75 private static final short STAG_ID = 100;
76 private static final short CTAG_ID = 200;
77 private static final short CTAG_ANY_ID = 0; // only the *IdAny flag is set
79 private final Tag STAG_100;
80 private final Tag CTAG_200;
81 private final Tag CTAG_ANY;
83 public SubInterfaceCustomizerTest() {
84 STAG_100 = generateTag((short) 0, SVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId((int) STAG_ID)));
85 CTAG_200 = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId(200)));
86 CTAG_ANY = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(Dot1qTag.VlanId.Enumeration.Any));
90 public void setUp() throws Exception {
91 namingContext = new NamingContext("generatedSubInterfaceName", IFC_TEST_INSTANCE);
92 customizer = new SubInterfaceCustomizer(api, namingContext);
93 defineMapping(mappingContext, SUB_IFACE_NAME, SUBIF_INDEX, IFC_TEST_INSTANCE);
94 defineMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_ID, IFC_TEST_INSTANCE);
97 private SubInterface generateSubInterface(final boolean enabled, final List<Tag> tagList) {
98 SubInterfaceBuilder builder = new SubInterfaceBuilder();
99 builder.setVlanType(_802dot1ad.class);
100 builder.setIdentifier(11L);
101 final TagsBuilder tags = new TagsBuilder();
103 tags.setTag(tagList);
105 builder.setTags(tags.build());
107 builder.setMatch(generateMatch());
108 builder.setEnabled(enabled);
109 return builder.build();
112 private static Tag generateTag(final short index, final Class<? extends Dot1qTagVlanType> tagType,
113 final Dot1qTag.VlanId vlanId) {
114 TagBuilder tag = new TagBuilder();
116 tag.setKey(new TagKey(index));
117 final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
118 dtag.setTagType(tagType);
119 dtag.setVlanId(vlanId);
120 tag.setDot1qTag(dtag.build());
124 private static Match generateMatch() {
125 final MatchBuilder match = new MatchBuilder();
126 final VlanTaggedBuilder tagged = new VlanTaggedBuilder();
127 tagged.setMatchExactTags(true);
129 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.VlanTaggedBuilder()
130 .setVlanTagged(tagged.build()).build());
131 return match.build();
134 private CreateSubif generateSubInterfaceRequest(final int superIfId, final short innerVlanId,
135 final boolean isInnerAny) {
136 CreateSubif request = new CreateSubif();
138 request.swIfIndex = superIfId;
140 request.innerVlanId = innerVlanId;
141 request.innerVlanIdAny = (byte) (isInnerAny
145 request.outerVlanId = STAG_ID;
149 private SwInterfaceSetFlags generateSwInterfaceEnableRequest(final int swIfIndex) {
150 SwInterfaceSetFlags request = new SwInterfaceSetFlags();
151 request.swIfIndex = swIfIndex;
152 request.adminUpDown = 1;
156 private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long index) {
157 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
158 SubinterfaceAugmentation.class).child(SubInterfaces.class)
159 .child(SubInterface.class, new SubInterfaceKey(index));
162 private void whenCreateSubifThenSuccess() {
163 doReturn(future(new CreateSubifReply())).when(api).createSubif(any(CreateSubif.class));
167 * Failure response send
169 private void whenCreateSubifThenFailure() {
170 doReturn(failedFuture()).when(api).createSubif(any(CreateSubif.class));
173 private void whenSwInterfaceSetFlagsThenSuccess() {
174 doReturn(future(new SwInterfaceSetFlagsReply())).when(api).swInterfaceSetFlags(any(SwInterfaceSetFlags.class));
177 private SwInterfaceSetFlags verifySwInterfaceSetFlagsWasInvoked(final SwInterfaceSetFlags expected)
178 throws VppBaseCallException {
179 ArgumentCaptor<SwInterfaceSetFlags> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlags.class);
180 verify(api).swInterfaceSetFlags(argumentCaptor.capture());
181 final SwInterfaceSetFlags actual = argumentCaptor.getValue();
183 assertEquals(expected.swIfIndex, actual.swIfIndex);
184 assertEquals(expected.adminUpDown, actual.adminUpDown);
189 public void testCreateTwoTags() throws Exception {
190 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
191 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
193 whenCreateSubifThenSuccess();
194 whenSwInterfaceSetFlagsThenSuccess();
196 customizer.writeCurrentAttributes(id, subInterface, writeContext);
198 verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
199 verify(mappingContext)
200 .put(eq(mappingIid(SUB_IFACE_NAME, IFC_TEST_INSTANCE)), eq(
201 mapping(SUB_IFACE_NAME, 0).get()));
205 public void testCreateDot1qAnyTag() throws Exception {
206 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_ANY));
207 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
209 whenCreateSubifThenSuccess();
210 whenSwInterfaceSetFlagsThenSuccess();
212 customizer.writeCurrentAttributes(id, subInterface, writeContext);
214 verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ANY_ID, true));
215 verify(mappingContext)
216 .put(eq(mappingIid(SUB_IFACE_NAME, IFC_TEST_INSTANCE)), eq(
217 mapping(SUB_IFACE_NAME, 0).get()));
221 public void testCreateFailed() 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 whenCreateSubifThenFailure();
228 customizer.writeCurrentAttributes(id, subInterface, writeContext);
229 } catch (WriteFailedException.CreateFailedException e) {
230 assertTrue(e.getCause() instanceof VppBaseCallException);
231 verify(api).createSubif(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
232 verify(mappingContext, times(0)).put(
233 eq(mappingIid(SUPER_IF_NAME, IFC_TEST_INSTANCE)),
234 eq(mapping(SUPER_IF_NAME, 0).get()));
237 fail("WriteFailedException.CreateFailedException was expected");
241 public void testUpdate() throws Exception {
242 final List<Tag> tags = Arrays.asList(STAG_100, CTAG_200);
243 final SubInterface before = generateSubInterface(false, tags);
244 final SubInterface after = generateSubInterface(true, tags);
245 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
247 whenSwInterfaceSetFlagsThenSuccess();
248 customizer.updateCurrentAttributes(id, before, after, writeContext);
250 verifySwInterfaceSetFlagsWasInvoked(generateSwInterfaceEnableRequest(SUBIF_INDEX));
253 @Test(expected = UnsupportedOperationException.class)
254 public void testDelete() throws Exception {
255 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
256 customizer.deleteCurrentAttributes(null, subInterface, writeContext);