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