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.v3po.translate.v3po.interfacesstate.ip;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
26 import com.google.common.collect.ImmutableList;
27 import io.fd.honeycomb.v3po.translate.ModificationCache;
28 import io.fd.honeycomb.v3po.translate.read.ReadContext;
29 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
30 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.concurrent.CompletableFuture;
34 import java.util.stream.Collectors;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.openvpp.jvpp.dto.IpAddressDetails;
49 import org.openvpp.jvpp.dto.IpAddressDetailsReplyDump;
50 import org.openvpp.jvpp.dto.IpAddressDump;
51 import org.openvpp.jvpp.future.FutureJVpp;
53 public class Ipv4AddressCustomizerTest {
56 public void testGetBuilder() {
57 assertNotNull(new Ipv4AddressCustomizer(mock(FutureJVpp.class)).getBuilder(null));
61 public void testReadCurrentAttributesFromCache() throws ReadFailedException {
62 ReadContext context = mock(ReadContext.class);
63 ModificationCache cache = new ModificationCache();
64 FutureJVpp jvpp = mock(FutureJVpp.class);
66 IpAddressDetails detail1 = new IpAddressDetails();
67 IpAddressDetails detail2 = new IpAddressDetails();
68 IpAddressDetails detail3 = new IpAddressDetails();
70 detail1.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
71 detail2.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")));
72 detail3.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3")));
74 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
75 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
77 cache.put(Ipv4AddressCustomizer.class.getName(), reply);
78 when(context.getModificationCache()).thenReturn(cache);
80 AddressBuilder builder = new AddressBuilder();
81 InstanceIdentifier<Address> id = InstanceIdentifier.builder(InterfacesState.class)
82 .child(Interface.class)
83 .augmentation(Interface2.class)
85 .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))))
88 new Ipv4AddressCustomizer(jvpp).readCurrentAttributes(id, builder, context);
90 assertEquals("1.2.168.192", builder.getIp().getValue());
94 public void testReadCurrentAttributesFromOperationalData() throws ReadFailedException {
95 ReadContext context = mock(ReadContext.class);
96 ModificationCache cache = new ModificationCache();
97 FutureJVpp jvpp = mock(FutureJVpp.class);
99 IpAddressDetails detail1 = new IpAddressDetails();
100 IpAddressDetails detail2 = new IpAddressDetails();
101 IpAddressDetails detail3 = new IpAddressDetails();
103 detail1.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
104 detail2.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")));
105 detail3.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3")));
107 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
108 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
110 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
111 future.complete(reply);
113 when(jvpp.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
114 when(context.getModificationCache()).thenReturn(cache);
116 AddressBuilder builder = new AddressBuilder();
117 InstanceIdentifier<Address> id = InstanceIdentifier.builder(InterfacesState.class)
118 .child(Interface.class)
119 .augmentation(Interface2.class)
121 .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))))
124 new Ipv4AddressCustomizer(jvpp).readCurrentAttributes(id, builder, context);
126 assertEquals("1.2.168.192", builder.getIp().getValue());
130 public void testGetAllIdsFromCache() throws ReadFailedException {
131 ReadContext context = mock(ReadContext.class);
132 ModificationCache cache = new ModificationCache();
133 FutureJVpp jvpp = mock(FutureJVpp.class);
135 IpAddressDetails detail1 = new IpAddressDetails();
136 IpAddressDetails detail2 = new IpAddressDetails();
137 IpAddressDetails detail3 = new IpAddressDetails();
139 detail1.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
140 detail2.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")));
141 detail3.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3")));
143 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
144 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
146 cache.put(Ipv4AddressCustomizer.class.getName(), reply);
147 when(context.getModificationCache()).thenReturn(cache);
148 List<Ipv4AddressNoZone> ids = new Ipv4AddressCustomizer(jvpp).getAllIds(null, context).stream()
149 .map(id -> id.getIp())
150 .collect(Collectors.toList());
152 verify(jvpp, times(0)).ipAddressDump(Mockito.any(IpAddressDump.class));
153 assertEquals(3, ids.size());
154 assertEquals(true, "1.2.168.192".equals(ids.get(0).getValue()));
155 assertEquals(true, "2.2.168.192".equals(ids.get(1).getValue()));
156 assertEquals(true, "3.2.168.192".equals(ids.get(2).getValue()));
160 public void testGetAllIdsFromOperationalData() throws ReadFailedException {
161 ReadContext context = mock(ReadContext.class);
162 ModificationCache cache = new ModificationCache();
163 FutureJVpp jvpp = mock(FutureJVpp.class);
165 IpAddressDetails detail1 = new IpAddressDetails();
166 IpAddressDetails detail2 = new IpAddressDetails();
167 IpAddressDetails detail3 = new IpAddressDetails();
169 detail1.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")));
170 detail2.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")));
171 detail3.ip = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3")));
173 IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
174 reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3);
176 CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>();
177 future.complete(reply);
179 when(jvpp.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future);
180 when(context.getModificationCache()).thenReturn(cache);
181 List<Ipv4AddressNoZone> ids = new Ipv4AddressCustomizer(jvpp).getAllIds(null, context).stream()
182 .map(id -> id.getIp())
183 .collect(Collectors.toList());
185 assertEquals(3, ids.size());
186 assertEquals(true, "1.2.168.192".equals(ids.get(0).getValue()));
187 assertEquals(true, "2.2.168.192".equals(ids.get(1).getValue()));
188 assertEquals(true, "3.2.168.192".equals(ids.get(2).getValue()));
192 public void testMerge() {
194 Address address = new AddressBuilder().build();
195 Ipv4Builder ipv4Builder = new Ipv4Builder();
196 new Ipv4AddressCustomizer(mock(FutureJVpp.class)).merge(ipv4Builder, Arrays.asList(address));
198 assertEquals(1, ipv4Builder.getAddress().size());
199 assertEquals(true, ipv4Builder.getAddress().contains(address));