85f528e1e9f8a806c5c29b9eccbf6ae810b23a0a
[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.honeycomb.v3po.translate.v3po.interfacesstate.ip;
18
19 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid;
21 import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.reverseBytes;
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import com.google.common.base.Optional;
29 import com.google.common.collect.ImmutableList;
30 import com.google.common.collect.Lists;
31 import io.fd.honeycomb.v3po.translate.ModificationCache;
32 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
33 import io.fd.honeycomb.v3po.translate.spi.read.RootReaderCustomizer;
34 import io.fd.honeycomb.v3po.translate.v3po.test.ListReaderCustomizerTest;
35 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
36 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.concurrent.CompletableFuture;
40 import java.util.stream.Collectors;
41 import org.junit.Test;
42 import org.mockito.Mockito;
43 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
44 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
46 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
60 import org.openvpp.jvpp.dto.IpAddressDetails;
61 import org.openvpp.jvpp.dto.IpAddressDetailsReplyDump;
62 import org.openvpp.jvpp.dto.IpAddressDump;
63
64 public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder> {
65
66     private static final String IFACE_NAME = "eth0";
67     private static final int IFACE_ID = 1;
68
69     private NamingContext interfacesContext;
70
71     public Ipv4AddressCustomizerTest() {
72         super(Address.class);
73     }
74
75     @Override
76     public void setUpBefore() {
77         interfacesContext = new NamingContext("generatedIfaceName", "test-instance");
78     }
79
80     @Override
81     protected RootReaderCustomizer<Address, AddressBuilder> initCustomizer() {
82         final KeyedInstanceIdentifier<Mapping, MappingKey> eth0Id = getMappingIid(IFACE_NAME, "test-instance");
83         final Optional<Mapping> eth0 = getMapping(IFACE_NAME, IFACE_ID);
84
85         final List<Mapping> allMappings = Lists.newArrayList(getMapping(IFACE_NAME, IFACE_ID).get());
86         final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build();
87         doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(eth0Id.firstIdentifierOf(Mappings.class));
88
89         doReturn(eth0).when(mappingContext).read(eth0Id);
90
91         return new Ipv4AddressCustomizer(api, interfacesContext);
92     }
93
94     private static InstanceIdentifier<Address> getId(final String address) {
95         return InstanceIdentifier.builder(InterfacesState.class)
96             .child(Interface.class, new InterfaceKey(IFACE_NAME))
97             .augmentation(Interface2.class)
98             .child(Ipv4.class)
99             .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address))))
100             .build();
101     }
102
103     @Test
104     public void testReadCurrentAttributesFromCache() throws ReadFailedException {
105         ModificationCache cache = new ModificationCache();
106
107         IpAddressDetails detail1 = new IpAddressDetails();
108         IpAddressDetails detail2 = new IpAddressDetails();
109         IpAddressDetails detail3 = new IpAddressDetails();
110
111         detail1.ip = reverseBytes(
112             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
113         detail2.ip = reverseBytes(
114             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
115         detail3.ip = reverseBytes(
116             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
117
118         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
119         reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
120
121         cache.put(Ipv4AddressCustomizer.class.getName(), reply);
122         when(ctx.getModificationCache()).thenReturn(cache);
123
124         final AddressBuilder builder = new AddressBuilder();
125         final InstanceIdentifier<Address> id = getId("192.168.2.1");
126
127         getCustomizer().readCurrentAttributes(id, builder, ctx);
128
129         assertEquals("192.168.2.1", builder.getIp().getValue());
130     }
131
132     @Test
133     public void testReadCurrentAttributesFromOperationalData() throws ReadFailedException {
134         ModificationCache cache = new ModificationCache();
135
136         IpAddressDetails detail1 = new IpAddressDetails();
137         IpAddressDetails detail2 = new IpAddressDetails();
138         IpAddressDetails detail3 = new IpAddressDetails();
139
140         detail1.ip = reverseBytes(
141             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
142         detail2.ip = reverseBytes(
143             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
144         detail3.ip = reverseBytes(
145             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
146
147         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
148         reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
149
150         CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
151         future.complete(reply);
152
153         when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
154         when(ctx.getModificationCache()).thenReturn(cache);
155
156
157         final AddressBuilder builder = new AddressBuilder();
158         final InstanceIdentifier<Address> id = getId("192.168.2.1");
159
160         getCustomizer().readCurrentAttributes(id, builder, ctx);
161
162         assertEquals("192.168.2.1", builder.getIp().getValue());
163     }
164
165     @Test
166     public void testGetAllIdsFromCache() throws ReadFailedException {
167         ModificationCache cache = new ModificationCache();
168
169         IpAddressDetails detail1 = new IpAddressDetails();
170         IpAddressDetails detail2 = new IpAddressDetails();
171         IpAddressDetails detail3 = new IpAddressDetails();
172
173         detail1.ip = reverseBytes(
174             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
175         detail2.ip = reverseBytes(
176             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
177         detail3.ip = reverseBytes(
178             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
179
180         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
181         reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
182
183         cache.put(Ipv4AddressCustomizer.class.getName(), reply);
184         when(ctx.getModificationCache()).thenReturn(cache);
185
186         final InstanceIdentifier<Address> id = getId("192.168.2.1");
187
188         List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
189             .map(key -> key.getIp())
190             .collect(Collectors.toList());
191
192         verify(api, times(0)).ipAddressDump(Mockito.any(IpAddressDump.class));
193         assertEquals(3, ids.size());
194         assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue()));
195         assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue()));
196         assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue()));
197     }
198
199     @Test
200     public void testGetAllIdsFromOperationalData() throws ReadFailedException {
201         ModificationCache cache = new ModificationCache();
202
203         IpAddressDetails detail1 = new IpAddressDetails();
204         IpAddressDetails detail2 = new IpAddressDetails();
205         IpAddressDetails detail3 = new IpAddressDetails();
206
207         detail1.ip = reverseBytes(
208             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))));
209         detail2.ip = reverseBytes(
210             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))));
211         detail3.ip = reverseBytes(
212             TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))));
213
214         IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
215         reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
216
217         CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
218         future.complete(reply);
219
220         when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
221         when(ctx.getModificationCache()).thenReturn(cache);
222
223         final InstanceIdentifier<Address> id = getId("192.168.2.1");
224
225         List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream()
226             .map(key -> key.getIp())
227             .collect(Collectors.toList());
228
229         assertEquals(3, ids.size());
230         assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue()));
231         assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue()));
232         assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue()));
233     }
234
235     @Test
236     public void testMerge() {
237
238         Address address = new AddressBuilder().build();
239         Ipv4Builder ipv4Builder = new Ipv4Builder();
240         getCustomizer().merge(ipv4Builder, Arrays.asList(address));
241
242         assertEquals(1, ipv4Builder.getAddress().size());
243         assertEquals(true, ipv4Builder.getAddress().contains(address));
244     }
245
246 }