Code clean up
[bgpcep.git] / bgp / extensions / l3vpn / src / test / java / org / opendaylight / protocol / bgp / l3vpn / unicast / ipv4 / VpnIpv4NlriParserTest.java
1 /*
2  * Copyright (c) 2016 Cisco 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.ipv4;
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.Ipv4Prefix;
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.Ipv4AddressFamily;
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.ipv4.rev180329.l3vpn.ipv4.destination.VpnIpv4DestinationBuilder;
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 VpnIpv4NlriParserTest {
47
48     private static final VpnIpv4NlriParser PARSER = new VpnIpv4NlriParser();
49
50     /* Reach NLRI prefix value.
51     *
52     * prefix contents:
53     * 70          <- length 112
54     * 00 16 3     <- labelValue 355
55     * 1           <- 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     * 22 01 16    <- prefixType IPV4=34.1.22.0/24
60     */
61     private static final byte[] REACH_NLRI = new byte[] {
62         (byte) 0x70,
63         (byte) 0x00, (byte) 0x16, (byte) 0x31,
64         0, 1, 1, 2, 3, 4, 1, 2,
65         (byte) 0x22, (byte) 0x01, (byte) 0x16,
66     };
67
68     /* Unreach NLRI prefix value.
69     *
70     * prefix contents:
71     * 70          <- length 112
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     * 22 01 16    <- prefixType IPV4=34.1.22.0/24
77     */
78     private static final byte[] UNREACH_NLRI = new byte[] {
79         (byte) 0x70,
80         (byte) 0x80, (byte) 0x00, (byte) 0x00,
81         0, 1, 1, 2, 3, 4, 1, 2,
82         (byte) 0x22, (byte) 0x01, (byte) 0x16,
83     };
84
85     static final IpPrefix IPV4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
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 IPV4_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER)
91             .setPrefix(IPV4_PREFIX).setPathId(NON_PATH_ID).setLabelStack(LABEL_STACK).build();
92     private static final VpnDestination IPV4_VPN_WITHOUT_LABELS = new VpnDestinationBuilder()
93             .setRouteDistinguisher(DISTINGUISHER).setPrefix(IPV4_PREFIX).build();
94
95     @Test
96     public void testMpReachNlri() throws BGPParsingException {
97         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
98         mpBuilder.setAfi(Ipv4AddressFamily.class);
99         mpBuilder.setSafi(MplsLabeledVpnSubsequentAddressFamily.class);
100         mpBuilder.setAdvertizedRoutes(
101             new AdvertizedRoutesBuilder().setDestinationType(
102                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
103                         .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv4CaseBuilder()
104                         .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
105                                 Collections.singletonList(new VpnDestinationBuilder(IPV4_VPN).setPathId(null)
106                                         .build())).build()).build()).build()).build();
107
108         final MpReachNlri mpReachExpected = mpBuilder.build();
109
110         final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
111         testBuilder.setAfi(Ipv4AddressFamily.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(Ipv4AddressFamily.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.ipv4.rev180329.update
134                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder()
135                         .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
136                                 Collections.singletonList(IPV4_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.ipv4.rev180329.update
142                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder()
143                         .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
144                                 Collections.singletonList(IPV4_VPN)).build()).build()).build()).build();
145         final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
146
147         final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
148         testBuilder.setAfi(Ipv4AddressFamily.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 }