4eee1a32d3ebd0c58088b431c8a5ac7d30b494c4
[bgpcep.git] / bgp / parser-api / src / test / java / org / opendaylight / protocol / bgp / parser / APITest.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;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
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.assertTrue;
16
17 import java.net.UnknownHostException;
18 import java.util.Collections;
19 import java.util.Map;
20
21 import org.junit.Test;
22 import org.opendaylight.protocol.bgp.concepts.BGPAddressFamily;
23 import org.opendaylight.protocol.bgp.concepts.BGPSubsequentAddressFamily;
24 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
25 import org.opendaylight.protocol.bgp.concepts.BaseBGPObjectState;
26 import org.opendaylight.protocol.bgp.concepts.Community;
27 import org.opendaylight.protocol.bgp.concepts.ExtendedCommunity;
28 import org.opendaylight.protocol.bgp.concepts.IPv4NextHop;
29 import org.opendaylight.protocol.bgp.linkstate.ISISAreaIdentifier;
30 import org.opendaylight.protocol.bgp.linkstate.LinkProtectionType;
31 import org.opendaylight.protocol.bgp.linkstate.NetworkLinkState;
32 import org.opendaylight.protocol.bgp.linkstate.NetworkNodeState;
33 import org.opendaylight.protocol.bgp.linkstate.NetworkObjectState;
34 import org.opendaylight.protocol.bgp.linkstate.NetworkPrefixState;
35 import org.opendaylight.protocol.bgp.linkstate.NetworkRouteState;
36 import org.opendaylight.protocol.bgp.linkstate.RouteTag;
37 import org.opendaylight.protocol.bgp.linkstate.RouterIdentifier;
38 import org.opendaylight.protocol.bgp.linkstate.TopologyIdentifier;
39 import org.opendaylight.protocol.bgp.parser.message.BGPKeepAliveMessage;
40 import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
41 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
42 import org.opendaylight.protocol.bgp.parser.parameter.AS4BytesCapability;
43 import org.opendaylight.protocol.bgp.parser.parameter.CapabilityParameter;
44 import org.opendaylight.protocol.bgp.parser.parameter.GracefulCapability;
45 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
46 import org.opendaylight.protocol.concepts.ASNumber;
47 import org.opendaylight.protocol.concepts.IPv4;
48 import org.opendaylight.protocol.concepts.IPv4Address;
49 import org.opendaylight.protocol.concepts.Metric;
50 import org.opendaylight.protocol.concepts.TEMetric;
51 import org.opendaylight.protocol.framework.DocumentedException;
52 import org.opendaylight.protocol.util.DefaultingTypesafeContainer;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
54
55 import com.google.common.collect.Maps;
56 import com.google.common.collect.Sets;
57
58 public class APITest {
59
60         BaseBGPObjectState objState = new BaseBGPObjectState(BgpOrigin.Egp, null);
61         NetworkObjectState netObjState = new NetworkObjectState(null, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet());
62
63         @Test
64         public void testAPI() {
65                 final BGPRouteState<IPv4Address> route1 = new BGPRouteState<IPv4Address>(this.objState, new NetworkRouteState<IPv4Address>(new IPv4NextHop(IPv4.FAMILY.addressForString("192.168.5.4"))));
66                 final BGPRouteState<IPv4Address> route2 = new BGPRouteState<IPv4Address>(new BaseBGPObjectState(BgpOrigin.Igp, null), new NetworkRouteState<IPv4Address>(new IPv4NextHop(IPv4.FAMILY.addressForString("172.168.5.42"))));
67
68                 assertEquals(route1, route1.newInstance());
69                 assertNotSame(route1.hashCode(), new BGPRouteState<IPv4Address>(route2).hashCode());
70                 assertEquals(route1.toString(), route1.toString());
71                 assertNull(route1.getAggregator());
72                 assertNotNull(route1.getObjectState().getNextHop());
73                 assertEquals(route1.getOrigin(), BgpOrigin.Egp);
74
75                 final BGPParsingException e = new BGPParsingException("Some error message.");
76                 assertEquals("Some error message.", e.getError());
77
78                 final BGPParsingException e2 = new BGPParsingException("Some error message.", new IllegalAccessException());
79                 assertEquals("Some error message.", e2.getError());
80                 assertTrue(e2.getCause() instanceof IllegalAccessException);
81         }
82
83         @Test
84         public void testPrefixes() throws UnknownHostException {
85                 final NetworkPrefixState state = new NetworkPrefixState(this.netObjState, Sets.<RouteTag> newTreeSet(), null);
86
87                 final BGPPrefixState ipv4 = new BGPPrefixState(this.objState, state);
88                 final BGPPrefixState ipv6 = new BGPPrefixState(new BaseBGPObjectState(BgpOrigin.Egp, null), state);
89
90                 assertEquals(ipv4.toString(), ipv4.newInstance().toString());
91                 assertNotSame(ipv4, new BGPPrefixState(ipv6));
92         }
93
94         @Test
95         public void testNodeState() {
96                 final BGPNodeState n1 = new BGPNodeState(this.objState, new NetworkNodeState(this.netObjState, Collections.<TopologyIdentifier> emptySet(), Collections.<ISISAreaIdentifier> emptySet(), false, false, false, false, Collections.<RouterIdentifier> emptySet(), ""));
97                 assertEquals(n1, n1.newInstance());
98                 assertEquals(n1, new BGPNodeState(n1));
99         }
100
101         @Test
102         public void testLinkState() {
103                 final DefaultingTypesafeContainer<Metric<?>> m = new DefaultingTypesafeContainer<Metric<?>>();
104                 m.setDefaultEntry(new TEMetric(15L));
105                 final BGPLinkState l1 = new BGPLinkState(this.objState, new NetworkLinkState(this.netObjState, m, null, null, null, LinkProtectionType.UNPROTECTED, null, null, null, null, Collections.<RouterIdentifier> emptySet(), Collections.<RouterIdentifier> emptySet()));
106                 assertEquals(l1, l1.newInstance());
107                 assertEquals(l1, new BGPLinkState(l1));
108         }
109
110         @Test
111         public void testBGPParameter() {
112
113                 final BGPTableType t = new BGPTableType(BGPAddressFamily.LinkState, BGPSubsequentAddressFamily.Unicast);
114                 final BGPTableType t1 = new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast);
115
116                 final BGPParameter tlv1 = new MultiprotocolCapability(t);
117
118                 final BGPParameter tlv2 = new MultiprotocolCapability(t1);
119
120                 final Map<BGPTableType, Boolean> tt = Maps.newHashMap();
121                 tt.put(t, true);
122                 tt.put(t1, false);
123
124                 final BGPParameter tlv3 = new GracefulCapability(false, 0, tt);
125
126                 final BGPParameter tlv4 = new AS4BytesCapability(new ASNumber(40));
127
128                 assertFalse(((GracefulCapability) tlv3).isRestartFlag());
129
130                 assertEquals(0, ((GracefulCapability) tlv3).getRestartTimerValue());
131
132                 assertEquals(tlv1.getType(), tlv2.getType());
133
134                 assertFalse(tlv1.equals(tlv2));
135
136                 assertNotSame(tlv1.hashCode(), tlv3.hashCode());
137
138                 assertNotSame(tlv2.toString(), tlv3.toString());
139
140                 assertEquals(((GracefulCapability) tlv3).getTableTypes(), tt);
141
142                 assertNotSame(((CapabilityParameter) tlv1).getCode(), ((CapabilityParameter) tlv3).getCode());
143
144                 assertEquals(((MultiprotocolCapability) tlv1).getSafi(), ((MultiprotocolCapability) tlv2).getSafi());
145
146                 assertNotSame(((MultiprotocolCapability) tlv1).getAfi(), ((MultiprotocolCapability) tlv2).getAfi());
147
148                 assertEquals(40, ((AS4BytesCapability) tlv4).getASNumber().getAsn());
149
150                 assertEquals(new AS4BytesCapability(new ASNumber(40)).toString(), tlv4.toString());
151         }
152
153         @Test
154         public void testDocumentedException() {
155                 final DocumentedException de = new BGPDocumentedException("Some message", BGPError.BAD_BGP_ID);
156                 assertEquals("Some message", de.getMessage());
157                 assertEquals(BGPError.BAD_BGP_ID, ((BGPDocumentedException) de).getError());
158                 assertNull(((BGPDocumentedException) de).getData());
159         }
160
161         @Test
162         public void testBGPKeepAliveMessage() {
163                 final BGPMessage msg = new BGPKeepAliveMessage();
164                 assertTrue(msg instanceof BGPKeepAliveMessage);
165         }
166
167         @Test
168         public void testBGPNotificationMessage() {
169                 final BGPMessage msg = new BGPNotificationMessage(BGPError.AS_PATH_MALFORMED);
170                 assertTrue(msg instanceof BGPNotificationMessage);
171                 assertEquals(BGPError.AS_PATH_MALFORMED, ((BGPNotificationMessage) msg).getError());
172                 assertNull(((BGPNotificationMessage) msg).getData());
173         }
174
175         @Test
176         public void testBGPOpenMessage() {
177                 final BGPMessage msg = new BGPOpenMessage(new ASNumber(58), (short) 5, null, null);
178                 assertNull(((BGPOpenMessage) msg).getOptParams());
179         }
180
181         @Test
182         public void testToString() {
183                 final BGPMessage o = new BGPOpenMessage(new ASNumber(58), (short) 5, null, null);
184                 final BGPMessage n = new BGPNotificationMessage(BGPError.ATTR_FLAGS_MISSING);
185                 assertNotSame(o.toString(), n.toString());
186         }
187 }