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