2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.parser;
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;
17 import java.net.UnknownHostException;
18 import java.util.Collections;
21 import org.junit.Test;
22 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
23 import org.opendaylight.protocol.bgp.concepts.BaseBGPObjectState;
24 import org.opendaylight.protocol.bgp.linkstate.ISISAreaIdentifier;
25 import org.opendaylight.protocol.bgp.linkstate.LinkProtectionType;
26 import org.opendaylight.protocol.bgp.linkstate.NetworkLinkState;
27 import org.opendaylight.protocol.bgp.linkstate.NetworkNodeState;
28 import org.opendaylight.protocol.bgp.linkstate.NetworkObjectState;
29 import org.opendaylight.protocol.bgp.linkstate.NetworkPrefixState;
30 import org.opendaylight.protocol.bgp.linkstate.NetworkRouteState;
31 import org.opendaylight.protocol.bgp.linkstate.RouteTag;
32 import org.opendaylight.protocol.bgp.linkstate.RouterIdentifier;
33 import org.opendaylight.protocol.bgp.linkstate.TopologyIdentifier;
34 import org.opendaylight.protocol.bgp.parser.message.BGPKeepAliveMessage;
35 import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
36 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
37 import org.opendaylight.protocol.bgp.parser.parameter.AS4BytesCapability;
38 import org.opendaylight.protocol.bgp.parser.parameter.CapabilityParameter;
39 import org.opendaylight.protocol.bgp.parser.parameter.GracefulCapability;
40 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
41 import org.opendaylight.protocol.concepts.Metric;
42 import org.opendaylight.protocol.concepts.TEMetric;
43 import org.opendaylight.protocol.framework.DocumentedException;
44 import org.opendaylight.protocol.util.DefaultingTypesafeContainer;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.CIpv4NextHopBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.c.ipv4.next.hop.Ipv4NextHopBuilder;
56 import com.google.common.collect.Maps;
57 import com.google.common.collect.Sets;
59 public class APITest {
61 BaseBGPObjectState objState = new BaseBGPObjectState(BgpOrigin.Egp, null);
62 NetworkObjectState netObjState = new NetworkObjectState(null, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet());
65 public void testAPI() {
66 final BGPRouteState route1 = new BGPRouteState(this.objState, new NetworkRouteState(new CIpv4NextHopBuilder().setIpv4NextHop(
67 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("192.168.5.4")).build()).build()));
68 final BGPRouteState route2 = new BGPRouteState(new BaseBGPObjectState(BgpOrigin.Igp, null), new NetworkRouteState(new CIpv4NextHopBuilder().setIpv4NextHop(
69 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("172.168.5.42")).build()).build()));
71 assertEquals(route1, route1.newInstance());
72 assertNotSame(route1.hashCode(), new BGPRouteState(route2).hashCode());
73 assertEquals(route1.toString(), route1.toString());
74 assertNull(route1.getAggregator());
75 assertNotNull(route1.getObjectState().getNextHop());
76 assertEquals(route1.getOrigin(), BgpOrigin.Egp);
78 final BGPParsingException e = new BGPParsingException("Some error message.");
79 assertEquals("Some error message.", e.getError());
81 final BGPParsingException e2 = new BGPParsingException("Some error message.", new IllegalAccessException());
82 assertEquals("Some error message.", e2.getError());
83 assertTrue(e2.getCause() instanceof IllegalAccessException);
87 public void testPrefixes() throws UnknownHostException {
88 final NetworkPrefixState state = new NetworkPrefixState(this.netObjState, Sets.<RouteTag> newTreeSet(), null);
90 final BGPPrefixState ipv4 = new BGPPrefixState(this.objState, state);
91 final BGPPrefixState ipv6 = new BGPPrefixState(new BaseBGPObjectState(BgpOrigin.Egp, null), state);
93 assertEquals(ipv4.toString(), ipv4.newInstance().toString());
94 assertNotSame(ipv4, new BGPPrefixState(ipv6));
98 public void testNodeState() {
99 final BGPNodeState n1 = new BGPNodeState(this.objState, new NetworkNodeState(this.netObjState, Collections.<TopologyIdentifier> emptySet(), Collections.<ISISAreaIdentifier> emptySet(), false, false, false, false, Collections.<RouterIdentifier> emptySet(), ""));
100 assertEquals(n1, n1.newInstance());
101 assertEquals(n1, new BGPNodeState(n1));
105 public void testLinkState() {
106 final DefaultingTypesafeContainer<Metric<?>> m = new DefaultingTypesafeContainer<Metric<?>>();
107 m.setDefaultEntry(new TEMetric(15L));
108 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()));
109 assertEquals(l1, l1.newInstance());
110 assertEquals(l1, new BGPLinkState(l1));
114 public void testBGPParameter() {
116 final BGPTableType t = new BGPTableType(LinkstateAddressFamily.class, UnicastSubsequentAddressFamily.class);
117 final BGPTableType t1 = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
119 final BGPParameter tlv1 = new MultiprotocolCapability(t);
121 final BGPParameter tlv2 = new MultiprotocolCapability(t1);
123 final Map<BGPTableType, Boolean> tt = Maps.newHashMap();
127 final BGPParameter tlv3 = new GracefulCapability(false, 0, tt);
129 final BGPParameter tlv4 = new AS4BytesCapability(new AsNumber((long) 40));
131 assertFalse(((GracefulCapability) tlv3).isRestartFlag());
133 assertEquals(0, ((GracefulCapability) tlv3).getRestartTimerValue());
135 assertEquals(tlv1.getType(), tlv2.getType());
137 assertFalse(tlv1.equals(tlv2));
139 assertNotSame(tlv1.hashCode(), tlv3.hashCode());
141 assertNotSame(tlv2.toString(), tlv3.toString());
143 assertEquals(((GracefulCapability) tlv3).getTableTypes(), tt);
145 assertNotSame(((CapabilityParameter) tlv1).getCode(), ((CapabilityParameter) tlv3).getCode());
147 assertEquals(((MultiprotocolCapability) tlv1).getSafi(), ((MultiprotocolCapability) tlv2).getSafi());
149 assertNotSame(((MultiprotocolCapability) tlv1).getAfi(), ((MultiprotocolCapability) tlv2).getAfi());
151 assertEquals(40, ((AS4BytesCapability) tlv4).getASNumber().getValue().longValue());
153 assertEquals(new AS4BytesCapability(new AsNumber((long) 40)).toString(), tlv4.toString());
157 public void testDocumentedException() {
158 final DocumentedException de = new BGPDocumentedException("Some message", BGPError.BAD_BGP_ID);
159 assertEquals("Some message", de.getMessage());
160 assertEquals(BGPError.BAD_BGP_ID, ((BGPDocumentedException) de).getError());
161 assertNull(((BGPDocumentedException) de).getData());
165 public void testBGPKeepAliveMessage() {
166 final BGPMessage msg = new BGPKeepAliveMessage();
167 assertTrue(msg instanceof BGPKeepAliveMessage);
171 public void testBGPNotificationMessage() {
172 final BGPMessage msg = new BGPNotificationMessage(BGPError.AS_PATH_MALFORMED);
173 assertTrue(msg instanceof BGPNotificationMessage);
174 assertEquals(BGPError.AS_PATH_MALFORMED, ((BGPNotificationMessage) msg).getError());
175 assertNull(((BGPNotificationMessage) msg).getData());
179 public void testBGPOpenMessage() {
180 final BGPMessage msg = new BGPOpenMessage(new AsNumber((long) 58), (short) 5, null, null);
181 assertNull(((BGPOpenMessage) msg).getOptParams());
185 public void testToString() {
186 final BGPMessage o = new BGPOpenMessage(new AsNumber((long) 58), (short) 5, null, null);
187 final BGPMessage n = new BGPNotificationMessage(BGPError.ATTR_FLAGS_MISSING);
188 assertNotSame(o.toString(), n.toString());