Bug-2064: Implementation of RFC5492 Error handling.
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / ComplementaryTest.java
1 /*
2  * Copyright (c) 2013 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.parser.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotSame;
14 import static org.junit.Assert.fail;
15
16 import com.google.common.collect.Maps;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import java.util.Map;
20 import org.junit.Test;
21 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
22 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
23 import org.opendaylight.protocol.bgp.parser.impl.message.update.ExtendedCommunitiesAttributeParser;
24 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
25 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.protocol.util.NoopReferenceCache;
28 import org.opendaylight.protocol.util.ReferenceCache;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateAddressFamily;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.CParameters;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.As4BytesCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.As4BytesCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.as4.bytes._case.As4BytesCapabilityBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AggregatorBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.multiprotocol._case.MultiprotocolCapability;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.multiprotocol._case.MultiprotocolCapabilityBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpAggregator;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCase;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCase;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunityBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunityBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunityBuilder;
61
62 public class ComplementaryTest {
63
64     private final ReferenceCache ref = NoopReferenceCache.getInstance();
65
66     @Test
67     public void testBGPParameter() {
68
69         final BgpTableType t = new BgpTableTypeImpl(LinkstateAddressFamily.class, UnicastSubsequentAddressFamily.class);
70         final BgpTableType t1 = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
71
72         final MultiprotocolCapability cap = new MultiprotocolCapabilityBuilder().setAfi(LinkstateAddressFamily.class).setSafi(
73                 UnicastSubsequentAddressFamily.class).build();
74         final CParameters tlv1 = new MultiprotocolCaseBuilder().setMultiprotocolCapability(cap).build();
75
76         final MultiprotocolCapability cap1 = new MultiprotocolCapabilityBuilder().setAfi(Ipv4AddressFamily.class).setSafi(
77                 UnicastSubsequentAddressFamily.class).build();
78         final CParameters tlv2 = new MultiprotocolCaseBuilder().setMultiprotocolCapability(cap1).build();
79
80         final Map<BgpTableType, Boolean> tt = Maps.newHashMap();
81         tt.put(t, true);
82         tt.put(t1, false);
83
84         // FIXME: BUG-196: revive test for graceful capability tlv
85         // final BGPParameter tlv3 = new GracefulCapability(false, 0, tt);
86
87         final CParameters tlv4 = new As4BytesCaseBuilder().setAs4BytesCapability(
88                 new As4BytesCapabilityBuilder().setAsNumber(new AsNumber((long) 40)).build()).build();
89
90         // assertFalse(((GracefulCapability) tlv3).isRestartFlag());
91
92         // assertEquals(0, ((GracefulCapability) tlv3).getRestartTimerValue());
93
94         assertFalse(tlv1.equals(tlv2));
95
96         // assertNotSame(tlv1.hashCode(), tlv3.hashCode());
97
98         // assertNotSame(tlv2.toString(), tlv3.toString());
99
100         // assertEquals(((GracefulCapability) tlv3).getTableTypes(), tt);
101
102         assertEquals(cap.getSafi(), cap1.getSafi());
103
104         assertNotSame(cap.getAfi(), cap1.getAfi());
105
106         assertEquals(40, ((As4BytesCase) tlv4).getAs4BytesCapability().getAsNumber().getValue().longValue());
107
108         assertEquals(new As4BytesCaseBuilder().setAs4BytesCapability(
109                 new As4BytesCapabilityBuilder().setAsNumber(new AsNumber((long) 40)).build()).build(), tlv4);
110     }
111
112     @Test
113     public void testBGPAggregatorImpl() {
114         final BgpAggregator ipv4 = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5524)).setNetworkAddress(
115                 new Ipv4Address("124.55.42.1")).build();
116         final BgpAggregator ipv4i = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5525)).setNetworkAddress(
117                 new Ipv4Address("124.55.42.1")).build();
118
119         assertNotSame(ipv4.hashCode(), ipv4i.hashCode());
120
121         assertNotSame(ipv4.getAsNumber(), ipv4i.getAsNumber());
122
123         assertEquals(ipv4.getNetworkAddress(), ipv4i.getNetworkAddress());
124     }
125
126     @Test
127     public void testExtendedCommunitiesParser() {
128         ExtendedCommunities as = null;
129         try {
130             as = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 }));
131         } catch (final BGPDocumentedException e1) {
132             fail("Not expected exception: " + e1);
133         }
134         AsSpecificExtendedCommunityCase expected = new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
135                 new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(new ShortAsNumber(54L)).setLocalAdministrator(
136                         new byte[] { 0, 0, 1, 76 }).build()).build();
137         AsSpecificExtendedCommunityCase result = (AsSpecificExtendedCommunityCase) as.getExtendedCommunity();
138         assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), result.getAsSpecificExtendedCommunity().isTransitive());
139         assertEquals(expected.getAsSpecificExtendedCommunity().getGlobalAdministrator(),
140                 result.getAsSpecificExtendedCommunity().getGlobalAdministrator());
141         assertArrayEquals(expected.getAsSpecificExtendedCommunity().getLocalAdministrator(),
142                 result.getAsSpecificExtendedCommunity().getLocalAdministrator());
143         assertEquals(0, as.getCommType().intValue());
144
145         final ByteBuf serializedBuffer = Unpooled.buffer();
146         ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(as, serializedBuffer);
147         assertArrayEquals(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 }, ByteArray.readAllBytes(serializedBuffer));
148
149         try {
150             as = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 }));
151         } catch (final BGPDocumentedException e1) {
152             fail("Not expected exception: " + e1);
153         }
154         expected = new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
155                 new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new ShortAsNumber(54L)).setLocalAdministrator(
156                         new byte[] { 0, 0, 1, 76 }).build()).build();
157         result = (AsSpecificExtendedCommunityCase) as.getExtendedCommunity();
158         assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), result.getAsSpecificExtendedCommunity().isTransitive());
159         assertEquals(40, as.getCommType().intValue());
160
161         serializedBuffer.clear();
162         ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(as, serializedBuffer);
163         assertArrayEquals(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 }, ByteArray.readAllBytes(serializedBuffer));
164
165         ExtendedCommunities rtc = null;
166         try {
167             rtc = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 }));
168         } catch (final BGPDocumentedException e1) {
169             fail("Not expected exception: " + e1);
170         }
171         final RouteTargetExtendedCommunityCase rexpected = new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
172                 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(35L)).setLocalAdministrator(
173                         new byte[] { 4, 2, 8, 7 }).build()).build();
174         final RouteTargetExtendedCommunityCase rresult = (RouteTargetExtendedCommunityCase) rtc.getExtendedCommunity();
175         assertEquals(rexpected.getRouteTargetExtendedCommunity().getGlobalAdministrator(),
176                 rresult.getRouteTargetExtendedCommunity().getGlobalAdministrator());
177         assertArrayEquals(rexpected.getRouteTargetExtendedCommunity().getLocalAdministrator(),
178                 rresult.getRouteTargetExtendedCommunity().getLocalAdministrator());
179         assertEquals(1, rtc.getCommType().intValue());
180         assertEquals(2, rtc.getCommSubType().intValue());
181
182         serializedBuffer.clear();
183         ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(rtc, serializedBuffer);
184         assertArrayEquals(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 }, ByteArray.readAllBytes(serializedBuffer));
185
186         ExtendedCommunities roc = null;
187         try {
188             roc = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 }));
189         } catch (final BGPDocumentedException e1) {
190             fail("Not expected exception: " + e1);
191         }
192         final RouteOriginExtendedCommunityCase oexpected = new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
193                 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(24L)).setLocalAdministrator(
194                         new byte[] { 4, 2, 8, 7 }).build()).build();
195         final RouteOriginExtendedCommunityCase oresult = (RouteOriginExtendedCommunityCase) roc.getExtendedCommunity();
196         assertEquals(oexpected.getRouteOriginExtendedCommunity().getGlobalAdministrator(),
197                 oresult.getRouteOriginExtendedCommunity().getGlobalAdministrator());
198         assertArrayEquals(oexpected.getRouteOriginExtendedCommunity().getLocalAdministrator(),
199                 oresult.getRouteOriginExtendedCommunity().getLocalAdministrator());
200         assertEquals(0, roc.getCommType().intValue());
201         assertEquals(3, roc.getCommSubType().intValue());
202
203         serializedBuffer.clear();
204         ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(roc, serializedBuffer);
205         assertArrayEquals(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 }, ByteArray.readAllBytes(serializedBuffer));
206
207         ExtendedCommunities sec = null;
208         try {
209             sec = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 }));
210         } catch (final BGPDocumentedException e1) {
211             fail("Not expected exception: " + e1);
212         }
213         final Inet4SpecificExtendedCommunityCase iexpected = new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
214                 new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new Ipv4Address("12.51.2.5")).setLocalAdministrator(
215                         new byte[] { 21, 45 }).build()).build();
216         final Inet4SpecificExtendedCommunityCase iresult = (Inet4SpecificExtendedCommunityCase) sec.getExtendedCommunity();
217         assertEquals(iexpected.getInet4SpecificExtendedCommunity().isTransitive(),
218                 iresult.getInet4SpecificExtendedCommunity().isTransitive());
219         assertEquals(iexpected.getInet4SpecificExtendedCommunity().getGlobalAdministrator(),
220                 iresult.getInet4SpecificExtendedCommunity().getGlobalAdministrator());
221         assertArrayEquals(iexpected.getInet4SpecificExtendedCommunity().getLocalAdministrator(),
222                 iresult.getInet4SpecificExtendedCommunity().getLocalAdministrator());
223         assertEquals(41, sec.getCommType().intValue());
224
225         serializedBuffer.clear();
226         ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(sec, serializedBuffer);
227         assertArrayEquals(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 }, ByteArray.readAllBytes(serializedBuffer));
228
229         ExtendedCommunities oec = null;
230         try {
231             oec = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 }));
232         } catch (final BGPDocumentedException e1) {
233             fail("Not expected exception: " + e1);
234         }
235         final OpaqueExtendedCommunityCase oeexpected = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
236                 new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
237         final OpaqueExtendedCommunityCase oeresult = (OpaqueExtendedCommunityCase) oec.getExtendedCommunity();
238         assertEquals(oeexpected.getOpaqueExtendedCommunity().isTransitive(), oeresult.getOpaqueExtendedCommunity().isTransitive());
239         assertArrayEquals(oeexpected.getOpaqueExtendedCommunity().getValue(), oeresult.getOpaqueExtendedCommunity().getValue());
240         assertEquals(3, oec.getCommType().intValue());
241
242         serializedBuffer.clear();
243         ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(oec, serializedBuffer);
244         assertArrayEquals(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 }, ByteArray.readAllBytes(serializedBuffer));
245
246         try {
247             oec = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 }));
248         } catch (final BGPDocumentedException e1) {
249             fail("Not expected exception: " + e1);
250         }
251         final OpaqueExtendedCommunityCase oeexpected1 = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
252                 new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
253         final OpaqueExtendedCommunityCase oeresult1 = (OpaqueExtendedCommunityCase) oec.getExtendedCommunity();
254         assertEquals(oeexpected1.getOpaqueExtendedCommunity().isTransitive(), oeresult1.getOpaqueExtendedCommunity().isTransitive());
255         assertArrayEquals(oeexpected1.getOpaqueExtendedCommunity().getValue(), oeresult1.getOpaqueExtendedCommunity().getValue());
256         assertEquals(43, oec.getCommType().intValue());
257
258         serializedBuffer.clear();
259         ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(oec, serializedBuffer);
260         assertArrayEquals(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 }, ByteArray.readAllBytes(serializedBuffer));
261
262         try {
263             ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 }));
264             fail("Exception should have occured.");
265         } catch (final BGPDocumentedException e) {
266             assertEquals("Could not parse Extended Community type: 11", e.getMessage());
267         }
268     }
269
270     @Test
271     public void testBGPHeaderParser() throws Exception {
272         final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
273         try {
274             msgReg.parseMessage(Unpooled.copiedBuffer(new byte[] { (byte) 0, (byte) 0 }));
275             fail("Exception should have occured.");
276         } catch (final IllegalArgumentException e) {
277             assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
278         }
279     }
280
281     @Test
282     public void testMessageParser() throws Exception {
283         final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
284         String ex = "";
285         try {
286             msgReg.serializeMessage(null, Unpooled.EMPTY_BUFFER);
287         } catch (final NullPointerException e) {
288             ex = e.getMessage();
289         }
290         assertEquals("BGPMessage is mandatory.", ex);
291     }
292 }