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