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.v3po.translate.v3po.interfaces;
19 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.MockitoAnnotations.initMocks;
32 import com.google.common.base.Optional;
33 import io.fd.honeycomb.v3po.translate.MappingContext;
34 import io.fd.honeycomb.v3po.translate.v3po.test.TestHelperUtils;
35 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
36 import io.fd.honeycomb.v3po.translate.write.WriteContext;
37 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.concurrent.CompletableFuture;
41 import java.util.concurrent.ExecutionException;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.ArgumentCaptor;
45 import org.mockito.Mock;
46 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
47 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
48 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qTagVlanType;
49 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
50 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
51 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
52 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTagBuilder;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceAugmentation;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1ad;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.vlan.tagged.VlanTaggedBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Match;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.MatchBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.TagsBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.Tag;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagKey;
69 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
70 import org.openvpp.jvpp.VppBaseCallException;
71 import org.openvpp.jvpp.dto.CreateSubif;
72 import org.openvpp.jvpp.dto.CreateSubifReply;
73 import org.openvpp.jvpp.dto.SwInterfaceSetFlags;
74 import org.openvpp.jvpp.dto.SwInterfaceSetFlagsReply;
75 import org.openvpp.jvpp.future.FutureJVpp;
77 public class SubInterfaceCustomizerTest {
80 private FutureJVpp api;
82 private WriteContext writeContext;
84 private MappingContext mappingContext;
86 private NamingContext namingContext;
87 private SubInterfaceCustomizer customizer;
89 public static final String SUPER_IF_NAME = "local0";
90 public static final int SUPER_IF_ID = 1;
91 private static final String SUB_IFACE_NAME = "local0.11";
92 private static final int SUBIF_INDEX = 11;
94 private static final short STAG_ID = 100;
95 private static final short CTAG_ID = 200;
96 private static final short CTAG_ANY_ID = 0; // only the *IdAny flag is set
98 private final Tag STAG_100;
99 private final Tag CTAG_200;
100 private final Tag CTAG_ANY;
102 public SubInterfaceCustomizerTest() {
103 STAG_100 = generateTag((short) 0, SVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId((int) STAG_ID)));
104 CTAG_200 = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId(200)));
105 CTAG_ANY = generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(Dot1qTag.VlanId.Enumeration.Any));
109 public void setUp() throws Exception {
111 namingContext = new NamingContext("generatedSubInterfaceName", "test-instance");
112 doReturn(mappingContext).when(writeContext).getMappingContext();
113 // TODO create base class for tests using vppApi
114 customizer = new SubInterfaceCustomizer(api, namingContext);
115 doReturn(getMapping(SUPER_IF_NAME, SUPER_IF_ID)).when(mappingContext)
116 .read(getMappingIid(SUPER_IF_NAME, "test-instance"));
119 private SubInterface generateSubInterface(final boolean enabled, final List<Tag> tagList) {
120 SubInterfaceBuilder builder = new SubInterfaceBuilder();
121 builder.setVlanType(_802dot1ad.class);
122 builder.setIdentifier(11L);
123 final TagsBuilder tags = new TagsBuilder();
125 tags.setTag(tagList);
127 builder.setTags(tags.build());
129 builder.setMatch(generateMatch());
130 builder.setEnabled(enabled);
131 return builder.build();
134 private static Tag generateTag(final short index, final Class<? extends Dot1qTagVlanType> tagType,
135 final Dot1qTag.VlanId vlanId) {
136 TagBuilder tag = new TagBuilder();
138 tag.setKey(new TagKey(index));
139 final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
140 dtag.setTagType(tagType);
141 dtag.setVlanId(vlanId);
142 tag.setDot1qTag(dtag.build());
146 private static Match generateMatch() {
147 final MatchBuilder match = new MatchBuilder();
148 final VlanTaggedBuilder tagged = new VlanTaggedBuilder();
149 tagged.setMatchExactTags(true);
151 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.VlanTaggedBuilder()
152 .setVlanTagged(tagged.build()).build());
153 return match.build();
156 private CreateSubif generateSubInterfaceRequest(final int superIfId, final short innerVlanId,
157 final boolean isInnerAny) {
158 CreateSubif request = new CreateSubif();
160 request.swIfIndex = superIfId;
162 request.innerVlanId = innerVlanId;
163 request.innerVlanIdAny = (byte) (isInnerAny
167 request.outerVlanId = STAG_ID;
171 private SwInterfaceSetFlags generateSwInterfaceEnableRequest(final int swIfIndex) {
172 SwInterfaceSetFlags request = new SwInterfaceSetFlags();
173 request.swIfIndex = swIfIndex;
174 request.adminUpDown = 1;
178 private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long index) {
179 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
180 SubinterfaceAugmentation.class).child(SubInterfaces.class)
181 .child(SubInterface.class, new SubInterfaceKey(index));
184 private void whenCreateSubifThenSuccess() throws ExecutionException, InterruptedException, VppBaseCallException {
185 final CompletableFuture<CreateSubifReply> replyFuture = new CompletableFuture<>();
186 final CreateSubifReply reply = new CreateSubifReply();
187 replyFuture.complete(reply);
188 doReturn(replyFuture).when(api).createSubif(any(CreateSubif.class));
192 * Failure response send
194 private void whenCreateSubifThenFailure() throws ExecutionException, InterruptedException, VppBaseCallException {
195 doReturn(TestHelperUtils.<CreateSubifReply>createFutureException()).when(api)
196 .createSubif(any(CreateSubif.class));
199 private void whenSwInterfaceSetFlagsThenSuccess()
200 throws ExecutionException, InterruptedException, VppBaseCallException {
201 final CompletableFuture<SwInterfaceSetFlagsReply> replyFuture = new CompletableFuture<>();
202 final SwInterfaceSetFlagsReply reply = new SwInterfaceSetFlagsReply();
203 replyFuture.complete(reply);
204 doReturn(replyFuture).when(api).swInterfaceSetFlags(any(SwInterfaceSetFlags.class));
207 private CreateSubif verifyCreateSubifWasInvoked(final CreateSubif expected) throws VppBaseCallException {
208 ArgumentCaptor<CreateSubif> argumentCaptor = ArgumentCaptor.forClass(CreateSubif.class);
209 verify(api).createSubif(argumentCaptor.capture());
210 final CreateSubif actual = argumentCaptor.getValue();
212 assertEquals(expected.swIfIndex, actual.swIfIndex);
213 assertEquals(expected.subId, actual.subId);
214 assertEquals(expected.noTags, actual.noTags);
215 assertEquals(expected.oneTag, actual.oneTag);
216 assertEquals(expected.twoTags, actual.twoTags);
217 assertEquals(expected.dot1Ad, actual.dot1Ad);
218 assertEquals(expected.exactMatch, actual.exactMatch);
219 assertEquals(expected.defaultSub, actual.defaultSub);
220 assertEquals(expected.outerVlanIdAny, actual.outerVlanIdAny);
221 assertEquals(expected.innerVlanIdAny, actual.innerVlanIdAny);
222 assertEquals(expected.outerVlanId, actual.outerVlanId);
223 assertEquals(expected.innerVlanId, actual.innerVlanId);
227 private SwInterfaceSetFlags verifySwInterfaceSetFlagsWasInvoked(final SwInterfaceSetFlags expected)
228 throws VppBaseCallException {
229 ArgumentCaptor<SwInterfaceSetFlags> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlags.class);
230 verify(api).swInterfaceSetFlags(argumentCaptor.capture());
231 final SwInterfaceSetFlags actual = argumentCaptor.getValue();
233 assertEquals(expected.swIfIndex, actual.swIfIndex);
234 assertEquals(expected.adminUpDown, actual.adminUpDown);
239 public void testCreateTwoTags() throws Exception {
240 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
241 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
243 whenCreateSubifThenSuccess();
244 whenSwInterfaceSetFlagsThenSuccess();
246 customizer.writeCurrentAttributes(id, subInterface, writeContext);
248 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
249 verify(mappingContext)
250 .put(eq(getMappingIid(SUB_IFACE_NAME, "test-instance")), eq(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(getMappingIid(SUB_IFACE_NAME, "test-instance")), eq(getMapping(SUB_IFACE_NAME, 0).get()));
269 public void testCreateFailed() throws Exception {
270 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
271 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
273 whenCreateSubifThenFailure();
276 customizer.writeCurrentAttributes(id, subInterface, writeContext);
277 } catch (WriteFailedException.CreateFailedException e) {
278 assertTrue(e.getCause() instanceof VppBaseCallException);
279 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID, CTAG_ID, false));
280 verify(mappingContext, times(0)).put(
281 eq(getMappingIid(SUPER_IF_NAME, "test-instance")),
282 eq(getMapping(SUPER_IF_NAME, 0).get()));
285 fail("WriteFailedException.CreateFailedException was expected");
289 public void testUpdate() throws Exception {
290 final List<Tag> tags = Arrays.asList(STAG_100, CTAG_200);
291 final SubInterface before = generateSubInterface(false, tags);
292 final SubInterface after = generateSubInterface(true, tags);
293 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(SUPER_IF_NAME, SUBIF_INDEX);
295 whenSwInterfaceSetFlagsThenSuccess();
296 final Optional<Mapping> ifcMapping = getMapping(SUPER_IF_NAME, SUBIF_INDEX);
297 doReturn(ifcMapping).when(mappingContext).read(any());
299 customizer.updateCurrentAttributes(id, before, after, writeContext);
301 verifySwInterfaceSetFlagsWasInvoked(generateSwInterfaceEnableRequest(SUBIF_INDEX));
305 public void testUpdateNoStateChange() throws Exception {
306 final List<Tag> tags = Arrays.asList(STAG_100, CTAG_200);
307 final SubInterface before = generateSubInterface(false, tags);
308 final SubInterface after = generateSubInterface(false, tags);
309 customizer.updateCurrentAttributes(null, before, after, writeContext);
311 verify(api, never()).swInterfaceSetFlags(any());
314 @Test(expected = UnsupportedOperationException.class)
315 public void testDelete() throws Exception {
316 final SubInterface subInterface = generateSubInterface(false, Arrays.asList(STAG_100, CTAG_200));
317 customizer.deleteCurrentAttributes(null, subInterface, writeContext);