6392579140a7baac076071991f07486cf81ab8e6
[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 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;
28
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;
57
58 public class VlanTagRewriteCustomizerTest {
59
60     @Mock
61     private FutureJVpp api;
62     @Mock
63     private WriteContext writeContext;
64     @Mock
65     private MappingContext mappingContext;
66
67     private NamingContext namingContext;
68     private VlanTagRewriteCustomizer customizer;
69
70     public static final String VLAN_IF_NAME = "local0.0";
71     public static final int VLAN_IF_ID = 1;
72
73     @Before
74     public void setUp() throws Exception {
75         initMocks(this);
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"));
80     }
81
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);
85     }
86
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();
94     }
95
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;
101         request.tag1 = 100;
102         request.tag2 = 200;
103         return request;
104     }
105
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));
112     }
113
114     private void whenL2InterfaceVlanTagRewriteThenSuccess() throws ExecutionException, InterruptedException {
115         whenL2InterfaceVlanTagRewriteThen(0);
116     }
117
118     private void whenL2InterfaceVlanTagRewriteThenFailure() throws ExecutionException, InterruptedException {
119         whenL2InterfaceVlanTagRewriteThen(-1);
120     }
121
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);
131     }
132
133     private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() {
134         final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
135         request.swIfIndex = VLAN_IF_ID;
136         verifyL2InterfaceVlanTagRewriteWasInvoked(request);
137     }
138
139     @Test
140     public void testCreate() throws Exception {
141         final int vtrOp = 6;
142         final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
143         final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
144
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));
149
150         customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
151
152         verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOp));
153     }
154
155     @Test
156     public void testCreateFailed() throws Exception {
157         final int vtrOp = 6;
158         final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
159         final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
160
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));
165
166         try {
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));
171             return;
172         }
173         fail("WriteFailedException.CreateFailedException was expected");
174     }
175
176     @Test
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());
182     }
183
184     @Test
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);
190
191         whenL2InterfaceVlanTagRewriteThenSuccess();
192
193         customizer.updateCurrentAttributes(id, before, after, writeContext);
194
195         verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOpAfter));
196     }
197
198     @Test
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);
204
205         whenL2InterfaceVlanTagRewriteThenFailure();
206
207         try {
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));
212             return;
213         }
214         fail("WriteFailedException.UpdateFailedException was expected");
215     }
216
217     @Test
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);
222
223         whenL2InterfaceVlanTagRewriteThenSuccess();
224
225         customizer.deleteCurrentAttributes(id, builder.build(), writeContext);
226
227         verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
228     }
229
230     @Test
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);
235
236         whenL2InterfaceVlanTagRewriteThenFailure();
237
238         try {
239             customizer.deleteCurrentAttributes(id, builder.build(), writeContext);
240         } catch (WriteFailedException.DeleteFailedException e) {
241             assertEquals(VppApiInvocationException.class, e.getCause().getClass());
242             verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
243             return;
244         }
245         fail("WriteFailedException.DeleteFailedException was expected");
246     }
247 }