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.util.NamingContext;
33 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
34 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.stream.Collectors;
38 import org.hamcrest.CoreMatchers;
39 import org.junit.Test;
40 import org.mockito.Mockito;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.openvpp.jvpp.core.dto.IpAddressDetails;
54 import org.openvpp.jvpp.core.dto.IpAddressDetailsReplyDump;
55 import org.openvpp.jvpp.core.dto.IpAddressDump;
57 public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder> {
59 private static final String IFACE_NAME = "eth0";
60 private static final String IFACE_2_NAME = "eth1";
61 private static final int IFACE_ID = 1;
62 private static final int IFACE_2_ID = 2;
63 private static final String IFC_CTX_NAME = "ifc-test-instance";
65 private NamingContext interfacesContext;
67 public Ipv4AddressCustomizerTest() {
73 interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
74 ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
75 ContextTestUtils.mockMapping(mappingContext, IFACE_2_NAME, IFACE_2_ID, IFC_CTX_NAME);
79 protected ReaderCustomizer<Address, AddressBuilder> initCustomizer() {
80 return new Ipv4AddressCustomizer(api, interfacesContext);
83 private static InstanceIdentifier<Address> getId(final String address, final String ifaceName) {
84 return InstanceIdentifier.builder(InterfacesState.class)
85 .child(Interface.class, new InterfaceKey(ifaceName))
86 .augmentation(Interface2.class)
88 .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address))))
93 public void testReadCurrentAttributesFromCache() throws ReadFailedException {
94 ModificationCache cache = new ModificationCache();
96 IpAddressDetails detail1 = new IpAddressDetails();
97 IpAddressDetails detail2 = new IpAddressDetails();
98 IpAddressDetails detail3 = new IpAddressDetails();
100 detail1.ip = reverseBytes(
101 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
102 detail2.ip = reverseBytes(
103 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
104 detail3.ip = reverseBytes(
105 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
107 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
108 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
110 cache.put(Ipv4ReadUtils.CACHE_KEY + IFACE_NAME, reply);
111 when(ctx.getModificationCache()).thenReturn(cache);
113 final AddressBuilder builder = new AddressBuilder();
114 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
116 getCustomizer().readCurrentAttributes(id, builder, ctx);
118 assertEquals("192.168.2.1", builder.getIp().getValue());
122 public void testReadCurrentAttributesFor2Ifcs() throws ReadFailedException {
123 ModificationCache cache = new ModificationCache();
125 IpAddressDetails detail1 = new IpAddressDetails();
126 IpAddressDetails detail2 = new IpAddressDetails();
128 detail1.ip = reverseBytes(
129 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
130 detail2.ip = reverseBytes(
131 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
133 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
134 reply.ipAddressDetails = ImmutableList.of(detail1);
135 IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump();
136 reply2.ipAddressDetails = ImmutableList.of(detail2);
138 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)).thenReturn(future(reply2));
139 when(ctx.getModificationCache()).thenReturn(cache);
141 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
142 final InstanceIdentifier<Address> id2 = getId("192.168.2.2", IFACE_2_NAME);
144 final List<AddressKey> ifc1Ids = getCustomizer().getAllIds(id, ctx);
145 assertThat(ifc1Ids.size(), is(1));
146 assertThat(ifc1Ids, CoreMatchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))));
147 final List<AddressKey> ifc2Ids = getCustomizer().getAllIds(id2, ctx);
148 assertThat(ifc2Ids.size(), is(1));
149 assertThat(ifc2Ids, CoreMatchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.2"))));
151 AddressBuilder builder = new AddressBuilder();
152 getCustomizer().readCurrentAttributes(id, builder, ctx);
153 assertEquals(builder.getIp().getValue(), "192.168.2.1");
154 builder = new AddressBuilder();
155 getCustomizer().readCurrentAttributes(id2, builder, ctx);
156 assertEquals(builder.getIp().getValue(), "192.168.2.2");
160 public void testReadCurrentAttributesFromOperationalData() throws ReadFailedException {
161 ModificationCache cache = new ModificationCache();
163 IpAddressDetails detail1 = new IpAddressDetails();
164 IpAddressDetails detail2 = new IpAddressDetails();
165 IpAddressDetails detail3 = new IpAddressDetails();
167 detail1.ip = reverseBytes(
168 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
169 detail2.ip = reverseBytes(
170 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
171 detail3.ip = reverseBytes(
172 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
174 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
175 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
176 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply));
177 when(ctx.getModificationCache()).thenReturn(cache);
179 final AddressBuilder builder = new AddressBuilder();
180 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
182 getCustomizer().readCurrentAttributes(id, builder, ctx);
184 assertEquals("192.168.2.1", builder.getIp().getValue());
188 public void testGetAllIdsFromCache() throws ReadFailedException {
189 ModificationCache cache = new ModificationCache();
191 IpAddressDetails detail1 = new IpAddressDetails();
192 IpAddressDetails detail2 = new IpAddressDetails();
193 IpAddressDetails detail3 = new IpAddressDetails();
195 detail1.ip = reverseBytes(
196 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
197 detail2.ip = reverseBytes(
198 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
199 detail3.ip = reverseBytes(
200 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
202 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
203 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
205 cache.put(Ipv4ReadUtils.CACHE_KEY + IFACE_NAME, reply);
206 when(ctx.getModificationCache()).thenReturn(cache);
208 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
210 List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
211 .map(key -> key.getIp())
212 .collect(Collectors.toList());
214 verify(api, times(0)).ipAddressDump(Mockito.any(IpAddressDump.class));
215 assertEquals(3, ids.size());
216 assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue()));
217 assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue()));
218 assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue()));
222 public void testGetAllIdsFromOperationalData() throws ReadFailedException {
223 ModificationCache cache = new ModificationCache();
225 IpAddressDetails detail1 = new IpAddressDetails();
226 IpAddressDetails detail2 = new IpAddressDetails();
227 IpAddressDetails detail3 = new IpAddressDetails();
229 detail1.ip = reverseBytes(
230 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
231 detail2.ip = reverseBytes(
232 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
233 detail3.ip = reverseBytes(
234 TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
236 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
237 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
238 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply));
239 when(ctx.getModificationCache()).thenReturn(cache);
241 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
243 List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
244 .map(key -> key.getIp())
245 .collect(Collectors.toList());
247 assertEquals(3, ids.size());
248 assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue()));
249 assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue()));
250 assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue()));
254 public void testMerge() {
256 Address address = new AddressBuilder().build();
257 Ipv4Builder ipv4Builder = new Ipv4Builder();
258 getCustomizer().merge(ipv4Builder, Arrays.asList(address));
260 assertEquals(1, ipv4Builder.getAddress().size());
261 assertEquals(true, ipv4Builder.getAddress().contains(address));