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