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