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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNotSame;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.fail;
16 import java.io.IOException;
17 import java.util.Collections;
20 import org.junit.Test;
21 import org.opendaylight.protocol.bgp.concepts.ASSpecificExtendedCommunity;
22 import org.opendaylight.protocol.bgp.concepts.Inet4SpecificExtendedCommunity;
23 import org.opendaylight.protocol.bgp.concepts.OpaqueExtendedCommunity;
24 import org.opendaylight.protocol.bgp.concepts.RouteOriginCommunity;
25 import org.opendaylight.protocol.bgp.concepts.RouteTargetCommunity;
26 import org.opendaylight.protocol.bgp.linkstate.IPv4InterfaceIdentifier;
27 import org.opendaylight.protocol.bgp.linkstate.ISISLANIdentifier;
28 import org.opendaylight.protocol.bgp.linkstate.ISISRouterIdentifier;
29 import org.opendaylight.protocol.bgp.linkstate.InterfaceIdentifier;
30 import org.opendaylight.protocol.bgp.linkstate.LinkAnchor;
31 import org.opendaylight.protocol.bgp.linkstate.LinkIdentifier;
32 import org.opendaylight.protocol.bgp.linkstate.NodeIdentifier;
33 import org.opendaylight.protocol.bgp.linkstate.NodeIdentifierFactory;
34 import org.opendaylight.protocol.bgp.linkstate.SourceProtocol;
35 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
36 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
37 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesParser;
38 import org.opendaylight.protocol.bgp.parser.impl.message.update.MPReachParser;
39 import org.opendaylight.protocol.concepts.ASNumber;
40 import org.opendaylight.protocol.concepts.IPv4;
41 import org.opendaylight.protocol.concepts.IPv4Address;
42 import org.opendaylight.protocol.concepts.ISOSystemIdentifier;
43 import org.opendaylight.protocol.framework.DeserializerException;
44 import org.opendaylight.protocol.framework.DocumentedException;
46 import com.google.common.collect.Sets;
48 public class ComplementaryTest {
51 public void testBGPAggregatorImpl() {
52 final BGPAggregatorImpl<IPv4Address> ipv4 = new BGPAggregatorImpl<IPv4Address>(new ASNumber(5524), IPv4.FAMILY.addressForString("124.55.42.1"));
53 final BGPAggregatorImpl<IPv4Address> ipv4i = new BGPAggregatorImpl<IPv4Address>(new ASNumber(5525), IPv4.FAMILY.addressForString("124.55.42.1"));
55 assertNotSame(ipv4.hashCode(), ipv4i.hashCode());
57 assertEquals(ipv4, new BGPAggregatorImpl<IPv4Address>(new ASNumber(5524), IPv4.FAMILY.addressForString("124.55.42.1")));
59 assertNotSame(ipv4.getASNumber(), ipv4i.getASNumber());
61 assertEquals(ipv4.getNetworkAddress(), ipv4i.getNetworkAddress());
65 public void testBGPLinkMP() {
66 final NodeIdentifier localnodeid = new NodeIdentifier(new ASNumber(25600, 0), null, null, new ISISRouterIdentifier(new ISOSystemIdentifier(new byte[] {
67 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 })));
68 final NodeIdentifier remotenodeid = NodeIdentifierFactory.localIdentifier(new ISISLANIdentifier(new ISOSystemIdentifier(new byte[] {
69 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 }), (short) 1));
71 final InterfaceIdentifier ifaceid = new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("10.1.1.1"));
73 final LinkIdentifier l = new LinkIdentifier(null, new LinkAnchor(localnodeid, ifaceid), new LinkAnchor(remotenodeid, ifaceid));
75 final Set<LinkIdentifier> links = Sets.newHashSet(l);
77 final BGPLinkMP link = new BGPLinkMP(0, SourceProtocol.Direct, true, links);
79 final BGPLinkMP link1 = new BGPLinkMP(0, SourceProtocol.Direct, true, Collections.<LinkIdentifier> emptySet());
81 assertNotSame(link.hashCode(), link1.hashCode());
83 assertEquals(link, new BGPLinkMP(0, SourceProtocol.Direct, true, links));
85 assertEquals(link.hashCode(), (new BGPLinkMP(0, SourceProtocol.Direct, true, links)).hashCode());
87 assertNotSame(link.toString(), link1.toString());
91 public void testBGPUpdateMessageImpl() {
92 final BGPUpdateMessageImpl msg = new BGPUpdateMessageImpl(null, null);
93 final BGPUpdateMessageImpl msg1 = new BGPUpdateMessageImpl(null, null);
95 assertEquals(msg, msg1);
97 assertEquals(msg.hashCode(), msg1.hashCode());
99 assertNotNull(msg.toString());
101 assertNull(msg.getAddedObjects());
102 assertNull(msg.getRemovedObjects());
104 assertNotSame(msg1, null);
108 public void testCommunitiesParser() {
109 ASSpecificExtendedCommunity as = null;
111 as = (ASSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 });
112 } catch (final BGPDocumentedException e1) {
113 fail("Not expected exception: " + e1);
115 assertEquals(as, new ASSpecificExtendedCommunity(false, 5, new ASNumber(54), new byte[] { 0, 0, 1, 76 }));
118 as = (ASSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 });
119 } catch (final BGPDocumentedException e1) {
120 fail("Not expected exception: " + e1);
122 assertEquals(as, new ASSpecificExtendedCommunity(true, 5, new ASNumber(54), new byte[] { 0, 0, 1, 76 }));
124 RouteTargetCommunity rtc = null;
126 rtc = (RouteTargetCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 });
127 } catch (final BGPDocumentedException e1) {
128 fail("Not expected exception: " + e1);
130 assertEquals(rtc, new RouteTargetCommunity(new ASNumber(35), new byte[] { 4, 2, 8, 7 }));
132 RouteOriginCommunity roc = null;
134 roc = (RouteOriginCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 });
135 } catch (final BGPDocumentedException e1) {
136 fail("Not expected exception: " + e1);
138 assertEquals(roc, new RouteOriginCommunity(new ASNumber(24), new byte[] { 4, 2, 8, 7 }));
140 Inet4SpecificExtendedCommunity sec = null;
142 sec = (Inet4SpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 });
143 } catch (final BGPDocumentedException e1) {
144 fail("Not expected exception: " + e1);
146 assertEquals(sec, new Inet4SpecificExtendedCommunity(true, 6, IPv4.FAMILY.addressForString("12.51.2.5"), new byte[] { 21, 45 }));
148 OpaqueExtendedCommunity oec = null;
150 oec = (OpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 });
151 } catch (final BGPDocumentedException e1) {
152 fail("Not expected exception: " + e1);
154 assertEquals(oec, new OpaqueExtendedCommunity(false, 6, new byte[] { 21, 45, 5, 4, 3, 1 }));
157 oec = (OpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 });
158 } catch (final BGPDocumentedException e1) {
159 fail("Not expected exception: " + e1);
161 assertEquals(oec, new OpaqueExtendedCommunity(true, 6, new byte[] { 21, 45, 5, 4, 3, 1 }));
164 CommunitiesParser.parseExtendedCommunity(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 });
165 fail("Exception should have occured.");
166 } catch (final BGPDocumentedException e) {
167 assertEquals("Could not parse Extended Community type: 11", e.getMessage());
172 public void testMPReachParser() {
175 MPReachParser.parseSafi(5);
176 fail("Exception shoul have occured.");
177 } catch (final BGPParsingException e) {
178 msg = e.getMessage();
180 assertEquals("Subsequent Address Family Identifier: '5' not supported.", msg);
183 MPReachParser.parseAfi(6);
184 fail("Exception should have occured.");
185 } catch (final BGPParsingException e) {
186 msg = e.getMessage();
188 assertEquals("Address Family Identifier: '6' not supported.", msg);
192 public void testBGPHeaderParser() throws IOException {
193 final BGPMessageFactory h = new BGPMessageFactory();
195 h.parse(new byte[] { (byte) 0, (byte) 0 });
196 fail("Exception should have occured.");
197 } catch (final IllegalArgumentException e) {
198 assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
199 } catch (final DeserializerException e) {
200 fail("Not this exception should have occured:" + e);
201 } catch (final DocumentedException e) {
202 fail("Not this exception should have occured:" + e);
207 public void testByteList() {
208 final ByteList b1 = new ByteList();
209 b1.add(new byte[] { 3, 4, 8 });
210 b1.add(new byte[] { 3, 4, 9 });
212 final ByteList b2 = new ByteList();
213 b2.add(new byte[] { 3, 4, 8 });
214 b2.add(new byte[] { 3, 4, 9 });
216 assertEquals(b1, b2);
217 assertEquals(b1.toString(), b2.toString());
221 public void testMessageParser() throws IOException {
222 final BGPMessageFactory parser = new BGPMessageFactory();
226 } catch (final IllegalArgumentException e) {
229 assertEquals("BGPMessage is mandatory.", ex);