2 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.route.targetcontrain.impl.nlri;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.junit.runners.Parameterized;
21 import org.opendaylight.protocol.bgp.route.targetcontrain.impl.activators.NlriActivator;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.RouteTargetConstrainChoice;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.route.target.constrain.choice.RouteTargetConstrainAs4ExtendedCommunityCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.route.target.constrain.choice.RouteTargetConstrainDefaultCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.route.target.constrain.choice.RouteTargetConstrainIpv4RouteCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.route.target.constrain.choice.RouteTargetConstrainRouteCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ShortAsNumber;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.route.target.extended.community.grouping.As4RouteTargetExtendedCommunityBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommon;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommonBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.constrain._default.route.grouping.RouteTargetConstrainDefaultRouteBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.extended.community.grouping.RouteTargetExtendedCommunityBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.ipv4.grouping.RouteTargetIpv4Builder;
37 import org.opendaylight.yangtools.yang.common.Uint16;
38 import org.opendaylight.yangtools.yang.common.Uint32;
40 @RunWith(Parameterized.class)
41 public class SimpleRouteTargetConstrainNlriRegistryTest {
42 private static final As4SpecificCommon AS_COMMON = new As4SpecificCommonBuilder()
43 .setAsNumber(new AsNumber(Uint32.valueOf(20)))
44 .setLocalAdministrator(Uint16.valueOf(100)).build();
46 private static final byte[] RT_DEFAULT_BUFF = new byte[0];
47 private static final Integer RT_2_OCT_TYPE = 0;
48 private static final byte[] RT_2_OCT_BUFF = new byte[]{
51 private static final byte[] RT_2_OCT_BUFF_WT = new byte[]{
52 0, 2, 0, 35, 4, 2, 8, 7
54 private static final Integer RT_IPV4_TYPE = 1;
55 private static final byte[] RT_IPV4_BUFF = new byte[]{
56 12, 51, 2, 5, 0x15, 0x2d
58 private static final byte[] RT_IPV4_BUFF_WT = new byte[]{
59 1, 2, 12, 51, 2, 5, 0x15, 0x2d
61 private static final Integer RT_4_OCT_TYPE = 2;
62 private static final byte[] RT_4_OCT_BUFF = new byte[]{
65 private static final byte[] RT_4_OCT_BUFF_WT = new byte[]{
66 2, 2, 0, 0, 0, 20, 0, 100
68 private static RouteTargetConstrainChoice RT_DEFAULT = new RouteTargetConstrainDefaultCaseBuilder()
69 .setRouteTargetConstrainDefaultRoute(new RouteTargetConstrainDefaultRouteBuilder()
71 private static RouteTargetConstrainChoice RT_AS_2_OCT = new RouteTargetConstrainRouteCaseBuilder()
72 .setRouteTargetExtendedCommunity(new RouteTargetExtendedCommunityBuilder()
73 .setGlobalAdministrator(new ShortAsNumber(Uint32.valueOf(35)))
74 .setLocalAdministrator(new byte[]{4, 2, 8, 7})
76 private static RouteTargetConstrainChoice RT_IPV4 = new RouteTargetConstrainIpv4RouteCaseBuilder()
77 .setRouteTargetIpv4(new RouteTargetIpv4Builder()
78 .setGlobalAdministrator(new Ipv4AddressNoZone("12.51.2.5"))
79 .setLocalAdministrator(Uint16.valueOf(5421))
81 private static RouteTargetConstrainChoice RT_AS_4_OCT = new RouteTargetConstrainAs4ExtendedCommunityCaseBuilder()
82 .setAs4RouteTargetExtendedCommunity(new As4RouteTargetExtendedCommunityBuilder()
83 .setAs4SpecificCommon(AS_COMMON).build()).build();
85 private final SimpleRouteTargetConstrainNlriRegistry nlriRegistry
86 = SimpleRouteTargetConstrainNlriRegistry.getInstance();
87 private final Integer type;
88 private final byte[] expectedBuffer;
89 private final byte[] expectedBufferWithType;
90 private final RouteTargetConstrainChoice expected;
92 public SimpleRouteTargetConstrainNlriRegistryTest(
93 final RouteTargetConstrainChoice routeTargetConstrainChoice,
95 final byte[] expectedBuffer,
96 final byte[] expectedBufferWithType
98 this.expected = routeTargetConstrainChoice;
100 this.expectedBuffer = expectedBuffer;
101 this.expectedBufferWithType = expectedBufferWithType;
104 @Parameterized.Parameters
105 public static Collection<Object[]> data() {
106 return Arrays.asList(new Object[][]{
107 {RT_AS_4_OCT, RT_4_OCT_TYPE, RT_4_OCT_BUFF, RT_4_OCT_BUFF_WT},
108 {RT_IPV4, RT_IPV4_TYPE, RT_IPV4_BUFF, RT_IPV4_BUFF_WT},
109 {RT_AS_2_OCT, RT_2_OCT_TYPE, RT_2_OCT_BUFF, RT_2_OCT_BUFF_WT},
110 {RT_DEFAULT, null, RT_DEFAULT_BUFF, RT_DEFAULT_BUFF}
115 public void setUp() {
116 NlriActivator.registerNlriParsers(new ArrayList<>());
120 public void testHandler() {
121 assertEquals(this.expected, this.nlriRegistry.parseRouteTargetConstrain(this.type,
122 Unpooled.copiedBuffer(this.expectedBuffer)));
123 assertArrayEquals(this.expectedBufferWithType,
124 ByteArray.getAllBytes(this.nlriRegistry.serializeRouteTargetConstrain(this.expected)));