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.assertNotNull;
13 import static org.junit.Assert.assertNotSame;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.fail;
17 import java.io.IOException;
19 import org.junit.Test;
20 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
21 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesParser;
22 import org.opendaylight.protocol.framework.DeserializerException;
23 import org.opendaylight.protocol.framework.DocumentedException;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.path.attributes.AggregatorBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpAggregator;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CAsSpecificExtendedCommunity;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CAsSpecificExtendedCommunityBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CInet4SpecificExtendedCommunity;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CInet4SpecificExtendedCommunityBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.COpaqueExtendedCommunity;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.COpaqueExtendedCommunityBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteOriginExtendedCommunity;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteOriginExtendedCommunityBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteTargetExtendedCommunity;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteTargetExtendedCommunityBuilder;
38 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;
39 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;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.c.opaque.extended.community.OpaqueExtendedCommunityBuilder;
41 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;
42 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;
44 public class ComplementaryTest {
47 public void testBGPAggregatorImpl() {
48 final BgpAggregator ipv4 = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5524)).setNetworkAddress(
49 new Ipv4Address("124.55.42.1")).build();
50 final BgpAggregator ipv4i = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5525)).setNetworkAddress(
51 new Ipv4Address("124.55.42.1")).build();
53 assertNotSame(ipv4.hashCode(), ipv4i.hashCode());
55 assertNotSame(ipv4.getAsNumber(), ipv4i.getAsNumber());
57 assertEquals(ipv4.getNetworkAddress(), ipv4i.getNetworkAddress());
61 public void testBGPUpdateMessageImpl() {
62 final BGPUpdateMessageImpl msg = new BGPUpdateMessageImpl(null, null);
63 final BGPUpdateMessageImpl msg1 = new BGPUpdateMessageImpl(null, null);
65 assertEquals(msg, msg1);
67 assertEquals(msg.hashCode(), msg1.hashCode());
69 assertNotNull(msg.toString());
71 assertNull(msg.getAddedObjects());
72 assertNull(msg.getRemovedObjects());
74 assertNotSame(msg1, null);
78 public void testCommunitiesParser() {
79 CAsSpecificExtendedCommunity as = null;
81 as = (CAsSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 });
82 } catch (final BGPDocumentedException e1) {
83 fail("Not expected exception: " + e1);
85 CAsSpecificExtendedCommunity expected = new CAsSpecificExtendedCommunityBuilder().setAsSpecificExtendedCommunity(
86 new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(new AsNumber(54L)).setLocalAdministrator(
87 new byte[] { 0, 0, 1, 76 }).build()).build();
88 assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), as.getAsSpecificExtendedCommunity().isTransitive());
89 assertEquals(expected.getAsSpecificExtendedCommunity().getGlobalAdministrator(),
90 as.getAsSpecificExtendedCommunity().getGlobalAdministrator());
91 assertArrayEquals(expected.getAsSpecificExtendedCommunity().getLocalAdministrator(),
92 as.getAsSpecificExtendedCommunity().getLocalAdministrator());
95 as = (CAsSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 });
96 } catch (final BGPDocumentedException e1) {
97 fail("Not expected exception: " + e1);
99 expected = new CAsSpecificExtendedCommunityBuilder().setAsSpecificExtendedCommunity(
100 new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new AsNumber(54L)).setLocalAdministrator(
101 new byte[] { 0, 0, 1, 76 }).build()).build();
102 assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), as.getAsSpecificExtendedCommunity().isTransitive());
104 CRouteTargetExtendedCommunity rtc = null;
106 rtc = (CRouteTargetExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 });
107 } catch (final BGPDocumentedException e1) {
108 fail("Not expected exception: " + e1);
110 final CRouteTargetExtendedCommunity rexpected = new CRouteTargetExtendedCommunityBuilder().setRouteTargetExtendedCommunity(
111 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new AsNumber(35L)).setLocalAdministrator(
112 new byte[] { 4, 2, 8, 7 }).build()).build();
113 assertEquals(rexpected.getRouteTargetExtendedCommunity().getGlobalAdministrator(),
114 rtc.getRouteTargetExtendedCommunity().getGlobalAdministrator());
115 assertArrayEquals(rexpected.getRouteTargetExtendedCommunity().getLocalAdministrator(),
116 rtc.getRouteTargetExtendedCommunity().getLocalAdministrator());
118 CRouteOriginExtendedCommunity roc = null;
120 roc = (CRouteOriginExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 });
121 } catch (final BGPDocumentedException e1) {
122 fail("Not expected exception: " + e1);
124 final CRouteOriginExtendedCommunity oexpected = new CRouteOriginExtendedCommunityBuilder().setRouteOriginExtendedCommunity(
125 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new AsNumber(24L)).setLocalAdministrator(
126 new byte[] { 4, 2, 8, 7 }).build()).build();
127 assertEquals(oexpected.getRouteOriginExtendedCommunity().getGlobalAdministrator(),
128 roc.getRouteOriginExtendedCommunity().getGlobalAdministrator());
129 assertArrayEquals(oexpected.getRouteOriginExtendedCommunity().getLocalAdministrator(),
130 roc.getRouteOriginExtendedCommunity().getLocalAdministrator());
132 CInet4SpecificExtendedCommunity sec = null;
134 sec = (CInet4SpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 });
135 } catch (final BGPDocumentedException e1) {
136 fail("Not expected exception: " + e1);
138 final CInet4SpecificExtendedCommunity iexpected = new CInet4SpecificExtendedCommunityBuilder().setInet4SpecificExtendedCommunity(
139 new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new Ipv4Address("12.51.2.5")).setLocalAdministrator(
140 new byte[] { 21, 45 }).build()).build();
141 assertEquals(iexpected.getInet4SpecificExtendedCommunity().isTransitive(), sec.getInet4SpecificExtendedCommunity().isTransitive());
142 assertEquals(iexpected.getInet4SpecificExtendedCommunity().getGlobalAdministrator(),
143 sec.getInet4SpecificExtendedCommunity().getGlobalAdministrator());
144 assertArrayEquals(iexpected.getInet4SpecificExtendedCommunity().getLocalAdministrator(),
145 sec.getInet4SpecificExtendedCommunity().getLocalAdministrator());
147 COpaqueExtendedCommunity oec = null;
149 oec = (COpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 });
150 } catch (final BGPDocumentedException e1) {
151 fail("Not expected exception: " + e1);
153 final COpaqueExtendedCommunity oeexpected = new COpaqueExtendedCommunityBuilder().setOpaqueExtendedCommunity(
154 new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
155 assertEquals(oeexpected.getOpaqueExtendedCommunity().isTransitive(), oec.getOpaqueExtendedCommunity().isTransitive());
156 assertArrayEquals(oeexpected.getOpaqueExtendedCommunity().getValue(), oec.getOpaqueExtendedCommunity().getValue());
159 oec = (COpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 });
160 } catch (final BGPDocumentedException e1) {
161 fail("Not expected exception: " + e1);
163 final COpaqueExtendedCommunity oeexpected1 = new COpaqueExtendedCommunityBuilder().setOpaqueExtendedCommunity(
164 new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
165 assertEquals(oeexpected1.getOpaqueExtendedCommunity().isTransitive(), oec.getOpaqueExtendedCommunity().isTransitive());
166 assertArrayEquals(oeexpected1.getOpaqueExtendedCommunity().getValue(), oec.getOpaqueExtendedCommunity().getValue());
169 CommunitiesParser.parseExtendedCommunity(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 });
170 fail("Exception should have occured.");
171 } catch (final BGPDocumentedException e) {
172 assertEquals("Could not parse Extended Community type: 11", e.getMessage());
177 public void testBGPHeaderParser() throws IOException {
178 final BGPMessageFactoryImpl h = new BGPMessageFactoryImpl();
180 h.parse(new byte[] { (byte) 0, (byte) 0 });
181 fail("Exception should have occured.");
182 } catch (final IllegalArgumentException e) {
183 assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
184 } catch (final DeserializerException e) {
185 fail("Not this exception should have occured:" + e);
186 } catch (final DocumentedException e) {
187 fail("Not this exception should have occured:" + e);
192 public void testByteList() {
193 final ByteList b1 = new ByteList();
194 b1.add(new byte[] { 3, 4, 8 });
195 b1.add(new byte[] { 3, 4, 9 });
197 final ByteList b2 = new ByteList();
198 b2.add(new byte[] { 3, 4, 8 });
199 b2.add(new byte[] { 3, 4, 9 });
201 assertEquals(b1, b2);
202 assertEquals(b1.toString(), b2.toString());
206 public void testMessageParser() throws IOException {
207 final BGPMessageFactoryImpl parser = new BGPMessageFactoryImpl();
211 } catch (final IllegalArgumentException e) {
214 assertEquals("BGPMessage is mandatory.", ex);