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