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;
20 import static org.hamcrest.Matchers.is;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertThat;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
26 import com.google.common.collect.ImmutableList;
27 import io.fd.honeycomb.translate.ModificationCache;
28 import io.fd.honeycomb.translate.read.ReadFailedException;
29 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
30 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
31 import io.fd.honeycomb.translate.v3po.util.Ipv4Translator;
32 import io.fd.honeycomb.translate.v3po.util.NamingContext;
33 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.concurrent.CompletableFuture;
37 import java.util.stream.Collectors;
38 import org.hamcrest.Matchers;
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> implements
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";
65 public static final String CACHE_KEY = Ipv4AddressCustomizer.class.getName();
67 private NamingContext interfacesContext;
69 public Ipv4AddressCustomizerTest() {
75 interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
76 ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
77 ContextTestUtils.mockMapping(mappingContext, IFACE_2_NAME, IFACE_2_ID, IFC_CTX_NAME);
81 protected ReaderCustomizer<Address, AddressBuilder> initCustomizer() {
82 return new Ipv4AddressCustomizer(api, interfacesContext);
85 private static InstanceIdentifier<Address> getId(final String address, final String ifaceName) {
86 return InstanceIdentifier.builder(InterfacesState.class)
87 .child(Interface.class, new InterfaceKey(ifaceName))
88 .augmentation(Interface2.class)
90 .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address))))
95 public void testReadCurrentAttributesFor2Ifcs() throws ReadFailedException {
96 //changed to mock to not store first dumped data(otherwise that double thenReturn on line 118 is not gonna work)
97 ModificationCache cache = mock(ModificationCache.class);
99 IpAddressDetails detail1 = new IpAddressDetails();
100 IpAddressDetails detail2 = new IpAddressDetails();
102 detail1.ip = reverseBytes(
103 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
104 detail2.ip = reverseBytes(
105 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
107 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
108 reply.ipAddressDetails = ImmutableList.of(detail1);
109 IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump();
110 reply2.ipAddressDetails = ImmutableList.of(detail2);
112 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
113 future.complete(reply);
114 CompletableFuture<IpAddressDetailsReplyDump> future2 = new CompletableFuture<>();
115 future2.complete(reply2);
117 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future).thenReturn(future2)
118 .thenReturn(future).thenReturn(future2);
119 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)).thenReturn(future(reply2))
120 .thenReturn(future(reply)).thenReturn(future(reply2));
121 when(ctx.getModificationCache()).thenReturn(cache);
124 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
125 final InstanceIdentifier<Address> id2 = getId("192.168.2.2", IFACE_2_NAME);
127 final List<AddressKey> ifc1Ids = getCustomizer().getAllIds(id, ctx);
128 assertThat(ifc1Ids.size(), is(1));
129 assertThat(ifc1Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))));
130 final List<AddressKey> ifc2Ids = getCustomizer().getAllIds(id2, ctx);
131 assertThat(ifc2Ids.size(), is(1));
132 assertThat(ifc2Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.2"))));
134 AddressBuilder builder = new AddressBuilder();
135 getCustomizer().readCurrentAttributes(id, builder, ctx);
136 assertEquals(builder.getIp().getValue(), "192.168.2.1");
137 builder = new AddressBuilder();
138 getCustomizer().readCurrentAttributes(id2, builder, ctx);
139 assertEquals(builder.getIp().getValue(), "192.168.2.2");
143 public void testReadCurrentAttributesSuccessfull() throws ReadFailedException {
144 ModificationCache cache = new ModificationCache();
146 IpAddressDetails detail1 = new IpAddressDetails();
147 IpAddressDetails detail2 = new IpAddressDetails();
148 IpAddressDetails detail3 = new IpAddressDetails();
150 detail1.ip = reverseBytes(
151 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
152 detail2.ip = reverseBytes(
153 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
154 detail3.ip = reverseBytes(
155 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
157 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
158 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
159 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply));
160 when(ctx.getModificationCache()).thenReturn(cache);
162 final AddressBuilder builder = new AddressBuilder();
163 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
165 getCustomizer().readCurrentAttributes(id, builder, ctx);
167 assertEquals("192.168.2.1", builder.getIp().getValue());
171 public void testGetAllIdsFromSuccessfull() throws ReadFailedException {
172 ModificationCache cache = new ModificationCache();
174 IpAddressDetails detail1 = new IpAddressDetails();
175 IpAddressDetails detail2 = new IpAddressDetails();
176 IpAddressDetails detail3 = new IpAddressDetails();
178 detail1.ip = reverseBytes(
179 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
180 detail2.ip = reverseBytes(
181 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
182 detail3.ip = reverseBytes(
183 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
185 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
186 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
187 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply));
188 when(ctx.getModificationCache()).thenReturn(cache);
190 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
192 List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
193 .map(key -> key.getIp())
194 .collect(Collectors.toList());
196 assertEquals(3, ids.size());
197 assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue()));
198 assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue()));
199 assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue()));
203 public void testMerge() {
205 Address address = new AddressBuilder().build();
206 Ipv4Builder ipv4Builder = new Ipv4Builder();
207 getCustomizer().merge(ipv4Builder, Arrays.asList(address));
209 assertEquals(1, ipv4Builder.getAddress().size());
210 assertEquals(true, ipv4Builder.getAddress().contains(address));