2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfacesstate.ip;
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;
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.translate.v3po.test.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;
58 public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder> {
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";
66 private NamingContext interfacesContext;
68 public Ipv4AddressCustomizerTest() {
73 public void setUpBefore() {
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);
80 protected ReaderCustomizer<Address, AddressBuilder> initCustomizer() {
81 return new Ipv4AddressCustomizer(api, interfacesContext);
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)
89 .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address))))
94 public void testReadCurrentAttributesFromCache() throws ReadFailedException {
95 ModificationCache cache = new ModificationCache();
97 IpAddressDetails detail1 = new IpAddressDetails();
98 IpAddressDetails detail2 = new IpAddressDetails();
99 IpAddressDetails detail3 = new IpAddressDetails();
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"))));
108 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
109 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
111 cache.put(Ipv4ReadUtils.CACHE_KEY + IFACE_NAME, reply);
112 when(ctx.getModificationCache()).thenReturn(cache);
114 final AddressBuilder builder = new AddressBuilder();
115 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
117 getCustomizer().readCurrentAttributes(id, builder, ctx);
119 assertEquals("192.168.2.1", builder.getIp().getValue());
123 public void testReadCurrentAttributesFor2Ifcs() throws ReadFailedException {
124 ModificationCache cache = new ModificationCache();
126 IpAddressDetails detail1 = new IpAddressDetails();
127 IpAddressDetails detail2 = new IpAddressDetails();
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"))));
134 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
135 reply.ipAddressDetails = ImmutableList.of(detail1);
136 IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump();
137 reply2.ipAddressDetails = ImmutableList.of(detail2);
139 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
140 future.complete(reply);
141 CompletableFuture<IpAddressDetailsReplyDump> future2 = new CompletableFuture<>();
142 future2.complete(reply2);
144 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future).thenReturn(future2);
145 when(ctx.getModificationCache()).thenReturn(cache);
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);
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"))));
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");
166 public void testReadCurrentAttributesFromOperationalData() throws ReadFailedException {
167 ModificationCache cache = new ModificationCache();
169 IpAddressDetails detail1 = new IpAddressDetails();
170 IpAddressDetails detail2 = new IpAddressDetails();
171 IpAddressDetails detail3 = new IpAddressDetails();
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"))));
180 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
181 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
183 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
184 future.complete(reply);
186 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
187 when(ctx.getModificationCache()).thenReturn(cache);
190 final AddressBuilder builder = new AddressBuilder();
191 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
193 getCustomizer().readCurrentAttributes(id, builder, ctx);
195 assertEquals("192.168.2.1", builder.getIp().getValue());
199 public void testGetAllIdsFromCache() throws ReadFailedException {
200 ModificationCache cache = new ModificationCache();
202 IpAddressDetails detail1 = new IpAddressDetails();
203 IpAddressDetails detail2 = new IpAddressDetails();
204 IpAddressDetails detail3 = new IpAddressDetails();
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"))));
213 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
214 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
216 cache.put(Ipv4ReadUtils.CACHE_KEY + IFACE_NAME, reply);
217 when(ctx.getModificationCache()).thenReturn(cache);
219 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
221 List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
222 .map(key -> key.getIp())
223 .collect(Collectors.toList());
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()));
233 public void testGetAllIdsFromOperationalData() throws ReadFailedException {
234 ModificationCache cache = new ModificationCache();
236 IpAddressDetails detail1 = new IpAddressDetails();
237 IpAddressDetails detail2 = new IpAddressDetails();
238 IpAddressDetails detail3 = new IpAddressDetails();
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"))));
247 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
248 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
250 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
251 future.complete(reply);
253 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
254 when(ctx.getModificationCache()).thenReturn(cache);
256 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
258 List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
259 .map(key -> key.getIp())
260 .collect(Collectors.toList());
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()));
269 public void testMerge() {
271 Address address = new AddressBuilder().build();
272 Ipv4Builder ipv4Builder = new Ipv4Builder();
273 getCustomizer().merge(ipv4Builder, Arrays.asList(address));
275 assertEquals(1, ipv4Builder.getAddress().size());
276 assertEquals(true, ipv4Builder.getAddress().contains(address));