HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / GreCustomizerTest.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 org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28
29 import com.google.common.net.InetAddresses;
30 import io.fd.honeycomb.translate.vpp.util.NamingContext;
31 import io.fd.honeycomb.translate.write.WriteFailedException;
32 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
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.rev161214.VppInterfaceAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Gre;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.GreBuilder;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import io.fd.vpp.jvpp.VppBaseCallException;
45 import io.fd.vpp.jvpp.VppInvocationException;
46 import io.fd.vpp.jvpp.core.dto.GreAddDelTunnel;
47 import io.fd.vpp.jvpp.core.dto.GreAddDelTunnelReply;
48
49 public class GreCustomizerTest extends WriterCustomizerTest {
50
51     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
52     private final String IFACE_NAME = "eth0";
53     private final int IFACE_ID = 1;
54     private InstanceIdentifier<Gre> id = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
55         .augmentation(VppInterfaceAugmentation.class).child(Gre.class);
56     private static final byte ADD_GRE = 1;
57     private static final byte DEL_GRE = 0;
58
59     private GreCustomizer customizer;
60
61     @Override
62     public void setUp() throws Exception {
63         InterfaceTypeTestUtils.setupWriteContext(writeContext,
64             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.GreTunnel.class);
65         customizer = new GreCustomizer(api, new NamingContext("generateInterfaceNAme", IFC_TEST_INSTANCE));
66     }
67
68     private void whenGreAddDelTunnelThenSuccess() {
69         final GreAddDelTunnelReply reply = new GreAddDelTunnelReply();
70         reply.swIfIndex = IFACE_ID;
71         doReturn(future(reply)).when(api).greAddDelTunnel(any(GreAddDelTunnel.class));
72     }
73
74     private void whenGreAddDelTunnelThenFailure() {
75         doReturn(failedFuture()).when(api).greAddDelTunnel(any(GreAddDelTunnel.class));
76     }
77
78     private GreAddDelTunnel verifyGreAddDelTunnelWasInvoked(final Gre gre) throws VppInvocationException {
79         ArgumentCaptor<GreAddDelTunnel> argumentCaptor = ArgumentCaptor.forClass(GreAddDelTunnel.class);
80         verify(api).greAddDelTunnel(argumentCaptor.capture());
81         final GreAddDelTunnel actual = argumentCaptor.getValue();
82         assertEquals(0, actual.isIpv6);
83         assertArrayEquals(InetAddresses.forString(gre.getSrc().getIpv4Address().getValue()).getAddress(),
84                 actual.srcAddress);
85         assertArrayEquals(InetAddresses.forString(gre.getDst().getIpv4Address().getValue()).getAddress(),
86                 actual.dstAddress);
87         assertEquals(gre.getOuterFibId().intValue(), actual.outerFibId);
88         return actual;
89     }
90
91     private void verifyGreAddWasInvoked(final Gre gre) throws VppInvocationException {
92         final GreAddDelTunnel actual = verifyGreAddDelTunnelWasInvoked(gre);
93         assertEquals(ADD_GRE, actual.isAdd);
94     }
95
96     private void verifyGreDeleteWasInvoked(final Gre gre) throws VppInvocationException {
97         final GreAddDelTunnel actual = verifyGreAddDelTunnelWasInvoked(gre);
98         assertEquals(DEL_GRE, actual.isAdd);
99     }
100
101     private static Gre generateGre() {
102         final GreBuilder builder = new GreBuilder();
103         builder.setSrc(new IpAddress(new Ipv4Address("192.168.20.10")));
104         builder.setDst(new IpAddress(new Ipv4Address("192.168.20.11")));
105         builder.setOuterFibId(Long.valueOf(123));
106         return builder.build();
107     }
108
109     @Test
110     public void testWriteCurrentAttributes() throws Exception {
111         final Gre gre = generateGre();
112
113         whenGreAddDelTunnelThenSuccess();
114
115         noMappingDefined(mappingContext, IFACE_NAME, IFC_TEST_INSTANCE);
116
117         customizer.writeCurrentAttributes(id, gre, writeContext);
118         verifyGreAddWasInvoked(gre);
119         verify(mappingContext).put(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)),
120             eq(mapping(IFACE_NAME, IFACE_ID).get()));
121     }
122
123     @Test
124     public void testWriteCurrentAttributesMappingAlreadyPresent() throws Exception {
125         final Gre gre = generateGre();
126
127         whenGreAddDelTunnelThenSuccess();
128         defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_TEST_INSTANCE);
129
130         customizer.writeCurrentAttributes(id, gre, writeContext);
131         verifyGreAddWasInvoked(gre);
132
133         // Remove the first mapping before putting in the new one
134         verify(mappingContext).delete(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)));
135         verify(mappingContext).put(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)), eq(mapping(IFACE_NAME, IFACE_ID).get()));
136     }
137
138     @Test
139     public void testWriteCurrentAttributesFailed() throws Exception {
140         final Gre gre = generateGre();
141
142         whenGreAddDelTunnelThenFailure();
143
144         try {
145             customizer.writeCurrentAttributes(id, gre, writeContext);
146         } catch (WriteFailedException.CreateFailedException e) {
147             assertTrue(e.getCause() instanceof VppBaseCallException);
148             verifyGreAddWasInvoked(gre);
149             // Mapping not stored due to failure
150             verify(mappingContext, times(0)).put(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)), eq(mapping(
151                 IFACE_NAME, 0).get()));
152             return;
153         }
154         fail("WriteFailedException.CreateFailedException was expected");
155     }
156
157     @Test
158     public void testUpdateCurrentAttributes() throws Exception {
159         try {
160             customizer.updateCurrentAttributes(id, generateGre(), generateGre(), writeContext);
161         } catch (WriteFailedException.UpdateFailedException e) {
162             assertEquals(UnsupportedOperationException.class, e.getCause().getClass());
163             return;
164         }
165         fail("WriteFailedException.UpdateFailedException was expected");
166     }
167
168     @Test
169     public void testDeleteCurrentAttributes() throws Exception {
170         final Gre gre = generateGre();
171
172         whenGreAddDelTunnelThenSuccess();
173         defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_TEST_INSTANCE);
174
175         customizer.deleteCurrentAttributes(id, gre, writeContext);
176         verifyGreDeleteWasInvoked(gre);
177         verify(mappingContext).delete(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)));
178     }
179
180     @Test
181     public void testDeleteCurrentAttributesaFailed() throws Exception {
182         final Gre gre = generateGre();
183
184         whenGreAddDelTunnelThenFailure();
185         defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_TEST_INSTANCE);
186
187         try {
188             customizer.deleteCurrentAttributes(id, gre, writeContext);
189         } catch (WriteFailedException.DeleteFailedException e) {
190             assertTrue(e.getCause() instanceof VppBaseCallException);
191             verifyGreDeleteWasInvoked(gre);
192             verify(mappingContext, times(0)).delete(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)));
193             return;
194         }
195         fail("WriteFailedException.DeleteFailedException was expected");
196     }
197 }