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