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.verify;
26 import static org.mockito.MockitoAnnotations.initMocks;
28 import com.google.common.base.Optional;
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.InterfaceBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.TagRewriteOperation;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanTag;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanType;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.L2;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.l2.VlanTagRewrite;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.l2.VlanTagRewriteBuilder;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewrite;
54 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
55 import org.openvpp.jvpp.future.FutureJVpp;
57 public class VlanTagRewriteCustomizerTest {
60 private FutureJVpp api;
62 private WriteContext writeContext;
64 private MappingContext mappingContext;
66 private NamingContext namingContext;
67 private VlanTagRewriteCustomizer customizer;
69 public static final String VLAN_IF_NAME = "local0.0";
70 public static final int VLAN_IF_ID = 1;
73 public void setUp() throws Exception {
75 namingContext = new NamingContext("generatedSubInterfaceName", "test-instance");
76 doReturn(mappingContext).when(writeContext).getMappingContext();
77 customizer = new VlanTagRewriteCustomizer(api, namingContext);
78 doReturn(getMapping(VLAN_IF_NAME, VLAN_IF_ID)).when(mappingContext).read(getMappingIid(VLAN_IF_NAME, "test-instance"));
81 private InstanceIdentifier<VlanTagRewrite> getVlanTagRewriteId(final String name) {
82 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
83 VppInterfaceAugmentation.class).child(L2.class).child(VlanTagRewrite.class);
86 private VlanTagRewrite generateVlanTagRewrite(final int vtrOp) {
87 final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
88 builder.setRewriteOperation(TagRewriteOperation.forValue(vtrOp));
89 builder.setTag1(new VlanTag(100));
90 builder.setTag2(new VlanTag(200));
91 builder.setFirstPushed(VlanType._802dot1ad);
92 return builder.build();
95 private L2InterfaceVlanTagRewrite generateL2InterfaceVlanTagRewrite(final int superIfId, final int vtrOp) {
96 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
97 request.swIfIndex = superIfId;
98 request.vtrOp = vtrOp;
99 request.pushDot1Q = 0;
105 private void whenL2InterfaceVlanTagRewriteThen(final int retval) throws ExecutionException, InterruptedException {
106 final CompletableFuture<L2InterfaceVlanTagRewriteReply> replyFuture = new CompletableFuture<>();
107 final L2InterfaceVlanTagRewriteReply reply = new L2InterfaceVlanTagRewriteReply();
108 reply.retval = retval;
109 replyFuture.complete(reply);
110 doReturn(replyFuture).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
113 private void whenL2InterfaceVlanTagRewriteThenSuccess() throws ExecutionException, InterruptedException {
114 whenL2InterfaceVlanTagRewriteThen(0);
117 private void whenL2InterfaceVlanTagRewriteThenFailure() throws ExecutionException, InterruptedException {
118 whenL2InterfaceVlanTagRewriteThen(-1);
121 private void verifyL2InterfaceVlanTagRewriteWasInvoked(final L2InterfaceVlanTagRewrite expected) {
122 ArgumentCaptor<L2InterfaceVlanTagRewrite> argumentCaptor = ArgumentCaptor.forClass(L2InterfaceVlanTagRewrite.class);
123 verify(api).l2InterfaceVlanTagRewrite(argumentCaptor.capture());
124 final L2InterfaceVlanTagRewrite actual = argumentCaptor.getValue();
125 assertEquals(expected.swIfIndex, actual.swIfIndex);
126 assertEquals(expected.vtrOp, actual.vtrOp);
127 assertEquals(expected.pushDot1Q, actual.pushDot1Q);
128 assertEquals(expected.tag1, actual.tag1);
129 assertEquals(expected.tag2, actual.tag2);
132 private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() {
133 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
134 request.swIfIndex = VLAN_IF_ID;
135 verifyL2InterfaceVlanTagRewriteWasInvoked(request);
139 public void testCreate() throws Exception {
141 final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
142 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
144 whenL2InterfaceVlanTagRewriteThenSuccess();
145 // Vlan Tag rewrite is checking ifc type by reading its configuration from write context
146 doReturn(Optional.of(new InterfaceBuilder().setType(SubInterface.class).build()))
147 .when(writeContext).readAfter(any(InstanceIdentifier.class));
149 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
151 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOp));
155 public void testCreateFailed() throws Exception {
157 final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
158 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
160 whenL2InterfaceVlanTagRewriteThenFailure();
161 // Vlan Tag rewrite is checking ifc type by reading its configuration from write context
162 doReturn(Optional.of(new InterfaceBuilder().setType(SubInterface.class).build()))
163 .when(writeContext).readAfter(any(InstanceIdentifier.class));
166 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
167 } catch (WriteFailedException.CreateFailedException e) {
168 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
169 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOp));
172 fail("WriteFailedException.CreateFailedException was expected");
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");