a892bee4fa069f6ea1956fa27490102ee0d496f9
[bgpcep.git] / bgp / extensions / route-target / src / test / java / org / opendaylight / protocol / bgp / route / targetcontrain / impl / nlri / SimpleRouteTargetConstrainNlriRegistryTest.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bgp.route.targetcontrain.impl.nlri;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.junit.runners.Parameterized;
22 import org.opendaylight.protocol.bgp.route.targetcontrain.impl.activators.NlriActivator;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.RouteTargetConstrainChoice;
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.RouteTargetConstrainAs4ExtendedCommunityCaseBuilder;
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.RouteTargetConstrainDefaultCaseBuilder;
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.RouteTargetConstrainIpv4RouteCaseBuilder;
30 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;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.route.target.constrain.choice.route.target.constrain._default._case.RouteTargetConstrainDefaultRouteBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ShortAsNumber;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.route.target.extended.community.grouping.As4RouteTargetExtendedCommunityBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommon;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommonBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.extended.community.grouping.RouteTargetExtendedCommunityBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.ipv4.grouping.RouteTargetIpv4Builder;
38
39 @RunWith(Parameterized.class)
40 public class SimpleRouteTargetConstrainNlriRegistryTest {
41     private static final As4SpecificCommon AS_COMMON = new As4SpecificCommonBuilder()
42             .setAsNumber(new AsNumber(20L))
43             .setLocalAdministrator(100).build();
44
45     private static final byte[] RT_DEFAULT_BUFF = new byte[0];
46     private static final Integer RT_2_OCT_TYPE = 0;
47     private static final byte[] RT_2_OCT_BUFF = new byte[]{
48         0, 35, 4, 2, 8, 7
49     };
50     private static final byte[] RT_2_OCT_BUFF_WT = new byte[]{
51         0, 2, 0, 35, 4, 2, 8, 7
52     };
53     private static final Integer RT_IPV4_TYPE = 1;
54     private static final byte[] RT_IPV4_BUFF = new byte[]{
55         12, 51, 2, 5, 0x15, 0x2d
56     };
57     private static final byte[] RT_IPV4_BUFF_WT = new byte[]{
58         1, 2, 12, 51, 2, 5, 0x15, 0x2d
59     };
60     private static final Integer RT_4_OCT_TYPE = 2;
61     private static final byte[] RT_4_OCT_BUFF = new byte[]{
62         0, 0, 0, 20, 0, 100
63     };
64     private static final byte[] RT_4_OCT_BUFF_WT = new byte[]{
65         2, 2, 0, 0, 0, 20, 0, 100
66     };
67     private static RouteTargetConstrainChoice RT_DEFAULT = new RouteTargetConstrainDefaultCaseBuilder()
68             .setRouteTargetConstrainDefaultRoute(new RouteTargetConstrainDefaultRouteBuilder()
69                     .build()).build();
70     private static RouteTargetConstrainChoice RT_AS_2_OCT = new RouteTargetConstrainRouteCaseBuilder()
71             .setRouteTargetExtendedCommunity(new RouteTargetExtendedCommunityBuilder()
72                     .setGlobalAdministrator(new ShortAsNumber(35L))
73                     .setLocalAdministrator(new byte[]{4, 2, 8, 7})
74                     .build()).build();
75     private static RouteTargetConstrainChoice RT_IPV4 = new RouteTargetConstrainIpv4RouteCaseBuilder()
76             .setRouteTargetIpv4(new RouteTargetIpv4Builder()
77                     .setGlobalAdministrator(new Ipv4Address("12.51.2.5"))
78                     .setLocalAdministrator(5421)
79                     .build()).build();
80     private static RouteTargetConstrainChoice RT_AS_4_OCT = new RouteTargetConstrainAs4ExtendedCommunityCaseBuilder()
81             .setAs4RouteTargetExtendedCommunity(new As4RouteTargetExtendedCommunityBuilder()
82                     .setAs4SpecificCommon(AS_COMMON).build()).build();
83
84     private final SimpleRouteTargetConstrainNlriRegistry nlriRegistry
85             = SimpleRouteTargetConstrainNlriRegistry.getInstance();
86     private final Integer type;
87     private final byte[] expectedBuffer;
88     private final byte[] expectedBufferWithType;
89     private RouteTargetConstrainChoice expected;
90
91     public SimpleRouteTargetConstrainNlriRegistryTest(
92             final RouteTargetConstrainChoice routeTargetConstrainChoice,
93             final Integer type,
94             final byte[] expectedBuffer,
95             final byte[] expectedBufferWithType
96     ) {
97         this.expected = routeTargetConstrainChoice;
98         this.type = type;
99         this.expectedBuffer = expectedBuffer;
100         this.expectedBufferWithType = expectedBufferWithType;
101     }
102
103     @Parameterized.Parameters
104     public static Collection<Object[]> data() {
105         return Arrays.asList(new Object[][]{
106                 {RT_AS_4_OCT, RT_4_OCT_TYPE, RT_4_OCT_BUFF, RT_4_OCT_BUFF_WT},
107                 {RT_IPV4, RT_IPV4_TYPE, RT_IPV4_BUFF, RT_IPV4_BUFF_WT},
108                 {RT_AS_2_OCT, RT_2_OCT_TYPE, RT_2_OCT_BUFF, RT_2_OCT_BUFF_WT},
109                 {RT_DEFAULT, null, RT_DEFAULT_BUFF, RT_DEFAULT_BUFF}
110         });
111     }
112
113     @Before
114     public void setUp() {
115         NlriActivator.registerNlriParsers(new ArrayList<>());
116     }
117
118     @Test
119     public void testHandler() {
120         assertEquals(this.expected, this.nlriRegistry.parseRouteTargetConstrain(this.type,
121                 Unpooled.copiedBuffer(this.expectedBuffer)));
122         assertArrayEquals(this.expectedBufferWithType,
123                 ByteArray.getAllBytes(this.nlriRegistry.serializeRouteTargetConstrain(this.expected)));
124     }
125 }