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.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;
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.core.dto.IpAddressDetails;
64 import org.openvpp.jvpp.core.dto.IpAddressDetailsReplyDump;
65 import org.openvpp.jvpp.core.dto.IpAddressDump;
67 public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder> {
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;
74 private NamingContext interfacesContext;
76 public Ipv4AddressCustomizerTest() {
81 public void setUpBefore() {
82 interfacesContext = new NamingContext("generatedIfaceName", "test-instance");
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);
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));
96 doReturn(eth0).when(mappingContext).read(eth0Id);
97 doReturn(eth1).when(mappingContext).read(eth1Id);
99 return new Ipv4AddressCustomizer(api, interfacesContext);
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)
107 .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address))))
112 public void testReadCurrentAttributesFromCache() throws ReadFailedException {
113 ModificationCache cache = new ModificationCache();
115 IpAddressDetails detail1 = new IpAddressDetails();
116 IpAddressDetails detail2 = new IpAddressDetails();
117 IpAddressDetails detail3 = new IpAddressDetails();
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"))));
126 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
127 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
129 cache.put(Ipv4ReadUtils.CACHE_KEY + IFACE_NAME, reply);
130 when(ctx.getModificationCache()).thenReturn(cache);
132 final AddressBuilder builder = new AddressBuilder();
133 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
135 getCustomizer().readCurrentAttributes(id, builder, ctx);
137 assertEquals("192.168.2.1", builder.getIp().getValue());
141 public void testReadCurrentAttributesFor2Ifcs() throws ReadFailedException {
142 ModificationCache cache = new ModificationCache();
144 IpAddressDetails detail1 = new IpAddressDetails();
145 IpAddressDetails detail2 = new IpAddressDetails();
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"))));
152 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
153 reply.ipAddressDetails = ImmutableList.of(detail1);
154 IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump();
155 reply2.ipAddressDetails = ImmutableList.of(detail2);
157 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
158 future.complete(reply);
159 CompletableFuture<IpAddressDetailsReplyDump> future2 = new CompletableFuture<>();
160 future2.complete(reply2);
162 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future).thenReturn(future2);
163 when(ctx.getModificationCache()).thenReturn(cache);
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);
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"))));
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");
184 public void testReadCurrentAttributesFromOperationalData() throws ReadFailedException {
185 ModificationCache cache = new ModificationCache();
187 IpAddressDetails detail1 = new IpAddressDetails();
188 IpAddressDetails detail2 = new IpAddressDetails();
189 IpAddressDetails detail3 = new IpAddressDetails();
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"))));
198 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
199 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
201 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
202 future.complete(reply);
204 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
205 when(ctx.getModificationCache()).thenReturn(cache);
208 final AddressBuilder builder = new AddressBuilder();
209 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
211 getCustomizer().readCurrentAttributes(id, builder, ctx);
213 assertEquals("192.168.2.1", builder.getIp().getValue());
217 public void testGetAllIdsFromCache() throws ReadFailedException {
218 ModificationCache cache = new ModificationCache();
220 IpAddressDetails detail1 = new IpAddressDetails();
221 IpAddressDetails detail2 = new IpAddressDetails();
222 IpAddressDetails detail3 = new IpAddressDetails();
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"))));
231 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
232 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
234 cache.put(Ipv4ReadUtils.CACHE_KEY + IFACE_NAME, reply);
235 when(ctx.getModificationCache()).thenReturn(cache);
237 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
239 List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
240 .map(key -> key.getIp())
241 .collect(Collectors.toList());
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()));
251 public void testGetAllIdsFromOperationalData() throws ReadFailedException {
252 ModificationCache cache = new ModificationCache();
254 IpAddressDetails detail1 = new IpAddressDetails();
255 IpAddressDetails detail2 = new IpAddressDetails();
256 IpAddressDetails detail3 = new IpAddressDetails();
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"))));
265 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
266 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
268 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
269 future.complete(reply);
271 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
272 when(ctx.getModificationCache()).thenReturn(cache);
274 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
276 List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
277 .map(key -> key.getIp())
278 .collect(Collectors.toList());
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()));
287 public void testMerge() {
289 Address address = new AddressBuilder().build();
290 Ipv4Builder ipv4Builder = new Ipv4Builder();
291 getCustomizer().merge(ipv4Builder, Arrays.asList(address));
293 assertEquals(1, ipv4Builder.getAddress().size());
294 assertEquals(true, ipv4Builder.getAddress().contains(address));