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.fail;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.MockitoAnnotations.initMocks;
27 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
28 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
29 import io.fd.honeycomb.v3po.translate.write.WriteContext;
30 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
31 import java.util.concurrent.CompletableFuture;
32 import java.util.concurrent.ExecutionException;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mock;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.TagRewriteOperation;
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.L2;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.l2.VlanTagRewrite;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.l2.VlanTagRewriteBuilder;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewrite;
49 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
50 import org.openvpp.jvpp.future.FutureJVpp;
52 public class VlanTagRewriteCustomizerTest {
55 private FutureJVpp api;
57 private WriteContext writeContext;
59 private NamingContext namingContext;
60 private VlanTagRewriteCustomizer customizer;
62 public static final String VLAN_IF_NAME = "local0.0";
63 public static final int VLAN_IF_ID = 1;
66 public void setUp() throws Exception {
68 namingContext = new NamingContext("generatedSubInterfaceName");
69 customizer = new VlanTagRewriteCustomizer(api, namingContext);
70 namingContext.addName(VLAN_IF_ID, VLAN_IF_NAME);
73 private InstanceIdentifier<VlanTagRewrite> getVlanTagRewriteId(final String name) {
74 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
75 VppInterfaceAugmentation.class).child(L2.class).child(VlanTagRewrite.class);
78 private VlanTagRewrite generateVlanTagRewrite(final int vtrOp) {
79 final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
80 builder.setRewriteOperation(TagRewriteOperation.forValue(vtrOp));
81 builder.setTag1(new VlanTag(100));
82 builder.setTag2(new VlanTag(200));
83 builder.setFirstPushed(VlanType._802dot1ad);
84 return builder.build();
87 private L2InterfaceVlanTagRewrite generateL2InterfaceVlanTagRewrite(final int superIfId, final int vtrOp) {
88 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
89 request.swIfIndex = superIfId;
90 request.vtrOp = vtrOp;
91 request.pushDot1Q = 0;
97 private void whenL2InterfaceVlanTagRewriteThen(final int retval) throws ExecutionException, InterruptedException {
98 final CompletableFuture<L2InterfaceVlanTagRewriteReply> replyFuture = new CompletableFuture<>();
99 final L2InterfaceVlanTagRewriteReply reply = new L2InterfaceVlanTagRewriteReply();
100 reply.retval = retval;
101 replyFuture.complete(reply);
102 doReturn(replyFuture).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
105 private void whenL2InterfaceVlanTagRewriteThenSuccess() throws ExecutionException, InterruptedException {
106 whenL2InterfaceVlanTagRewriteThen(0);
109 private void whenL2InterfaceVlanTagRewriteThenFailure() throws ExecutionException, InterruptedException {
110 whenL2InterfaceVlanTagRewriteThen(-1);
113 private void verifyL2InterfaceVlanTagRewriteWasInvoked(final L2InterfaceVlanTagRewrite expected) {
114 ArgumentCaptor<L2InterfaceVlanTagRewrite> argumentCaptor = ArgumentCaptor.forClass(L2InterfaceVlanTagRewrite.class);
115 verify(api).l2InterfaceVlanTagRewrite(argumentCaptor.capture());
116 final L2InterfaceVlanTagRewrite actual = argumentCaptor.getValue();
117 assertEquals(expected.swIfIndex, actual.swIfIndex);
118 assertEquals(expected.vtrOp, actual.vtrOp);
119 assertEquals(expected.pushDot1Q, actual.pushDot1Q);
120 assertEquals(expected.tag1, actual.tag1);
121 assertEquals(expected.tag2, actual.tag2);
124 private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() {
125 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
126 request.swIfIndex = VLAN_IF_ID;
127 verifyL2InterfaceVlanTagRewriteWasInvoked(request);
131 public void testCreate() throws Exception {
133 final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
134 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
136 whenL2InterfaceVlanTagRewriteThenSuccess();
138 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
140 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOp));
144 public void testCreateFailed() throws Exception {
146 final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
147 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
149 whenL2InterfaceVlanTagRewriteThenFailure();
152 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
153 } catch (WriteFailedException.CreateFailedException e) {
154 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
155 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOp));
158 fail("WriteFailedException.CreateFailedException was expected");
162 public void testUpdateNoChange() throws Exception {
163 final VlanTagRewrite before = generateVlanTagRewrite(6);
164 final VlanTagRewrite after = generateVlanTagRewrite(6);
165 customizer.updateCurrentAttributes(null, before, after, writeContext);
166 verify(api, never()).l2InterfaceVlanTagRewrite(any());
170 public void testUpdate() throws Exception {
171 final int vtrOpAfter = 5;
172 final VlanTagRewrite before = generateVlanTagRewrite(6);
173 final VlanTagRewrite after = generateVlanTagRewrite(vtrOpAfter);
174 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
176 whenL2InterfaceVlanTagRewriteThenSuccess();
178 customizer.updateCurrentAttributes(id, before, after, writeContext);
180 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOpAfter));
184 public void testUpdateFailed() throws Exception {
185 final int vtrOpAfter = 5;
186 final VlanTagRewrite before = generateVlanTagRewrite(6);
187 final VlanTagRewrite after = generateVlanTagRewrite(vtrOpAfter);
188 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
190 whenL2InterfaceVlanTagRewriteThenFailure();
193 customizer.updateCurrentAttributes(id, before, after, writeContext);
194 } catch (WriteFailedException.UpdateFailedException e) {
195 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
196 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOpAfter));
199 fail("WriteFailedException.UpdateFailedException was expected");
203 public void testDelete() throws Exception {
204 final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
205 builder.setRewriteOperation(TagRewriteOperation.Disabled);
206 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
208 whenL2InterfaceVlanTagRewriteThenSuccess();
210 customizer.deleteCurrentAttributes(id, builder.build(), writeContext);
212 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
216 public void testDeleteFailed() throws Exception {
217 final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
218 builder.setRewriteOperation(TagRewriteOperation.Disabled);
219 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
221 whenL2InterfaceVlanTagRewriteThenFailure();
224 customizer.deleteCurrentAttributes(id, builder.build(), writeContext);
225 } catch (WriteFailedException.DeleteFailedException e) {
226 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
227 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
230 fail("WriteFailedException.DeleteFailedException was expected");