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";
63 public static final String CACHE_KEY = Ipv4AddressCustomizer.class.getName();
65 private NamingContext interfacesContext;
67 public Ipv4AddressCustomizerTest() {
68 super(Address.class, Ipv4Builder.class);
73 interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
74 defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
75 defineMapping(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 testReadCurrentAttributesFor2Ifcs() throws ReadFailedException {
94 //changed to mock to not store first dumped data(otherwise that double thenReturn on line 118 is not gonna work)
95 ModificationCache cache = mock(ModificationCache.class);
97 IpAddressDetails detail1 = new IpAddressDetails();
98 IpAddressDetails detail2 = new IpAddressDetails();
100 detail1.ip = reverseBytes(
101 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
102 detail2.ip = reverseBytes(
103 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
105 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
106 reply.ipAddressDetails = ImmutableList.of(detail1);
107 IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump();
108 reply2.ipAddressDetails = ImmutableList.of(detail2);
110 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
111 future.complete(reply);
112 CompletableFuture<IpAddressDetailsReplyDump> future2 = new CompletableFuture<>();
113 future2.complete(reply2);
115 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future).thenReturn(future2)
116 .thenReturn(future).thenReturn(future2);
117 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)).thenReturn(future(reply2))
118 .thenReturn(future(reply)).thenReturn(future(reply2));
119 when(ctx.getModificationCache()).thenReturn(cache);
122 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
123 final InstanceIdentifier<Address> id2 = getId("192.168.2.2", IFACE_2_NAME);
125 final List<AddressKey> ifc1Ids = getCustomizer().getAllIds(id, ctx);
126 assertThat(ifc1Ids.size(), is(1));
127 assertThat(ifc1Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))));
128 final List<AddressKey> ifc2Ids = getCustomizer().getAllIds(id2, ctx);
129 assertThat(ifc2Ids.size(), is(1));
130 assertThat(ifc2Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.2"))));
132 AddressBuilder builder = new AddressBuilder();
133 getCustomizer().readCurrentAttributes(id, builder, ctx);
134 assertEquals(builder.getIp().getValue(), "192.168.2.1");
135 builder = new AddressBuilder();
136 getCustomizer().readCurrentAttributes(id2, builder, ctx);
137 assertEquals(builder.getIp().getValue(), "192.168.2.2");
141 public void testReadCurrentAttributesSuccessfull() throws ReadFailedException {
142 ModificationCache cache = new ModificationCache();
144 IpAddressDetails detail1 = new IpAddressDetails();
145 IpAddressDetails detail2 = new IpAddressDetails();
146 IpAddressDetails detail3 = new IpAddressDetails();
148 detail1.ip = reverseBytes(
149 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
150 detail2.ip = reverseBytes(
151 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
152 detail3.ip = reverseBytes(
153 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
155 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
156 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
157 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply));
158 when(ctx.getModificationCache()).thenReturn(cache);
160 final AddressBuilder builder = new AddressBuilder();
161 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
163 getCustomizer().readCurrentAttributes(id, builder, ctx);
165 assertEquals("192.168.2.1", builder.getIp().getValue());
169 public void testGetAllIdsFromSuccessfull() throws ReadFailedException {
170 ModificationCache cache = new ModificationCache();
172 IpAddressDetails detail1 = new IpAddressDetails();
173 IpAddressDetails detail2 = new IpAddressDetails();
174 IpAddressDetails detail3 = new IpAddressDetails();
176 detail1.ip = reverseBytes(
177 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
178 detail2.ip = reverseBytes(
179 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
180 detail3.ip = reverseBytes(
181 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
183 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
184 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
185 when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply));
186 when(ctx.getModificationCache()).thenReturn(cache);
188 final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME);
190 List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
191 .map(key -> key.getIp())
192 .collect(Collectors.toList());
194 assertEquals(3, ids.size());
195 assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue()));
196 assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue()));
197 assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue()));