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.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.v3po.translate.v3po.ContextTestUtils.getMappingIid;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.fail;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.MockitoAnnotations.initMocks;
30 import io.fd.honeycomb.v3po.translate.MappingContext;
31 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
32 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
33 import io.fd.honeycomb.v3po.translate.write.WriteContext;
34 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.ExecutionException;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.ArgumentCaptor;
40 import org.mockito.Mock;
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.v3po.rev150105.VlanTag;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanType;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.SubInterface;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.SubInterfaceBuilder;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.openvpp.jvpp.dto.CreateSubif;
51 import org.openvpp.jvpp.dto.CreateSubifReply;
52 import org.openvpp.jvpp.future.FutureJVpp;
54 public class SubInterfaceCustomizerTest {
57 private FutureJVpp api;
59 private WriteContext writeContext;
61 private MappingContext mappingContext;
63 private NamingContext namingContext;
64 private SubInterfaceCustomizer customizer;
65 public static final String SUPER_IF_NAME = "local0";
66 public static final int SUPER_IF_ID = 1;
69 public void setUp() throws Exception {
71 InterfaceTypeTestUtils.setupWriteContext(writeContext,
72 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface.class);
73 namingContext = new NamingContext("generatedSubInterfaceName", "test-instance");
74 doReturn(mappingContext).when(writeContext).getMappingContext();
75 // TODO create base class for tests using vppApi
76 customizer = new SubInterfaceCustomizer(api, namingContext);
77 doReturn(getMapping(SUPER_IF_NAME, SUPER_IF_ID)).when(mappingContext).read(getMappingIid(SUPER_IF_NAME, "test-instance"));
80 private SubInterface generateSubInterface(final String superIfName) {
81 SubInterfaceBuilder builder = new SubInterfaceBuilder();
82 builder.setVlanType(VlanType._802dot1ad);
83 builder.setIdentifier(11L);
84 builder.setNumberOfTags((short)1);
85 builder.setOuterId(new VlanTag(100));
86 builder.setInnerId(new VlanTag(200));
87 builder.setSuperInterface(superIfName);
88 return builder.build();
91 private CreateSubif generateSubInterfaceRequest(final int superIfId) {
92 CreateSubif request = new CreateSubif();
94 request.swIfIndex = superIfId;
97 request.outerVlanId = 100;
98 request.innerVlanId = 200;
102 private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name) {
103 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
104 VppInterfaceAugmentation.class).child(SubInterface.class);
107 private void whenCreateSubifThen(final int retval) throws ExecutionException, InterruptedException {
108 final CompletableFuture<CreateSubifReply> replyFuture = new CompletableFuture<>();
109 final CreateSubifReply reply = new CreateSubifReply();
110 reply.retval = retval;
111 replyFuture.complete(reply);
112 doReturn(replyFuture).when(api).createSubif(any(CreateSubif.class));
115 private void whenCreateSubifThenSuccess() throws ExecutionException, InterruptedException {
116 whenCreateSubifThen(0);
119 private void whenCreateSubifThenFailure() throws ExecutionException, InterruptedException {
120 whenCreateSubifThen(-1);
123 private CreateSubif verifyCreateSubifWasInvoked(final CreateSubif expected) {
124 ArgumentCaptor<CreateSubif> argumentCaptor = ArgumentCaptor.forClass(CreateSubif.class);
125 verify(api).createSubif(argumentCaptor.capture());
126 final CreateSubif actual = argumentCaptor.getValue();
128 assertEquals(expected.swIfIndex, actual.swIfIndex);
129 assertEquals(expected.subId, actual.subId);
130 assertEquals(expected.noTags, actual.noTags);
131 assertEquals(expected.oneTag, actual.oneTag);
132 assertEquals(expected.twoTags, actual.twoTags);
133 assertEquals(expected.dot1Ad, actual.dot1Ad);
134 assertEquals(expected.exactMatch, actual.exactMatch);
135 assertEquals(expected.defaultSub, actual.defaultSub);
136 assertEquals(expected.outerVlanIdAny, actual.outerVlanIdAny);
137 assertEquals(expected.innerVlanIdAny, actual.innerVlanIdAny);
138 assertEquals(expected.outerVlanId, actual.outerVlanId);
139 assertEquals(expected.innerVlanId, actual.innerVlanId);
144 public void testCreate() throws Exception {
145 final SubInterface subInterface = generateSubInterface(SUPER_IF_NAME);
146 final String subIfaceName = "local0.sub1";
147 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(subIfaceName);
149 whenCreateSubifThenSuccess();
151 customizer.writeCurrentAttributes(id, subInterface, writeContext);
153 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID));
154 verify(mappingContext).put(eq(getMappingIid(subIfaceName, "test-instance")), eq(getMapping(subIfaceName, 0).get()));
158 public void testCreateFailed() throws Exception {
159 final SubInterface subInterface = generateSubInterface(SUPER_IF_NAME);
160 final String subIfaceName = "local0.sub1";
161 final InstanceIdentifier<SubInterface> id = getSubInterfaceId(subIfaceName);
163 whenCreateSubifThenFailure();
166 customizer.writeCurrentAttributes(id, subInterface, writeContext);
167 } catch (WriteFailedException.CreateFailedException e) {
168 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
169 verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID));
170 verify(mappingContext, times(0)).put(
171 eq(getMappingIid(subIfaceName, "test-instance")),
172 eq(getMapping(subIfaceName, 0).get()));
175 fail("WriteFailedException.CreateFailedException was expected");
179 public void testUpdateNoChange() throws Exception {
180 final SubInterface before = generateSubInterface(SUPER_IF_NAME);
181 final SubInterface after = generateSubInterface(SUPER_IF_NAME);
182 customizer.updateCurrentAttributes(null, before, after, writeContext);
185 @Test(expected = UnsupportedOperationException.class)
186 public void testUpdate() throws Exception {
187 final SubInterface before = generateSubInterface("eth0");
188 final SubInterface after = generateSubInterface("eth1");
189 customizer.updateCurrentAttributes(null, before, after, writeContext);
192 @Test(expected = UnsupportedOperationException.class)
193 public void testDelete() throws Exception {
194 final SubInterface subInterface = generateSubInterface("eth0");
195 customizer.deleteCurrentAttributes(null, subInterface, writeContext);