cfc7bc6dd49f9b7794ac8fdf7f3c098870d35931
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.hc2vpp.v3po.interfacesstate.ip;
18
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;
30
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.hc2vpp.v3po.interfacesstate.ip.v4.subinterface.SubInterfaceIpv4AddressCustomizer;
36 import io.fd.honeycomb.translate.read.ReadFailedException;
37 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
38 import io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory;
39 import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory;
40 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
41 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
42 import java.util.Arrays;
43 import java.util.List;
44 import org.junit.Assert;
45 import org.junit.Test;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceStateAugmentation;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfaces;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Address;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLength;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLengthBuilder;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63
64 public class SubInterfaceIpv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder>
65         implements Ipv4Translator {
66
67     private static final String IFC_CTX_NAME = "ifc-test-instance";
68     private static final String IF_NAME = "local0";
69     private static final int IF_INDEX = 1;
70     private static final String SUB_IF_NAME = "local0.1";
71     private static final String SUB_IF_2_NAME = "local0.2";
72     private static final long SUB_IF_ID = 1;
73     private static final int SUB_IF_INDEX = 11;
74     private static final int SUB_IF_2_INDEX = 12;
75     private static final InstanceIdentifier<Ipv4> IP4_IID =
76             InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME))
77                     .augmentation(SubinterfaceStateAugmentation.class)
78                     .child(SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(SUB_IF_ID))
79                     .child(Ipv4.class);
80     private static final Ipv4AddressNoZone IP1 = new Ipv4AddressNoZone("10.1.1.1");
81     private static final Ipv4AddressNoZone IP2 = new Ipv4AddressNoZone("10.1.1.2");
82     private static final short PREFIX_LENGTH = 16;
83     private InstanceIdentifier<Address> ifaceOneAddressOneIdentifier;
84     private InstanceIdentifier<Address> ifaceTwoAddressOneIdentifier;
85     private CacheKeyFactory cacheKeyFactory;
86     private NamingContext interfaceContext;
87
88     public SubInterfaceIpv4AddressCustomizerTest() {
89         super(Address.class, Ipv4Builder.class);
90     }
91
92     private static InstanceIdentifier<Address> getId() {
93         return IP4_IID.child(Address.class);
94     }
95
96     private static InstanceIdentifier<Address> getId(final Ipv4AddressNoZone ip) {
97         return IP4_IID.child(Address.class, new AddressKey(ip));
98     }
99
100     @Override
101     protected void setUp() throws Exception {
102         interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
103
104         ifaceOneAddressOneIdentifier = InstanceIdentifier.create(InterfacesState.class)
105                 .child(Interface.class, new InterfaceKey(IF_NAME))
106                 .augmentation(SubinterfaceStateAugmentation.class)
107                 .child(SubInterfaces.class)
108                 .child(SubInterface.class, new SubInterfaceKey(1L))
109                 .child(Ipv4.class)
110                 .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1")));
111         ifaceTwoAddressOneIdentifier = InstanceIdentifier.create(InterfacesState.class)
112                 .child(Interface.class, new InterfaceKey(IF_NAME))
113                 .augmentation(SubinterfaceStateAugmentation.class)
114                 .child(SubInterfaces.class)
115                 .child(SubInterface.class, new SubInterfaceKey(2L))
116                 .child(Ipv4.class)
117                 .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1")));
118
119         // to simulate complex key
120         cacheKeyFactory = new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class,
121                 ImmutableSet.of(SubInterface.class));
122
123         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME);
124         defineMapping(mappingContext, SUB_IF_NAME, SUB_IF_INDEX, IFC_CTX_NAME);
125         defineMapping(mappingContext, SUB_IF_2_NAME, SUB_IF_2_INDEX, IFC_CTX_NAME);
126     }
127
128     @Override
129     protected ListReaderCustomizer<Address, AddressKey, AddressBuilder> initCustomizer() {
130         return new SubInterfaceIpv4AddressCustomizer(api, interfaceContext);
131     }
132
133     @Test
134     public void testRead() throws ReadFailedException {
135         final AddressBuilder builder = mock(AddressBuilder.class);
136         when(api.ipAddressDump(any())).thenReturn(future(dump()));
137         getCustomizer().readCurrentAttributes(getId(IP2), builder, ctx);
138         verify(builder).setIp(IP2);
139         verify(builder).setSubnet(new PrefixLengthBuilder().setPrefixLength(PREFIX_LENGTH).build());
140     }
141
142     @Test(expected = ReadFailedException.class)
143     public void testReadFailed() throws ReadFailedException {
144         when(api.ipAddressDump(any())).thenReturn(failedFuture());
145         getCustomizer().readCurrentAttributes(getId(IP1), mock(AddressBuilder.class), ctx);
146     }
147
148     @Test
149     public void testGetAllIds() throws ReadFailedException {
150         when(api.ipAddressDump(any())).thenReturn(future(dump()));
151         final List<AddressKey> allIds = getCustomizer().getAllIds(getId(), ctx);
152         assertThat(allIds, hasSize(2));
153         assertThat(allIds, containsInAnyOrder(new AddressKey(IP1), new AddressKey(IP2)));
154     }
155
156     @Test(expected = ReadFailedException.class)
157     public void testGetAllIdsFailed() throws ReadFailedException {
158         when(api.ipAddressDump(any())).thenReturn(failedFuture());
159         getCustomizer().getAllIds(getId(), ctx);
160     }
161
162     @Test
163     public void testCacheScope() {
164
165     }
166
167     @Test
168     public void testCachingScopeSpecificRequest() throws ReadFailedException {
169         fillCacheForTwoIfaces();
170         final AddressBuilder ifaceOneAddressBuilder = new AddressBuilder();
171         final AddressBuilder ifaceTwoAddressBuilder = new AddressBuilder();
172
173         getCustomizer().readCurrentAttributes(ifaceOneAddressOneIdentifier, ifaceOneAddressBuilder, ctx);
174         getCustomizer().readCurrentAttributes(ifaceTwoAddressOneIdentifier, ifaceTwoAddressBuilder, ctx);
175
176         // addresses have caching scope of parent interface, so returned address should have respective prefix lengths
177         assertEquals("192.168.2.1", ifaceOneAddressBuilder.getIp().getValue());
178         assertTrue(ifaceOneAddressBuilder.getSubnet() instanceof PrefixLength);
179         assertEquals(22, PrefixLength.class.cast(ifaceOneAddressBuilder.getSubnet()).getPrefixLength().intValue());
180
181         assertEquals("192.168.2.1", ifaceTwoAddressBuilder.getIp().getValue());
182         assertTrue(ifaceTwoAddressBuilder.getSubnet() instanceof PrefixLength);
183         assertEquals(23, PrefixLength.class.cast(ifaceTwoAddressBuilder.getSubnet()).getPrefixLength().intValue());
184     }
185
186     @Test
187     public void testCachingScopeGetAll() throws ReadFailedException {
188         fillCacheForFirstIfaceSecondEmpty();
189
190         final List<AddressKey> keysForIfaceOne = getCustomizer().getAllIds(ifaceOneAddressOneIdentifier, ctx);
191         Assert.assertThat(keysForIfaceOne, hasSize(1));
192         final AddressKey keyIfaceOne = keysForIfaceOne.get(0);
193         assertEquals("192.168.2.1", keyIfaceOne.getIp().getValue());
194
195         final List<AddressKey> keysForIfaceTwo = getCustomizer().getAllIds(ifaceTwoAddressOneIdentifier, ctx);
196         Assert.assertThat(keysForIfaceTwo, is(empty()));
197     }
198
199     private void fillCacheForTwoIfaces() {
200         IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails();
201         IpAddressDetails detailIfaceTwoAddressOne = new IpAddressDetails();
202         IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump();
203         IpAddressDetailsReplyDump replyIfaceTwo = new IpAddressDetailsReplyDump();
204
205         replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne);
206         replyIfaceTwo.ipAddressDetails = Arrays.asList(detailIfaceTwoAddressOne);
207
208         detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
209         detailIfaceOneAddressOne.prefixLength = 22;
210
211         detailIfaceTwoAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
212         detailIfaceTwoAddressOne.prefixLength = 23;
213
214         cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne);
215         cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), replyIfaceTwo);
216     }
217
218     private void fillCacheForFirstIfaceSecondEmpty() {
219         IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails();
220         IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump();
221         replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne);
222
223         detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
224         detailIfaceOneAddressOne.prefixLength = 22;
225
226         cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne);
227         cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), new IpAddressDetailsReplyDump());
228     }
229
230     private IpAddressDetailsReplyDump dump() {
231         final IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
232
233         final IpAddressDetails details1 = new IpAddressDetails();
234         details1.ip = new byte[]{10, 1, 1, 1};
235         details1.prefixLength = (byte) PREFIX_LENGTH;
236         reply.ipAddressDetails.add(details1);
237
238         final IpAddressDetails details2 = new IpAddressDetails();
239         details2.ip = new byte[]{10, 1, 1, 2};
240         details2.prefixLength = (byte) PREFIX_LENGTH;
241         reply.ipAddressDetails.add(details2);
242
243         return reply;
244     }
245 }