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.linkstate.IPv4InterfaceIdentifier;
25 import org.opendaylight.protocol.bgp.linkstate.ISISLANIdentifier;
26 import org.opendaylight.protocol.bgp.linkstate.ISISRouterIdentifier;
27 import org.opendaylight.protocol.bgp.linkstate.InterfaceIdentifier;
28 import org.opendaylight.protocol.bgp.linkstate.LinkAnchor;
29 import org.opendaylight.protocol.bgp.linkstate.LinkIdentifier;
30 import org.opendaylight.protocol.bgp.linkstate.NodeIdentifier;
31 import org.opendaylight.protocol.bgp.linkstate.NodeIdentifierFactory;
32 import org.opendaylight.protocol.bgp.linkstate.SourceProtocol;
33 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
34 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesParser;
35 import org.opendaylight.protocol.concepts.IPv4;
36 import org.opendaylight.protocol.concepts.ISOSystemIdentifier;
37 import org.opendaylight.protocol.framework.DeserializerException;
38 import org.opendaylight.protocol.framework.DocumentedException;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.path.attributes.AggregatorBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpAggregator;
44 import com.google.common.collect.Sets;
46 public class ComplementaryTest {
49 public void testBGPAggregatorImpl() {
50 final BgpAggregator ipv4 = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5524)).setNetworkAddress(
51 new Ipv4Address("124.55.42.1")).build();
52 final BgpAggregator ipv4i = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5525)).setNetworkAddress(
53 new Ipv4Address("124.55.42.1")).build();
55 assertNotSame(ipv4.hashCode(), ipv4i.hashCode());
57 assertNotSame(ipv4.getAsNumber(), ipv4i.getAsNumber());
59 assertEquals(ipv4.getNetworkAddress(), ipv4i.getNetworkAddress());
63 public void testBGPLinkMP() {
64 final NodeIdentifier localnodeid = new NodeIdentifier(new AsNumber((long) 25600), null, null, new ISISRouterIdentifier(new ISOSystemIdentifier(new byte[] {
65 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 })));
66 final NodeIdentifier remotenodeid = NodeIdentifierFactory.localIdentifier(new ISISLANIdentifier(new ISOSystemIdentifier(new byte[] {
67 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 }), (short) 1));
69 final InterfaceIdentifier ifaceid = new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("10.1.1.1"));
71 final LinkIdentifier l = new LinkIdentifier(null, new LinkAnchor(localnodeid, ifaceid), new LinkAnchor(remotenodeid, ifaceid));
73 final Set<LinkIdentifier> links = Sets.newHashSet(l);
75 final BGPLinkMP link = new BGPLinkMP(0, SourceProtocol.Direct, true, links);
77 final BGPLinkMP link1 = new BGPLinkMP(0, SourceProtocol.Direct, true, Collections.<LinkIdentifier> emptySet());
79 assertNotSame(link.hashCode(), link1.hashCode());
81 assertEquals(link, new BGPLinkMP(0, SourceProtocol.Direct, true, links));
83 assertEquals(link.hashCode(), (new BGPLinkMP(0, SourceProtocol.Direct, true, links)).hashCode());
85 assertNotSame(link.toString(), link1.toString());
89 public void testBGPUpdateMessageImpl() {
90 final BGPUpdateMessageImpl msg = new BGPUpdateMessageImpl(null, null);
91 final BGPUpdateMessageImpl msg1 = new BGPUpdateMessageImpl(null, null);
93 assertEquals(msg, msg1);
95 assertEquals(msg.hashCode(), msg1.hashCode());
97 assertNotNull(msg.toString());
99 assertNull(msg.getAddedObjects());
100 assertNull(msg.getRemovedObjects());
102 assertNotSame(msg1, null);
106 public void testCommunitiesParser() {
107 ASSpecificExtendedCommunity as = null;
109 as = (ASSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 });
110 } catch (final BGPDocumentedException e1) {
111 fail("Not expected exception: " + e1);
113 assertEquals(as, new ASSpecificExtendedCommunity(false, 5, new AsNumber((long) 54), new byte[] { 0, 0, 1, 76 }));
116 as = (ASSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 });
117 } catch (final BGPDocumentedException e1) {
118 fail("Not expected exception: " + e1);
120 assertEquals(as, new ASSpecificExtendedCommunity(true, 5, new AsNumber((long) 54), new byte[] { 0, 0, 1, 76 }));
122 RouteTargetCommunity rtc = null;
124 rtc = (RouteTargetCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 });
125 } catch (final BGPDocumentedException e1) {
126 fail("Not expected exception: " + e1);
128 assertEquals(rtc, new RouteTargetCommunity(new AsNumber((long) 35), new byte[] { 4, 2, 8, 7 }));
130 RouteOriginCommunity roc = null;
132 roc = (RouteOriginCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 });
133 } catch (final BGPDocumentedException e1) {
134 fail("Not expected exception: " + e1);
136 assertEquals(roc, new RouteOriginCommunity(new AsNumber((long) 24), new byte[] { 4, 2, 8, 7 }));
138 Inet4SpecificExtendedCommunity sec = null;
140 sec = (Inet4SpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 });
141 } catch (final BGPDocumentedException e1) {
142 fail("Not expected exception: " + e1);
144 assertEquals(sec, new Inet4SpecificExtendedCommunity(true, 6, IPv4.FAMILY.addressForString("12.51.2.5"), new byte[] { 21, 45 }));
146 OpaqueExtendedCommunity oec = null;
148 oec = (OpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 });
149 } catch (final BGPDocumentedException e1) {
150 fail("Not expected exception: " + e1);
152 assertEquals(oec, new OpaqueExtendedCommunity(false, 6, new byte[] { 21, 45, 5, 4, 3, 1 }));
155 oec = (OpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 });
156 } catch (final BGPDocumentedException e1) {
157 fail("Not expected exception: " + e1);
159 assertEquals(oec, new OpaqueExtendedCommunity(true, 6, new byte[] { 21, 45, 5, 4, 3, 1 }));
162 CommunitiesParser.parseExtendedCommunity(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 });
163 fail("Exception should have occured.");
164 } catch (final BGPDocumentedException e) {
165 assertEquals("Could not parse Extended Community type: 11", e.getMessage());
170 public void testBGPHeaderParser() throws IOException {
171 final BGPMessageFactoryImpl h = new BGPMessageFactoryImpl();
173 h.parse(new byte[] { (byte) 0, (byte) 0 });
174 fail("Exception should have occured.");
175 } catch (final IllegalArgumentException e) {
176 assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
177 } catch (final DeserializerException e) {
178 fail("Not this exception should have occured:" + e);
179 } catch (final DocumentedException e) {
180 fail("Not this exception should have occured:" + e);
185 public void testByteList() {
186 final ByteList b1 = new ByteList();
187 b1.add(new byte[] { 3, 4, 8 });
188 b1.add(new byte[] { 3, 4, 9 });
190 final ByteList b2 = new ByteList();
191 b2.add(new byte[] { 3, 4, 8 });
192 b2.add(new byte[] { 3, 4, 9 });
194 assertEquals(b1, b2);
195 assertEquals(b1.toString(), b2.toString());
199 public void testMessageParser() throws IOException {
200 final BGPMessageFactoryImpl parser = new BGPMessageFactoryImpl();
204 } catch (final IllegalArgumentException e) {
207 assertEquals("BGPMessage is mandatory.", ex);