BUG-45 : migrated IsoSystemIdentifier to generated source code.
[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.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;
16
17 import java.io.IOException;
18 import java.util.Collections;
19 import java.util.Set;
20
21 import org.junit.Test;
22 import org.opendaylight.protocol.bgp.linkstate.IPv4InterfaceIdentifier;
23 import org.opendaylight.protocol.bgp.linkstate.ISISLANIdentifier;
24 import org.opendaylight.protocol.bgp.linkstate.ISISRouterIdentifier;
25 import org.opendaylight.protocol.bgp.linkstate.InterfaceIdentifier;
26 import org.opendaylight.protocol.bgp.linkstate.LinkAnchor;
27 import org.opendaylight.protocol.bgp.linkstate.LinkIdentifier;
28 import org.opendaylight.protocol.bgp.linkstate.NodeIdentifier;
29 import org.opendaylight.protocol.bgp.linkstate.NodeIdentifierFactory;
30 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
31 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesParser;
32 import org.opendaylight.protocol.concepts.IPv4;
33 import org.opendaylight.protocol.framework.DeserializerException;
34 import org.opendaylight.protocol.framework.DocumentedException;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.ProtocolId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.path.attributes.AggregatorBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpAggregator;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CAsSpecificExtendedCommunity;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CAsSpecificExtendedCommunityBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CInet4SpecificExtendedCommunity;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CInet4SpecificExtendedCommunityBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.COpaqueExtendedCommunity;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.COpaqueExtendedCommunityBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteOriginExtendedCommunity;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteOriginExtendedCommunityBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteTargetExtendedCommunity;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteTargetExtendedCommunityBuilder;
50 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;
51 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;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.c.opaque.extended.community.OpaqueExtendedCommunityBuilder;
53 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;
54 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;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nps.concepts.rev130930.IsoSystemIdentifier;
56
57 import com.google.common.collect.Sets;
58
59 public class ComplementaryTest {
60
61         @Test
62         public void testBGPAggregatorImpl() {
63                 final BgpAggregator ipv4 = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5524)).setNetworkAddress(
64                                 new Ipv4Address("124.55.42.1")).build();
65                 final BgpAggregator ipv4i = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5525)).setNetworkAddress(
66                                 new Ipv4Address("124.55.42.1")).build();
67
68                 assertNotSame(ipv4.hashCode(), ipv4i.hashCode());
69
70                 assertNotSame(ipv4.getAsNumber(), ipv4i.getAsNumber());
71
72                 assertEquals(ipv4.getNetworkAddress(), ipv4i.getNetworkAddress());
73         }
74
75         @Test
76         public void testBGPLinkMP() {
77                 final NodeIdentifier localnodeid = new NodeIdentifier(new AsNumber((long) 25600), null, null, new ISISRouterIdentifier(new IsoSystemIdentifier(new byte[] {
78                                 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 })));
79                 final NodeIdentifier remotenodeid = NodeIdentifierFactory.localIdentifier(new ISISLANIdentifier(new IsoSystemIdentifier(new byte[] {
80                                 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 }), (short) 1));
81
82                 final InterfaceIdentifier ifaceid = new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("10.1.1.1"));
83
84                 final LinkIdentifier l = new LinkIdentifier(null, new LinkAnchor(localnodeid, ifaceid), new LinkAnchor(remotenodeid, ifaceid));
85
86                 final Set<LinkIdentifier> links = Sets.newHashSet(l);
87
88                 final BGPLinkMP link = new BGPLinkMP(0, ProtocolId.Direct, true, links);
89
90                 final BGPLinkMP link1 = new BGPLinkMP(0, ProtocolId.Direct, true, Collections.<LinkIdentifier> emptySet());
91
92                 assertNotSame(link.hashCode(), link1.hashCode());
93
94                 assertEquals(link, new BGPLinkMP(0, ProtocolId.Direct, true, links));
95
96                 assertEquals(link.hashCode(), (new BGPLinkMP(0, ProtocolId.Direct, true, links)).hashCode());
97
98                 assertNotSame(link.toString(), link1.toString());
99         }
100
101         @Test
102         public void testBGPUpdateMessageImpl() {
103                 final BGPUpdateMessageImpl msg = new BGPUpdateMessageImpl(null, null);
104                 final BGPUpdateMessageImpl msg1 = new BGPUpdateMessageImpl(null, null);
105
106                 assertEquals(msg, msg1);
107
108                 assertEquals(msg.hashCode(), msg1.hashCode());
109
110                 assertNotNull(msg.toString());
111
112                 assertNull(msg.getAddedObjects());
113                 assertNull(msg.getRemovedObjects());
114
115                 assertNotSame(msg1, null);
116         }
117
118         @Test
119         public void testCommunitiesParser() {
120                 CAsSpecificExtendedCommunity as = null;
121                 try {
122                         as = (CAsSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 });
123                 } catch (final BGPDocumentedException e1) {
124                         fail("Not expected exception: " + e1);
125                 }
126                 CAsSpecificExtendedCommunity expected = new CAsSpecificExtendedCommunityBuilder().setAsSpecificExtendedCommunity(
127                                 new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(new AsNumber(54L)).setLocalAdministrator(
128                                                 new byte[] { 0, 0, 1, 76 }).build()).build();
129                 assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), as.getAsSpecificExtendedCommunity().isTransitive());
130                 assertEquals(expected.getAsSpecificExtendedCommunity().getGlobalAdministrator(),
131                                 as.getAsSpecificExtendedCommunity().getGlobalAdministrator());
132                 assertArrayEquals(expected.getAsSpecificExtendedCommunity().getLocalAdministrator(),
133                                 as.getAsSpecificExtendedCommunity().getLocalAdministrator());
134
135                 try {
136                         as = (CAsSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 });
137                 } catch (final BGPDocumentedException e1) {
138                         fail("Not expected exception: " + e1);
139                 }
140                 expected = new CAsSpecificExtendedCommunityBuilder().setAsSpecificExtendedCommunity(
141                                 new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new AsNumber(54L)).setLocalAdministrator(
142                                                 new byte[] { 0, 0, 1, 76 }).build()).build();
143                 assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), as.getAsSpecificExtendedCommunity().isTransitive());
144
145                 CRouteTargetExtendedCommunity rtc = null;
146                 try {
147                         rtc = (CRouteTargetExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 });
148                 } catch (final BGPDocumentedException e1) {
149                         fail("Not expected exception: " + e1);
150                 }
151                 final CRouteTargetExtendedCommunity rexpected = new CRouteTargetExtendedCommunityBuilder().setRouteTargetExtendedCommunity(
152                                 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new AsNumber(35L)).setLocalAdministrator(
153                                                 new byte[] { 4, 2, 8, 7 }).build()).build();
154                 assertEquals(rexpected.getRouteTargetExtendedCommunity().getGlobalAdministrator(),
155                                 rtc.getRouteTargetExtendedCommunity().getGlobalAdministrator());
156                 assertArrayEquals(rexpected.getRouteTargetExtendedCommunity().getLocalAdministrator(),
157                                 rtc.getRouteTargetExtendedCommunity().getLocalAdministrator());
158
159                 CRouteOriginExtendedCommunity roc = null;
160                 try {
161                         roc = (CRouteOriginExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 });
162                 } catch (final BGPDocumentedException e1) {
163                         fail("Not expected exception: " + e1);
164                 }
165                 final CRouteOriginExtendedCommunity oexpected = new CRouteOriginExtendedCommunityBuilder().setRouteOriginExtendedCommunity(
166                                 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new AsNumber(24L)).setLocalAdministrator(
167                                                 new byte[] { 4, 2, 8, 7 }).build()).build();
168                 assertEquals(oexpected.getRouteOriginExtendedCommunity().getGlobalAdministrator(),
169                                 roc.getRouteOriginExtendedCommunity().getGlobalAdministrator());
170                 assertArrayEquals(oexpected.getRouteOriginExtendedCommunity().getLocalAdministrator(),
171                                 roc.getRouteOriginExtendedCommunity().getLocalAdministrator());
172
173                 CInet4SpecificExtendedCommunity sec = null;
174                 try {
175                         sec = (CInet4SpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 });
176                 } catch (final BGPDocumentedException e1) {
177                         fail("Not expected exception: " + e1);
178                 }
179                 final CInet4SpecificExtendedCommunity iexpected = new CInet4SpecificExtendedCommunityBuilder().setInet4SpecificExtendedCommunity(
180                                 new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new Ipv4Address("/12.51.2.5")).setLocalAdministrator(
181                                                 new byte[] { 21, 45 }).build()).build();
182                 assertEquals(iexpected.getInet4SpecificExtendedCommunity().isTransitive(), sec.getInet4SpecificExtendedCommunity().isTransitive());
183                 assertEquals(iexpected.getInet4SpecificExtendedCommunity().getGlobalAdministrator(),
184                                 sec.getInet4SpecificExtendedCommunity().getGlobalAdministrator());
185                 assertArrayEquals(iexpected.getInet4SpecificExtendedCommunity().getLocalAdministrator(),
186                                 sec.getInet4SpecificExtendedCommunity().getLocalAdministrator());
187
188                 COpaqueExtendedCommunity oec = null;
189                 try {
190                         oec = (COpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 });
191                 } catch (final BGPDocumentedException e1) {
192                         fail("Not expected exception: " + e1);
193                 }
194                 final COpaqueExtendedCommunity oeexpected = new COpaqueExtendedCommunityBuilder().setOpaqueExtendedCommunity(
195                                 new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
196                 assertEquals(oeexpected.getOpaqueExtendedCommunity().isTransitive(), oec.getOpaqueExtendedCommunity().isTransitive());
197                 assertArrayEquals(oeexpected.getOpaqueExtendedCommunity().getValue(), oec.getOpaqueExtendedCommunity().getValue());
198
199                 try {
200                         oec = (COpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 });
201                 } catch (final BGPDocumentedException e1) {
202                         fail("Not expected exception: " + e1);
203                 }
204                 final COpaqueExtendedCommunity oeexpected1 = new COpaqueExtendedCommunityBuilder().setOpaqueExtendedCommunity(
205                                 new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
206                 assertEquals(oeexpected1.getOpaqueExtendedCommunity().isTransitive(), oec.getOpaqueExtendedCommunity().isTransitive());
207                 assertArrayEquals(oeexpected1.getOpaqueExtendedCommunity().getValue(), oec.getOpaqueExtendedCommunity().getValue());
208
209                 try {
210                         CommunitiesParser.parseExtendedCommunity(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 });
211                         fail("Exception should have occured.");
212                 } catch (final BGPDocumentedException e) {
213                         assertEquals("Could not parse Extended Community type: 11", e.getMessage());
214                 }
215         }
216
217         @Test
218         public void testBGPHeaderParser() throws IOException {
219                 final BGPMessageFactoryImpl h = new BGPMessageFactoryImpl();
220                 try {
221                         h.parse(new byte[] { (byte) 0, (byte) 0 });
222                         fail("Exception should have occured.");
223                 } catch (final IllegalArgumentException e) {
224                         assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
225                 } catch (final DeserializerException e) {
226                         fail("Not this exception should have occured:" + e);
227                 } catch (final DocumentedException e) {
228                         fail("Not this exception should have occured:" + e);
229                 }
230         }
231
232         @Test
233         public void testByteList() {
234                 final ByteList b1 = new ByteList();
235                 b1.add(new byte[] { 3, 4, 8 });
236                 b1.add(new byte[] { 3, 4, 9 });
237
238                 final ByteList b2 = new ByteList();
239                 b2.add(new byte[] { 3, 4, 8 });
240                 b2.add(new byte[] { 3, 4, 9 });
241
242                 assertEquals(b1, b2);
243                 assertEquals(b1.toString(), b2.toString());
244         }
245
246         @Test
247         public void testMessageParser() throws IOException {
248                 final BGPMessageFactoryImpl parser = new BGPMessageFactoryImpl();
249                 String ex = "";
250                 try {
251                         parser.put(null);
252                 } catch (final IllegalArgumentException e) {
253                         ex = e.getMessage();
254                 }
255                 assertEquals("BGPMessage is mandatory.", ex);
256         }
257 }