655bda525e7721edce0c2e5f6234be9deb6b8a62
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.v3po.translate.v3po.interfaces;
18
19 import com.google.common.base.Optional;
20 import io.fd.honeycomb.v3po.translate.MappingContext;
21 import io.fd.honeycomb.v3po.translate.v3po.test.TestHelperUtils;
22 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
23 import io.fd.honeycomb.v3po.translate.v3po.util.TagRewriteOperation;
24 import io.fd.honeycomb.v3po.translate.write.WriteContext;
25 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.Mock;
31 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.*;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1q;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.Rewrite;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.RewriteBuilder;
43 import org.opendaylight.yangtools.yang.binding.ChildOf;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.openvpp.jvpp.VppBaseCallException;
46 import org.openvpp.jvpp.VppInvocationException;
47 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewrite;
48 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
49 import org.openvpp.jvpp.future.FutureJVpp;
50
51 import java.util.concurrent.CompletableFuture;
52 import java.util.concurrent.ExecutionException;
53
54 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
55 import static junit.framework.TestCase.assertTrue;
56 import static org.junit.Assert.assertEquals;
57 import static org.junit.Assert.fail;
58 import static org.mockito.Matchers.any;
59 import static org.mockito.Mockito.doReturn;
60 import static org.mockito.Mockito.verify;
61 import static org.mockito.MockitoAnnotations.initMocks;
62
63 public class RewriteCustomizerTest {
64
65     @Mock
66     private FutureJVpp api;
67     @Mock
68     private WriteContext writeContext;
69     @Mock
70     private MappingContext mappingContext;
71
72     private NamingContext namingContext;
73     private RewriteCustomizer customizer;
74
75     public static final String VLAN_IF_NAME = "local0.1";
76     public static final int VLAN_IF_ID = 1;
77     public static final int VLAN_IF_INDEX = 11;
78
79     @Before
80     public void setUp() throws Exception {
81         initMocks(this);
82         namingContext = new NamingContext("generatedSubInterfaceName", "test-instance");
83         doReturn(mappingContext).when(writeContext).getMappingContext();
84         customizer = new RewriteCustomizer(api, namingContext);
85
86         final Optional<Mapping> ifcMapping = getMapping(VLAN_IF_NAME, VLAN_IF_INDEX);
87         doReturn(ifcMapping).when(mappingContext).read(any());
88     }
89
90     private InstanceIdentifier<Rewrite> getVlanTagRewriteId(final String name, final long index) {
91         final Class<ChildOf<? super SubInterface>> child = (Class)Rewrite.class;
92         final InstanceIdentifier id =
93                 InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
94                         SubinterfaceAugmentation.class).child(SubInterfaces.class)
95                         .child(SubInterface.class, new SubInterfaceKey(index))
96                         .child(child);
97         return id;
98     }
99
100     private Rewrite generateRewrite(final TagRewriteOperation op) {
101         final RewriteBuilder builder = new RewriteBuilder();
102         builder.setPopTags((short) op.getPopTags());
103         builder.setVlanType(_802dot1q.class);
104         return builder.build();
105     }
106
107     private L2InterfaceVlanTagRewrite generateL2InterfaceVlanTagRewrite(final int swIfIndex, final TagRewriteOperation op) {
108         final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
109         request.swIfIndex = swIfIndex;
110         request.vtrOp = op.ordinal();
111         request.pushDot1Q = 1;
112         return request;
113     }
114
115     /**
116      * Positive response
117      */
118     private void whenL2InterfaceVlanTagRewriteThen() throws ExecutionException, InterruptedException, VppInvocationException {
119         final CompletableFuture<L2InterfaceVlanTagRewriteReply> replyFuture = new CompletableFuture<>();
120         final L2InterfaceVlanTagRewriteReply reply = new L2InterfaceVlanTagRewriteReply();
121         replyFuture.complete(reply);
122         doReturn(replyFuture).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
123     }
124
125     /**
126      * Failure response send
127      */
128     private void whenL2InterfaceVlanTagRewriteFailedThen(final int retval) throws ExecutionException, InterruptedException, VppInvocationException {
129         doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException(retval)).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
130     }
131
132     private void whenL2InterfaceVlanTagRewriteThenSuccess() throws ExecutionException, InterruptedException, VppInvocationException {
133         whenL2InterfaceVlanTagRewriteThen();
134     }
135
136     private void whenL2InterfaceVlanTagRewriteThenFailure() throws ExecutionException, InterruptedException, VppInvocationException {
137         whenL2InterfaceVlanTagRewriteFailedThen(-1);
138     }
139
140     private void verifyL2InterfaceVlanTagRewriteWasInvoked(final L2InterfaceVlanTagRewrite expected) throws VppInvocationException {
141         ArgumentCaptor<L2InterfaceVlanTagRewrite> argumentCaptor = ArgumentCaptor.forClass(L2InterfaceVlanTagRewrite.class);
142         verify(api).l2InterfaceVlanTagRewrite(argumentCaptor.capture());
143         final L2InterfaceVlanTagRewrite actual = argumentCaptor.getValue();
144         assertEquals(expected.swIfIndex, actual.swIfIndex);
145         assertEquals(expected.vtrOp, actual.vtrOp);
146         assertEquals(expected.pushDot1Q, actual.pushDot1Q);
147         assertEquals(expected.tag1, actual.tag1);
148         assertEquals(expected.tag2, actual.tag2);
149     }
150
151     private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() throws VppInvocationException {
152         final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
153         request.swIfIndex = VLAN_IF_INDEX;
154         verifyL2InterfaceVlanTagRewriteWasInvoked(request);
155     }
156
157     @Test
158     public void testCreate() throws Exception {
159         final TagRewriteOperation op = TagRewriteOperation.pop_2;
160         final Rewrite vlanTagRewrite = generateRewrite(op);
161         final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
162
163         whenL2InterfaceVlanTagRewriteThenSuccess();
164
165         customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
166
167         verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
168     }
169
170     @Test
171     public void testCreateFailed() throws Exception {
172         final TagRewriteOperation op = TagRewriteOperation.pop_2;
173         final Rewrite vlanTagRewrite = generateRewrite(op);
174         final String subIfaceName = "local0.11";
175         final int subifIndex = 1;
176         final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(subIfaceName, subifIndex);
177
178         whenL2InterfaceVlanTagRewriteThenFailure();
179
180         try {
181             customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
182         } catch (WriteFailedException.CreateFailedException e) {
183             assertTrue(e.getCause() instanceof VppBaseCallException);
184             verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
185             return;
186         }
187         fail("WriteFailedException.CreateFailedException was expected");
188     }
189
190     @Test
191     public void testUpdate() throws Exception {
192         final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
193         final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
194         final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
195
196         whenL2InterfaceVlanTagRewriteThenSuccess();
197
198         customizer.updateCurrentAttributes(id, before, after, writeContext);
199
200         verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
201     }
202
203     @Test
204     public void testUpdateFailed() throws Exception {
205         final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
206         final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
207         final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
208
209         whenL2InterfaceVlanTagRewriteThenFailure();
210
211         try {
212             customizer.updateCurrentAttributes(id, before, after, writeContext);
213         } catch (WriteFailedException.UpdateFailedException e) {
214             assertTrue(e.getCause() instanceof VppBaseCallException);
215             verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
216             return;
217         }
218         fail("WriteFailedException.UpdateFailedException was expected");
219     }
220
221     @Test
222     public void testDelete() throws Exception {
223         final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
224
225         whenL2InterfaceVlanTagRewriteThenSuccess();
226
227         customizer.deleteCurrentAttributes(id, null, writeContext);
228
229         verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
230     }
231
232     @Test
233     public void testDeleteFailed() throws Exception {
234         final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
235
236         whenL2InterfaceVlanTagRewriteThenFailure();
237
238         try {
239             customizer.deleteCurrentAttributes(id, null, writeContext);
240         } catch (WriteFailedException.DeleteFailedException e) {
241             Assert.assertTrue(e.getCause() instanceof VppBaseCallException);
242             verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
243             return;
244         }
245         fail("WriteFailedException.DeleteFailedException was expected");
246     }
247 }