20208954c2d3de2432877cbb2bc127283d769b78
[bgpcep.git] / bgp / extensions / mvpn / src / test / java / org / opendaylight / protocol / bgp / mvpn / impl / attributes / PMSITunnelAttributeHandlerTest.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.mvpn.impl.attributes;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.BIDIR_PIM_EXPECTED;
13 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.INGRESS_REPLICATION_EXPECTED;
14 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.M_LDP_MP_2_MP_LSP_EXPECTED;
15 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.M_LDP_MP_2_MP_LSP_WRONG;
16 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.M_LDP_P2MP_LSP_EXPECTED_IPV4;
17 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.M_LDP_P2MP_LSP_EXPECTED_IPV4_2;
18 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.M_LDP_P2MP_LSP_EXPECTED_IPV6;
19 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.M_LDP_P2MP_LSP_EXPECTED_L2VPN;
20 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.M_LDP_P2MP_LSP_EXPECTED_WRONG_FAMILY;
21 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.NO_TUNNEL_INFORMATION_PRESENT_EXPECTED;
22 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.PIM_SM_TREE_EXPECTED;
23 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.PIM_SSM_TREE_EXPECTED;
24 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.RSVP_TE_P2MP_LSP_LSP_EXPECTED;
25 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildBidirPimTreeAttribute;
26 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildIngressReplicationAttribute;
27 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildMLDpMp2mPLspAttribute;
28 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildMldpMP2mpLspWrongAttribute;
29 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildMldpP2mpLspIpv4Attribute;
30 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildMldpP2mpLspIpv6Attribute;
31 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildMldpp2MPLspL2vpnAttribute;
32 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildNoSupportedFamilyAttribute;
33 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildNoSupportedOpaqueAttribute;
34 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildPimSMTreeAttribute;
35 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildPimSSMTreeAttribute;
36 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildRsvpTep2MPLspAttribute;
37 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.PMSITunnelAttributeHandlerTestUtil.buildWOTunnelInfAttribute;
38
39 import io.netty.buffer.ByteBuf;
40 import io.netty.buffer.Unpooled;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.opendaylight.protocol.bgp.mvpn.impl.BGPActivator;
44 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
45 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
46 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
47 import org.opendaylight.protocol.util.ByteArray;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
50
51 public final class PMSITunnelAttributeHandlerTest {
52
53     private AttributeRegistry handler;
54
55     @Before
56     public void setUp() {
57         final BGPExtensionProviderContext ctx = new SimpleBGPExtensionProviderContext();
58
59         final org.opendaylight.protocol.bgp.parser.impl.BGPActivator inetActivator =
60                 new org.opendaylight.protocol.bgp.parser.impl.BGPActivator();
61         inetActivator.start(ctx);
62         final BGPActivator bgpActivator = new BGPActivator();
63         bgpActivator.start(ctx);
64         this.handler = ctx.getAttributeRegistry();
65     }
66
67     @Test
68     public void testBidirPimTree() throws Exception {
69         final Attributes attributes = buildBidirPimTreeAttribute();
70         final ByteBuf actual = Unpooled.buffer();
71         this.handler.serializeAttribute(attributes, actual);
72         assertArrayEquals(BIDIR_PIM_EXPECTED, ByteArray.readAllBytes(actual));
73         final Attributes expected = buildBidirPimTreeAttribute();
74         final Attributes actualAttr = this.handler.parseAttributes(
75                 Unpooled.wrappedBuffer(BIDIR_PIM_EXPECTED), null).getAttributes();
76         assertEquals(expected, actualAttr);
77     }
78
79     @Test
80     public void testPimSMTree() throws Exception {
81         final Attributes attributes = buildPimSMTreeAttribute();
82         final ByteBuf actual = Unpooled.buffer();
83         this.handler.serializeAttribute(attributes, actual);
84         assertArrayEquals(PIM_SM_TREE_EXPECTED, ByteArray.readAllBytes(actual));
85         final Attributes expected = buildPimSMTreeAttribute();
86         final Attributes actualAttr = this.handler.parseAttributes(
87                 Unpooled.wrappedBuffer(PIM_SM_TREE_EXPECTED), null).getAttributes();
88         assertEquals(expected, actualAttr);
89     }
90
91     @Test
92     public void serializePimSSMTree() {
93         final Attributes attributes = buildPimSSMTreeAttribute();
94         final ByteBuf actual = Unpooled.buffer();
95         this.handler.serializeAttribute(attributes, actual);
96         assertArrayEquals(PIM_SSM_TREE_EXPECTED, ByteArray.readAllBytes(actual));
97     }
98
99     @Test
100     public void parsePimSSMTree() throws Exception {
101         final Attributes expected = buildPimSSMTreeAttribute();
102         final Attributes actual = this.handler.parseAttributes(
103                 Unpooled.wrappedBuffer(PIM_SSM_TREE_EXPECTED), null).getAttributes();
104         assertEquals(expected, actual);
105     }
106
107     @Test
108     public void testMldpP2MpLsp() throws Exception {
109         final Attributes expectedIpv4Att = buildMldpP2mpLspIpv4Attribute();
110         final ByteBuf actualIpv4 = Unpooled.buffer();
111         this.handler.serializeAttribute(expectedIpv4Att, actualIpv4);
112         assertArrayEquals(M_LDP_P2MP_LSP_EXPECTED_IPV4, ByteArray.readAllBytes(actualIpv4));
113
114         final Attributes actualIpv4Attribute = this.handler.parseAttributes(
115                 Unpooled.wrappedBuffer(M_LDP_P2MP_LSP_EXPECTED_IPV4_2), null).getAttributes();
116         assertEquals(expectedIpv4Att, actualIpv4Attribute);
117
118         final Attributes expectedIpv6Att = buildMldpP2mpLspIpv6Attribute();
119         final ByteBuf actualIpv6 = Unpooled.buffer();
120         this.handler.serializeAttribute(expectedIpv6Att, actualIpv6);
121         assertArrayEquals(M_LDP_P2MP_LSP_EXPECTED_IPV6, ByteArray.readAllBytes(actualIpv6));
122
123         final Attributes actualIpv6Attribute = this.handler.parseAttributes(
124                 Unpooled.wrappedBuffer(M_LDP_P2MP_LSP_EXPECTED_IPV6), null).getAttributes();
125         assertEquals(expectedIpv6Att, actualIpv6Attribute);
126
127         final ByteBuf actualL2vpn = Unpooled.buffer();
128         this.handler.serializeAttribute(buildNoSupportedFamilyAttribute(), actualL2vpn);
129         assertArrayEquals(new byte[0], ByteArray.readAllBytes(actualIpv4));
130
131
132         final Attributes actualWrongFamily = this.handler.parseAttributes(
133                 Unpooled.wrappedBuffer(M_LDP_P2MP_LSP_EXPECTED_WRONG_FAMILY), null).getAttributes();
134         assertEquals(buildWOTunnelInfAttribute(), actualWrongFamily);
135
136         final Attributes expectedL2vpnAtt = buildMldpp2MPLspL2vpnAttribute();
137
138         final Attributes actualL2vpnAttribute = this.handler.parseAttributes(
139                 Unpooled.wrappedBuffer(M_LDP_P2MP_LSP_EXPECTED_L2VPN), null).getAttributes();
140         assertEquals(expectedL2vpnAtt, actualL2vpnAttribute);
141
142         final ByteBuf actualL2vp = Unpooled.buffer();
143         this.handler.serializeAttribute(expectedL2vpnAtt, actualL2vp);
144         assertArrayEquals(M_LDP_P2MP_LSP_EXPECTED_L2VPN, ByteArray.readAllBytes(actualL2vp));
145
146         final ByteBuf actualNonSupportedOpaques = Unpooled.buffer();
147         this.handler.serializeAttribute(buildNoSupportedOpaqueAttribute(), actualNonSupportedOpaques);
148         assertArrayEquals(NO_TUNNEL_INFORMATION_PRESENT_EXPECTED, ByteArray.readAllBytes(actualNonSupportedOpaques));
149     }
150
151     @Test
152     public void testRsvpteP2MplspLsp() throws Exception {
153         final Attributes expected = buildRsvpTep2MPLspAttribute();
154         final ByteBuf actual = Unpooled.buffer();
155         this.handler.serializeAttribute(expected, actual);
156         assertArrayEquals(RSVP_TE_P2MP_LSP_LSP_EXPECTED, ByteArray.readAllBytes(actual));
157         final Attributes actualAttr = this.handler.parseAttributes(
158                 Unpooled.wrappedBuffer(RSVP_TE_P2MP_LSP_LSP_EXPECTED), null).getAttributes();
159         assertEquals(expected, actualAttr);
160     }
161
162     @Test
163     public void testIngressReplication() throws Exception {
164         final Attributes expected = buildIngressReplicationAttribute();
165         final ByteBuf actual = Unpooled.buffer();
166         this.handler.serializeAttribute(expected, actual);
167         assertArrayEquals(INGRESS_REPLICATION_EXPECTED, ByteArray.readAllBytes(actual));
168         final Attributes actualAttr = this.handler.parseAttributes(
169                 Unpooled.wrappedBuffer(INGRESS_REPLICATION_EXPECTED), null).getAttributes();
170         assertEquals(expected, actualAttr);
171     }
172
173     @Test
174     public void testMldpmP2MpLsp() throws Exception {
175         final Attributes expected = buildMLDpMp2mPLspAttribute();
176         final ByteBuf actual = Unpooled.buffer();
177         this.handler.serializeAttribute(expected, actual);
178         assertArrayEquals(M_LDP_MP_2_MP_LSP_EXPECTED, ByteArray.readAllBytes(actual));
179
180         final Attributes actualAttr = this.handler.parseAttributes(
181                 Unpooled.wrappedBuffer(M_LDP_MP_2_MP_LSP_EXPECTED), null).getAttributes();
182         assertEquals(expected, actualAttr);
183
184         final Attributes actualWrong = this.handler.parseAttributes(
185                 Unpooled.wrappedBuffer(M_LDP_MP_2_MP_LSP_WRONG), null).getAttributes();
186         assertEquals(buildWOTunnelInfAttribute(), actualWrong);
187
188         final Attributes wrongAttribute = buildMldpMP2mpLspWrongAttribute();
189         final ByteBuf actualWrongBuf = Unpooled.buffer();
190         this.handler.serializeAttribute(wrongAttribute, actualWrongBuf);
191         assertArrayEquals(NO_TUNNEL_INFORMATION_PRESENT_EXPECTED, ByteArray.readAllBytes(actualWrongBuf));
192     }
193
194
195     @Test
196     public void serializeNoTunnelInfPresentExpected() throws Exception {
197         final Attributes attributes = buildWOTunnelInfAttribute();
198         final ByteBuf actual = Unpooled.buffer();
199         this.handler.serializeAttribute(attributes, actual);
200         assertArrayEquals(NO_TUNNEL_INFORMATION_PRESENT_EXPECTED, ByteArray.readAllBytes(actual));
201         final Attributes expected = buildWOTunnelInfAttribute();
202         final Attributes actualAttr = this.handler.parseAttributes(
203                 Unpooled.wrappedBuffer(NO_TUNNEL_INFORMATION_PRESENT_EXPECTED), null).getAttributes();
204         assertEquals(expected, actualAttr);
205     }
206
207     @Test
208     public void testPMSITunnelAttributeParser() {
209         final PMSITunnelAttributeHandler pmsiHandler = new PMSITunnelAttributeHandler();
210         assertEquals(22, pmsiHandler.getType());
211         final AttributesBuilder builder = new AttributesBuilder();
212         final ByteBuf emptyBuffer = Unpooled.buffer();
213         pmsiHandler.parseAttribute(emptyBuffer, builder, null);
214         final Attributes emptyAttributes = new AttributesBuilder().build();
215         assertEquals(emptyAttributes, builder.build());
216         pmsiHandler.serializeAttribute(emptyAttributes, emptyBuffer);
217         assertEquals(Unpooled.buffer(), emptyBuffer);
218     }
219 }