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 junit.framework.TestCase.assertTrue;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.fail;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.MockitoAnnotations.initMocks;
27 import com.google.common.base.Optional;
28 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
29 import io.fd.honeycomb.translate.write.WriteContext;
30 import io.fd.honeycomb.translate.MappingContext;
31 import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
32 import io.fd.honeycomb.translate.v3po.util.NamingContext;
33 import io.fd.honeycomb.translate.v3po.util.TagRewriteOperation;
34 import io.fd.honeycomb.translate.write.WriteFailedException;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.ExecutionException;
37 import org.junit.Assert;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.Mock;
42 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceAugmentation;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1q;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.Rewrite;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.RewriteBuilder;
53 import org.opendaylight.yangtools.yang.binding.ChildOf;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.openvpp.jvpp.VppBaseCallException;
56 import org.openvpp.jvpp.VppInvocationException;
57 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewrite;
58 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
59 import org.openvpp.jvpp.future.FutureJVpp;
61 public class RewriteCustomizerTest {
64 private FutureJVpp api;
66 private WriteContext writeContext;
68 private MappingContext mappingContext;
70 private NamingContext namingContext;
71 private RewriteCustomizer customizer;
73 public static final String VLAN_IF_NAME = "local0.1";
74 public static final int VLAN_IF_ID = 1;
75 public static final int VLAN_IF_INDEX = 11;
78 public void setUp() throws Exception {
80 namingContext = new NamingContext("generatedSubInterfaceName", "test-instance");
81 doReturn(mappingContext).when(writeContext).getMappingContext();
82 customizer = new RewriteCustomizer(api, namingContext);
84 final Optional<Mapping> ifcMapping = ContextTestUtils.getMapping(VLAN_IF_NAME, VLAN_IF_INDEX);
85 doReturn(ifcMapping).when(mappingContext).read(any());
88 private InstanceIdentifier<Rewrite> getVlanTagRewriteId(final String name, final long index) {
89 final Class<ChildOf<? super SubInterface>> child = (Class)Rewrite.class;
90 final InstanceIdentifier id =
91 InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
92 SubinterfaceAugmentation.class).child(SubInterfaces.class)
93 .child(SubInterface.class, new SubInterfaceKey(index))
98 private Rewrite generateRewrite(final TagRewriteOperation op) {
99 final RewriteBuilder builder = new RewriteBuilder();
100 builder.setPopTags((short) op.getPopTags());
101 builder.setVlanType(_802dot1q.class);
102 return builder.build();
105 private L2InterfaceVlanTagRewrite generateL2InterfaceVlanTagRewrite(final int swIfIndex,
106 final TagRewriteOperation op) {
107 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
108 request.swIfIndex = swIfIndex;
109 request.vtrOp = op.ordinal();
110 request.pushDot1Q = 1;
117 private void whenL2InterfaceVlanTagRewriteThenSuccess()
118 throws ExecutionException, InterruptedException, VppInvocationException {
119 final CompletableFuture<L2InterfaceVlanTagRewriteReply> replyFuture = new CompletableFuture<>();
120 final L2InterfaceVlanTagRewriteReply reply = new L2InterfaceVlanTagRewriteReply();
121 replyFuture.complete(reply);
122 doReturn(replyFuture).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
126 * Failure response send
128 private void whenL2InterfaceVlanTagRewriteThenFailure()
129 throws ExecutionException, InterruptedException, VppInvocationException {
130 doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
131 .l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
134 private void verifyL2InterfaceVlanTagRewriteWasInvoked(final L2InterfaceVlanTagRewrite expected)
135 throws VppInvocationException {
136 ArgumentCaptor<L2InterfaceVlanTagRewrite> argumentCaptor =
137 ArgumentCaptor.forClass(L2InterfaceVlanTagRewrite.class);
138 verify(api).l2InterfaceVlanTagRewrite(argumentCaptor.capture());
139 final L2InterfaceVlanTagRewrite actual = argumentCaptor.getValue();
140 assertEquals(expected.swIfIndex, actual.swIfIndex);
141 assertEquals(expected.vtrOp, actual.vtrOp);
142 assertEquals(expected.pushDot1Q, actual.pushDot1Q);
143 assertEquals(expected.tag1, actual.tag1);
144 assertEquals(expected.tag2, actual.tag2);
147 private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() throws VppInvocationException {
148 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
149 request.swIfIndex = VLAN_IF_INDEX;
150 verifyL2InterfaceVlanTagRewriteWasInvoked(request);
154 public void testCreate() throws Exception {
155 final TagRewriteOperation op = TagRewriteOperation.pop_2;
156 final Rewrite vlanTagRewrite = generateRewrite(op);
157 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
159 whenL2InterfaceVlanTagRewriteThenSuccess();
161 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
163 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
167 public void testCreateFailed() throws Exception {
168 final TagRewriteOperation op = TagRewriteOperation.pop_2;
169 final Rewrite vlanTagRewrite = generateRewrite(op);
170 final String subIfaceName = "local0.11";
171 final int subifIndex = 1;
172 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(subIfaceName, subifIndex);
174 whenL2InterfaceVlanTagRewriteThenFailure();
177 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
178 } catch (WriteFailedException.CreateFailedException e) {
179 assertTrue(e.getCause() instanceof VppBaseCallException);
180 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
183 fail("WriteFailedException.CreateFailedException was expected");
187 public void testUpdate() throws Exception {
188 final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
189 final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
190 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
192 whenL2InterfaceVlanTagRewriteThenSuccess();
194 customizer.updateCurrentAttributes(id, before, after, writeContext);
196 verifyL2InterfaceVlanTagRewriteWasInvoked(
197 generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
201 public void testUpdateFailed() throws Exception {
202 final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
203 final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
204 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
206 whenL2InterfaceVlanTagRewriteThenFailure();
209 customizer.updateCurrentAttributes(id, before, after, writeContext);
210 } catch (WriteFailedException.UpdateFailedException e) {
211 assertTrue(e.getCause() instanceof VppBaseCallException);
212 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX,
213 TagRewriteOperation.pop_1));
216 fail("WriteFailedException.UpdateFailedException was expected");
220 public void testDelete() throws Exception {
221 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
223 whenL2InterfaceVlanTagRewriteThenSuccess();
225 customizer.deleteCurrentAttributes(id, null, writeContext);
227 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
231 public void testDeleteFailed() throws Exception {
232 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
234 whenL2InterfaceVlanTagRewriteThenFailure();
237 customizer.deleteCurrentAttributes(id, null, writeContext);
238 } catch (WriteFailedException.DeleteFailedException e) {
239 Assert.assertTrue(e.getCause() instanceof VppBaseCallException);
240 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
243 fail("WriteFailedException.DeleteFailedException was expected");