ae768145ae9fbe136c78846665d524e1a97fdb1f
[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 org.junit.Assert.assertEquals;
20 import static org.junit.Assert.fail;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
27 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
28 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
29 import io.fd.honeycomb.v3po.translate.write.WriteContext;
30 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
31 import java.util.concurrent.CompletableFuture;
32 import java.util.concurrent.ExecutionException;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mock;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.TagRewriteOperation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanTag;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.L2;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.l2.VlanTagRewrite;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.l2.VlanTagRewriteBuilder;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewrite;
49 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
50 import org.openvpp.jvpp.future.FutureJVpp;
51
52 public class VlanTagRewriteCustomizerTest {
53
54     @Mock
55     private FutureJVpp api;
56     @Mock
57     private WriteContext writeContext;
58
59     private NamingContext namingContext;
60     private VlanTagRewriteCustomizer customizer;
61
62     public static final String VLAN_IF_NAME = "local0.0";
63     public static final int VLAN_IF_ID = 1;
64
65     @Before
66     public void setUp() throws Exception {
67         initMocks(this);
68         namingContext = new NamingContext("generatedSubInterfaceName");
69         customizer = new VlanTagRewriteCustomizer(api, namingContext);
70         namingContext.addName(VLAN_IF_ID, VLAN_IF_NAME);
71     }
72
73     private InstanceIdentifier<VlanTagRewrite> getVlanTagRewriteId(final String name) {
74         return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
75                 VppInterfaceAugmentation.class).child(L2.class).child(VlanTagRewrite.class);
76     }
77
78     private VlanTagRewrite generateVlanTagRewrite(final int vtrOp) {
79         final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
80         builder.setRewriteOperation(TagRewriteOperation.forValue(vtrOp));
81         builder.setTag1(new VlanTag(100));
82         builder.setTag2(new VlanTag(200));
83         builder.setFirstPushed(VlanType._802dot1ad);
84         return builder.build();
85     }
86
87     private L2InterfaceVlanTagRewrite generateL2InterfaceVlanTagRewrite(final int superIfId, final int vtrOp) {
88         final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
89         request.swIfIndex = superIfId;
90         request.vtrOp = vtrOp;
91         request.pushDot1Q = 0;
92         request.tag1 = 100;
93         request.tag2 = 200;
94         return request;
95     }
96
97     private void whenL2InterfaceVlanTagRewriteThen(final int retval) throws ExecutionException, InterruptedException {
98         final CompletableFuture<L2InterfaceVlanTagRewriteReply> replyFuture = new CompletableFuture<>();
99         final L2InterfaceVlanTagRewriteReply reply = new L2InterfaceVlanTagRewriteReply();
100         reply.retval = retval;
101         replyFuture.complete(reply);
102         doReturn(replyFuture).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
103     }
104
105     private void whenL2InterfaceVlanTagRewriteThenSuccess() throws ExecutionException, InterruptedException {
106         whenL2InterfaceVlanTagRewriteThen(0);
107     }
108
109     private void whenL2InterfaceVlanTagRewriteThenFailure() throws ExecutionException, InterruptedException {
110         whenL2InterfaceVlanTagRewriteThen(-1);
111     }
112
113     private void verifyL2InterfaceVlanTagRewriteWasInvoked(final L2InterfaceVlanTagRewrite expected) {
114         ArgumentCaptor<L2InterfaceVlanTagRewrite> argumentCaptor = ArgumentCaptor.forClass(L2InterfaceVlanTagRewrite.class);
115         verify(api).l2InterfaceVlanTagRewrite(argumentCaptor.capture());
116         final L2InterfaceVlanTagRewrite actual = argumentCaptor.getValue();
117         assertEquals(expected.swIfIndex, actual.swIfIndex);
118         assertEquals(expected.vtrOp, actual.vtrOp);
119         assertEquals(expected.pushDot1Q, actual.pushDot1Q);
120         assertEquals(expected.tag1, actual.tag1);
121         assertEquals(expected.tag2, actual.tag2);
122     }
123
124     private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() {
125         final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
126         request.swIfIndex = VLAN_IF_ID;
127         verifyL2InterfaceVlanTagRewriteWasInvoked(request);
128     }
129
130     @Test
131     public void testCreate() throws Exception {
132         final int vtrOp = 6;
133         final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
134         final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
135
136         whenL2InterfaceVlanTagRewriteThenSuccess();
137
138         customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
139
140         verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOp));
141     }
142
143     @Test
144     public void testCreateFailed() throws Exception {
145         final int vtrOp = 6;
146         final VlanTagRewrite vlanTagRewrite = generateVlanTagRewrite(vtrOp);
147         final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
148
149         whenL2InterfaceVlanTagRewriteThenFailure();
150
151         try {
152             customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
153         } catch (WriteFailedException.CreateFailedException e) {
154             assertEquals(VppApiInvocationException.class, e.getCause().getClass());
155             verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOp));
156             return;
157         }
158         fail("WriteFailedException.CreateFailedException was expected");
159     }
160
161     @Test
162     public void testUpdateNoChange() throws Exception {
163         final VlanTagRewrite before = generateVlanTagRewrite(6);
164         final VlanTagRewrite after = generateVlanTagRewrite(6);
165         customizer.updateCurrentAttributes(null, before, after, writeContext);
166         verify(api, never()).l2InterfaceVlanTagRewrite(any());
167     }
168
169     @Test
170     public void testUpdate() throws Exception {
171         final int vtrOpAfter = 5;
172         final VlanTagRewrite before = generateVlanTagRewrite(6);
173         final VlanTagRewrite after = generateVlanTagRewrite(vtrOpAfter);
174         final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
175
176         whenL2InterfaceVlanTagRewriteThenSuccess();
177
178         customizer.updateCurrentAttributes(id, before, after, writeContext);
179
180         verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOpAfter));
181     }
182
183     @Test
184     public void testUpdateFailed() throws Exception {
185         final int vtrOpAfter = 5;
186         final VlanTagRewrite before = generateVlanTagRewrite(6);
187         final VlanTagRewrite after = generateVlanTagRewrite(vtrOpAfter);
188         final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
189
190         whenL2InterfaceVlanTagRewriteThenFailure();
191
192         try {
193             customizer.updateCurrentAttributes(id, before, after, writeContext);
194         } catch (WriteFailedException.UpdateFailedException e) {
195             assertEquals(VppApiInvocationException.class, e.getCause().getClass());
196             verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_ID, vtrOpAfter));
197             return;
198         }
199         fail("WriteFailedException.UpdateFailedException was expected");
200     }
201
202     @Test
203     public void testDelete() throws Exception {
204         final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
205         builder.setRewriteOperation(TagRewriteOperation.Disabled);
206         final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
207
208         whenL2InterfaceVlanTagRewriteThenSuccess();
209
210         customizer.deleteCurrentAttributes(id, builder.build(), writeContext);
211
212         verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
213     }
214
215     @Test
216     public void testDeleteFailed() throws Exception {
217         final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
218         builder.setRewriteOperation(TagRewriteOperation.Disabled);
219         final InstanceIdentifier<VlanTagRewrite> id = getVlanTagRewriteId(VLAN_IF_NAME);
220
221         whenL2InterfaceVlanTagRewriteThenFailure();
222
223         try {
224             customizer.deleteCurrentAttributes(id, builder.build(), writeContext);
225         } catch (WriteFailedException.DeleteFailedException e) {
226             assertEquals(VppApiInvocationException.class, e.getCause().getClass());
227             verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
228             return;
229         }
230         fail("WriteFailedException.DeleteFailedException was expected");
231     }
232 }