BUG-6020: Implement Ribsupport for extensions
[bgpcep.git] / bgp / l3vpn / src / test / java / org / opendaylight / protocol / bgp / l3vpn / 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.ipv6;
9
10 import com.google.common.collect.Lists;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.List;
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStackBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisherBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev160331.l3vpn.ipv6.destination.VpnIpv6DestinationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev160413.l3vpn.ip.destination.type.VpnDestination;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev160413.l3vpn.ip.destination.type.VpnDestinationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
40
41 /**
42  * @author Kevin Wang
43  */
44 public class VpnIpv6NlriParserTest {
45
46     private static final VpnIpv6NlriParser PARSER = new VpnIpv6NlriParser();
47
48     private static final byte[] NLRI_TYPE1 = new byte[]{
49         (byte) 0x88,    // length
50         (byte) 0x00, (byte) 0x16, (byte) 0x31,  // label
51         0, 1, 1, 2, 3, 4, 1, 2, // route distinguisher
52         (byte) 0x20, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x56, (byte) 0x89    // specify the IPv6 prefix here
53     };
54     static final IpPrefix IPv6_PREFIX = new IpPrefix(new Ipv6Prefix("2001:2345:5689::/48"));
55     static final List<LabelStack> LABEL_STACK = Lists.newArrayList(
56         new LabelStackBuilder().setLabelValue(new MplsLabel(355L)).build());
57     static final RouteDistinguisher DISTINGUISHER = RouteDistinguisherBuilder.getDefaultInstance("1.2.3.4:258");
58     static final VpnDestination IPV6_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER).setPrefix(IPv6_PREFIX).setLabelStack(LABEL_STACK).build();
59
60     @Test
61     public void testMpReachNlri() throws BGPParsingException {
62         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
63         mpBuilder.setAfi(Ipv6AddressFamily.class);
64         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
65             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev160331.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv6CaseBuilder().setVpnIpv6Destination(
66                 new VpnIpv6DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV6_VPN)).build()
67             ).build()
68             ).build()
69         ).build();
70
71         final MpReachNlri mpReachExpected = mpBuilder.build();
72
73         final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
74         testBuilder.setAfi(Ipv6AddressFamily.class);
75         PARSER.parseNlri(Unpooled.copiedBuffer(NLRI_TYPE1), testBuilder);
76         Assert.assertEquals(mpReachExpected, testBuilder.build());
77
78         final ByteBuf output = Unpooled.buffer();
79         PARSER.serializeAttribute(
80             new AttributesBuilder().addAugmentation(Attributes1.class,
81                 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()
82             ).build(), output
83         );
84         Assert.assertArrayEquals(NLRI_TYPE1, ByteArray.readAllBytes(output));
85     }
86
87     @Test
88     public void testMpUnreachNlri() throws BGPParsingException {
89         final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
90         mpBuilder.setAfi(Ipv6AddressFamily.class);
91         mpBuilder.setWithdrawnRoutes(
92             new WithdrawnRoutesBuilder().setDestinationType(
93                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev160331.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv6CaseBuilder().setVpnIpv6Destination(
94                     new VpnIpv6DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV6_VPN)).build()
95                 ).build()
96             ).build()
97         ).build();
98
99         final MpUnreachNlri mpUnreachExpected = mpBuilder.build();
100
101         final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
102         testBuilder.setAfi(Ipv6AddressFamily.class);
103         PARSER.parseNlri(Unpooled.copiedBuffer(NLRI_TYPE1), testBuilder);
104         Assert.assertEquals(mpUnreachExpected, testBuilder.build());
105
106         final ByteBuf output = Unpooled.buffer();
107         PARSER.serializeAttribute(
108             new AttributesBuilder().addAugmentation(Attributes2.class,
109                 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected).build()
110             ).build(), output
111         );
112         Assert.assertArrayEquals(NLRI_TYPE1, ByteArray.readAllBytes(output));
113     }
114 }