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.Mockito.doReturn;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.MockitoAnnotations.initMocks;
29 import io.fd.honeycomb.v3po.translate.MappingContext;
30 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
31 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
32 import io.fd.honeycomb.v3po.translate.write.WriteContext;
33 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.ExecutionException;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Mock;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.TagRewriteOperation;
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.L2;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.l2.VlanTagRewrite;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.l2.VlanTagRewriteBuilder;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewrite;
52 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
53 import org.openvpp.jvpp.future.FutureJVpp;
55 public class VlanTagRewriteCustomizerTest {
58 private FutureJVpp api;
60 private WriteContext writeContext;
62 private MappingContext mappingContext;
64 private NamingContext namingContext;
65 private VlanTagRewriteCustomizer customizer;
67 public static final String VLAN_IF_NAME = "local0.0";
68 public static final int VLAN_IF_ID = 1;
71 public void setUp() throws Exception {
73 namingContext = new NamingContext("generatedSubInterfaceName", "test-instance");
74 doReturn(mappingContext).when(writeContext).getMappingContext();
75 customizer = new VlanTagRewriteCustomizer(api, namingContext);
76 doReturn(getMapping(VLAN_IF_NAME, VLAN_IF_ID)).when(mappingContext).read(getMappingIid(VLAN_IF_NAME, "test-instance"));
79 private InstanceIdentifier<VlanTagRewrite> getVlanTagRewriteId(final String name) {
80 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
81 VppInterfaceAugmentation.class).child(L2.class).child(VlanTagRewrite.class);
84 private VlanTagRewrite generateVlanTagRewrite(final int vtrOp) {
85 final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
86 builder.setRewriteOperation(TagRewriteOperation.forValue(vtrOp));
87 builder.setTag1(new VlanTag(100));
88 builder.setTag2(new VlanTag(200));
89 builder.setFirstPushed(VlanType._802dot1ad);
90 return builder.build();
93 private L2InterfaceVlanTagRewrite generateL2InterfaceVlanTagRewrite(final int superIfId, final int vtrOp) {
94 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
95 request.swIfIndex = superIfId;
96 request.vtrOp = vtrOp;
97 request.pushDot1Q = 0;
103 private void whenL2InterfaceVlanTagRewriteThen(final int retval) throws ExecutionException, InterruptedException {
104 final CompletableFuture<L2InterfaceVlanTagRewriteReply> replyFuture = new CompletableFuture<>();
105 final L2InterfaceVlanTagRewriteReply reply = new L2InterfaceVlanTagRewriteReply();
106 reply.retval = retval;
107 replyFuture.complete(reply);
108 doReturn(replyFuture).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
111 private void whenL2InterfaceVlanTagRewriteThenSuccess() throws ExecutionException, InterruptedException {
112 whenL2InterfaceVlanTagRewriteThen(0);
115 private void whenL2InterfaceVlanTagRewriteThenFailure() throws ExecutionException, InterruptedException {
116 whenL2InterfaceVlanTagRewriteThen(-1);
119 private void verifyL2InterfaceVlanTagRewriteWasInvoked(final L2InterfaceVlanTagRewrite expected) {
120 ArgumentCaptor<L2InterfaceVlanTagRewrite> argumentCaptor = ArgumentCaptor.forClass(L2InterfaceVlanTagRewrite.class);
121 verify(api).l2InterfaceVlanTagRewrite(argumentCaptor.capture());
122 final L2InterfaceVlanTagRewrite actual = argumentCaptor.getValue();
123 assertEquals(expected.swIfIndex, actual.swIfIndex);
124 assertEquals(expected.vtrOp, actual.vtrOp);
125 assertEquals(expected.pushDot1Q, actual.pushDot1Q);
126 assertEquals(expected.tag1, actual.tag1);
127 assertEquals(expected.tag2, actual.tag2);
130 private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() {
131 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
132 request.swIfIndex = VLAN_IF_ID;
133 verifyL2InterfaceVlanTagRewriteWasInvoked(request);
137 public void testCreate() throws Exception {
139 final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
140 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
142 whenL2InterfaceVlanTagRewriteThenSuccess();
144 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
146 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOp));
150 public void testCreateFailed() throws Exception {
152 final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
153 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
155 whenL2InterfaceVlanTagRewriteThenFailure();
158 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
159 } catch (WriteFailedException.CreateFailedException e) {
160 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
161 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOp));
164 fail("WriteFailedException.CreateFailedException was expected");
168 public void testUpdateNoChange() throws Exception {
169 final VlanTagRewrite before = generateVlanTagRewrite(6);
170 final VlanTagRewrite after = generateVlanTagRewrite(6);
171 customizer.updateCurrentAttributes(null, before, after, writeContext);
172 verify(api, never()).l2InterfaceVlanTagRewrite(any());
176 public void testUpdate() throws Exception {
177 final int vtrOpAfter = 5;
178 final VlanTagRewrite before = generateVlanTagRewrite(6);
179 final VlanTagRewrite after = generateVlanTagRewrite(vtrOpAfter);
180 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
182 whenL2InterfaceVlanTagRewriteThenSuccess();
184 customizer.updateCurrentAttributes(id, before, after, writeContext);
186 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOpAfter));
190 public void testUpdateFailed() throws Exception {
191 final int vtrOpAfter = 5;
192 final VlanTagRewrite before = generateVlanTagRewrite(6);
193 final VlanTagRewrite after = generateVlanTagRewrite(vtrOpAfter);
194 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
196 whenL2InterfaceVlanTagRewriteThenFailure();
199 customizer.updateCurrentAttributes(id, before, after, writeContext);
200 } catch (WriteFailedException.UpdateFailedException e) {
201 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
202 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOpAfter));
205 fail("WriteFailedException.UpdateFailedException was expected");
209 public void testDelete() throws Exception {
210 final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
211 builder.setRewriteOperation(TagRewriteOperation.Disabled);
212 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
214 whenL2InterfaceVlanTagRewriteThenSuccess();
216 customizer.deleteCurrentAttributes(id, builder.build(), writeContext);
218 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
222 public void testDeleteFailed() throws Exception {
223 final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
224 builder.setRewriteOperation(TagRewriteOperation.Disabled);
225 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
227 whenL2InterfaceVlanTagRewriteThenFailure();
230 customizer.deleteCurrentAttributes(id, builder.build(), writeContext);
231 } catch (WriteFailedException.DeleteFailedException e) {
232 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
233 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
236 fail("WriteFailedException.DeleteFailedException was expected");