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 org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.MockitoAnnotations.initMocks;
28 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
29 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
30 import io.fd.honeycomb.v3po.translate.write.WriteContext;
31 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
32 import java.util.concurrent.CompletableFuture;
33 import java.util.concurrent.ExecutionException;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Mock;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanTag;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.SubInterface;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.SubInterfaceBuilder;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.openvpp.jvpp.dto.CreateSubif;
48 import org.openvpp.jvpp.dto.CreateSubifReply;
49 import org.openvpp.jvpp.future.FutureJVpp;
51 public class SubInterfaceCustomizerTest {
54 private FutureJVpp api;
56 private WriteContext writeContext;
58 private NamingContext namingContext;
59 private SubInterfaceCustomizer customizer;
60 public static final String SUPER_IF_NAME = "local0";
61 public static final int SUPER_IF_ID = 1;
64 public void setUp() throws Exception {
66 InterfaceTypeTestUtils.setupWriteContext(writeContext,
67 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface.class);
68 namingContext = new NamingContext("generatedSubInterfaceName");
69 // TODO create base class for tests using vppApi
70 customizer = new SubInterfaceCustomizer(api, namingContext);
71 namingContext.addName(SUPER_IF_ID, SUPER_IF_NAME);
74 private SubInterface generateSubInterface(final String superIfName) {
75 SubInterfaceBuilder builder = new SubInterfaceBuilder();
76 builder.setVlanType(VlanType._802dot1q);
77 builder.setIdentifier(11L);
78 builder.setNumberOfTags((short)1);
79 builder.setOuterId(new VlanTag(100));
80 builder.setInnerId(new VlanTag(200));
81 builder.setSuperInterface(superIfName);
82 return builder.build();
85 private CreateSubif generateSubInterfaceRequest(final int superIfId) {
86 CreateSubif request = new CreateSubif();
88 request.swIfIndex = superIfId;
91 request.outerVlanId = 100;
92 request.innerVlanId = 200;
96 private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name) {
97 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
98 VppInterfaceAugmentation.class).child(SubInterface.class);
101 private void whenCreateSubifThen(final int retval) throws ExecutionException, InterruptedException {
102 final CompletableFuture<CreateSubifReply> replyFuture = new CompletableFuture<>();
103 final CreateSubifReply reply = new CreateSubifReply();
104 reply.retval = retval;
105 replyFuture.complete(reply);
106 doReturn(replyFuture).when(api).createSubif(any(CreateSubif.class));
109 private void whenCreateSubifThenSuccess() throws ExecutionException, InterruptedException {
110 whenCreateSubifThen(0);
113 private void whenCreateSubifThenFailure() throws ExecutionException, InterruptedException {
114 whenCreateSubifThen(-1);
117 private CreateSubif verifyCreateSubifWasInvoked(final CreateSubif expected) {
118 ArgumentCaptor<CreateSubif> argumentCaptor = ArgumentCaptor.forClass(CreateSubif.class);
119 verify(api).createSubif(argumentCaptor.capture());
120 final CreateSubif actual = argumentCaptor.getValue();
122 assertEquals(expected.swIfIndex, actual.swIfIndex);
123 assertEquals(expected.subId, actual.subId);
124 assertEquals(expected.noTags, actual.noTags);
125 assertEquals(expected.oneTag, actual.oneTag);
126 assertEquals(expected.twoTags, actual.twoTags);
127 assertEquals(expected.dot1Ad, actual.dot1Ad);
128 assertEquals(expected.exactMatch, actual.exactMatch);
129 assertEquals(expected.defaultSub, actual.defaultSub);
130 assertEquals(expected.outerVlanIdAny, actual.outerVlanIdAny);
131 assertEquals(expected.innerVlanIdAny, actual.innerVlanIdAny);
132 assertEquals(expected.outerVlanId, actual.outerVlanId);
133 assertEquals(expected.innerVlanId, actual.innerVlanId);
138 public void testCreate() throws Exception {
139 final SubInterface subInterface = generateSubInterface(SUPER_IF_NAME);
140 final String subIfaceName = "local0.sub1";
141 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(subIfaceName);
143 whenCreateSubifThenSuccess();
145 customizer.writeCurrentAttributes(id, subInterface, writeContext);
147 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID));
148 assertTrue(namingContext.containsIndex(subIfaceName));
152 public void testCreateFailed() throws Exception {
153 final SubInterface subInterface = generateSubInterface(SUPER_IF_NAME);
154 final String subIfaceName = "local0.sub1";
155 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(subIfaceName);
157 whenCreateSubifThenFailure();
160 customizer.writeCurrentAttributes(id, subInterface, writeContext);
161 } catch (WriteFailedException.CreateFailedException e) {
162 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
163 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID));
164 assertFalse(namingContext.containsIndex(subIfaceName));
167 fail("WriteFailedException.CreateFailedException was expected");
171 public void testUpdateNoChange() throws Exception {
172 final SubInterface before = generateSubInterface(SUPER_IF_NAME);
173 final SubInterface after = generateSubInterface(SUPER_IF_NAME);
174 customizer.updateCurrentAttributes(null, before, after, writeContext);
177 @Test(expected = UnsupportedOperationException.class)
178 public void testUpdate() throws Exception {
179 final SubInterface before = generateSubInterface("eth0");
180 final SubInterface after = generateSubInterface("eth1");
181 customizer.updateCurrentAttributes(null, before, after, writeContext);
184 @Test(expected = UnsupportedOperationException.class)
185 public void testDelete() throws Exception {
186 final SubInterface subInterface = generateSubInterface("eth0");
187 customizer.deleteCurrentAttributes(null, subInterface, writeContext);