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