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