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.srv6.util.function;
19 import static org.mockito.Mockito.when;
21 import com.google.common.base.Optional;
22 import io.fd.hc2vpp.common.translate.util.AddressTranslator;
23 import io.fd.hc2vpp.fib.management.FibManagementIIds;
24 import io.fd.hc2vpp.srv6.util.JvppRequestTest;
25 import io.fd.hc2vpp.srv6.write.sid.request.LocalSidFunctionRequest;
26 import io.fd.hc2vpp.srv6.write.sid.request.NoProtocolLocalSidRequest;
27 import io.fd.hc2vpp.srv6.write.sid.request.TableLookupLocalSidRequest;
28 import io.fd.hc2vpp.srv6.write.sid.request.XConnectLocalSidRequest;
29 import io.fd.honeycomb.translate.read.ReadContext;
30 import io.fd.vpp.jvpp.core.dto.SrLocalsidsDetails;
31 import java.util.Collections;
32 import org.junit.Assert;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.multi.paths.v6.PathsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.multi.paths.v6.paths.PathBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6._static.cfg.Sid;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6._static.cfg.SidBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.End;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndBuilder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDt4;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDt4Builder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDt6;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDt6Builder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx2;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx2Builder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx4;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx4Builder;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx6;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx6Builder;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndT;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndTBuilder;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndX;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndXBuilder;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.base.rev180301.PathAttrsCmn;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndDT4;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndDT6;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndDX2;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndDX4;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndDX6;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.TableId;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.Ipv4;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.Ipv6;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.VniReference;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.Table;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.TableBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.TableKey;
74 public class LocalSidFunctionBindingRegistryTest extends JvppRequestTest {
76 private static final Ipv6Address A = new Ipv6Address("A::101");
77 private static final Ipv6AddressNoZone A_NO_ZONE = new Ipv6AddressNoZone("a::101");
78 private static final Ipv4Address V4HOP = new Ipv4Address("0.0.1.0");
79 private static final Ipv4AddressNoZone A_V4 = new Ipv4AddressNoZone("10.0.0.1");
80 private static final String LOCAL_0 = "local0";
81 private static final TableId TABLE_ID_4 = new TableId(4L);
82 private static final TableKey TABLE_4_IPV6_KEY = new TableKey(Ipv6.class, new VniReference(TABLE_ID_4.getValue()));
83 private static final TableKey TABLE_4_IPV4_KEY = new TableKey(Ipv4.class, new VniReference(TABLE_ID_4.getValue()));
86 private ReadContext readCtx;
89 protected void init() {
90 MockitoAnnotations.initMocks(this);
91 defineMapping(mappingContext, "local0", 1, "interface-context");
92 defineMapping(mappingContext, "vlan0", 2, "interface-context");
93 when(ctx.getMappingContext()).thenReturn(mappingContext);
94 when(readCtx.getMappingContext()).thenReturn(mappingContext);
95 when(ctx.readAfter(FibManagementIIds.FM_FIB_TABLES.child(Table.class, TABLE_4_IPV6_KEY)))
96 .thenReturn(Optional.of(
97 new TableBuilder().setTableId(TABLE_4_IPV6_KEY.getTableId()).withKey(TABLE_4_IPV6_KEY)
98 .setAddressFamily(TABLE_4_IPV6_KEY.getAddressFamily()).build()));
99 when(ctx.readAfter(FibManagementIIds.FM_FIB_TABLES.child(Table.class, TABLE_4_IPV4_KEY)))
100 .thenReturn(Optional.of(
101 new TableBuilder().setTableId(TABLE_4_IPV4_KEY.getTableId()).withKey(TABLE_4_IPV4_KEY)
102 .setAddressFamily(TABLE_4_IPV4_KEY.getAddressFamily()).build()));
106 public void testEnd() {
107 End end = new EndBuilder().build();
108 Sid localSid = new SidBuilder()
111 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.End.class)
113 LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
114 Assert.assertTrue(request instanceof NoProtocolLocalSidRequest);
115 Assert.assertEquals(1, request.getFunction());
119 public void testEndVpp() {
120 SrLocalsidsDetails details = new SrLocalsidsDetails();
121 details.behavior = 1;
123 SidBuilder builder = new SidBuilder();
124 READ_REGISTRY.bind(details, readCtx, builder);
125 Assert.assertNotNull(builder.getEnd());
129 public void testEndX() {
130 EndX endX = new EndXBuilder()
131 .setPaths(new PathsBuilder().setPath(Collections.singletonList(
132 new PathBuilder().setRole(PathAttrsCmn.Role.PRIMARY)
134 .setPathIndex((short) 1)
135 .setInterface(LOCAL_0)
139 Sid localSid = new SidBuilder()
142 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndX.class)
144 LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
145 Assert.assertTrue(request instanceof XConnectLocalSidRequest);
146 Assert.assertEquals(2, request.getFunction());
147 XConnectLocalSidRequest xConnectRequest = XConnectLocalSidRequest.class.cast(request);
148 Assert.assertEquals(A, xConnectRequest.getNextHopAddress().getIpv6Address());
149 Assert.assertEquals(1, xConnectRequest.getOutgoingInterfaceIndex());
153 public void testEndXVpp() {
154 SrLocalsidsDetails details = new SrLocalsidsDetails();
155 details.behavior = 2;
157 details.xconnectNhAddr6 = AddressTranslator.INSTANCE.ipv6AddressNoZoneToArray(A);
158 details.xconnectIfaceOrVrfTable = 1;
159 SidBuilder builder = new SidBuilder();
160 READ_REGISTRY.bind(details, readCtx, builder);
161 Assert.assertNotNull(builder.getEndX());
162 Assert.assertEquals(LOCAL_0, builder.getEndX().getPaths().getPath().get(0).getInterface());
163 Assert.assertEquals(A_NO_ZONE, builder.getEndX().getPaths().getPath().get(0).getNextHop());
167 public void testEndDX2() {
168 EndDx2 endDx2 = new EndDx2Builder().setPaths(
169 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.end.dx2.PathsBuilder()
170 .setInterface(LOCAL_0)
172 Sid localSid = new SidBuilder()
174 .setEndBehaviorType(EndDX2.class)
176 LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
177 Assert.assertTrue(request instanceof XConnectLocalSidRequest);
178 Assert.assertEquals(5, request.getFunction());
179 XConnectLocalSidRequest xConnectRequest = XConnectLocalSidRequest.class.cast(request);
180 Assert.assertNull(xConnectRequest.getNextHopAddress());
181 Assert.assertEquals(1, xConnectRequest.getOutgoingInterfaceIndex());
185 public void testEndDX2Vpp() {
186 SrLocalsidsDetails details = new SrLocalsidsDetails();
187 details.behavior = 5;
188 details.xconnectIfaceOrVrfTable = 1;
189 SidBuilder builder = new SidBuilder();
190 READ_REGISTRY.bind(details, readCtx, builder);
191 Assert.assertNotNull(builder.getEndDx2());
192 Assert.assertEquals(LOCAL_0, builder.getEndDx2().getPaths().getInterface());
196 public void testEndDX6() {
197 EndDx6 endDx6 = new EndDx6Builder()
198 .setPaths(new PathsBuilder()
199 .setPath(Collections.singletonList(new PathBuilder()
201 .setInterface(LOCAL_0)
205 Sid localSid = new SidBuilder()
207 .setEndBehaviorType(EndDX6.class)
209 LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
210 Assert.assertTrue(request instanceof XConnectLocalSidRequest);
211 Assert.assertEquals(6, request.getFunction());
212 XConnectLocalSidRequest xConnectRequest = XConnectLocalSidRequest.class.cast(request);
213 Assert.assertEquals(A, xConnectRequest.getNextHopAddress().getIpv6Address());
214 Assert.assertEquals(1, xConnectRequest.getOutgoingInterfaceIndex());
218 public void testEndDX6Vpp() {
219 SrLocalsidsDetails details = new SrLocalsidsDetails();
220 details.behavior = 6;
221 details.xconnectIfaceOrVrfTable = 1;
222 details.xconnectNhAddr6 = AddressTranslator.INSTANCE.ipv6AddressNoZoneToArray(A);
223 SidBuilder builder = new SidBuilder();
224 READ_REGISTRY.bind(details, readCtx, builder);
225 Assert.assertNotNull(builder.getEndDx6());
226 Assert.assertEquals(LOCAL_0, builder.getEndDx6().getPaths().getPath().get(0).getInterface());
227 Assert.assertEquals(A_NO_ZONE, builder.getEndDx6().getPaths().getPath().get(0).getNextHop());
231 public void testEndDX4() {
232 EndDx4 endDx4 = new EndDx4Builder()
234 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.multi.paths.v4.PathsBuilder()
235 .setPath(Collections.singletonList(
236 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.multi.paths.v4.paths.PathBuilder()
238 .setInterface(LOCAL_0)
242 Sid localSid = new SidBuilder()
244 .setEndBehaviorType(EndDX4.class)
246 LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
247 Assert.assertTrue(request instanceof XConnectLocalSidRequest);
248 Assert.assertEquals(7, request.getFunction());
249 XConnectLocalSidRequest xConnectRequest = XConnectLocalSidRequest.class.cast(request);
250 Assert.assertEquals(V4HOP, xConnectRequest.getNextHopAddress().getIpv4Address());
251 Assert.assertEquals(1, xConnectRequest.getOutgoingInterfaceIndex());
255 public void testEndDX4Vpp() {
256 SrLocalsidsDetails details = new SrLocalsidsDetails();
257 details.behavior = 7;
258 details.xconnectIfaceOrVrfTable = 1;
259 details.xconnectNhAddr4 = AddressTranslator.INSTANCE.ipv4AddressNoZoneToArray(A_V4);
260 SidBuilder builder = new SidBuilder();
261 READ_REGISTRY.bind(details, readCtx, builder);
262 Assert.assertNotNull(builder.getEndDx4());
263 Assert.assertEquals(LOCAL_0, builder.getEndDx4().getPaths().getPath().get(0).getInterface());
264 Assert.assertEquals(A_V4, builder.getEndDx4().getPaths().getPath().get(0).getNextHop());
268 public void testEndT() {
269 EndT endT = new EndTBuilder().setLookupTableIpv6(TABLE_ID_4).build();
270 Sid localSid = new SidBuilder()
273 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndT.class)
275 LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
276 Assert.assertTrue(request instanceof TableLookupLocalSidRequest);
277 Assert.assertEquals(3, request.getFunction());
278 TableLookupLocalSidRequest tableLookupRequest = TableLookupLocalSidRequest.class.cast(request);
279 Assert.assertEquals(TABLE_ID_4.getValue().intValue(), tableLookupRequest.getLookupFibTable());
283 public void testEndDTVpp() {
284 SrLocalsidsDetails details = new SrLocalsidsDetails();
285 details.behavior = 3;
286 details.xconnectIfaceOrVrfTable = TABLE_ID_4.getValue().intValue();
287 SidBuilder builder = new SidBuilder();
288 READ_REGISTRY.bind(details, readCtx, builder);
289 Assert.assertNotNull(builder.getEndT());
290 Assert.assertEquals(TABLE_ID_4.getValue().intValue(),
291 builder.getEndT().getLookupTableIpv6().getValue().intValue());
295 public void testEndDT6() {
296 EndDt6 endDt6 = new EndDt6Builder().setLookupTableIpv6(TABLE_ID_4).build();
297 Sid localSid = new SidBuilder()
299 .setEndBehaviorType(EndDT6.class)
301 LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
302 Assert.assertTrue(request instanceof TableLookupLocalSidRequest);
303 Assert.assertEquals(8, request.getFunction());
304 TableLookupLocalSidRequest tableLookupRequest = TableLookupLocalSidRequest.class.cast(request);
305 Assert.assertEquals(TABLE_ID_4.getValue().intValue(), tableLookupRequest.getLookupFibTable());
309 public void testEndDT6Vpp() {
310 SrLocalsidsDetails details = new SrLocalsidsDetails();
311 details.behavior = 8;
312 details.xconnectIfaceOrVrfTable = TABLE_ID_4.getValue().intValue();
313 SidBuilder builder = new SidBuilder();
314 READ_REGISTRY.bind(details, readCtx, builder);
315 Assert.assertNotNull(builder.getEndDt6());
316 Assert.assertEquals(TABLE_ID_4.getValue().intValue(),
317 builder.getEndDt6().getLookupTableIpv6().getValue().intValue());
321 public void testEndDT4() {
322 EndDt4 endDt4 = new EndDt4Builder().setLookupTableIpv4(TABLE_ID_4).build();
323 Sid localSid = new SidBuilder()
325 .setEndBehaviorType(EndDT4.class)
327 LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
328 Assert.assertTrue(request instanceof TableLookupLocalSidRequest);
329 Assert.assertEquals(9, request.getFunction());
330 TableLookupLocalSidRequest tableLookupRequest = TableLookupLocalSidRequest.class.cast(request);
331 Assert.assertEquals(TABLE_ID_4.getValue().intValue(), tableLookupRequest.getLookupFibTable());
335 public void testEndDT4Vpp() {
336 SrLocalsidsDetails details = new SrLocalsidsDetails();
337 details.behavior = 9;
338 details.xconnectIfaceOrVrfTable = TABLE_ID_4.getValue().intValue();
339 SidBuilder builder = new SidBuilder();
340 READ_REGISTRY.bind(details, readCtx, builder);
341 Assert.assertNotNull(builder.getEndDt4());
342 Assert.assertEquals(TABLE_ID_4.getValue().intValue(),
343 builder.getEndDt4().getLookupTableIpv4().getValue().intValue());