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.never;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.MockitoAnnotations.initMocks;
30 import com.google.common.base.Optional;
31 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
32 import io.fd.honeycomb.translate.write.WriteContext;
33 import io.fd.honeycomb.translate.MappingContext;
34 import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
35 import io.fd.honeycomb.translate.v3po.util.NamingContext;
36 import io.fd.honeycomb.translate.write.WriteFailedException;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.concurrent.CompletableFuture;
40 import java.util.concurrent.ExecutionException;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.ArgumentCaptor;
44 import org.mockito.Mock;
45 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
46 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
47 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qTagVlanType;
48 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
49 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
50 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
51 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTagBuilder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceAugmentation;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1ad;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.vlan.tagged.VlanTaggedBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Match;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.MatchBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.TagsBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.Tag;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagKey;
68 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
69 import org.openvpp.jvpp.VppBaseCallException;
70 import org.openvpp.jvpp.dto.CreateSubif;
71 import org.openvpp.jvpp.dto.CreateSubifReply;
72 import org.openvpp.jvpp.dto.SwInterfaceSetFlags;
73 import org.openvpp.jvpp.dto.SwInterfaceSetFlagsReply;
74 import org.openvpp.jvpp.future.FutureJVpp;
76 public class SubInterfaceCustomizerTest {
79 private FutureJVpp api;
81 private WriteContext writeContext;
83 private MappingContext mappingContext;
85 private NamingContext namingContext;
86 private SubInterfaceCustomizer customizer;
88 public static final String SUPER_IF_NAME = "local0";
89 public static final int SUPER_IF_ID = 1;
90 private static final String SUB_IFACE_NAME = "local0.11";
91 private static final int SUBIF_INDEX = 11;
93 private static final short STAG_ID = 100;
94 private static final short CTAG_ID = 200;
95 private static final short CTAG_ANY_ID = 0; // only the *IdAny flag is set
97 private final Tag STAG_100;
98 private final Tag CTAG_200;
99 private final Tag CTAG_ANY;
101 public SubInterfaceCustomizerTest() {
102 STAG_100 = generateTag((short) 0, SVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId((int) STAG_ID)));
103 CTAG_200 = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId(200)));
104 CTAG_ANY = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(Dot1qTag.VlanId.Enumeration.Any));
108 public void setUp() throws Exception {
110 namingContext = new NamingContext("generatedSubInterfaceName", "test-instance");
111 doReturn(mappingContext).when(writeContext).getMappingContext();
112 // TODO create base class for tests using vppApi
113 customizer = new SubInterfaceCustomizer(api, namingContext);
114 doReturn(ContextTestUtils.getMapping(SUPER_IF_NAME, SUPER_IF_ID)).when(mappingContext)
115 .read(ContextTestUtils.getMappingIid(SUPER_IF_NAME, "test-instance"));
118 private SubInterface generateSubInterface(final boolean enabled, final List<Tag> tagList) {
119 SubInterfaceBuilder builder = new SubInterfaceBuilder();
120 builder.setVlanType(_802dot1ad.class);
121 builder.setIdentifier(11L);
122 final TagsBuilder tags = new TagsBuilder();
124 tags.setTag(tagList);
126 builder.setTags(tags.build());
128 builder.setMatch(generateMatch());
129 builder.setEnabled(enabled);
130 return builder.build();
133 private static Tag generateTag(final short index, final Class<? extends Dot1qTagVlanType> tagType,
134 final Dot1qTag.VlanId vlanId) {
135 TagBuilder tag = new TagBuilder();
137 tag.setKey(new TagKey(index));
138 final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
139 dtag.setTagType(tagType);
140 dtag.setVlanId(vlanId);
141 tag.setDot1qTag(dtag.build());
145 private static Match generateMatch() {
146 final MatchBuilder match = new MatchBuilder();
147 final VlanTaggedBuilder tagged = new VlanTaggedBuilder();
148 tagged.setMatchExactTags(true);
150 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.VlanTaggedBuilder()
151 .setVlanTagged(tagged.build()).build());
152 return match.build();
155 private CreateSubif generateSubInterfaceRequest(final int superIfId, final short innerVlanId,
156 final boolean isInnerAny) {
157 CreateSubif request = new CreateSubif();
159 request.swIfIndex = superIfId;
161 request.innerVlanId = innerVlanId;
162 request.innerVlanIdAny = (byte) (isInnerAny
166 request.outerVlanId = STAG_ID;
170 private SwInterfaceSetFlags generateSwInterfaceEnableRequest(final int swIfIndex) {
171 SwInterfaceSetFlags request = new SwInterfaceSetFlags();
172 request.swIfIndex = swIfIndex;
173 request.adminUpDown = 1;
177 private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long index) {
178 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
179 SubinterfaceAugmentation.class).child(SubInterfaces.class)
180 .child(SubInterface.class, new SubInterfaceKey(index));
183 private void whenCreateSubifThenSuccess() throws ExecutionException, InterruptedException, VppBaseCallException {
184 final CompletableFuture<CreateSubifReply> replyFuture = new CompletableFuture<>();
185 final CreateSubifReply reply = new CreateSubifReply();
186 replyFuture.complete(reply);
187 doReturn(replyFuture).when(api).createSubif(any(CreateSubif.class));
191 * Failure response send
193 private void whenCreateSubifThenFailure() throws ExecutionException, InterruptedException, VppBaseCallException {
194 doReturn(TestHelperUtils.<CreateSubifReply>createFutureException()).when(api)
195 .createSubif(any(CreateSubif.class));
198 private void whenSwInterfaceSetFlagsThenSuccess()
199 throws ExecutionException, InterruptedException, VppBaseCallException {
200 final CompletableFuture<SwInterfaceSetFlagsReply> replyFuture = new CompletableFuture<>();
201 final SwInterfaceSetFlagsReply reply = new SwInterfaceSetFlagsReply();
202 replyFuture.complete(reply);
203 doReturn(replyFuture).when(api).swInterfaceSetFlags(any(SwInterfaceSetFlags.class));
206 private CreateSubif verifyCreateSubifWasInvoked(final CreateSubif expected) throws VppBaseCallException {
207 ArgumentCaptor<CreateSubif> argumentCaptor = ArgumentCaptor.forClass(CreateSubif.class);
208 verify(api).createSubif(argumentCaptor.capture());
209 final CreateSubif actual = argumentCaptor.getValue();
211 assertEquals(expected.swIfIndex, actual.swIfIndex);
212 assertEquals(expected.subId, actual.subId);
213 assertEquals(expected.noTags, actual.noTags);
214 assertEquals(expected.oneTag, actual.oneTag);
215 assertEquals(expected.twoTags, actual.twoTags);
216 assertEquals(expected.dot1Ad, actual.dot1Ad);
217 assertEquals(expected.exactMatch, actual.exactMatch);
218 assertEquals(expected.defaultSub, actual.defaultSub);
219 assertEquals(expected.outerVlanIdAny, actual.outerVlanIdAny);
220 assertEquals(expected.innerVlanIdAny, actual.innerVlanIdAny);
221 assertEquals(expected.outerVlanId, actual.outerVlanId);
222 assertEquals(expected.innerVlanId, actual.innerVlanId);
226 private SwInterfaceSetFlags verifySwInterfaceSetFlagsWasInvoked(final SwInterfaceSetFlags expected)
227 throws VppBaseCallException {
228 ArgumentCaptor<SwInterfaceSetFlags> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlags.class);
229 verify(api).swInterfaceSetFlags(argumentCaptor.capture());
230 final SwInterfaceSetFlags actual = argumentCaptor.getValue();
232 assertEquals(expected.swIfIndex, actual.swIfIndex);
233 assertEquals(expected.adminUpDown, actual.adminUpDown);
238 public void testCreateTwoTags() throws Exception {
239 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
240 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
242 whenCreateSubifThenSuccess();
243 whenSwInterfaceSetFlagsThenSuccess();
245 customizer.writeCurrentAttributes(id, subInterface, writeContext);
247 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
248 verify(mappingContext)
249 .put(eq(ContextTestUtils.getMappingIid(SUB_IFACE_NAME, "test-instance")), eq(
250 ContextTestUtils.getMapping(SUB_IFACE_NAME, 0).get()));
254 public void testCreateDot1qAnyTag() throws Exception {
255 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_ANY));
256 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
258 whenCreateSubifThenSuccess();
259 whenSwInterfaceSetFlagsThenSuccess();
261 customizer.writeCurrentAttributes(id, subInterface, writeContext);
263 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ANY_ID, true));
264 verify(mappingContext)
265 .put(eq(ContextTestUtils.getMappingIid(SUB_IFACE_NAME, "test-instance")), eq(
266 ContextTestUtils.getMapping(SUB_IFACE_NAME, 0).get()));
270 public void testCreateFailed() throws Exception {
271 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
272 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
274 whenCreateSubifThenFailure();
277 customizer.writeCurrentAttributes(id, subInterface, writeContext);
278 } catch (WriteFailedException.CreateFailedException e) {
279 assertTrue(e.getCause() instanceof VppBaseCallException);
280 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
281 verify(mappingContext, times(0)).put(
282 eq(ContextTestUtils.getMappingIid(SUPER_IF_NAME, "test-instance")),
283 eq(ContextTestUtils.getMapping(SUPER_IF_NAME, 0).get()));
286 fail("WriteFailedException.CreateFailedException was expected");
290 public void testUpdate() throws Exception {
291 final List<Tag> tags = Arrays.asList(STAG_100, CTAG_200);
292 final SubInterface before = generateSubInterface(false, tags);
293 final SubInterface after = generateSubInterface(true, tags);
294 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
296 whenSwInterfaceSetFlagsThenSuccess();
297 final Optional<Mapping> ifcMapping = ContextTestUtils.getMapping(SUPER_IF_NAME, SUBIF_INDEX);
298 doReturn(ifcMapping).when(mappingContext).read(any());
300 customizer.updateCurrentAttributes(id, before, after, writeContext);
302 verifySwInterfaceSetFlagsWasInvoked(generateSwInterfaceEnableRequest(SUBIF_INDEX));
306 public void testUpdateNoStateChange() throws Exception {
307 final List<Tag> tags = Arrays.asList(STAG_100, CTAG_200);
308 final SubInterface before = generateSubInterface(false, tags);
309 final SubInterface after = generateSubInterface(false, tags);
310 customizer.updateCurrentAttributes(null, before, after, writeContext);
312 verify(api, never()).swInterfaceSetFlags(any());
315 @Test(expected = UnsupportedOperationException.class)
316 public void testDelete() throws Exception {
317 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
318 customizer.deleteCurrentAttributes(null, subInterface, writeContext);