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.util.Ipv4Translator;
31 import io.fd.honeycomb.translate.v3po.util.NamingContext;
32 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
33 import java.util.List;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.stream.Collectors;
36 import org.hamcrest.Matchers;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.openvpp.jvpp.core.dto.IpAddressDetails;
52 import org.openvpp.jvpp.core.dto.IpAddressDetailsReplyDump;
53 import org.openvpp.jvpp.core.dto.IpAddressDump;
55 public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder> implements
58 private static final String IFACE_NAME = "eth0";
59 private static final String IFACE_2_NAME = "eth1";
60 private static final int IFACE_ID = 1;
61 private static final int IFACE_2_ID = 2;
62 private static final String IFC_CTX_NAME = "ifc-test-instance";
64 private NamingContext interfacesContext;
66 public Ipv4AddressCustomizerTest() {
67 super(Address.class, Ipv4Builder.class);
72 interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
73 defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
74 defineMapping(mappingContext, IFACE_2_NAME, IFACE_2_ID, IFC_CTX_NAME);
78 protected ReaderCustomizer<Address, AddressBuilder> initCustomizer() {
79 return new Ipv4AddressCustomizer(api, interfacesContext);
82 private static InstanceIdentifier<Address> getId(final String address, final String ifaceName) {
83 return InstanceIdentifier.builder(InterfacesState.class)
84 .child(Interface.class, new InterfaceKey(ifaceName))
85 .augmentation(Interface2.class)
87 .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address))))
92 public void testReadCurrentAttributesFor2Ifcs() throws ReadFailedException {
93 //changed to mock to not store first dumped data(otherwise that double thenReturn on line 118 is not gonna work)
94 ModificationCache cache = mock(ModificationCache.class);
96 IpAddressDetails detail1 = new IpAddressDetails();
97 IpAddressDetails detail2 = new IpAddressDetails();
99 detail1.ip = reverseBytes(
100 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
101 detail2.ip = reverseBytes(
102 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
104 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
105 reply.ipAddressDetails = ImmutableList.of(detail1);
106 IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump();
107 reply2.ipAddressDetails = ImmutableList.of(detail2);
109 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
110 future.complete(reply);
111 CompletableFuture<IpAddressDetailsReplyDump> future2 = new CompletableFuture<>();
112 future2.complete(reply2);
114 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future).thenReturn(future2)
115 .thenReturn(future).thenReturn(future2);
116 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)).thenReturn(future(reply2))
117 .thenReturn(future(reply)).thenReturn(future(reply2));
118 when(ctx.getModificationCache()).thenReturn(cache);
121 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
122 final InstanceIdentifier<Address> id2 = getId("192.168.2.2", IFACE_2_NAME);
124 final List<AddressKey> ifc1Ids = getCustomizer().getAllIds(id, ctx);
125 assertThat(ifc1Ids.size(), is(1));
126 assertThat(ifc1Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))));
127 final List<AddressKey> ifc2Ids = getCustomizer().getAllIds(id2, ctx);
128 assertThat(ifc2Ids.size(), is(1));
129 assertThat(ifc2Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.2"))));
131 AddressBuilder builder = new AddressBuilder();
132 getCustomizer().readCurrentAttributes(id, builder, ctx);
133 assertEquals(builder.getIp().getValue(), "192.168.2.1");
134 builder = new AddressBuilder();
135 getCustomizer().readCurrentAttributes(id2, builder, ctx);
136 assertEquals(builder.getIp().getValue(), "192.168.2.2");
140 public void testReadCurrentAttributesSuccessfull() throws ReadFailedException {
141 ModificationCache cache = new ModificationCache();
143 IpAddressDetails detail1 = new IpAddressDetails();
144 IpAddressDetails detail2 = new IpAddressDetails();
145 IpAddressDetails detail3 = new IpAddressDetails();
147 detail1.ip = reverseBytes(
148 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
149 detail2.ip = reverseBytes(
150 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
151 detail3.ip = reverseBytes(
152 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
154 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
155 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
156 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply));
157 when(ctx.getModificationCache()).thenReturn(cache);
159 final AddressBuilder builder = new AddressBuilder();
160 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
162 getCustomizer().readCurrentAttributes(id, builder, ctx);
164 assertEquals("192.168.2.1", builder.getIp().getValue());
168 public void testGetAllIdsFromSuccessfull() throws ReadFailedException {
169 ModificationCache cache = new ModificationCache();
171 IpAddressDetails detail1 = new IpAddressDetails();
172 IpAddressDetails detail2 = new IpAddressDetails();
173 IpAddressDetails detail3 = new IpAddressDetails();
175 detail1.ip = reverseBytes(
176 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
177 detail2.ip = reverseBytes(
178 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
179 detail3.ip = reverseBytes(
180 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
182 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
183 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
184 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply));
185 when(ctx.getModificationCache()).thenReturn(cache);
187 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
189 List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
190 .map(key -> key.getIp())
191 .collect(Collectors.toList());
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()));