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 com.google.common.base.Optional;
20 import io.fd.honeycomb.v3po.translate.MappingContext;
21 import io.fd.honeycomb.v3po.translate.v3po.test.TestHelperUtils;
22 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
23 import io.fd.honeycomb.v3po.translate.v3po.util.TagRewriteOperation;
24 import io.fd.honeycomb.v3po.translate.write.WriteContext;
25 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.Mock;
31 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.*;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1q;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.Rewrite;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.RewriteBuilder;
43 import org.opendaylight.yangtools.yang.binding.ChildOf;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.openvpp.jvpp.VppBaseCallException;
46 import org.openvpp.jvpp.VppInvocationException;
47 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewrite;
48 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
49 import org.openvpp.jvpp.future.FutureJVpp;
51 import java.util.concurrent.CompletableFuture;
52 import java.util.concurrent.ExecutionException;
54 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
55 import static junit.framework.TestCase.assertTrue;
56 import static org.junit.Assert.assertEquals;
57 import static org.junit.Assert.fail;
58 import static org.mockito.Matchers.any;
59 import static org.mockito.Mockito.doReturn;
60 import static org.mockito.Mockito.verify;
61 import static org.mockito.MockitoAnnotations.initMocks;
63 public class RewriteCustomizerTest {
66 private FutureJVpp api;
68 private WriteContext writeContext;
70 private MappingContext mappingContext;
72 private NamingContext namingContext;
73 private RewriteCustomizer customizer;
75 public static final String VLAN_IF_NAME = "local0.1";
76 public static final int VLAN_IF_ID = 1;
77 public static final int VLAN_IF_INDEX = 11;
80 public void setUp() throws Exception {
82 namingContext = new NamingContext("generatedSubInterfaceName", "test-instance");
83 doReturn(mappingContext).when(writeContext).getMappingContext();
84 customizer = new RewriteCustomizer(api, namingContext);
86 final Optional<Mapping> ifcMapping = getMapping(VLAN_IF_NAME, VLAN_IF_INDEX);
87 doReturn(ifcMapping).when(mappingContext).read(any());
90 private InstanceIdentifier<Rewrite> getVlanTagRewriteId(final String name, final long index) {
91 final Class<ChildOf<? super SubInterface>> child = (Class)Rewrite.class;
92 final InstanceIdentifier id =
93 InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
94 SubinterfaceAugmentation.class).child(SubInterfaces.class)
95 .child(SubInterface.class, new SubInterfaceKey(index))
100 private Rewrite generateRewrite(final TagRewriteOperation op) {
101 final RewriteBuilder builder = new RewriteBuilder();
102 builder.setPopTags((short) op.getPopTags());
103 builder.setVlanType(_802dot1q.class);
104 return builder.build();
107 private L2InterfaceVlanTagRewrite generateL2InterfaceVlanTagRewrite(final int swIfIndex, final TagRewriteOperation op) {
108 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
109 request.swIfIndex = swIfIndex;
110 request.vtrOp = op.ordinal();
111 request.pushDot1Q = 1;
118 private void whenL2InterfaceVlanTagRewriteThen() 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 whenL2InterfaceVlanTagRewriteFailedThen(final int retval) throws ExecutionException, InterruptedException, VppInvocationException {
129 doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException(retval)).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
132 private void whenL2InterfaceVlanTagRewriteThenSuccess() throws ExecutionException, InterruptedException, VppInvocationException {
133 whenL2InterfaceVlanTagRewriteThen();
136 private void whenL2InterfaceVlanTagRewriteThenFailure() throws ExecutionException, InterruptedException, VppInvocationException {
137 whenL2InterfaceVlanTagRewriteFailedThen(-1);
140 private void verifyL2InterfaceVlanTagRewriteWasInvoked(final L2InterfaceVlanTagRewrite expected) throws VppInvocationException {
141 ArgumentCaptor<L2InterfaceVlanTagRewrite> argumentCaptor = ArgumentCaptor.forClass(L2InterfaceVlanTagRewrite.class);
142 verify(api).l2InterfaceVlanTagRewrite(argumentCaptor.capture());
143 final L2InterfaceVlanTagRewrite actual = argumentCaptor.getValue();
144 assertEquals(expected.swIfIndex, actual.swIfIndex);
145 assertEquals(expected.vtrOp, actual.vtrOp);
146 assertEquals(expected.pushDot1Q, actual.pushDot1Q);
147 assertEquals(expected.tag1, actual.tag1);
148 assertEquals(expected.tag2, actual.tag2);
151 private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() throws VppInvocationException {
152 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
153 request.swIfIndex = VLAN_IF_INDEX;
154 verifyL2InterfaceVlanTagRewriteWasInvoked(request);
158 public void testCreate() throws Exception {
159 final TagRewriteOperation op = TagRewriteOperation.pop_2;
160 final Rewrite vlanTagRewrite = generateRewrite(op);
161 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
163 whenL2InterfaceVlanTagRewriteThenSuccess();
165 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
167 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
171 public void testCreateFailed() throws Exception {
172 final TagRewriteOperation op = TagRewriteOperation.pop_2;
173 final Rewrite vlanTagRewrite = generateRewrite(op);
174 final String subIfaceName = "local0.11";
175 final int subifIndex = 1;
176 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(subIfaceName, subifIndex);
178 whenL2InterfaceVlanTagRewriteThenFailure();
181 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
182 } catch (WriteFailedException.CreateFailedException e) {
183 assertTrue(e.getCause() instanceof VppBaseCallException);
184 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
187 fail("WriteFailedException.CreateFailedException was expected");
191 public void testUpdate() throws Exception {
192 final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
193 final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
194 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
196 whenL2InterfaceVlanTagRewriteThenSuccess();
198 customizer.updateCurrentAttributes(id, before, after, writeContext);
200 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
204 public void testUpdateFailed() throws Exception {
205 final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
206 final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
207 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
209 whenL2InterfaceVlanTagRewriteThenFailure();
212 customizer.updateCurrentAttributes(id, before, after, writeContext);
213 } catch (WriteFailedException.UpdateFailedException e) {
214 assertTrue(e.getCause() instanceof VppBaseCallException);
215 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
218 fail("WriteFailedException.UpdateFailedException was expected");
222 public void testDelete() throws Exception {
223 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
225 whenL2InterfaceVlanTagRewriteThenSuccess();
227 customizer.deleteCurrentAttributes(id, null, writeContext);
229 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
233 public void testDeleteFailed() throws Exception {
234 final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
236 whenL2InterfaceVlanTagRewriteThenFailure();
239 customizer.deleteCurrentAttributes(id, null, writeContext);
240 } catch (WriteFailedException.DeleteFailedException e) {
241 Assert.assertTrue(e.getCause() instanceof VppBaseCallException);
242 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
245 fail("WriteFailedException.DeleteFailedException was expected");