Code clean up
[bgpcep.git] / bgp / l3vpn / src / test / java / org / opendaylight / protocol / bgp / l3vpn / unicast / ipv6 / VpnIpv6NlriParserTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  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 package org.opendaylight.protocol.bgp.l3vpn.unicast.ipv6;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.NON_PATH_ID;
13
14 import com.google.common.collect.Lists;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.Collections;
18 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStackBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.MplsLabeledVpnSubsequentAddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisher;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisherBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.l3vpn.ipv6.destination.VpnIpv6DestinationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestination;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestinationBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
45
46 public class VpnIpv6NlriParserTest {
47
48     private static final VpnIpv6NlriParser PARSER = new VpnIpv6NlriParser();
49
50     /* Reach NLRI prefix value.
51     *
52     * prefix contents:
53     * 88          <- length 136
54     * 00 16 3     <- labelValue 355
55     * 01          <- bottomBit 1
56     * 00 01       <- routeDistinguisher Type=1
57     * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
58     * 01 02       <- routeDistinguisher AS=258
59     * 20 01 23 45 56 89 <- prefixType IPV6=2001:2345:5689::/48
60     */
61     private static final byte[] REACH_NLRI = new byte[] {
62         (byte) 0x88,
63         (byte) 0x00, (byte) 0x16, (byte) 0x31,
64         0, 1, 1, 2, 3, 4, 1, 2,
65         (byte) 0x20, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x56, (byte) 0x89
66     };
67
68     /* Unreach NLRI prefix value.
69     *
70     * prefix contents:
71     * 88          <- length 136
72     * 80 00 00    <- labelValue for withdraw
73     * 00 01       <- routeDistinguisher Type=1
74     * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
75     * 01 02       <- routeDistinguisher AS=258
76     * 20 01 23 45 56 89 <- prefixType IPV6=2001:2345:5689::/48
77     */
78     private static final byte[] UNREACH_NLRI = new byte[] {
79         (byte) 0x88,
80         (byte) 0x80, (byte) 0x00, (byte) 0x00,
81         0, 1, 1, 2, 3, 4, 1, 2,
82         (byte) 0x20, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x56, (byte) 0x89
83     };
84
85     static final IpPrefix IPV6PREFIX = new IpPrefix(new Ipv6Prefix("2001:2345:5689::/48"));
86     static final List<LabelStack> LABEL_STACK = Lists.newArrayList(
87         new LabelStackBuilder().setLabelValue(new MplsLabel(355L)).build());
88     static final RouteDistinguisher DISTINGUISHER = RouteDistinguisherBuilder
89             .getDefaultInstance("1.2.3.4:258");
90     static final VpnDestination IPV6_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER)
91             .setPrefix(IPV6PREFIX).setLabelStack(LABEL_STACK).setPathId(NON_PATH_ID)
92             .build();
93     private static final VpnDestination IPV6_VPN_WITHOUT_LABELS = new VpnDestinationBuilder()
94             .setRouteDistinguisher(DISTINGUISHER).setPrefix(IPV6PREFIX).build();
95
96     @Test
97     public void testMpReachNlri() throws BGPParsingException {
98         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
99         mpBuilder.setAfi(Ipv6AddressFamily.class);
100         mpBuilder.setSafi(MplsLabeledVpnSubsequentAddressFamily.class);
101         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
102             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.update
103                     .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv6CaseBuilder()
104                     .setVpnIpv6Destination(new VpnIpv6DestinationBuilder().setVpnDestination(
105                             Collections.singletonList(new VpnDestinationBuilder(IPV6_VPN).setPathId(null).build()))
106                             .build()).build()).build()).build();
107
108         final MpReachNlri mpReachExpected = mpBuilder.build();
109
110         final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
111         testBuilder.setAfi(Ipv6AddressFamily.class);
112         testBuilder.setSafi(MplsLabeledVpnSubsequentAddressFamily.class);
113         PARSER.parseNlri(Unpooled.copiedBuffer(REACH_NLRI), testBuilder, null);
114         assertEquals(mpReachExpected, testBuilder.build());
115
116         final ByteBuf output = Unpooled.buffer();
117         PARSER.serializeAttribute(
118             new AttributesBuilder().addAugmentation(Attributes1.class,
119                 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()
120             ).build(), output
121         );
122         assertArrayEquals(REACH_NLRI, ByteArray.readAllBytes(output));
123     }
124
125     @Test
126     public void testMpUnreachNlri() throws BGPParsingException {
127         final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
128         mpBuilder.setAfi(Ipv6AddressFamily.class);
129         mpBuilder.setSafi(MplsLabeledVpnSubsequentAddressFamily.class);
130
131         mpBuilder.setWithdrawnRoutes(
132             new WithdrawnRoutesBuilder().setDestinationType(
133                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.update
134                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv6CaseBuilder()
135                         .setVpnIpv6Destination(new VpnIpv6DestinationBuilder().setVpnDestination(
136                                 Collections.singletonList(IPV6_VPN_WITHOUT_LABELS)).build()).build()).build()).build();
137         final MpUnreachNlri mpUnreachExpected1 = mpBuilder.build();
138
139         mpBuilder.setWithdrawnRoutes(
140             new WithdrawnRoutesBuilder().setDestinationType(
141                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.update
142                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv6CaseBuilder()
143                         .setVpnIpv6Destination(new VpnIpv6DestinationBuilder().setVpnDestination(
144                                 Collections.singletonList(IPV6_VPN)).build()).build()).build()).build();
145         final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
146
147         final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
148         testBuilder.setAfi(Ipv6AddressFamily.class);
149         testBuilder.setSafi(MplsLabeledVpnSubsequentAddressFamily.class);
150         PARSER.parseNlri(Unpooled.copiedBuffer(UNREACH_NLRI), testBuilder, null);
151         assertEquals(mpUnreachExpected1, testBuilder.build());
152
153         final ByteBuf output = Unpooled.buffer();
154         PARSER.serializeAttribute(
155             new AttributesBuilder().addAugmentation(Attributes2.class,
156                 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected2).build()
157             ).build(), output
158         );
159         assertArrayEquals(UNREACH_NLRI, ByteArray.readAllBytes(output));
160     }
161 }