Initial framework migration to netty.
[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.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;
15
16 import java.io.IOException;
17 import java.util.Collections;
18 import java.util.Set;
19
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;
45
46 import com.google.common.collect.Sets;
47
48 public class ComplementaryTest {
49
50         @Test
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"));
54
55                 assertNotSame(ipv4.hashCode(), ipv4i.hashCode());
56
57                 assertEquals(ipv4, new BGPAggregatorImpl<IPv4Address>(new ASNumber(5524), IPv4.FAMILY.addressForString("124.55.42.1")));
58
59                 assertNotSame(ipv4.getASNumber(), ipv4i.getASNumber());
60
61                 assertEquals(ipv4.getNetworkAddress(), ipv4i.getNetworkAddress());
62         }
63
64         @Test
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));
70
71                 final InterfaceIdentifier ifaceid = new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("10.1.1.1"));
72
73                 final LinkIdentifier l = new LinkIdentifier(null, new LinkAnchor(localnodeid, ifaceid), new LinkAnchor(remotenodeid, ifaceid));
74
75                 final Set<LinkIdentifier> links = Sets.newHashSet(l);
76
77                 final BGPLinkMP link = new BGPLinkMP(0, SourceProtocol.Direct, true, links);
78
79                 final BGPLinkMP link1 = new BGPLinkMP(0, SourceProtocol.Direct, true, Collections.<LinkIdentifier> emptySet());
80
81                 assertNotSame(link.hashCode(), link1.hashCode());
82
83                 assertEquals(link, new BGPLinkMP(0, SourceProtocol.Direct, true, links));
84
85                 assertEquals(link.hashCode(), (new BGPLinkMP(0, SourceProtocol.Direct, true, links)).hashCode());
86
87                 assertNotSame(link.toString(), link1.toString());
88         }
89
90         @Test
91         public void testBGPUpdateMessageImpl() {
92                 final BGPUpdateMessageImpl msg = new BGPUpdateMessageImpl(null, null);
93                 final BGPUpdateMessageImpl msg1 = new BGPUpdateMessageImpl(null, null);
94
95                 assertEquals(msg, msg1);
96
97                 assertEquals(msg.hashCode(), msg1.hashCode());
98
99                 assertNotNull(msg.toString());
100
101                 assertNull(msg.getAddedObjects());
102                 assertNull(msg.getRemovedObjects());
103
104                 assertNotSame(msg1, null);
105         }
106
107         @Test
108         public void testCommunitiesParser() {
109                 ASSpecificExtendedCommunity as = null;
110                 try {
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);
114                 }
115                 assertEquals(as, new ASSpecificExtendedCommunity(false, 5, new ASNumber(54), new byte[] { 0, 0, 1, 76 }));
116
117                 try {
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);
121                 }
122                 assertEquals(as, new ASSpecificExtendedCommunity(true, 5, new ASNumber(54), new byte[] { 0, 0, 1, 76 }));
123
124                 RouteTargetCommunity rtc = null;
125                 try {
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);
129                 }
130                 assertEquals(rtc, new RouteTargetCommunity(new ASNumber(35), new byte[] { 4, 2, 8, 7 }));
131
132                 RouteOriginCommunity roc = null;
133                 try {
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);
137                 }
138                 assertEquals(roc, new RouteOriginCommunity(new ASNumber(24), new byte[] { 4, 2, 8, 7 }));
139
140                 Inet4SpecificExtendedCommunity sec = null;
141                 try {
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);
145                 }
146                 assertEquals(sec, new Inet4SpecificExtendedCommunity(true, 6, IPv4.FAMILY.addressForString("12.51.2.5"), new byte[] { 21, 45 }));
147
148                 OpaqueExtendedCommunity oec = null;
149                 try {
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);
153                 }
154                 assertEquals(oec, new OpaqueExtendedCommunity(false, 6, new byte[] { 21, 45, 5, 4, 3, 1 }));
155
156                 try {
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);
160                 }
161                 assertEquals(oec, new OpaqueExtendedCommunity(true, 6, new byte[] { 21, 45, 5, 4, 3, 1 }));
162
163                 try {
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());
168                 }
169         }
170
171         @Test
172         public void testMPReachParser() {
173                 String msg = "";
174                 try {
175                         MPReachParser.parseSafi(5);
176                         fail("Exception shoul have occured.");
177                 } catch (final BGPParsingException e) {
178                         msg = e.getMessage();
179                 }
180                 assertEquals("Subsequent Address Family Identifier: '5' not supported.", msg);
181
182                 try {
183                         MPReachParser.parseAfi(6);
184                         fail("Exception should have occured.");
185                 } catch (final BGPParsingException e) {
186                         msg = e.getMessage();
187                 }
188                 assertEquals("Address Family Identifier: '6' not supported.", msg);
189         }
190
191         @Test
192         public void testBGPHeaderParser() throws IOException {
193                 final BGPMessageFactory h = new BGPMessageFactory();
194                 try {
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);
203                 }
204         }
205
206         @Test
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 });
211
212                 final ByteList b2 = new ByteList();
213                 b2.add(new byte[] { 3, 4, 8 });
214                 b2.add(new byte[] { 3, 4, 9 });
215
216                 assertEquals(b1, b2);
217                 assertEquals(b1.toString(), b2.toString());
218         }
219
220         @Test
221         public void testMessageParser() throws IOException {
222                 final BGPMessageFactory parser = new BGPMessageFactory();
223                 String ex = "";
224                 try {
225                         parser.put(null);
226                 } catch (final IllegalArgumentException e) {
227                         ex = e.getMessage();
228                 }
229                 assertEquals("BGPMessage is mandatory.", ex);
230         }
231 }