c46f6d67c5c435f6cea98229c7e3c4cafa6a1def
[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.types.rev180329.ShortAsNumber;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.route.target.extended.community.grouping.As4RouteTargetExtendedCommunityBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommon;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommonBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.constrain._default.route.grouping.RouteTargetConstrainDefaultRouteBuilder;
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 import org.opendaylight.yangtools.yang.common.Uint16;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40
41 @RunWith(Parameterized.class)
42 public class SimpleRouteTargetConstrainNlriRegistryTest {
43     private static final As4SpecificCommon AS_COMMON = new As4SpecificCommonBuilder()
44             .setAsNumber(new AsNumber(Uint32.valueOf(20)))
45             .setLocalAdministrator(Uint16.valueOf(100)).build();
46
47     private static final byte[] RT_DEFAULT_BUFF = new byte[0];
48     private static final Integer RT_2_OCT_TYPE = 0;
49     private static final byte[] RT_2_OCT_BUFF = new byte[]{
50         0, 35, 4, 2, 8, 7
51     };
52     private static final byte[] RT_2_OCT_BUFF_WT = new byte[]{
53         0, 2, 0, 35, 4, 2, 8, 7
54     };
55     private static final Integer RT_IPV4_TYPE = 1;
56     private static final byte[] RT_IPV4_BUFF = new byte[]{
57         12, 51, 2, 5, 0x15, 0x2d
58     };
59     private static final byte[] RT_IPV4_BUFF_WT = new byte[]{
60         1, 2, 12, 51, 2, 5, 0x15, 0x2d
61     };
62     private static final Integer RT_4_OCT_TYPE = 2;
63     private static final byte[] RT_4_OCT_BUFF = new byte[]{
64         0, 0, 0, 20, 0, 100
65     };
66     private static final byte[] RT_4_OCT_BUFF_WT = new byte[]{
67         2, 2, 0, 0, 0, 20, 0, 100
68     };
69     private static RouteTargetConstrainChoice RT_DEFAULT = new RouteTargetConstrainDefaultCaseBuilder()
70             .setRouteTargetConstrainDefaultRoute(new RouteTargetConstrainDefaultRouteBuilder()
71                     .build()).build();
72     private static RouteTargetConstrainChoice RT_AS_2_OCT = new RouteTargetConstrainRouteCaseBuilder()
73             .setRouteTargetExtendedCommunity(new RouteTargetExtendedCommunityBuilder()
74                     .setGlobalAdministrator(new ShortAsNumber(Uint32.valueOf(35)))
75                     .setLocalAdministrator(new byte[]{4, 2, 8, 7})
76                     .build()).build();
77     private static RouteTargetConstrainChoice RT_IPV4 = new RouteTargetConstrainIpv4RouteCaseBuilder()
78             .setRouteTargetIpv4(new RouteTargetIpv4Builder()
79                     .setGlobalAdministrator(new Ipv4Address("12.51.2.5"))
80                     .setLocalAdministrator(Uint16.valueOf(5421))
81                     .build()).build();
82     private static RouteTargetConstrainChoice RT_AS_4_OCT = new RouteTargetConstrainAs4ExtendedCommunityCaseBuilder()
83             .setAs4RouteTargetExtendedCommunity(new As4RouteTargetExtendedCommunityBuilder()
84                     .setAs4SpecificCommon(AS_COMMON).build()).build();
85
86     private final SimpleRouteTargetConstrainNlriRegistry nlriRegistry
87             = SimpleRouteTargetConstrainNlriRegistry.getInstance();
88     private final Integer type;
89     private final byte[] expectedBuffer;
90     private final byte[] expectedBufferWithType;
91     private final RouteTargetConstrainChoice expected;
92
93     public SimpleRouteTargetConstrainNlriRegistryTest(
94             final RouteTargetConstrainChoice routeTargetConstrainChoice,
95             final Integer type,
96             final byte[] expectedBuffer,
97             final byte[] expectedBufferWithType
98     ) {
99         this.expected = routeTargetConstrainChoice;
100         this.type = type;
101         this.expectedBuffer = expectedBuffer;
102         this.expectedBufferWithType = expectedBufferWithType;
103     }
104
105     @Parameterized.Parameters
106     public static Collection<Object[]> data() {
107         return Arrays.asList(new Object[][]{
108                 {RT_AS_4_OCT, RT_4_OCT_TYPE, RT_4_OCT_BUFF, RT_4_OCT_BUFF_WT},
109                 {RT_IPV4, RT_IPV4_TYPE, RT_IPV4_BUFF, RT_IPV4_BUFF_WT},
110                 {RT_AS_2_OCT, RT_2_OCT_TYPE, RT_2_OCT_BUFF, RT_2_OCT_BUFF_WT},
111                 {RT_DEFAULT, null, RT_DEFAULT_BUFF, RT_DEFAULT_BUFF}
112         });
113     }
114
115     @Before
116     public void setUp() {
117         NlriActivator.registerNlriParsers(new ArrayList<>());
118     }
119
120     @Test
121     public void testHandler() {
122         assertEquals(this.expected, this.nlriRegistry.parseRouteTargetConstrain(this.type,
123                 Unpooled.copiedBuffer(this.expectedBuffer)));
124         assertArrayEquals(this.expectedBufferWithType,
125                 ByteArray.getAllBytes(this.nlriRegistry.serializeRouteTargetConstrain(this.expected)));
126     }
127 }