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 org.hamcrest.MatcherAssert.assertThat;
20 import static org.hamcrest.Matchers.containsInAnyOrder;
21 import static org.hamcrest.Matchers.empty;
22 import static org.hamcrest.Matchers.hasSize;
23 import static org.hamcrest.Matchers.is;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import com.google.common.collect.ImmutableSet;
32 import io.fd.honeycomb.translate.read.ReadFailedException;
33 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
34 import io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory;
35 import io.fd.honeycomb.translate.util.read.cache.IdentifierCacheKeyFactory;
36 import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
37 import io.fd.honeycomb.translate.vpp.util.NamingContext;
38 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
39 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
40 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
41 import java.util.Arrays;
42 import java.util.List;
43 import org.junit.Assert;
44 import org.junit.Test;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceStateAugmentation;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfaces;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Address;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLength;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLengthBuilder;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 public class SubInterfaceIpv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder>
64 implements Ipv4Translator {
66 private static final String IFC_CTX_NAME = "ifc-test-instance";
67 private static final String IF_NAME = "local0";
68 private static final int IF_INDEX = 1;
69 private static final String SUB_IF_NAME = "local0.1";
70 private static final String SUB_IF_2_NAME = "local0.2";
71 private static final long SUB_IF_ID = 1;
72 private static final int SUB_IF_INDEX = 11;
73 private static final int SUB_IF_2_INDEX = 12;
74 private static final InstanceIdentifier<Ipv4> IP4_IID =
75 InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME))
76 .augmentation(SubinterfaceStateAugmentation.class)
77 .child(SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(SUB_IF_ID))
79 private InstanceIdentifier<Address> ifaceOneAddressOneIdentifier;
80 private InstanceIdentifier<Address> ifaceTwoAddressOneIdentifier;
81 private CacheKeyFactory cacheKeyFactory;
82 private static final Ipv4AddressNoZone IP1 = new Ipv4AddressNoZone("10.1.1.1");
83 private static final Ipv4AddressNoZone IP2 = new Ipv4AddressNoZone("10.1.1.2");
84 private static final short PREFIX_LENGTH = 16;
86 private NamingContext interfaceContext;
88 public SubInterfaceIpv4AddressCustomizerTest() {
89 super(Address.class, Ipv4Builder.class);
93 protected void setUp() throws Exception {
94 interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
96 ifaceOneAddressOneIdentifier = InstanceIdentifier.create(InterfacesState.class)
97 .child(Interface.class, new InterfaceKey(IF_NAME))
98 .augmentation(SubinterfaceStateAugmentation.class)
99 .child(SubInterfaces.class)
100 .child(SubInterface.class, new SubInterfaceKey(1L))
102 .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1")));
103 ifaceTwoAddressOneIdentifier = InstanceIdentifier.create(InterfacesState.class)
104 .child(Interface.class, new InterfaceKey(IF_NAME))
105 .augmentation(SubinterfaceStateAugmentation.class)
106 .child(SubInterfaces.class)
107 .child(SubInterface.class, new SubInterfaceKey(2L))
109 .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1")));
111 // to simulate complex key
112 cacheKeyFactory = new IdentifierCacheKeyFactory(ImmutableSet.of(SubInterface.class));
114 defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME);
115 defineMapping(mappingContext, SUB_IF_NAME, SUB_IF_INDEX, IFC_CTX_NAME);
116 defineMapping(mappingContext, SUB_IF_2_NAME, SUB_IF_2_INDEX, IFC_CTX_NAME);
120 protected ListReaderCustomizer<Address, AddressKey, AddressBuilder> initCustomizer() {
121 return new SubInterfaceIpv4AddressCustomizer(api, interfaceContext);
124 private static InstanceIdentifier<Address> getId() {
125 return IP4_IID.child(Address.class);
128 private static InstanceIdentifier<Address> getId(final Ipv4AddressNoZone ip) {
129 return IP4_IID.child(Address.class, new AddressKey(ip));
133 public void testRead() throws ReadFailedException {
134 final AddressBuilder builder = mock(AddressBuilder.class);
135 when(api.ipAddressDump(any())).thenReturn(future(dump()));
136 getCustomizer().readCurrentAttributes(getId(IP2), builder, ctx);
137 verify(builder).setIp(IP2);
138 verify(builder).setSubnet(new PrefixLengthBuilder().setPrefixLength(PREFIX_LENGTH).build());
141 @Test(expected = ReadFailedException.class)
142 public void testReadFailed() throws ReadFailedException {
143 when(api.ipAddressDump(any())).thenReturn(failedFuture());
144 getCustomizer().readCurrentAttributes(getId(IP1), mock(AddressBuilder.class), ctx);
148 public void testGetAllIds() throws ReadFailedException {
149 when(api.ipAddressDump(any())).thenReturn(future(dump()));
150 final List<AddressKey> allIds = getCustomizer().getAllIds(getId(), ctx);
151 assertThat(allIds, hasSize(2));
152 assertThat(allIds, containsInAnyOrder(new AddressKey(IP1), new AddressKey(IP2)));
155 @Test(expected = ReadFailedException.class)
156 public void testGetAllIdsFailed() throws ReadFailedException {
157 when(api.ipAddressDump(any())).thenReturn(failedFuture());
158 getCustomizer().getAllIds(getId(), ctx);
162 public void testCacheScope() {
167 public void testCachingScopeSpecificRequest() throws ReadFailedException {
168 fillCacheForTwoIfaces();
169 final AddressBuilder ifaceOneAddressBuilder = new AddressBuilder();
170 final AddressBuilder ifaceTwoAddressBuilder = new AddressBuilder();
172 getCustomizer().readCurrentAttributes(ifaceOneAddressOneIdentifier, ifaceOneAddressBuilder, ctx);
173 getCustomizer().readCurrentAttributes(ifaceTwoAddressOneIdentifier, ifaceTwoAddressBuilder, ctx);
175 // addresses have caching scope of parent interface, so returned address should have respective prefix lengths
176 assertEquals("192.168.2.1", ifaceOneAddressBuilder.getIp().getValue());
177 assertTrue(ifaceOneAddressBuilder.getSubnet() instanceof PrefixLength);
178 assertEquals(22, PrefixLength.class.cast(ifaceOneAddressBuilder.getSubnet()).getPrefixLength().intValue());
180 assertEquals("192.168.2.1", ifaceTwoAddressBuilder.getIp().getValue());
181 assertTrue(ifaceTwoAddressBuilder.getSubnet() instanceof PrefixLength);
182 assertEquals(23, PrefixLength.class.cast(ifaceTwoAddressBuilder.getSubnet()).getPrefixLength().intValue());
186 public void testCachingScopeGetAll() throws ReadFailedException {
187 fillCacheForFirstIfaceSecondEmpty();
189 final List<AddressKey> keysForIfaceOne = getCustomizer().getAllIds(ifaceOneAddressOneIdentifier, ctx);
190 Assert.assertThat(keysForIfaceOne, hasSize(1));
191 final AddressKey keyIfaceOne = keysForIfaceOne.get(0);
192 assertEquals("192.168.2.1", keyIfaceOne.getIp().getValue());
194 final List<AddressKey> keysForIfaceTwo = getCustomizer().getAllIds(ifaceTwoAddressOneIdentifier, ctx);
195 Assert.assertThat(keysForIfaceTwo, is(empty()));
198 private void fillCacheForTwoIfaces() {
199 IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails();
200 IpAddressDetails detailIfaceTwoAddressOne = new IpAddressDetails();
201 IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump();
202 IpAddressDetailsReplyDump replyIfaceTwo = new IpAddressDetailsReplyDump();
204 replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne);
205 replyIfaceTwo.ipAddressDetails = Arrays.asList(detailIfaceTwoAddressOne);
207 detailIfaceOneAddressOne.ip = reverseBytes(
208 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
209 detailIfaceOneAddressOne.prefixLength = 22;
211 detailIfaceTwoAddressOne.ip = reverseBytes(
212 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
213 detailIfaceTwoAddressOne.prefixLength = 23;
215 cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne);
216 cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), replyIfaceTwo);
219 private void fillCacheForFirstIfaceSecondEmpty() {
220 IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails();
221 IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump();
222 replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne);
224 detailIfaceOneAddressOne.ip = reverseBytes(
225 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
226 detailIfaceOneAddressOne.prefixLength = 22;
228 cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne);
229 cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), new IpAddressDetailsReplyDump());
232 private IpAddressDetailsReplyDump dump() {
233 final IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
235 final IpAddressDetails details1 = new IpAddressDetails();
236 details1.ip = new byte[]{1, 1, 1, 10};
237 details1.prefixLength = (byte) PREFIX_LENGTH;
238 reply.ipAddressDetails.add(details1);
240 final IpAddressDetails details2 = new IpAddressDetails();
241 details2.ip = new byte[]{2, 1, 1, 10};
242 details2.prefixLength = (byte) PREFIX_LENGTH;
243 reply.ipAddressDetails.add(details2);