2 * Copyright (c) 2018 Bell Canada, Pantheon Technologies 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.fib.management.read;
19 import static org.hamcrest.Matchers.hasItems;
20 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
21 import static org.junit.Assert.assertThat;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.when;
25 import io.fd.hc2vpp.common.test.read.InitializingListReaderCustomizerTest;
26 import io.fd.hc2vpp.common.translate.util.AddressTranslator;
27 import io.fd.hc2vpp.fib.management.FibManagementIIds;
28 import io.fd.honeycomb.translate.ModificationCache;
29 import io.fd.honeycomb.translate.read.ReadFailedException;
30 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
31 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
32 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
33 import io.fd.vpp.jvpp.core.dto.Ip6FibDetails;
34 import io.fd.vpp.jvpp.core.dto.Ip6FibDetailsReplyDump;
35 import io.fd.vpp.jvpp.core.dto.IpFibDetails;
36 import io.fd.vpp.jvpp.core.dto.IpFibDetailsReplyDump;
37 import io.fd.vpp.jvpp.core.types.FibPath;
38 import java.util.Collections;
39 import java.util.List;
40 import org.junit.Assert;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.Ipv4;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.Ipv6;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.VniReference;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.FibTablesBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.Table;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.TableBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.TableKey;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 public class FibTableCustomizerTest extends InitializingListReaderCustomizerTest<Table, TableKey, TableBuilder>
56 implements AddressTranslator {
58 private static final String IPV4_VRF_1 = "IPV4_VRF_1";
59 private static final IpAddress NEXT_HOP_1 = new IpAddress(new Ipv6Address("a::1"));
60 private static final IpAddress NEXT_HOP_2 = new IpAddress(new Ipv4Address("10.0.0.254"));
61 private static final InstanceIdentifier<Table> TABLE_V4_ID =
62 FibManagementIIds.FM_FIB_TABLES.child(Table.class, new TableKey(Ipv4.class, new VniReference(1L)));
63 private static final InstanceIdentifier<Table> TABLE_V6_ID =
64 FibManagementIIds.FM_FIB_TABLES.child(Table.class, new TableKey(Ipv6.class, new VniReference(1L)));
65 private static final IpAddress IP_ADDR_1 = new IpAddress(new Ipv6Address("a::"));
66 private static final IpAddress IP_ADDR_2 = new IpAddress(new Ipv4Address("10.0.0.1"));
67 private DumpCacheManager<Ip6FibDetailsReplyDump, Void> manager_v6;
68 private DumpCacheManager<IpFibDetailsReplyDump, Void> manager_v4;
71 private EntityDumpExecutor<Ip6FibDetailsReplyDump, Void> executor_v6;
74 private EntityDumpExecutor<IpFibDetailsReplyDump, Void> executor_v4;
77 private ModificationCache cache;
79 public FibTableCustomizerTest() {
80 super(Table.class, FibTablesBuilder.class);
84 public void setUp() throws ReadFailedException {
85 manager_v6 = new DumpCacheManager.DumpCacheManagerBuilder<Ip6FibDetailsReplyDump, Void>()
86 .withExecutor(executor_v6)
87 .acceptOnly(Ip6FibDetailsReplyDump.class)
89 manager_v4 = new DumpCacheManager.DumpCacheManagerBuilder<IpFibDetailsReplyDump, Void>()
90 .withExecutor(executor_v4)
91 .acceptOnly(IpFibDetailsReplyDump.class)
94 when(executor_v6.executeDump(any(), any())).thenReturn(replyDumpV6());
95 when(executor_v4.executeDump(any(), any())).thenReturn(replyDumpV4());
96 when(ctx.getModificationCache()).thenReturn(cache);
99 private Ip6FibDetailsReplyDump replyDumpV6() {
100 Ip6FibDetailsReplyDump replyDump = new Ip6FibDetailsReplyDump();
103 Ip6FibDetails ip6FibDetails = new Ip6FibDetails();
104 ip6FibDetails.tableId = 1;
105 ip6FibDetails.address = ipAddressToArray(IP_ADDR_1);
106 ip6FibDetails.addressLength = 22;
107 ip6FibDetails.path = new FibPath[]{};
109 FibPath path = new FibPath();
111 path.nextHop = ipAddressToArray(NEXT_HOP_1);
114 ip6FibDetails.path = new FibPath[]{path};
116 replyDump.ip6FibDetails = Collections.singletonList(ip6FibDetails);
120 private IpFibDetailsReplyDump replyDumpV4() {
121 IpFibDetailsReplyDump replyDump = new IpFibDetailsReplyDump();
124 IpFibDetails detail = new IpFibDetails();
126 detail.address = ipAddressToArray(IP_ADDR_2);
127 detail.addressLength = 24;
128 detail.tableName = IPV4_VRF_1.getBytes();
129 detail.path = new FibPath[]{};
131 FibPath path = new FibPath();
133 path.nextHop = ipAddressToArray(NEXT_HOP_2);
136 detail.path = new FibPath[]{path};
138 replyDump.ipFibDetails = Collections.singletonList(detail);
143 public void getAllIds() throws Exception {
144 final List<TableKey> keys = getCustomizer().getAllIds(TABLE_V6_ID, ctx);
146 assertThat(keys, hasSize(2));
147 assertThat(keys, hasItems(new TableKey(Ipv6.class, new VniReference(1L)),
148 new TableKey(Ipv4.class, new VniReference(1L))));
152 public void readCurrentAttributesSimpleHop() throws Exception {
153 TableBuilder builder = new TableBuilder();
154 getCustomizer().readCurrentAttributes(TABLE_V6_ID, builder, ctx);
156 Assert.assertEquals(Ipv6.class, builder.getAddressFamily());
157 Assert.assertEquals(1L, builder.getTableId().getValue().longValue());
158 Assert.assertNull(builder.getName());
160 builder = new TableBuilder();
161 getCustomizer().readCurrentAttributes(TABLE_V4_ID, builder, ctx);
163 Assert.assertEquals(Ipv4.class, builder.getAddressFamily());
164 Assert.assertEquals(1L, builder.getTableId().getValue().longValue());
165 Assert.assertEquals(IPV4_VRF_1, builder.getName());
169 public void testInit() {
170 final Table data = new TableBuilder().build();
171 invokeInitTest(TABLE_V4_ID, data, TABLE_V4_ID, data);
175 protected ReaderCustomizer<Table, TableBuilder> initCustomizer() {
176 return new FibTableCustomizer(manager_v4, manager_v6);