HONEYCOMB-195: increase v3po coverage over 80%. WIP
[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.fail;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.verify;
24
25 import io.fd.honeycomb.translate.v3po.util.NamingContext;
26 import io.fd.honeycomb.translate.v3po.util.TagRewriteOperation;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1q;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.Rewrite;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.RewriteBuilder;
41 import org.opendaylight.yangtools.yang.binding.ChildOf;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.openvpp.jvpp.VppBaseCallException;
44 import org.openvpp.jvpp.VppInvocationException;
45 import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewrite;
46 import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewriteReply;
47
48 public class RewriteCustomizerTest extends WriterCustomizerTest {
49
50     private NamingContext namingContext;
51     private RewriteCustomizer customizer;
52
53     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
54     private static final String IF_NAME = "local0";
55     private static final String VLAN_IF_NAME = "local0.1";
56     private static final int VLAN_IF_ID = 1;
57     private static final int VLAN_IF_INDEX = 11;
58     private InstanceIdentifier<Rewrite> VLAN_IID;
59
60     @Override
61     public void setUp() throws Exception {
62         namingContext = new NamingContext("generatedSubInterfaceName", IFC_TEST_INSTANCE);
63         customizer = new RewriteCustomizer(api, namingContext);
64         VLAN_IID = getVlanTagRewriteId(IF_NAME, VLAN_IF_ID);
65         defineMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_TEST_INSTANCE);
66     }
67
68     private static InstanceIdentifier<Rewrite> getVlanTagRewriteId(final String name, final long index) {
69         final Class<ChildOf<? super SubInterface>> child = (Class) Rewrite.class;
70         final InstanceIdentifier id =
71             InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
72                 SubinterfaceAugmentation.class).child(SubInterfaces.class)
73                 .child(SubInterface.class, new SubInterfaceKey(index))
74                 .child(child);
75         return id;
76     }
77
78     private Rewrite generateRewrite(final TagRewriteOperation op) {
79         final RewriteBuilder builder = new RewriteBuilder();
80         builder.setPopTags((short) op.getPopTags());
81         builder.setVlanType(_802dot1q.class);
82         return builder.build();
83     }
84
85     private L2InterfaceVlanTagRewrite generateL2InterfaceVlanTagRewrite(final int swIfIndex,
86                                                                         final TagRewriteOperation op) {
87         final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
88         request.swIfIndex = swIfIndex;
89         request.vtrOp = op.ordinal();
90         request.pushDot1Q = 1;
91         return request;
92     }
93
94     /**
95      * Positive response
96      */
97     private void whenL2InterfaceVlanTagRewriteThenSuccess() {
98         doReturn(future(new L2InterfaceVlanTagRewriteReply())).when(api)
99             .l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
100     }
101
102     /**
103      * Failure response send
104      */
105     private void whenL2InterfaceVlanTagRewriteThenFailure() {
106         doReturn(failedFuture()).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
107     }
108
109     private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() throws VppInvocationException {
110         final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
111         request.swIfIndex = VLAN_IF_INDEX;
112         verify(api).l2InterfaceVlanTagRewrite(request);
113     }
114
115     @Test
116     public void testCreate() throws Exception {
117         final TagRewriteOperation op = TagRewriteOperation.pop_2;
118         final Rewrite vlanTagRewrite = generateRewrite(op);
119
120         whenL2InterfaceVlanTagRewriteThenSuccess();
121
122         customizer.writeCurrentAttributes(VLAN_IID, vlanTagRewrite, writeContext);
123
124         verify(api).l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
125     }
126
127     @Test
128     public void testCreateFailed() throws Exception {
129         final TagRewriteOperation op = TagRewriteOperation.pop_2;
130         final Rewrite vlanTagRewrite = generateRewrite(op);
131
132         whenL2InterfaceVlanTagRewriteThenFailure();
133
134         try {
135             customizer.writeCurrentAttributes(VLAN_IID, vlanTagRewrite, writeContext);
136         } catch (WriteFailedException.CreateFailedException e) {
137             assertTrue(e.getCause() instanceof VppBaseCallException);
138             verify(api).l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
139             return;
140         }
141         fail("WriteFailedException.CreateFailedException was expected");
142     }
143
144     @Test
145     public void testUpdate() throws Exception {
146         final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
147         final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
148
149         whenL2InterfaceVlanTagRewriteThenSuccess();
150
151         customizer.updateCurrentAttributes(VLAN_IID, before, after, writeContext);
152
153         verify(api)
154             .l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
155     }
156
157     @Test
158     public void testUpdateFailed() throws Exception {
159         final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
160         final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
161
162         whenL2InterfaceVlanTagRewriteThenFailure();
163
164         try {
165             customizer.updateCurrentAttributes(VLAN_IID, before, after, writeContext);
166         } catch (WriteFailedException.UpdateFailedException e) {
167             assertTrue(e.getCause() instanceof VppBaseCallException);
168             verify(api)
169                 .l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
170             return;
171         }
172         fail("WriteFailedException.UpdateFailedException was expected");
173     }
174
175     @Test
176     public void testDelete() throws Exception {
177         whenL2InterfaceVlanTagRewriteThenSuccess();
178
179         customizer.deleteCurrentAttributes(VLAN_IID, null, writeContext);
180
181         verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
182     }
183
184     @Test
185     public void testDeleteFailed() throws Exception {
186         whenL2InterfaceVlanTagRewriteThenFailure();
187
188         try {
189             customizer.deleteCurrentAttributes(VLAN_IID, null, writeContext);
190         } catch (WriteFailedException.DeleteFailedException e) {
191             Assert.assertTrue(e.getCause() instanceof VppBaseCallException);
192             verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
193             return;
194         }
195         fail("WriteFailedException.DeleteFailedException was expected");
196     }
197 }