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