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