3cedc4f71760642ae29e577a8e651dfbbd6d7701
[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.vpp.util.NamingContext;
26 import io.fd.honeycomb.translate.vpp.util.TagRewriteOperation;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
29 import io.fd.vpp.jvpp.VppBaseCallException;
30 import io.fd.vpp.jvpp.VppInvocationException;
31 import io.fd.vpp.jvpp.core.dto.L2InterfaceVlanTagRewrite;
32 import io.fd.vpp.jvpp.core.dto.L2InterfaceVlanTagRewriteReply;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubinterfaceAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1q;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.Rewrite;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.RewriteBuilder;
45 import org.opendaylight.yangtools.yang.binding.ChildOf;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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 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 e) {
167             assertTrue(e.getCause() instanceof VppBaseCallException);
168             verify(api)
169                     .l2InterfaceVlanTagRewrite(
170                             generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
171             return;
172         }
173         fail("WriteFailedException.UpdateFailedException was expected");
174     }
175
176     @Test
177     public void testDelete() throws Exception {
178         whenL2InterfaceVlanTagRewriteThenSuccess();
179
180         customizer.deleteCurrentAttributes(VLAN_IID, null, writeContext);
181
182         verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
183     }
184
185     @Test
186     public void testDeleteFailed() throws Exception {
187         whenL2InterfaceVlanTagRewriteThenFailure();
188
189         try {
190             customizer.deleteCurrentAttributes(VLAN_IID, null, writeContext);
191         } catch (WriteFailedException e) {
192             Assert.assertTrue(e.getCause() instanceof VppBaseCallException);
193             verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
194             return;
195         }
196         fail("WriteFailedException.DeleteFailedException was expected");
197     }
198 }