HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfacesstate / ip / Ipv4AddressCustomizerTest.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.interfacesstate.ip;
18
19
20 import static org.hamcrest.Matchers.empty;
21 import static org.hamcrest.Matchers.hasSize;
22 import static org.hamcrest.Matchers.is;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertThat;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.when;
27
28 import com.google.common.collect.ImmutableList;
29 import com.google.common.collect.ImmutableSet;
30 import io.fd.honeycomb.translate.read.ReadFailedException;
31 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
32 import io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory;
33 import io.fd.honeycomb.translate.util.read.cache.IdentifierCacheKeyFactory;
34 import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
35 import io.fd.honeycomb.translate.vpp.util.NamingContext;
36 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
37 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
38 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
39 import io.fd.vpp.jvpp.core.dto.IpAddressDump;
40 import java.util.Arrays;
41 import java.util.List;
42 import java.util.concurrent.CompletableFuture;
43 import java.util.stream.Collectors;
44 import org.hamcrest.Matchers;
45 import org.junit.Test;
46 import org.mockito.Mockito;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.address.subnet.PrefixLength;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
60
61 public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder> implements
62         Ipv4Translator {
63
64     private static final String IFACE_NAME = "eth0";
65     private static final String IFACE_2_NAME = "eth1";
66     private static final int IFACE_ID = 1;
67     private static final int IFACE_2_ID = 2;
68     private static final String IFC_CTX_NAME = "ifc-test-instance";
69
70     private NamingContext interfacesContext;
71     private InstanceIdentifier<Address> ifaceOneAddressOneIdentifier;
72     private InstanceIdentifier<Address> ifaceTwoAddressOneIdentifier;
73     private CacheKeyFactory cacheKeyFactory;
74
75     public Ipv4AddressCustomizerTest() {
76         super(Address.class, Ipv4Builder.class);
77     }
78
79     @Override
80     public void setUp() {
81         interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
82         defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
83         defineMapping(mappingContext, IFACE_2_NAME, IFACE_2_ID, IFC_CTX_NAME);
84
85         ifaceOneAddressOneIdentifier =
86                 InstanceIdentifier.create(InterfacesState.class)
87                         .child(Interface.class, new InterfaceKey(IFACE_NAME))
88                         .augmentation(Interface2.class)
89                         .child(Ipv4.class)
90                         .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1")));
91         ifaceTwoAddressOneIdentifier =
92                 InstanceIdentifier.create(InterfacesState.class)
93                         .child(Interface.class, new InterfaceKey(IFACE_2_NAME))
94                         .augmentation(Interface2.class)
95                         .child(Ipv4.class)
96                         .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1")));
97
98         // to simulate complex key
99         cacheKeyFactory = new IdentifierCacheKeyFactory(ImmutableSet.of(Interface.class));
100     }
101
102     @Override
103     protected ReaderCustomizer<Address, AddressBuilder> initCustomizer() {
104         return new Ipv4AddressCustomizer(api, interfacesContext);
105     }
106
107     private static InstanceIdentifier<Address> getId(final String address, final String ifaceName) {
108         return InstanceIdentifier.builder(InterfacesState.class)
109                 .child(Interface.class, new InterfaceKey(ifaceName))
110                 .augmentation(Interface2.class)
111                 .child(Ipv4.class)
112                 .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address))))
113                 .build();
114     }
115
116     @Test
117     public void testReadCurrentAttributesFor2Ifcs() throws ReadFailedException {
118         //changed to mock to not store first dumped data(otherwise that double thenReturn on line 118 is not gonna work)
119
120         IpAddressDetails detail1 = new IpAddressDetails();
121         IpAddressDetails detail2 = new IpAddressDetails();
122
123         detail1.ip = reverseBytes(
124                 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
125         detail2.ip = reverseBytes(
126                 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
127
128         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
129         reply.ipAddressDetails = ImmutableList.of(detail1);
130         IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump();
131         reply2.ipAddressDetails = ImmutableList.of(detail2);
132
133         CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
134         future.complete(reply);
135         CompletableFuture<IpAddressDetailsReplyDump> future2 = new CompletableFuture<>();
136         future2.complete(reply2);
137
138         when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future).thenReturn(future2)
139                 .thenReturn(future).thenReturn(future2);
140         when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)).thenReturn(future(reply2))
141                 .thenReturn(future(reply)).thenReturn(future(reply2));
142         when(ctx.getModificationCache()).thenReturn(cache);
143
144
145         final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
146         final InstanceIdentifier<Address> id2 = getId("192.168.2.2", IFACE_2_NAME);
147
148         final List<AddressKey> ifc1Ids = getCustomizer().getAllIds(id, ctx);
149         assertThat(ifc1Ids.size(), is(1));
150         assertThat(ifc1Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))));
151         final List<AddressKey> ifc2Ids = getCustomizer().getAllIds(id2, ctx);
152         assertThat(ifc2Ids.size(), is(1));
153         assertThat(ifc2Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.2"))));
154
155         AddressBuilder builder = new AddressBuilder();
156         getCustomizer().readCurrentAttributes(id, builder, ctx);
157         assertEquals(builder.getIp().getValue(), "192.168.2.1");
158         builder = new AddressBuilder();
159         getCustomizer().readCurrentAttributes(id2, builder, ctx);
160         assertEquals(builder.getIp().getValue(), "192.168.2.2");
161     }
162
163     @Test
164     public void testReadCurrentAttributesSuccessfull() throws ReadFailedException {
165         IpAddressDetails detail1 = new IpAddressDetails();
166         IpAddressDetails detail2 = new IpAddressDetails();
167         IpAddressDetails detail3 = new IpAddressDetails();
168
169         detail1.ip = reverseBytes(
170                 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
171         detail2.ip = reverseBytes(
172                 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
173         detail3.ip = reverseBytes(
174                 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
175
176         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
177         reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
178         when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply));
179         when(ctx.getModificationCache()).thenReturn(cache);
180
181         final AddressBuilder builder = new AddressBuilder();
182         final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
183
184         getCustomizer().readCurrentAttributes(id, builder, ctx);
185
186         assertEquals("192.168.2.1", builder.getIp().getValue());
187     }
188
189     @Test
190     public void testGetAllIdsFromSuccessfull() throws ReadFailedException {
191         IpAddressDetails detail1 = new IpAddressDetails();
192         IpAddressDetails detail2 = new IpAddressDetails();
193         IpAddressDetails detail3 = new IpAddressDetails();
194
195         detail1.ip = reverseBytes(
196                 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
197         detail2.ip = reverseBytes(
198                 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
199         detail3.ip = reverseBytes(
200                 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
201
202         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
203         reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
204         when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply));
205         when(ctx.getModificationCache()).thenReturn(cache);
206
207         final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
208
209         List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
210                 .map(key -> key.getIp())
211                 .collect(Collectors.toList());
212
213         assertEquals(3, ids.size());
214         assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue()));
215         assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue()));
216         assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue()));
217     }
218
219     @Test
220     public void testCachingScopeSpecificRequest() throws ReadFailedException {
221         fillCacheForTwoIfaces();
222         final AddressBuilder ifaceOneAddressBuilder = new AddressBuilder();
223         final AddressBuilder ifaceTwoAddressBuilder = new AddressBuilder();
224
225         getCustomizer().readCurrentAttributes(ifaceOneAddressOneIdentifier, ifaceOneAddressBuilder, ctx);
226         getCustomizer().readCurrentAttributes(ifaceTwoAddressOneIdentifier, ifaceTwoAddressBuilder, ctx);
227
228         // addresses have caching scope of parent interface, so returned address should have respective prefix lengths
229         assertEquals("192.168.2.1", ifaceOneAddressBuilder.getIp().getValue());
230         assertTrue(ifaceOneAddressBuilder.getSubnet() instanceof PrefixLength);
231         assertEquals(22, PrefixLength.class.cast(ifaceOneAddressBuilder.getSubnet()).getPrefixLength().intValue());
232
233         assertEquals("192.168.2.1", ifaceTwoAddressBuilder.getIp().getValue());
234         assertTrue(ifaceTwoAddressBuilder.getSubnet() instanceof PrefixLength);
235         assertEquals(23, PrefixLength.class.cast(ifaceTwoAddressBuilder.getSubnet()).getPrefixLength().intValue());
236     }
237
238     @Test
239     public void testCachingScopeGetAll() throws ReadFailedException {
240         fillCacheForFirstIfaceSecondEmpty();
241
242         final List<AddressKey> keysForIfaceOne = getCustomizer().getAllIds(ifaceOneAddressOneIdentifier, ctx);
243         assertThat(keysForIfaceOne, hasSize(1));
244         final AddressKey keyIfaceOne = keysForIfaceOne.get(0);
245         assertEquals("192.168.2.1", keyIfaceOne.getIp().getValue());
246
247         final List<AddressKey> keysForIfaceTwo = getCustomizer().getAllIds(ifaceTwoAddressOneIdentifier, ctx);
248         assertThat(keysForIfaceTwo, is(empty()));
249     }
250
251     private void fillCacheForTwoIfaces() {
252         IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails();
253         IpAddressDetails detailIfaceTwoAddressOne = new IpAddressDetails();
254         IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump();
255         IpAddressDetailsReplyDump replyIfaceTwo = new IpAddressDetailsReplyDump();
256
257         replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne);
258         replyIfaceTwo.ipAddressDetails = Arrays.asList(detailIfaceTwoAddressOne);
259
260         detailIfaceOneAddressOne.ip = reverseBytes(
261                 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
262         detailIfaceOneAddressOne.prefixLength = 22;
263
264         detailIfaceTwoAddressOne.ip = reverseBytes(
265                 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
266         detailIfaceTwoAddressOne.prefixLength = 23;
267
268         cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne);
269         cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), replyIfaceTwo);
270     }
271
272     private void fillCacheForFirstIfaceSecondEmpty() {
273         IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails();
274         IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump();
275         replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne);
276
277         detailIfaceOneAddressOne.ip = reverseBytes(
278                 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
279         detailIfaceOneAddressOne.prefixLength = 22;
280
281         cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne);
282         cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), new IpAddressDetailsReplyDump());
283     }
284 }