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.BGPNotificationMessage;
35 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
36 import org.opendaylight.protocol.bgp.parser.parameter.AS4BytesCapability;
37 import org.opendaylight.protocol.bgp.parser.parameter.CapabilityParameter;
38 import org.opendaylight.protocol.bgp.parser.parameter.GracefulCapability;
39 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
40 import org.opendaylight.protocol.concepts.Metric;
41 import org.opendaylight.protocol.concepts.TEMetric;
42 import org.opendaylight.protocol.framework.DocumentedException;
43 import org.opendaylight.protocol.util.DefaultingTypesafeContainer;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Keepalive;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.KeepaliveBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.CIpv4NextHopBuilder;
55 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 org.opendaylight.yangtools.yang.binding.Notification;
58 import com.google.common.collect.Maps;
59 import com.google.common.collect.Sets;
61 public class APITest {
63 BaseBGPObjectState objState = new BaseBGPObjectState(BgpOrigin.Egp, null);
64 NetworkObjectState netObjState = new NetworkObjectState(null, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet());
67 public void testAPI() {
68 final BGPRouteState route1 = new BGPRouteState(this.objState, new NetworkRouteState(new CIpv4NextHopBuilder().setIpv4NextHop(
69 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("192.168.5.4")).build()).build()));
70 final BGPRouteState route2 = new BGPRouteState(new BaseBGPObjectState(BgpOrigin.Igp, null), new NetworkRouteState(new CIpv4NextHopBuilder().setIpv4NextHop(
71 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("172.168.5.42")).build()).build()));
73 assertEquals(route1, route1.newInstance());
74 assertNotSame(route1.hashCode(), new BGPRouteState(route2).hashCode());
75 assertEquals(route1.toString(), route1.toString());
76 assertNull(route1.getAggregator());
77 assertNotNull(route1.getObjectState().getNextHop());
78 assertEquals(route1.getOrigin(), BgpOrigin.Egp);
80 final BGPParsingException e = new BGPParsingException("Some error message.");
81 assertEquals("Some error message.", e.getError());
83 final BGPParsingException e2 = new BGPParsingException("Some error message.", new IllegalAccessException());
84 assertEquals("Some error message.", e2.getError());
85 assertTrue(e2.getCause() instanceof IllegalAccessException);
89 public void testPrefixes() throws UnknownHostException {
90 final NetworkPrefixState state = new NetworkPrefixState(this.netObjState, Sets.<RouteTag> newTreeSet(), null);
92 final BGPPrefixState ipv4 = new BGPPrefixState(this.objState, state);
93 final BGPPrefixState ipv6 = new BGPPrefixState(new BaseBGPObjectState(BgpOrigin.Egp, null), state);
95 assertEquals(ipv4.toString(), ipv4.newInstance().toString());
96 assertNotSame(ipv4, new BGPPrefixState(ipv6));
100 public void testNodeState() {
101 final BGPNodeState n1 = new BGPNodeState(this.objState, new NetworkNodeState(this.netObjState, Collections.<TopologyIdentifier> emptySet(), Collections.<ISISAreaIdentifier> emptySet(), false, false, false, false, Collections.<RouterIdentifier> emptySet(), ""));
102 assertEquals(n1, n1.newInstance());
103 assertEquals(n1, new BGPNodeState(n1));
107 public void testLinkState() {
108 final DefaultingTypesafeContainer<Metric<?>> m = new DefaultingTypesafeContainer<Metric<?>>();
109 m.setDefaultEntry(new TEMetric(15L));
110 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()));
111 assertEquals(l1, l1.newInstance());
112 assertEquals(l1, new BGPLinkState(l1));
116 public void testBGPParameter() {
118 final BGPTableType t = new BGPTableType(LinkstateAddressFamily.class, UnicastSubsequentAddressFamily.class);
119 final BGPTableType t1 = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
121 final BGPParameter tlv1 = new MultiprotocolCapability(t);
123 final BGPParameter tlv2 = new MultiprotocolCapability(t1);
125 final Map<BGPTableType, Boolean> tt = Maps.newHashMap();
129 final BGPParameter tlv3 = new GracefulCapability(false, 0, tt);
131 final BGPParameter tlv4 = new AS4BytesCapability(new AsNumber((long) 40));
133 assertFalse(((GracefulCapability) tlv3).isRestartFlag());
135 assertEquals(0, ((GracefulCapability) tlv3).getRestartTimerValue());
137 assertEquals(tlv1.getType(), tlv2.getType());
139 assertFalse(tlv1.equals(tlv2));
141 assertNotSame(tlv1.hashCode(), tlv3.hashCode());
143 assertNotSame(tlv2.toString(), tlv3.toString());
145 assertEquals(((GracefulCapability) tlv3).getTableTypes(), tt);
147 assertNotSame(((CapabilityParameter) tlv1).getCode(), ((CapabilityParameter) tlv3).getCode());
149 assertEquals(((MultiprotocolCapability) tlv1).getSafi(), ((MultiprotocolCapability) tlv2).getSafi());
151 assertNotSame(((MultiprotocolCapability) tlv1).getAfi(), ((MultiprotocolCapability) tlv2).getAfi());
153 assertEquals(40, ((AS4BytesCapability) tlv4).getASNumber().getValue().longValue());
155 assertEquals(new AS4BytesCapability(new AsNumber((long) 40)).toString(), tlv4.toString());
159 public void testDocumentedException() {
160 final DocumentedException de = new BGPDocumentedException("Some message", BGPError.BAD_BGP_ID);
161 assertEquals("Some message", de.getMessage());
162 assertEquals(BGPError.BAD_BGP_ID, ((BGPDocumentedException) de).getError());
163 assertNull(((BGPDocumentedException) de).getData());
167 public void testBGPKeepAliveMessage() {
168 final Notification msg = new KeepaliveBuilder().build();
169 assertTrue(msg instanceof Keepalive);
173 public void testBGPNotificationMessage() {
174 final Notification msg = new BGPNotificationMessage(BGPError.AS_PATH_MALFORMED);
175 assertTrue(msg instanceof BGPNotificationMessage);
176 assertEquals(BGPError.AS_PATH_MALFORMED, ((BGPNotificationMessage) msg).getError());
177 assertNull(((BGPNotificationMessage) msg).getData());
181 public void testBGPOpenMessage() {
182 final Notification msg = new BGPOpenMessage(new AsNumber((long) 58), (short) 5, null, null);
183 assertNull(((BGPOpenMessage) msg).getOptParams());
187 public void testToString() {
188 final Notification o = new BGPOpenMessage(new AsNumber((long) 58), (short) 5, null, null);
189 final Notification n = new BGPNotificationMessage(BGPError.ATTR_FLAGS_MISSING);
190 assertNotSame(o.toString(), n.toString());