2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.parser.impl;
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;
16 import java.io.IOException;
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.BGPExtensionConsumerContextImpl;
25 import org.opendaylight.protocol.framework.DeserializerException;
26 import org.opendaylight.protocol.framework.DocumentedException;
27 import org.opendaylight.protocol.util.ByteList;
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.rev130918.LinkstateAddressFamily;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.CParameters;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.c.parameters.CAs4Bytes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.c.parameters.CAs4BytesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.c.parameters.c.as4.bytes.As4BytesCapabilityBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.path.attributes.AggregatorBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.BgpTableType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.CMultiprotocolBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.c.multiprotocol.MultiprotocolCapability;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.c.multiprotocol.MultiprotocolCapabilityBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpAggregator;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CAsSpecificExtendedCommunity;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CAsSpecificExtendedCommunityBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CInet4SpecificExtendedCommunity;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CInet4SpecificExtendedCommunityBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.COpaqueExtendedCommunity;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.COpaqueExtendedCommunityBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteOriginExtendedCommunity;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteOriginExtendedCommunityBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteTargetExtendedCommunity;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteTargetExtendedCommunityBuilder;
53 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;
54 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;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.c.opaque.extended.community.OpaqueExtendedCommunityBuilder;
56 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;
57 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;
59 import com.google.common.collect.Maps;
61 public class ComplementaryTest {
64 public void testBGPParameter() {
66 final BgpTableType t = new BgpTableTypeImpl(LinkstateAddressFamily.class, UnicastSubsequentAddressFamily.class);
67 final BgpTableType t1 = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
69 final MultiprotocolCapability cap = new MultiprotocolCapabilityBuilder().setAfi(LinkstateAddressFamily.class).setSafi(
70 UnicastSubsequentAddressFamily.class).build();
71 final CParameters tlv1 = new CMultiprotocolBuilder().setMultiprotocolCapability(cap).build();
73 final MultiprotocolCapability cap1 = new MultiprotocolCapabilityBuilder().setAfi(Ipv4AddressFamily.class).setSafi(
74 UnicastSubsequentAddressFamily.class).build();
75 final CParameters tlv2 = new CMultiprotocolBuilder().setMultiprotocolCapability(cap1).build();
77 final Map<BgpTableType, Boolean> tt = Maps.newHashMap();
81 // final BGPParameter tlv3 = new GracefulCapability(false, 0, tt);
83 final CParameters tlv4 = new CAs4BytesBuilder().setAs4BytesCapability(
84 new As4BytesCapabilityBuilder().setAsNumber(new AsNumber((long) 40)).build()).build();
86 // assertFalse(((GracefulCapability) tlv3).isRestartFlag());
88 // assertEquals(0, ((GracefulCapability) tlv3).getRestartTimerValue());
90 assertFalse(tlv1.equals(tlv2));
92 // assertNotSame(tlv1.hashCode(), tlv3.hashCode());
94 // assertNotSame(tlv2.toString(), tlv3.toString());
96 // assertEquals(((GracefulCapability) tlv3).getTableTypes(), tt);
98 assertEquals(cap.getSafi(), cap1.getSafi());
100 assertNotSame(cap.getAfi(), cap1.getAfi());
102 assertEquals(40, ((CAs4Bytes) tlv4).getAs4BytesCapability().getAsNumber().getValue().longValue());
104 // FIXME: no generated toString
105 // assertEquals(new As4BytesBuilder().setCAs4Bytes(new CAs4BytesBuilder().setAsNumber(new AsNumber((long)
106 // 40)).build()).build().toString(), tlv4.toString());
110 public void testBGPAggregatorImpl() {
111 final BgpAggregator ipv4 = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5524)).setNetworkAddress(
112 new Ipv4Address("124.55.42.1")).build();
113 final BgpAggregator ipv4i = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5525)).setNetworkAddress(
114 new Ipv4Address("124.55.42.1")).build();
116 assertNotSame(ipv4.hashCode(), ipv4i.hashCode());
118 assertNotSame(ipv4.getAsNumber(), ipv4i.getAsNumber());
120 assertEquals(ipv4.getNetworkAddress(), ipv4i.getNetworkAddress());
124 public void testCommunitiesParser() {
125 CAsSpecificExtendedCommunity as = null;
127 as = (CAsSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 });
128 } catch (final BGPDocumentedException e1) {
129 fail("Not expected exception: " + e1);
131 CAsSpecificExtendedCommunity expected = new CAsSpecificExtendedCommunityBuilder().setAsSpecificExtendedCommunity(
132 new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(new AsNumber(54L)).setLocalAdministrator(
133 new byte[] { 0, 0, 1, 76 }).build()).build();
134 assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), as.getAsSpecificExtendedCommunity().isTransitive());
135 assertEquals(expected.getAsSpecificExtendedCommunity().getGlobalAdministrator(),
136 as.getAsSpecificExtendedCommunity().getGlobalAdministrator());
137 assertArrayEquals(expected.getAsSpecificExtendedCommunity().getLocalAdministrator(),
138 as.getAsSpecificExtendedCommunity().getLocalAdministrator());
141 as = (CAsSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 });
142 } catch (final BGPDocumentedException e1) {
143 fail("Not expected exception: " + e1);
145 expected = new CAsSpecificExtendedCommunityBuilder().setAsSpecificExtendedCommunity(
146 new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new AsNumber(54L)).setLocalAdministrator(
147 new byte[] { 0, 0, 1, 76 }).build()).build();
148 assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), as.getAsSpecificExtendedCommunity().isTransitive());
150 CRouteTargetExtendedCommunity rtc = null;
152 rtc = (CRouteTargetExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 });
153 } catch (final BGPDocumentedException e1) {
154 fail("Not expected exception: " + e1);
156 final CRouteTargetExtendedCommunity rexpected = new CRouteTargetExtendedCommunityBuilder().setRouteTargetExtendedCommunity(
157 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new AsNumber(35L)).setLocalAdministrator(
158 new byte[] { 4, 2, 8, 7 }).build()).build();
159 assertEquals(rexpected.getRouteTargetExtendedCommunity().getGlobalAdministrator(),
160 rtc.getRouteTargetExtendedCommunity().getGlobalAdministrator());
161 assertArrayEquals(rexpected.getRouteTargetExtendedCommunity().getLocalAdministrator(),
162 rtc.getRouteTargetExtendedCommunity().getLocalAdministrator());
164 CRouteOriginExtendedCommunity roc = null;
166 roc = (CRouteOriginExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 });
167 } catch (final BGPDocumentedException e1) {
168 fail("Not expected exception: " + e1);
170 final CRouteOriginExtendedCommunity oexpected = new CRouteOriginExtendedCommunityBuilder().setRouteOriginExtendedCommunity(
171 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new AsNumber(24L)).setLocalAdministrator(
172 new byte[] { 4, 2, 8, 7 }).build()).build();
173 assertEquals(oexpected.getRouteOriginExtendedCommunity().getGlobalAdministrator(),
174 roc.getRouteOriginExtendedCommunity().getGlobalAdministrator());
175 assertArrayEquals(oexpected.getRouteOriginExtendedCommunity().getLocalAdministrator(),
176 roc.getRouteOriginExtendedCommunity().getLocalAdministrator());
178 CInet4SpecificExtendedCommunity sec = null;
180 sec = (CInet4SpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 });
181 } catch (final BGPDocumentedException e1) {
182 fail("Not expected exception: " + e1);
184 final CInet4SpecificExtendedCommunity iexpected = new CInet4SpecificExtendedCommunityBuilder().setInet4SpecificExtendedCommunity(
185 new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new Ipv4Address("12.51.2.5")).setLocalAdministrator(
186 new byte[] { 21, 45 }).build()).build();
187 assertEquals(iexpected.getInet4SpecificExtendedCommunity().isTransitive(), sec.getInet4SpecificExtendedCommunity().isTransitive());
188 assertEquals(iexpected.getInet4SpecificExtendedCommunity().getGlobalAdministrator(),
189 sec.getInet4SpecificExtendedCommunity().getGlobalAdministrator());
190 assertArrayEquals(iexpected.getInet4SpecificExtendedCommunity().getLocalAdministrator(),
191 sec.getInet4SpecificExtendedCommunity().getLocalAdministrator());
193 COpaqueExtendedCommunity oec = null;
195 oec = (COpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 });
196 } catch (final BGPDocumentedException e1) {
197 fail("Not expected exception: " + e1);
199 final COpaqueExtendedCommunity oeexpected = new COpaqueExtendedCommunityBuilder().setOpaqueExtendedCommunity(
200 new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
201 assertEquals(oeexpected.getOpaqueExtendedCommunity().isTransitive(), oec.getOpaqueExtendedCommunity().isTransitive());
202 assertArrayEquals(oeexpected.getOpaqueExtendedCommunity().getValue(), oec.getOpaqueExtendedCommunity().getValue());
205 oec = (COpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 });
206 } catch (final BGPDocumentedException e1) {
207 fail("Not expected exception: " + e1);
209 final COpaqueExtendedCommunity oeexpected1 = new COpaqueExtendedCommunityBuilder().setOpaqueExtendedCommunity(
210 new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
211 assertEquals(oeexpected1.getOpaqueExtendedCommunity().isTransitive(), oec.getOpaqueExtendedCommunity().isTransitive());
212 assertArrayEquals(oeexpected1.getOpaqueExtendedCommunity().getValue(), oec.getOpaqueExtendedCommunity().getValue());
215 CommunitiesParser.parseExtendedCommunity(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 });
216 fail("Exception should have occured.");
217 } catch (final BGPDocumentedException e) {
218 assertEquals("Could not parse Extended Community type: 11", e.getMessage());
223 public void testBGPHeaderParser() throws IOException {
224 final MessageRegistry msgReg = BGPExtensionConsumerContextImpl.getSingletonInstance().getMessageRegistry();
226 msgReg.parseMessage(new byte[] { (byte) 0, (byte) 0 });
227 fail("Exception should have occured.");
228 } catch (final IllegalArgumentException e) {
229 assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
230 } catch (final DeserializerException e) {
231 fail("Not this exception should have occured:" + e);
232 } catch (final DocumentedException e) {
233 fail("Not this exception should have occured:" + e);
238 public void testByteList() {
239 final ByteList b1 = new ByteList();
240 b1.add(new byte[] { 3, 4, 8 });
241 b1.add(new byte[] { 3, 4, 9 });
243 final ByteList b2 = new ByteList();
244 b2.add(new byte[] { 3, 4, 8 });
245 b2.add(new byte[] { 3, 4, 9 });
247 assertEquals(b1, b2);
248 assertEquals(b1.toString(), b2.toString());
252 public void testMessageParser() throws IOException {
253 final MessageRegistry msgReg = BGPExtensionConsumerContextImpl.getSingletonInstance().getMessageRegistry();
256 msgReg.serializeMessage(null);
257 } catch (final IllegalArgumentException e) {
260 assertEquals("BGPMessage is mandatory.", ex);