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.v4.subinterface;
19 import com.google.common.collect.ImmutableSet;
20 import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest;
21 import io.fd.hc2vpp.common.translate.util.Ipv4Translator;
22 import io.fd.hc2vpp.common.translate.util.NamingContext;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
25 import io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory;
26 import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory;
27 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
28 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceStateAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfaces;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Address;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLength;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLengthBuilder;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import java.util.Arrays;
50 import java.util.List;
52 import static org.hamcrest.MatcherAssert.assertThat;
53 import static org.hamcrest.Matchers.*;
54 import static org.junit.Assert.assertEquals;
55 import static org.junit.Assert.assertTrue;
56 import static org.mockito.Matchers.any;
57 import static org.mockito.Mockito.*;
59 public class SubInterfaceIpv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder>
60 implements Ipv4Translator {
62 private static final String IFC_CTX_NAME = "ifc-test-instance";
63 private static final String IF_NAME = "local0";
64 private static final int IF_INDEX = 1;
65 private static final String SUB_IF_NAME = "local0.1";
66 private static final String SUB_IF_2_NAME = "local0.2";
67 private static final long SUB_IF_ID = 1;
68 private static final int SUB_IF_INDEX = 11;
69 private static final int SUB_IF_2_INDEX = 12;
70 private static final InstanceIdentifier<Ipv4> IP4_IID =
71 InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME))
72 .augmentation(SubinterfaceStateAugmentation.class)
73 .child(SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(SUB_IF_ID))
75 private static final Ipv4AddressNoZone IP1 = new Ipv4AddressNoZone("10.1.1.1");
76 private static final Ipv4AddressNoZone IP2 = new Ipv4AddressNoZone("10.1.1.2");
77 private static final short PREFIX_LENGTH = 16;
78 private InstanceIdentifier<Address> ifaceOneAddressOneIdentifier;
79 private InstanceIdentifier<Address> ifaceTwoAddressOneIdentifier;
80 private CacheKeyFactory cacheKeyFactory;
81 private NamingContext interfaceContext;
83 public SubInterfaceIpv4AddressCustomizerTest() {
84 super(Address.class, Ipv4Builder.class);
87 private static InstanceIdentifier<Address> getId() {
88 return IP4_IID.child(Address.class);
91 private static InstanceIdentifier<Address> getId(final Ipv4AddressNoZone ip) {
92 return IP4_IID.child(Address.class, new AddressKey(ip));
96 protected void setUp() throws Exception {
97 interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
99 ifaceOneAddressOneIdentifier = InstanceIdentifier.create(InterfacesState.class)
100 .child(Interface.class, new InterfaceKey(IF_NAME))
101 .augmentation(SubinterfaceStateAugmentation.class)
102 .child(SubInterfaces.class)
103 .child(SubInterface.class, new SubInterfaceKey(1L))
105 .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1")));
106 ifaceTwoAddressOneIdentifier = InstanceIdentifier.create(InterfacesState.class)
107 .child(Interface.class, new InterfaceKey(IF_NAME))
108 .augmentation(SubinterfaceStateAugmentation.class)
109 .child(SubInterfaces.class)
110 .child(SubInterface.class, new SubInterfaceKey(2L))
112 .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1")));
114 // to simulate complex key
115 cacheKeyFactory = new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class,
116 ImmutableSet.of(SubInterface.class));
118 defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME);
119 defineMapping(mappingContext, SUB_IF_NAME, SUB_IF_INDEX, IFC_CTX_NAME);
120 defineMapping(mappingContext, SUB_IF_2_NAME, SUB_IF_2_INDEX, IFC_CTX_NAME);
124 protected ListReaderCustomizer<Address, AddressKey, AddressBuilder> initCustomizer() {
125 return new SubInterfaceIpv4AddressCustomizer(api, interfaceContext);
129 public void testRead() throws ReadFailedException {
130 final AddressBuilder builder = mock(AddressBuilder.class);
131 when(api.ipAddressDump(any())).thenReturn(future(dump()));
132 getCustomizer().readCurrentAttributes(getId(IP2), builder, ctx);
133 verify(builder).setIp(IP2);
134 verify(builder).setSubnet(new PrefixLengthBuilder().setPrefixLength(PREFIX_LENGTH).build());
137 @Test(expected = ReadFailedException.class)
138 public void testReadFailed() throws ReadFailedException {
139 when(api.ipAddressDump(any())).thenReturn(failedFuture());
140 getCustomizer().readCurrentAttributes(getId(IP1), mock(AddressBuilder.class), ctx);
144 public void testGetAllIds() throws ReadFailedException {
145 when(api.ipAddressDump(any())).thenReturn(future(dump()));
146 final List<AddressKey> allIds = getCustomizer().getAllIds(getId(), ctx);
147 assertThat(allIds, hasSize(2));
148 assertThat(allIds, containsInAnyOrder(new AddressKey(IP1), new AddressKey(IP2)));
151 @Test(expected = ReadFailedException.class)
152 public void testGetAllIdsFailed() throws ReadFailedException {
153 when(api.ipAddressDump(any())).thenReturn(failedFuture());
154 getCustomizer().getAllIds(getId(), ctx);
158 public void testCachingScopeSpecificRequest() throws ReadFailedException {
159 fillCacheForTwoIfaces();
160 final AddressBuilder ifaceOneAddressBuilder = new AddressBuilder();
161 final AddressBuilder ifaceTwoAddressBuilder = new AddressBuilder();
163 getCustomizer().readCurrentAttributes(ifaceOneAddressOneIdentifier, ifaceOneAddressBuilder, ctx);
164 getCustomizer().readCurrentAttributes(ifaceTwoAddressOneIdentifier, ifaceTwoAddressBuilder, ctx);
166 // addresses have caching scope of parent interface, so returned address should have respective prefix lengths
167 assertEquals("192.168.2.1", ifaceOneAddressBuilder.getIp().getValue());
168 assertTrue(ifaceOneAddressBuilder.getSubnet() instanceof PrefixLength);
169 assertEquals(22, PrefixLength.class.cast(ifaceOneAddressBuilder.getSubnet()).getPrefixLength().intValue());
171 assertEquals("192.168.2.1", ifaceTwoAddressBuilder.getIp().getValue());
172 assertTrue(ifaceTwoAddressBuilder.getSubnet() instanceof PrefixLength);
173 assertEquals(23, PrefixLength.class.cast(ifaceTwoAddressBuilder.getSubnet()).getPrefixLength().intValue());
177 public void testCachingScopeGetAll() throws ReadFailedException {
178 fillCacheForFirstIfaceSecondEmpty();
180 final List<AddressKey> keysForIfaceOne = getCustomizer().getAllIds(ifaceOneAddressOneIdentifier, ctx);
181 Assert.assertThat(keysForIfaceOne, hasSize(1));
182 final AddressKey keyIfaceOne = keysForIfaceOne.get(0);
183 assertEquals("192.168.2.1", keyIfaceOne.getIp().getValue());
185 final List<AddressKey> keysForIfaceTwo = getCustomizer().getAllIds(ifaceTwoAddressOneIdentifier, ctx);
186 Assert.assertThat(keysForIfaceTwo, is(empty()));
189 private void fillCacheForTwoIfaces() {
190 IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails();
191 IpAddressDetails detailIfaceTwoAddressOne = new IpAddressDetails();
192 IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump();
193 IpAddressDetailsReplyDump replyIfaceTwo = new IpAddressDetailsReplyDump();
195 replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne);
196 replyIfaceTwo.ipAddressDetails = Arrays.asList(detailIfaceTwoAddressOne);
198 detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
199 detailIfaceOneAddressOne.prefixLength = 22;
201 detailIfaceTwoAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
202 detailIfaceTwoAddressOne.prefixLength = 23;
204 cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne);
205 cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), replyIfaceTwo);
208 private void fillCacheForFirstIfaceSecondEmpty() {
209 IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails();
210 IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump();
211 replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne);
213 detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
214 detailIfaceOneAddressOne.prefixLength = 22;
216 cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne);
217 cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), new IpAddressDetailsReplyDump());
220 private IpAddressDetailsReplyDump dump() {
221 final IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
223 final IpAddressDetails details1 = new IpAddressDetails();
224 details1.ip = new byte[]{10, 1, 1, 1};
225 details1.prefixLength = (byte) PREFIX_LENGTH;
226 reply.ipAddressDetails.add(details1);
228 final IpAddressDetails details2 = new IpAddressDetails();
229 details2.ip = new byte[]{10, 1, 1, 2};
230 details2.prefixLength = (byte) PREFIX_LENGTH;
231 reply.ipAddressDetails.add(details2);