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.hc2vpp.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.hc2vpp.common.test.read.ListReaderCustomizerTest;
33 import io.fd.hc2vpp.common.translate.util.Ipv4Translator;
34 import io.fd.hc2vpp.common.translate.util.NamingContext;
35 import io.fd.honeycomb.translate.read.ReadFailedException;
36 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
37 import io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory;
38 import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory;
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 static final Ipv4AddressNoZone IP1 = new Ipv4AddressNoZone("10.1.1.1");
80 private static final Ipv4AddressNoZone IP2 = new Ipv4AddressNoZone("10.1.1.2");
81 private static final short PREFIX_LENGTH = 16;
82 private InstanceIdentifier<Address> ifaceOneAddressOneIdentifier;
83 private InstanceIdentifier<Address> ifaceTwoAddressOneIdentifier;
84 private CacheKeyFactory cacheKeyFactory;
85 private NamingContext interfaceContext;
87 public SubInterfaceIpv4AddressCustomizerTest() {
88 super(Address.class, Ipv4Builder.class);
91 private static InstanceIdentifier<Address> getId() {
92 return IP4_IID.child(Address.class);
95 private static InstanceIdentifier<Address> getId(final Ipv4AddressNoZone ip) {
96 return IP4_IID.child(Address.class, new AddressKey(ip));
100 protected void setUp() throws Exception {
101 interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
103 ifaceOneAddressOneIdentifier = 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(1L))
109 .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1")));
110 ifaceTwoAddressOneIdentifier = InstanceIdentifier.create(InterfacesState.class)
111 .child(Interface.class, new InterfaceKey(IF_NAME))
112 .augmentation(SubinterfaceStateAugmentation.class)
113 .child(SubInterfaces.class)
114 .child(SubInterface.class, new SubInterfaceKey(2L))
116 .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1")));
118 // to simulate complex key
119 cacheKeyFactory = new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class,
120 ImmutableSet.of(SubInterface.class));
122 defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME);
123 defineMapping(mappingContext, SUB_IF_NAME, SUB_IF_INDEX, IFC_CTX_NAME);
124 defineMapping(mappingContext, SUB_IF_2_NAME, SUB_IF_2_INDEX, IFC_CTX_NAME);
128 protected ListReaderCustomizer<Address, AddressKey, AddressBuilder> initCustomizer() {
129 return new SubInterfaceIpv4AddressCustomizer(api, interfaceContext);
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 = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
208 detailIfaceOneAddressOne.prefixLength = 22;
210 detailIfaceTwoAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
211 detailIfaceTwoAddressOne.prefixLength = 23;
213 cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne);
214 cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), replyIfaceTwo);
217 private void fillCacheForFirstIfaceSecondEmpty() {
218 IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails();
219 IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump();
220 replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne);
222 detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
223 detailIfaceOneAddressOne.prefixLength = 22;
225 cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne);
226 cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), new IpAddressDetailsReplyDump());
229 private IpAddressDetailsReplyDump dump() {
230 final IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
232 final IpAddressDetails details1 = new IpAddressDetails();
233 details1.ip = new byte[]{10, 1, 1, 1};
234 details1.prefixLength = (byte) PREFIX_LENGTH;
235 reply.ipAddressDetails.add(details1);
237 final IpAddressDetails details2 = new IpAddressDetails();
238 details2.ip = new byte[]{10, 1, 1, 2};
239 details2.prefixLength = (byte) PREFIX_LENGTH;
240 reply.ipAddressDetails.add(details2);