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.ArrayList;
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;
88 public static final String SUPER_IF_NAME = "local0";
89 public static final int SUPER_IF_ID = 1;
92 public void setUp() throws Exception {
94 namingContext = new NamingContext("generatedSubInterfaceName", "test-instance");
95 doReturn(mappingContext).when(writeContext).getMappingContext();
96 // TODO create base class for tests using vppApi
97 customizer = new SubInterfaceCustomizer(api, namingContext);
98 doReturn(getMapping(SUPER_IF_NAME, SUPER_IF_ID)).when(mappingContext)
99 .read(getMappingIid(SUPER_IF_NAME, "test-instance"));
102 private SubInterface generateSubInterface(final String superIfName, final boolean enabled) {
103 SubInterfaceBuilder builder = new SubInterfaceBuilder();
104 builder.setVlanType(_802dot1ad.class);
105 builder.setIdentifier(11L);
106 final TagsBuilder tags = new TagsBuilder();
107 final List<Tag> list = new ArrayList<>();
108 list.add(generateTag((short) 0, SVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId(100))));
109 list.add(generateTag((short) 1, CVlan.class, new Dot1qTag.VlanId(new Dot1qVlanId(200))));
113 builder.setTags(tags.build());
115 builder.setMatch(generateMatch());
116 builder.setEnabled(enabled);
117 return builder.build();
120 private static Tag generateTag(final short index, final Class<? extends Dot1qTagVlanType> tagType,
121 final Dot1qTag.VlanId vlanId) {
122 TagBuilder tag = new TagBuilder();
124 tag.setKey(new TagKey(index));
125 final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
126 dtag.setTagType(tagType);
127 dtag.setVlanId(vlanId);
128 tag.setDot1qTag(dtag.build());
132 private static Match generateMatch() {
133 final MatchBuilder match = new MatchBuilder();
134 final VlanTaggedBuilder tagged = new VlanTaggedBuilder();
135 tagged.setMatchExactTags(true);
137 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.VlanTaggedBuilder()
138 .setVlanTagged(tagged.build()).build());
139 return match.build();
142 private CreateSubif generateSubInterfaceRequest(final int superIfId) {
143 CreateSubif request = new CreateSubif();
145 request.swIfIndex = superIfId;
148 request.outerVlanId = 100;
149 request.innerVlanId = 200;
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).child(SubInterface.class, new SubInterfaceKey(index));
165 private void whenCreateSubifThen() throws ExecutionException, InterruptedException, VppBaseCallException {
166 final CompletableFuture<CreateSubifReply> replyFuture = new CompletableFuture<>();
167 final CreateSubifReply reply = new CreateSubifReply();
168 replyFuture.complete(reply);
169 doReturn(replyFuture).when(api).createSubif(any(CreateSubif.class));
173 * Failure response send
175 private void whenCreateSubifFailedThen(final int retval) throws ExecutionException, InterruptedException, VppBaseCallException {
176 doReturn(TestHelperUtils.<CreateSubifReply>createFutureException(retval)).when(api).createSubif(any(CreateSubif.class));
179 private void whenSwInterfaceSetFlagsThen(final int retval) throws ExecutionException, InterruptedException, VppBaseCallException {
180 final CompletableFuture<SwInterfaceSetFlagsReply> replyFuture = new CompletableFuture<>();
181 final SwInterfaceSetFlagsReply reply = new SwInterfaceSetFlagsReply();
182 replyFuture.complete(reply);
183 doReturn(replyFuture).when(api).swInterfaceSetFlags(any(SwInterfaceSetFlags.class));
186 private void whenCreateSubifThenSuccess() throws ExecutionException, InterruptedException, VppBaseCallException {
187 whenCreateSubifThen();
190 private void whenCreateSubifThenFailure() throws ExecutionException, InterruptedException, VppBaseCallException {
191 whenCreateSubifFailedThen(-1);
194 private void whenSwInterfaceSetFlagsThenSuccess() throws ExecutionException, InterruptedException, VppBaseCallException {
195 whenSwInterfaceSetFlagsThen(0);
198 private CreateSubif verifyCreateSubifWasInvoked(final CreateSubif expected) throws VppBaseCallException {
199 ArgumentCaptor<CreateSubif> argumentCaptor = ArgumentCaptor.forClass(CreateSubif.class);
200 verify(api).createSubif(argumentCaptor.capture());
201 final CreateSubif actual = argumentCaptor.getValue();
203 assertEquals(expected.swIfIndex, actual.swIfIndex);
204 assertEquals(expected.subId, actual.subId);
205 assertEquals(expected.noTags, actual.noTags);
206 assertEquals(expected.oneTag, actual.oneTag);
207 assertEquals(expected.twoTags, actual.twoTags);
208 assertEquals(expected.dot1Ad, actual.dot1Ad);
209 assertEquals(expected.exactMatch, actual.exactMatch);
210 assertEquals(expected.defaultSub, actual.defaultSub);
211 assertEquals(expected.outerVlanIdAny, actual.outerVlanIdAny);
212 assertEquals(expected.innerVlanIdAny, actual.innerVlanIdAny);
213 assertEquals(expected.outerVlanId, actual.outerVlanId);
214 assertEquals(expected.innerVlanId, actual.innerVlanId);
218 private SwInterfaceSetFlags verifySwInterfaceSetFlagsWasInvoked(final SwInterfaceSetFlags expected) throws VppBaseCallException{
219 ArgumentCaptor<SwInterfaceSetFlags> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlags.class);
220 verify(api).swInterfaceSetFlags(argumentCaptor.capture());
221 final SwInterfaceSetFlags actual = argumentCaptor.getValue();
223 assertEquals(expected.swIfIndex, actual.swIfIndex);
224 assertEquals(expected.adminUpDown, actual.adminUpDown);
229 public void testCreate() throws Exception {
230 final SubInterface subInterface = generateSubInterface(SUPER_IF_NAME, false);
231 final String superIfName = "local0";
232 final String subIfaceName = "local0.11";
233 final long subifIndex = 11;
234 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(superIfName, subifIndex);
236 whenCreateSubifThenSuccess();
237 whenSwInterfaceSetFlagsThenSuccess();
239 customizer.writeCurrentAttributes(id, subInterface, writeContext);
241 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID));
242 verify(mappingContext)
243 .put(eq(getMappingIid(subIfaceName, "test-instance")), eq(getMapping(subIfaceName, 0).get()));
247 public void testCreateFailed() throws Exception {
248 final SubInterface subInterface = generateSubInterface(SUPER_IF_NAME, false);
249 final String superIfName = "local0";
250 final String subIfaceName = "local0.11";
251 final long subifIndex = 11;
252 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(superIfName, subifIndex);
254 whenCreateSubifThenFailure();
257 customizer.writeCurrentAttributes(id, subInterface, writeContext);
258 } catch (WriteFailedException.CreateFailedException e) {
259 assertTrue(e.getCause() instanceof VppBaseCallException );
260 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID));
261 verify(mappingContext, times(0)).put(
262 eq(getMappingIid(subIfaceName, "test-instance")),
263 eq(getMapping(subIfaceName, 0).get()));
266 fail("WriteFailedException.CreateFailedException was expected");
270 public void testUpdate() throws Exception {
271 final SubInterface before = generateSubInterface("eth0", false);
272 final SubInterface after = generateSubInterface("eth1", true);
273 final String superIfName = "local0";
274 final String subIfaceName = "local0.11";
275 final int subifIndex = 11;
276 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(superIfName, subifIndex);
278 whenSwInterfaceSetFlagsThenSuccess();
279 final Optional<Mapping> ifcMapping = getMapping(subIfaceName, subifIndex);
280 doReturn(ifcMapping).when(mappingContext).read(any());
282 customizer.updateCurrentAttributes(id, before, after, writeContext);
284 verifySwInterfaceSetFlagsWasInvoked(generateSwInterfaceEnableRequest(subifIndex));
288 public void testUpdateNoStateChange() throws Exception {
289 final SubInterface before = generateSubInterface(SUPER_IF_NAME, false);
290 final SubInterface after = generateSubInterface(SUPER_IF_NAME, false);
291 customizer.updateCurrentAttributes(null, before, after, writeContext);
293 verify(api, never()).swInterfaceSetFlags(any());
296 @Test(expected = UnsupportedOperationException.class)
297 public void testDelete() throws Exception {
298 final SubInterface subInterface = generateSubInterface("eth0", false);
299 customizer.deleteCurrentAttributes(null, subInterface, writeContext);