Move path-attributes into a grouping
[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.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;
43
44 import com.google.common.collect.Sets;
45
46 public class ComplementaryTest {
47
48         @Test
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();
54
55                 assertNotSame(ipv4.hashCode(), ipv4i.hashCode());
56
57                 assertNotSame(ipv4.getAsNumber(), ipv4i.getAsNumber());
58
59                 assertEquals(ipv4.getNetworkAddress(), ipv4i.getNetworkAddress());
60         }
61
62         @Test
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));
68
69                 final InterfaceIdentifier ifaceid = new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("10.1.1.1"));
70
71                 final LinkIdentifier l = new LinkIdentifier(null, new LinkAnchor(localnodeid, ifaceid), new LinkAnchor(remotenodeid, ifaceid));
72
73                 final Set<LinkIdentifier> links = Sets.newHashSet(l);
74
75                 final BGPLinkMP link = new BGPLinkMP(0, SourceProtocol.Direct, true, links);
76
77                 final BGPLinkMP link1 = new BGPLinkMP(0, SourceProtocol.Direct, true, Collections.<LinkIdentifier> emptySet());
78
79                 assertNotSame(link.hashCode(), link1.hashCode());
80
81                 assertEquals(link, new BGPLinkMP(0, SourceProtocol.Direct, true, links));
82
83                 assertEquals(link.hashCode(), (new BGPLinkMP(0, SourceProtocol.Direct, true, links)).hashCode());
84
85                 assertNotSame(link.toString(), link1.toString());
86         }
87
88         @Test
89         public void testBGPUpdateMessageImpl() {
90                 final BGPUpdateMessageImpl msg = new BGPUpdateMessageImpl(null, null);
91                 final BGPUpdateMessageImpl msg1 = new BGPUpdateMessageImpl(null, null);
92
93                 assertEquals(msg, msg1);
94
95                 assertEquals(msg.hashCode(), msg1.hashCode());
96
97                 assertNotNull(msg.toString());
98
99                 assertNull(msg.getAddedObjects());
100                 assertNull(msg.getRemovedObjects());
101
102                 assertNotSame(msg1, null);
103         }
104
105         @Test
106         public void testCommunitiesParser() {
107                 ASSpecificExtendedCommunity as = null;
108                 try {
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);
112                 }
113                 assertEquals(as, new ASSpecificExtendedCommunity(false, 5, new AsNumber((long) 54), new byte[] { 0, 0, 1, 76 }));
114
115                 try {
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);
119                 }
120                 assertEquals(as, new ASSpecificExtendedCommunity(true, 5, new AsNumber((long) 54), new byte[] { 0, 0, 1, 76 }));
121
122                 RouteTargetCommunity rtc = null;
123                 try {
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);
127                 }
128                 assertEquals(rtc, new RouteTargetCommunity(new AsNumber((long) 35), new byte[] { 4, 2, 8, 7 }));
129
130                 RouteOriginCommunity roc = null;
131                 try {
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);
135                 }
136                 assertEquals(roc, new RouteOriginCommunity(new AsNumber((long) 24), new byte[] { 4, 2, 8, 7 }));
137
138                 Inet4SpecificExtendedCommunity sec = null;
139                 try {
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);
143                 }
144                 assertEquals(sec, new Inet4SpecificExtendedCommunity(true, 6, IPv4.FAMILY.addressForString("12.51.2.5"), new byte[] { 21, 45 }));
145
146                 OpaqueExtendedCommunity oec = null;
147                 try {
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);
151                 }
152                 assertEquals(oec, new OpaqueExtendedCommunity(false, 6, new byte[] { 21, 45, 5, 4, 3, 1 }));
153
154                 try {
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);
158                 }
159                 assertEquals(oec, new OpaqueExtendedCommunity(true, 6, new byte[] { 21, 45, 5, 4, 3, 1 }));
160
161                 try {
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());
166                 }
167         }
168
169         @Test
170         public void testBGPHeaderParser() throws IOException {
171                 final BGPMessageFactoryImpl h = new BGPMessageFactoryImpl();
172                 try {
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);
181                 }
182         }
183
184         @Test
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 });
189
190                 final ByteList b2 = new ByteList();
191                 b2.add(new byte[] { 3, 4, 8 });
192                 b2.add(new byte[] { 3, 4, 9 });
193
194                 assertEquals(b1, b2);
195                 assertEquals(b1.toString(), b2.toString());
196         }
197
198         @Test
199         public void testMessageParser() throws IOException {
200                 final BGPMessageFactoryImpl parser = new BGPMessageFactoryImpl();
201                 String ex = "";
202                 try {
203                         parser.put(null);
204                 } catch (final IllegalArgumentException e) {
205                         ex = e.getMessage();
206                 }
207                 assertEquals("BGPMessage is mandatory.", ex);
208         }
209 }