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.fail;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.verify;
25 import io.fd.honeycomb.translate.v3po.util.NamingContext;
26 import io.fd.honeycomb.translate.v3po.util.TagRewriteOperation;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1q;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.Rewrite;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.RewriteBuilder;
41 import org.opendaylight.yangtools.yang.binding.ChildOf;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.openvpp.jvpp.VppBaseCallException;
44 import org.openvpp.jvpp.VppInvocationException;
45 import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewrite;
46 import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewriteReply;
48 public class RewriteCustomizerTest extends WriterCustomizerTest {
50 private NamingContext namingContext;
51 private RewriteCustomizer customizer;
53 private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
54 private static final String IF_NAME = "local0";
55 private static final String VLAN_IF_NAME = "local0.1";
56 private static final int VLAN_IF_ID = 1;
57 private static final int VLAN_IF_INDEX = 11;
58 private InstanceIdentifier<Rewrite> VLAN_IID;
61 public void setUp() throws Exception {
62 namingContext = new NamingContext("generatedSubInterfaceName", IFC_TEST_INSTANCE);
63 customizer = new RewriteCustomizer(api, namingContext);
64 VLAN_IID = getVlanTagRewriteId(IF_NAME, VLAN_IF_ID);
65 defineMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_TEST_INSTANCE);
68 private static InstanceIdentifier<Rewrite> getVlanTagRewriteId(final String name, final long index) {
69 final Class<ChildOf<? super SubInterface>> child = (Class) Rewrite.class;
70 final InstanceIdentifier id =
71 InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
72 SubinterfaceAugmentation.class).child(SubInterfaces.class)
73 .child(SubInterface.class, new SubInterfaceKey(index))
78 private Rewrite generateRewrite(final TagRewriteOperation op) {
79 final RewriteBuilder builder = new RewriteBuilder();
80 builder.setPopTags((short) op.getPopTags());
81 builder.setVlanType(_802dot1q.class);
82 return builder.build();
85 private L2InterfaceVlanTagRewrite generateL2InterfaceVlanTagRewrite(final int swIfIndex,
86 final TagRewriteOperation op) {
87 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
88 request.swIfIndex = swIfIndex;
89 request.vtrOp = op.ordinal();
90 request.pushDot1Q = 1;
97 private void whenL2InterfaceVlanTagRewriteThenSuccess() {
98 doReturn(future(new L2InterfaceVlanTagRewriteReply())).when(api)
99 .l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
103 * Failure response send
105 private void whenL2InterfaceVlanTagRewriteThenFailure() {
106 doReturn(failedFuture()).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
109 private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() throws VppInvocationException {
110 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
111 request.swIfIndex = VLAN_IF_INDEX;
112 verify(api).l2InterfaceVlanTagRewrite(request);
116 public void testCreate() throws Exception {
117 final TagRewriteOperation op = TagRewriteOperation.pop_2;
118 final Rewrite vlanTagRewrite = generateRewrite(op);
120 whenL2InterfaceVlanTagRewriteThenSuccess();
122 customizer.writeCurrentAttributes(VLAN_IID, vlanTagRewrite, writeContext);
124 verify(api).l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
128 public void testCreateFailed() throws Exception {
129 final TagRewriteOperation op = TagRewriteOperation.pop_2;
130 final Rewrite vlanTagRewrite = generateRewrite(op);
132 whenL2InterfaceVlanTagRewriteThenFailure();
135 customizer.writeCurrentAttributes(VLAN_IID, vlanTagRewrite, writeContext);
136 } catch (WriteFailedException.CreateFailedException e) {
137 assertTrue(e.getCause() instanceof VppBaseCallException);
138 verify(api).l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
141 fail("WriteFailedException.CreateFailedException was expected");
145 public void testUpdate() throws Exception {
146 final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
147 final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
149 whenL2InterfaceVlanTagRewriteThenSuccess();
151 customizer.updateCurrentAttributes(VLAN_IID, before, after, writeContext);
154 .l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
158 public void testUpdateFailed() throws Exception {
159 final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
160 final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
162 whenL2InterfaceVlanTagRewriteThenFailure();
165 customizer.updateCurrentAttributes(VLAN_IID, before, after, writeContext);
166 } catch (WriteFailedException.UpdateFailedException e) {
167 assertTrue(e.getCause() instanceof VppBaseCallException);
169 .l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
172 fail("WriteFailedException.UpdateFailedException was expected");
176 public void testDelete() throws Exception {
177 whenL2InterfaceVlanTagRewriteThenSuccess();
179 customizer.deleteCurrentAttributes(VLAN_IID, null, writeContext);
181 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
185 public void testDeleteFailed() throws Exception {
186 whenL2InterfaceVlanTagRewriteThenFailure();
189 customizer.deleteCurrentAttributes(VLAN_IID, null, writeContext);
190 } catch (WriteFailedException.DeleteFailedException e) {
191 Assert.assertTrue(e.getCause() instanceof VppBaseCallException);
192 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
195 fail("WriteFailedException.DeleteFailedException was expected");