6b2d8c4d76256a4f60892df740b522d0f56075d0
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2018 Bell Canada, Pantheon Technologies 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.fib.management.read;
18
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;
24
25 import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest;
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;
54
55 public class FibTableCustomizerTest extends ListReaderCustomizerTest<Table, TableKey, TableBuilder> implements
56         AddressTranslator {
57
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;
69
70     @Mock
71     private EntityDumpExecutor<Ip6FibDetailsReplyDump, Void> executor_v6;
72
73     @Mock
74     private EntityDumpExecutor<IpFibDetailsReplyDump, Void> executor_v4;
75
76     @Mock
77     private ModificationCache cache;
78
79     public FibTableCustomizerTest() {
80         super(Table.class, FibTablesBuilder.class);
81     }
82
83     @Override
84     public void setUp() throws ReadFailedException {
85         manager_v6 = new DumpCacheManager.DumpCacheManagerBuilder<Ip6FibDetailsReplyDump, Void>()
86                 .withExecutor(executor_v6)
87                 .acceptOnly(Ip6FibDetailsReplyDump.class)
88                 .build();
89         manager_v4 = new DumpCacheManager.DumpCacheManagerBuilder<IpFibDetailsReplyDump, Void>()
90                 .withExecutor(executor_v4)
91                 .acceptOnly(IpFibDetailsReplyDump.class)
92                 .build();
93
94         when(executor_v6.executeDump(any(), any())).thenReturn(replyDumpV6());
95         when(executor_v4.executeDump(any(), any())).thenReturn(replyDumpV4());
96         when(ctx.getModificationCache()).thenReturn(cache);
97     }
98
99     private Ip6FibDetailsReplyDump replyDumpV6() {
100         Ip6FibDetailsReplyDump replyDump = new Ip6FibDetailsReplyDump();
101
102         //simple
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[]{};
108
109         FibPath path = new FibPath();
110         path.weight = 3;
111         path.nextHop = ipAddressToArray(NEXT_HOP_1);
112         path.afi = 0;
113         path.swIfIndex = 1;
114         ip6FibDetails.path = new FibPath[]{path};
115
116         replyDump.ip6FibDetails = Collections.singletonList(ip6FibDetails);
117         return replyDump;
118     }
119
120     private IpFibDetailsReplyDump replyDumpV4() {
121         IpFibDetailsReplyDump replyDump = new IpFibDetailsReplyDump();
122
123         //simple
124         IpFibDetails detail = new IpFibDetails();
125         detail.tableId = 1;
126         detail.address = ipAddressToArray(IP_ADDR_2);
127         detail.addressLength = 24;
128         detail.tableName = IPV4_VRF_1.getBytes();
129         detail.path = new FibPath[]{};
130
131         FibPath path = new FibPath();
132         path.weight = 3;
133         path.nextHop = ipAddressToArray(NEXT_HOP_2);
134         path.afi = 0;
135         path.swIfIndex = 1;
136         detail.path = new FibPath[]{path};
137
138         replyDump.ipFibDetails = Collections.singletonList(detail);
139         return replyDump;
140     }
141
142     @Test
143     public void getAllIds() throws Exception {
144         final List<TableKey> keys = getCustomizer().getAllIds(TABLE_V6_ID, ctx);
145
146         assertThat(keys, hasSize(2));
147         assertThat(keys, hasItems(new TableKey(Ipv6.class, new VniReference(1L)),
148                 new TableKey(Ipv4.class, new VniReference(1L))));
149     }
150
151     @Test
152     public void readCurrentAttributesSimpleHop() throws Exception {
153         TableBuilder builder = new TableBuilder();
154         getCustomizer().readCurrentAttributes(TABLE_V6_ID, builder, ctx);
155
156         Assert.assertEquals(Ipv6.class, builder.getAddressFamily());
157         Assert.assertEquals(1L, builder.getTableId().getValue().longValue());
158         Assert.assertNull(builder.getName());
159
160         builder = new TableBuilder();
161         getCustomizer().readCurrentAttributes(TABLE_V4_ID, builder, ctx);
162
163         Assert.assertEquals(Ipv4.class, builder.getAddressFamily());
164         Assert.assertEquals(1L, builder.getTableId().getValue().longValue());
165         Assert.assertEquals(IPV4_VRF_1, builder.getName());
166     }
167
168     @Override
169     protected ReaderCustomizer<Table, TableBuilder> initCustomizer() {
170         return new FibTableCustomizer(manager_v4, manager_v6);
171     }
172 }