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.v3po.translate.v3po.interfacesstate.ip;
19 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid;
21 import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.reverseBytes;
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import com.google.common.base.Optional;
29 import com.google.common.collect.ImmutableList;
30 import com.google.common.collect.Lists;
31 import io.fd.honeycomb.v3po.translate.ModificationCache;
32 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
33 import io.fd.honeycomb.v3po.translate.spi.read.RootReaderCustomizer;
34 import io.fd.honeycomb.v3po.translate.v3po.test.ListReaderCustomizerTest;
35 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
36 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
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.junit.Test;
42 import org.mockito.Mockito;
43 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
44 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
46 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
60 import org.openvpp.jvpp.dto.IpAddressDetails;
61 import org.openvpp.jvpp.dto.IpAddressDetailsReplyDump;
62 import org.openvpp.jvpp.dto.IpAddressDump;
64 public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder> {
66 private static final String IFACE_NAME = "eth0";
67 private static final int IFACE_ID = 1;
69 private NamingContext interfacesContext;
71 public Ipv4AddressCustomizerTest() {
76 public void setUpBefore() {
77 interfacesContext = new NamingContext("generatedIfaceName", "test-instance");
81 protected RootReaderCustomizer<Address, AddressBuilder> initCustomizer() {
82 final KeyedInstanceIdentifier<Mapping, MappingKey> eth0Id = getMappingIid(IFACE_NAME, "test-instance");
83 final Optional<Mapping> eth0 = getMapping(IFACE_NAME, IFACE_ID);
85 final List<Mapping> allMappings = Lists.newArrayList(getMapping(IFACE_NAME, IFACE_ID).get());
86 final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build();
87 doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(eth0Id.firstIdentifierOf(Mappings.class));
89 doReturn(eth0).when(mappingContext).read(eth0Id);
91 return new Ipv4AddressCustomizer(api, interfacesContext);
94 private static InstanceIdentifier<Address> getId(final String address) {
95 return InstanceIdentifier.builder(InterfacesState.class)
96 .child(Interface.class, new InterfaceKey(IFACE_NAME))
97 .augmentation(Interface2.class)
99 .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address))))
104 public void testReadCurrentAttributesFromCache() throws ReadFailedException {
105 ModificationCache cache = new ModificationCache();
107 IpAddressDetails detail1 = new IpAddressDetails();
108 IpAddressDetails detail2 = new IpAddressDetails();
109 IpAddressDetails detail3 = new IpAddressDetails();
111 detail1.ip = reverseBytes(
112 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
113 detail2.ip = reverseBytes(
114 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
115 detail3.ip = reverseBytes(
116 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
118 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
119 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
121 cache.put(Ipv4AddressCustomizer.class.getName(), reply);
122 when(ctx.getModificationCache()).thenReturn(cache);
124 final AddressBuilder builder = new AddressBuilder();
125 final InstanceIdentifier<Address> id = getId("192.168.2.1");
127 getCustomizer().readCurrentAttributes(id, builder, ctx);
129 assertEquals("192.168.2.1", builder.getIp().getValue());
133 public void testReadCurrentAttributesFromOperationalData() throws ReadFailedException {
134 ModificationCache cache = new ModificationCache();
136 IpAddressDetails detail1 = new IpAddressDetails();
137 IpAddressDetails detail2 = new IpAddressDetails();
138 IpAddressDetails detail3 = new IpAddressDetails();
140 detail1.ip = reverseBytes(
141 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
142 detail2.ip = reverseBytes(
143 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
144 detail3.ip = reverseBytes(
145 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
147 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
148 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
150 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
151 future.complete(reply);
153 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
154 when(ctx.getModificationCache()).thenReturn(cache);
157 final AddressBuilder builder = new AddressBuilder();
158 final InstanceIdentifier<Address> id = getId("192.168.2.1");
160 getCustomizer().readCurrentAttributes(id, builder, ctx);
162 assertEquals("192.168.2.1", builder.getIp().getValue());
166 public void testGetAllIdsFromCache() 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 cache.put(Ipv4AddressCustomizer.class.getName(), reply);
184 when(ctx.getModificationCache()).thenReturn(cache);
186 final InstanceIdentifier<Address> id = getId("192.168.2.1");
188 List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
189 .map(key -> key.getIp())
190 .collect(Collectors.toList());
192 verify(api, times(0)).ipAddressDump(Mockito.any(IpAddressDump.class));
193 assertEquals(3, ids.size());
194 assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue()));
195 assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue()));
196 assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue()));
200 public void testGetAllIdsFromOperationalData() throws ReadFailedException {
201 ModificationCache cache = new ModificationCache();
203 IpAddressDetails detail1 = new IpAddressDetails();
204 IpAddressDetails detail2 = new IpAddressDetails();
205 IpAddressDetails detail3 = new IpAddressDetails();
207 detail1.ip = reverseBytes(
208 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
209 detail2.ip = reverseBytes(
210 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
211 detail3.ip = reverseBytes(
212 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
214 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
215 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
217 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
218 future.complete(reply);
220 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
221 when(ctx.getModificationCache()).thenReturn(cache);
223 final InstanceIdentifier<Address> id = getId("192.168.2.1");
225 List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
226 .map(key -> key.getIp())
227 .collect(Collectors.toList());
229 assertEquals(3, ids.size());
230 assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue()));
231 assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue()));
232 assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue()));
236 public void testMerge() {
238 Address address = new AddressBuilder().build();
239 Ipv4Builder ipv4Builder = new Ipv4Builder();
240 getCustomizer().merge(ipv4Builder, Arrays.asList(address));
242 assertEquals(1, ipv4Builder.getAddress().size());
243 assertEquals(true, ipv4Builder.getAddress().contains(address));