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 com.google.common.base.Optional;
30 import io.fd.honeycomb.v3po.translate.MappingContext;
31 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
32 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
33 import io.fd.honeycomb.v3po.translate.write.WriteContext;
34 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.ExecutionException;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.ArgumentCaptor;
40 import org.mockito.Mock;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.TagRewriteOperation;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanTag;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.L2;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.l2.VlanTagRewrite;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.l2.VlanTagRewriteBuilder;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewrite;
55 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
56 import org.openvpp.jvpp.future.FutureJVpp;
58 public class VlanTagRewriteCustomizerTest {
61 private FutureJVpp api;
63 private WriteContext writeContext;
65 private MappingContext mappingContext;
67 private NamingContext namingContext;
68 private VlanTagRewriteCustomizer customizer;
70 public static final String VLAN_IF_NAME = "local0.0";
71 public static final int VLAN_IF_ID = 1;
74 public void setUp() throws Exception {
76 namingContext = new NamingContext("generatedSubInterfaceName", "test-instance");
77 doReturn(mappingContext).when(writeContext).getMappingContext();
78 customizer = new VlanTagRewriteCustomizer(api, namingContext);
79 doReturn(getMapping(VLAN_IF_NAME, VLAN_IF_ID)).when(mappingContext).read(getMappingIid(VLAN_IF_NAME, "test-instance"));
82 private InstanceIdentifier<VlanTagRewrite> getVlanTagRewriteId(final String name) {
83 return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
84 VppInterfaceAugmentation.class).child(L2.class).child(VlanTagRewrite.class);
87 private VlanTagRewrite generateVlanTagRewrite(final int vtrOp) {
88 final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
89 builder.setRewriteOperation(TagRewriteOperation.forValue(vtrOp));
90 builder.setTag1(new VlanTag(100));
91 builder.setTag2(new VlanTag(200));
92 builder.setFirstPushed(VlanType._802dot1ad);
93 return builder.build();
96 private L2InterfaceVlanTagRewrite generateL2InterfaceVlanTagRewrite(final int superIfId, final int vtrOp) {
97 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
98 request.swIfIndex = superIfId;
99 request.vtrOp = vtrOp;
100 request.pushDot1Q = 0;
106 private void whenL2InterfaceVlanTagRewriteThen(final int retval) throws ExecutionException, InterruptedException {
107 final CompletableFuture<L2InterfaceVlanTagRewriteReply> replyFuture = new CompletableFuture<>();
108 final L2InterfaceVlanTagRewriteReply reply = new L2InterfaceVlanTagRewriteReply();
109 reply.retval = retval;
110 replyFuture.complete(reply);
111 doReturn(replyFuture).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
114 private void whenL2InterfaceVlanTagRewriteThenSuccess() throws ExecutionException, InterruptedException {
115 whenL2InterfaceVlanTagRewriteThen(0);
118 private void whenL2InterfaceVlanTagRewriteThenFailure() throws ExecutionException, InterruptedException {
119 whenL2InterfaceVlanTagRewriteThen(-1);
122 private void verifyL2InterfaceVlanTagRewriteWasInvoked(final L2InterfaceVlanTagRewrite expected) {
123 ArgumentCaptor<L2InterfaceVlanTagRewrite> argumentCaptor = ArgumentCaptor.forClass(L2InterfaceVlanTagRewrite.class);
124 verify(api).l2InterfaceVlanTagRewrite(argumentCaptor.capture());
125 final L2InterfaceVlanTagRewrite actual = argumentCaptor.getValue();
126 assertEquals(expected.swIfIndex, actual.swIfIndex);
127 assertEquals(expected.vtrOp, actual.vtrOp);
128 assertEquals(expected.pushDot1Q, actual.pushDot1Q);
129 assertEquals(expected.tag1, actual.tag1);
130 assertEquals(expected.tag2, actual.tag2);
133 private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() {
134 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
135 request.swIfIndex = VLAN_IF_ID;
136 verifyL2InterfaceVlanTagRewriteWasInvoked(request);
140 public void testCreate() throws Exception {
142 final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
143 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
145 whenL2InterfaceVlanTagRewriteThenSuccess();
146 // Vlan Tag rewrite is checking ifc type by reading its configuration from write context
147 doReturn(Optional.of(new InterfaceBuilder().setType(SubInterface.class).build()))
148 .when(writeContext).readAfter(any(InstanceIdentifier.class));
150 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
152 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOp));
156 public void testCreateFailed() throws Exception {
158 final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
159 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
161 whenL2InterfaceVlanTagRewriteThenFailure();
162 // Vlan Tag rewrite is checking ifc type by reading its configuration from write context
163 doReturn(Optional.of(new InterfaceBuilder().setType(SubInterface.class).build()))
164 .when(writeContext).readAfter(any(InstanceIdentifier.class));
167 customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
168 } catch (WriteFailedException.CreateFailedException e) {
169 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
170 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOp));
173 fail("WriteFailedException.CreateFailedException was expected");
177 public void testUpdateNoChange() throws Exception {
178 final VlanTagRewrite before = generateVlanTagRewrite(6);
179 final VlanTagRewrite after = generateVlanTagRewrite(6);
180 customizer.updateCurrentAttributes(null, before, after, writeContext);
181 verify(api, never()).l2InterfaceVlanTagRewrite(any());
185 public void testUpdate() throws Exception {
186 final int vtrOpAfter = 5;
187 final VlanTagRewrite before = generateVlanTagRewrite(6);
188 final VlanTagRewrite after = generateVlanTagRewrite(vtrOpAfter);
189 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
191 whenL2InterfaceVlanTagRewriteThenSuccess();
193 customizer.updateCurrentAttributes(id, before, after, writeContext);
195 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOpAfter));
199 public void testUpdateFailed() throws Exception {
200 final int vtrOpAfter = 5;
201 final VlanTagRewrite before = generateVlanTagRewrite(6);
202 final VlanTagRewrite after = generateVlanTagRewrite(vtrOpAfter);
203 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
205 whenL2InterfaceVlanTagRewriteThenFailure();
208 customizer.updateCurrentAttributes(id, before, after, writeContext);
209 } catch (WriteFailedException.UpdateFailedException e) {
210 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
211 verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOpAfter));
214 fail("WriteFailedException.UpdateFailedException was expected");
218 public void testDelete() throws Exception {
219 final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
220 builder.setRewriteOperation(TagRewriteOperation.Disabled);
221 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
223 whenL2InterfaceVlanTagRewriteThenSuccess();
225 customizer.deleteCurrentAttributes(id, builder.build(), writeContext);
227 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
231 public void testDeleteFailed() throws Exception {
232 final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
233 builder.setRewriteOperation(TagRewriteOperation.Disabled);
234 final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
236 whenL2InterfaceVlanTagRewriteThenFailure();
239 customizer.deleteCurrentAttributes(id, builder.build(), writeContext);
240 } catch (WriteFailedException.DeleteFailedException e) {
241 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
242 verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
245 fail("WriteFailedException.DeleteFailedException was expected");