e4814296d42b111b82beca1a23fadcb4e3e9d093
[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 import static io.fd.honeycomb.translate.v3po.test.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.translate.v3po.test.ContextTestUtils.getMappingIid;
21 import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.reverseBytes;
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertThat;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import com.google.common.base.Optional;
31 import com.google.common.collect.ImmutableList;
32 import com.google.common.collect.Lists;
33 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
34 import io.fd.honeycomb.translate.v3po.test.ListReaderCustomizerTest;
35 import io.fd.honeycomb.translate.ModificationCache;
36 import io.fd.honeycomb.translate.read.ReadFailedException;
37 import io.fd.honeycomb.translate.v3po.util.NamingContext;
38 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.concurrent.CompletableFuture;
42 import java.util.stream.Collectors;
43 import org.hamcrest.CoreMatchers;
44 import org.junit.Test;
45 import org.mockito.Mockito;
46 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
47 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
49 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
63 import org.openvpp.jvpp.dto.IpAddressDetails;
64 import org.openvpp.jvpp.dto.IpAddressDetailsReplyDump;
65 import org.openvpp.jvpp.dto.IpAddressDump;
66
67 public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder> {
68
69     private static final String IFACE_NAME = "eth0";
70     private static final String IFACE_2_NAME = "eth1";
71     private static final int IFACE_ID = 1;
72     private static final int IFACE_2_ID = 2;
73
74     private NamingContext interfacesContext;
75
76     public Ipv4AddressCustomizerTest() {
77         super(Address.class);
78     }
79
80     @Override
81     public void setUpBefore() {
82         interfacesContext = new NamingContext("generatedIfaceName", "test-instance");
83     }
84
85     @Override
86     protected ReaderCustomizer<Address, AddressBuilder> initCustomizer() {
87         final KeyedInstanceIdentifier<Mapping, MappingKey> eth0Id = getMappingIid(IFACE_NAME, "test-instance");
88         final KeyedInstanceIdentifier<Mapping, MappingKey> eth1Id = getMappingIid(IFACE_2_NAME, "test-instance");
89         final Optional<Mapping> eth0 = getMapping(IFACE_NAME, IFACE_ID);
90         final Optional<Mapping> eth1 = getMapping(IFACE_2_NAME, IFACE_2_ID);
91
92         final List<Mapping> allMappings = Lists.newArrayList(eth0.get(), eth1.get());
93         final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build();
94         doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(eth0Id.firstIdentifierOf(Mappings.class));
95
96         doReturn(eth0).when(mappingContext).read(eth0Id);
97         doReturn(eth1).when(mappingContext).read(eth1Id);
98
99         return new Ipv4AddressCustomizer(api, interfacesContext);
100     }
101
102     private static InstanceIdentifier<Address> getId(final String address, final String ifaceName) {
103         return InstanceIdentifier.builder(InterfacesState.class)
104                 .child(Interface.class, new InterfaceKey(ifaceName))
105                 .augmentation(Interface2.class)
106                 .child(Ipv4.class)
107                 .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address))))
108                 .build();
109     }
110
111     @Test
112     public void testReadCurrentAttributesFromCache() throws ReadFailedException {
113         ModificationCache cache = new ModificationCache();
114
115         IpAddressDetails detail1 = new IpAddressDetails();
116         IpAddressDetails detail2 = new IpAddressDetails();
117         IpAddressDetails detail3 = new IpAddressDetails();
118
119         detail1.ip = reverseBytes(
120             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
121         detail2.ip = reverseBytes(
122             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
123         detail3.ip = reverseBytes(
124             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
125
126         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
127         reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
128
129         cache.put(Ipv4ReadUtils.CACHE_KEY + IFACE_NAME, reply);
130         when(ctx.getModificationCache()).thenReturn(cache);
131
132         final AddressBuilder builder = new AddressBuilder();
133         final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
134
135         getCustomizer().readCurrentAttributes(id, builder, ctx);
136
137         assertEquals("192.168.2.1", builder.getIp().getValue());
138     }
139
140     @Test
141     public void testReadCurrentAttributesFor2Ifcs() throws ReadFailedException {
142         ModificationCache cache = new ModificationCache();
143
144         IpAddressDetails detail1 = new IpAddressDetails();
145         IpAddressDetails detail2 = new IpAddressDetails();
146
147         detail1.ip = reverseBytes(
148                 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
149         detail2.ip = reverseBytes(
150                 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
151
152         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
153         reply.ipAddressDetails = ImmutableList.of(detail1);
154         IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump();
155         reply2.ipAddressDetails = ImmutableList.of(detail2);
156
157         CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
158         future.complete(reply);
159         CompletableFuture<IpAddressDetailsReplyDump> future2 = new CompletableFuture<>();
160         future2.complete(reply2);
161
162         when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future).thenReturn(future2);
163         when(ctx.getModificationCache()).thenReturn(cache);
164
165         final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
166         final InstanceIdentifier<Address> id2 = getId("192.168.2.2", IFACE_2_NAME);
167
168         final List<AddressKey> ifc1Ids = getCustomizer().getAllIds(id, ctx);
169         assertThat(ifc1Ids.size(), is(1));
170         assertThat(ifc1Ids, CoreMatchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))));
171         final List<AddressKey> ifc2Ids = getCustomizer().getAllIds(id2, ctx);
172         assertThat(ifc2Ids.size(), is(1));
173         assertThat(ifc2Ids, CoreMatchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.2"))));
174
175         AddressBuilder builder = new AddressBuilder();
176         getCustomizer().readCurrentAttributes(id, builder, ctx);
177         assertEquals(builder.getIp().getValue(), "192.168.2.1");
178         builder = new AddressBuilder();
179         getCustomizer().readCurrentAttributes(id2, builder, ctx);
180         assertEquals(builder.getIp().getValue(), "192.168.2.2");
181     }
182
183     @Test
184     public void testReadCurrentAttributesFromOperationalData() throws ReadFailedException {
185         ModificationCache cache = new ModificationCache();
186
187         IpAddressDetails detail1 = new IpAddressDetails();
188         IpAddressDetails detail2 = new IpAddressDetails();
189         IpAddressDetails detail3 = new IpAddressDetails();
190
191         detail1.ip = reverseBytes(
192             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
193         detail2.ip = reverseBytes(
194             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
195         detail3.ip = reverseBytes(
196             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
197
198         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
199         reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
200
201         CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
202         future.complete(reply);
203
204         when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
205         when(ctx.getModificationCache()).thenReturn(cache);
206
207
208         final AddressBuilder builder = new AddressBuilder();
209         final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
210
211         getCustomizer().readCurrentAttributes(id, builder, ctx);
212
213         assertEquals("192.168.2.1", builder.getIp().getValue());
214     }
215
216     @Test
217     public void testGetAllIdsFromCache() throws ReadFailedException {
218         ModificationCache cache = new ModificationCache();
219
220         IpAddressDetails detail1 = new IpAddressDetails();
221         IpAddressDetails detail2 = new IpAddressDetails();
222         IpAddressDetails detail3 = new IpAddressDetails();
223
224         detail1.ip = reverseBytes(
225             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
226         detail2.ip = reverseBytes(
227             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
228         detail3.ip = reverseBytes(
229             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
230
231         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
232         reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
233
234         cache.put(Ipv4ReadUtils.CACHE_KEY + IFACE_NAME, reply);
235         when(ctx.getModificationCache()).thenReturn(cache);
236
237         final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
238
239         List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
240             .map(key -> key.getIp())
241             .collect(Collectors.toList());
242
243         verify(api, times(0)).ipAddressDump(Mockito.any(IpAddressDump.class));
244         assertEquals(3, ids.size());
245         assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue()));
246         assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue()));
247         assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue()));
248     }
249
250     @Test
251     public void testGetAllIdsFromOperationalData() throws ReadFailedException {
252         ModificationCache cache = new ModificationCache();
253
254         IpAddressDetails detail1 = new IpAddressDetails();
255         IpAddressDetails detail2 = new IpAddressDetails();
256         IpAddressDetails detail3 = new IpAddressDetails();
257
258         detail1.ip = reverseBytes(
259             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
260         detail2.ip = reverseBytes(
261             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
262         detail3.ip = reverseBytes(
263             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
264
265         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
266         reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
267
268         CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
269         future.complete(reply);
270
271         when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
272         when(ctx.getModificationCache()).thenReturn(cache);
273
274         final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
275
276         List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
277             .map(key -> key.getIp())
278             .collect(Collectors.toList());
279
280         assertEquals(3, ids.size());
281         assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue()));
282         assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue()));
283         assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue()));
284     }
285
286     @Test
287     public void testMerge() {
288
289         Address address = new AddressBuilder().build();
290         Ipv4Builder ipv4Builder = new Ipv4Builder();
291         getCustomizer().merge(ipv4Builder, Arrays.asList(address));
292
293         assertEquals(1, ipv4Builder.getAddress().size());
294         assertEquals(true, ipv4Builder.getAddress().contains(address));
295     }
296
297 }