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