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.BaseBGPObjectState;
23 import org.opendaylight.protocol.bgp.linkstate.ISISAreaIdentifier;
24 import org.opendaylight.protocol.bgp.linkstate.LinkProtectionType;
25 import org.opendaylight.protocol.bgp.linkstate.NetworkLinkState;
26 import org.opendaylight.protocol.bgp.linkstate.NetworkNodeState;
27 import org.opendaylight.protocol.bgp.linkstate.NetworkObjectState;
28 import org.opendaylight.protocol.bgp.linkstate.NetworkPrefixState;
29 import org.opendaylight.protocol.bgp.linkstate.NetworkRouteState;
30 import org.opendaylight.protocol.bgp.linkstate.RouteTag;
31 import org.opendaylight.protocol.bgp.linkstate.RouterIdentifier;
32 import org.opendaylight.protocol.bgp.linkstate.TopologyIdentifier;
33 import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
34 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
35 import org.opendaylight.protocol.bgp.parser.parameter.AS4BytesCapability;
36 import org.opendaylight.protocol.bgp.parser.parameter.CapabilityParameter;
37 import org.opendaylight.protocol.bgp.parser.parameter.GracefulCapability;
38 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
39 import org.opendaylight.protocol.concepts.Metric;
40 import org.opendaylight.protocol.concepts.TEMetric;
41 import org.opendaylight.protocol.framework.DocumentedException;
42 import org.opendaylight.protocol.util.DefaultingTypesafeContainer;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Keepalive;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.KeepaliveBuilder;
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;
55 import org.opendaylight.yangtools.yang.binding.Notification;
57 import com.google.common.collect.Maps;
58 import com.google.common.collect.Sets;
60 public class APITest {
62 BaseBGPObjectState objState = new BaseBGPObjectState(BgpOrigin.Egp, null);
63 NetworkObjectState netObjState = new NetworkObjectState(null, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet());
66 public void testAPI() {
67 final BGPRouteState route1 = new BGPRouteState(this.objState, new NetworkRouteState(new CIpv4NextHopBuilder().setIpv4NextHop(
68 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("192.168.5.4")).build()).build()));
69 final BGPRouteState route2 = new BGPRouteState(new BaseBGPObjectState(BgpOrigin.Igp, null), new NetworkRouteState(new CIpv4NextHopBuilder().setIpv4NextHop(
70 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("172.168.5.42")).build()).build()));
72 assertEquals(route1, route1.newInstance());
73 assertNotSame(route1.hashCode(), new BGPRouteState(route2).hashCode());
74 assertEquals(route1.toString(), route1.toString());
75 assertNull(route1.getAggregator());
76 assertNotNull(route1.getObjectState().getNextHop());
77 assertEquals(route1.getOrigin(), BgpOrigin.Egp);
79 final BGPParsingException e = new BGPParsingException("Some error message.");
80 assertEquals("Some error message.", e.getError());
82 final BGPParsingException e2 = new BGPParsingException("Some error message.", new IllegalAccessException());
83 assertEquals("Some error message.", e2.getError());
84 assertTrue(e2.getCause() instanceof IllegalAccessException);
88 public void testPrefixes() throws UnknownHostException {
89 final NetworkPrefixState state = new NetworkPrefixState(this.netObjState, Sets.<RouteTag> newTreeSet(), null);
91 final BGPPrefixState ipv4 = new BGPPrefixState(this.objState, state);
92 final BGPPrefixState ipv6 = new BGPPrefixState(new BaseBGPObjectState(BgpOrigin.Egp, null), state);
94 assertEquals(ipv4.toString(), ipv4.newInstance().toString());
95 assertNotSame(ipv4, new BGPPrefixState(ipv6));
99 public void testNodeState() {
100 final BGPNodeState n1 = new BGPNodeState(this.objState, new NetworkNodeState(this.netObjState, Collections.<TopologyIdentifier> emptySet(), Collections.<ISISAreaIdentifier> emptySet(), false, false, false, false, Collections.<RouterIdentifier> emptySet(), ""));
101 assertEquals(n1, n1.newInstance());
102 assertEquals(n1, new BGPNodeState(n1));
106 public void testLinkState() {
107 final DefaultingTypesafeContainer<Metric<?>> m = new DefaultingTypesafeContainer<Metric<?>>();
108 m.setDefaultEntry(new TEMetric(15L));
109 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()));
110 assertEquals(l1, l1.newInstance());
111 assertEquals(l1, new BGPLinkState(l1));
115 public void testBGPParameter() {
117 final BGPTableType t = new BGPTableType(LinkstateAddressFamily.class, UnicastSubsequentAddressFamily.class);
118 final BGPTableType t1 = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
120 final BGPParameter tlv1 = new MultiprotocolCapability(t);
122 final BGPParameter tlv2 = new MultiprotocolCapability(t1);
124 final Map<BGPTableType, Boolean> tt = Maps.newHashMap();
128 final BGPParameter tlv3 = new GracefulCapability(false, 0, tt);
130 final BGPParameter tlv4 = new AS4BytesCapability(new AsNumber((long) 40));
132 assertFalse(((GracefulCapability) tlv3).isRestartFlag());
134 assertEquals(0, ((GracefulCapability) tlv3).getRestartTimerValue());
136 assertEquals(tlv1.getType(), tlv2.getType());
138 assertFalse(tlv1.equals(tlv2));
140 assertNotSame(tlv1.hashCode(), tlv3.hashCode());
142 assertNotSame(tlv2.toString(), tlv3.toString());
144 assertEquals(((GracefulCapability) tlv3).getTableTypes(), tt);
146 assertNotSame(((CapabilityParameter) tlv1).getCode(), ((CapabilityParameter) tlv3).getCode());
148 assertEquals(((MultiprotocolCapability) tlv1).getSafi(), ((MultiprotocolCapability) tlv2).getSafi());
150 assertNotSame(((MultiprotocolCapability) tlv1).getAfi(), ((MultiprotocolCapability) tlv2).getAfi());
152 assertEquals(40, ((AS4BytesCapability) tlv4).getASNumber().getValue().longValue());
154 assertEquals(new AS4BytesCapability(new AsNumber((long) 40)).toString(), tlv4.toString());
158 public void testDocumentedException() {
159 final DocumentedException de = new BGPDocumentedException("Some message", BGPError.BAD_BGP_ID);
160 assertEquals("Some message", de.getMessage());
161 assertEquals(BGPError.BAD_BGP_ID, ((BGPDocumentedException) de).getError());
162 assertNull(((BGPDocumentedException) de).getData());
166 public void testBGPKeepAliveMessage() {
167 final Notification msg = new KeepaliveBuilder().build();
168 assertTrue(msg instanceof Keepalive);
172 public void testBGPNotificationMessage() {
173 final Notification msg = new BGPNotificationMessage(BGPError.AS_PATH_MALFORMED);
174 assertTrue(msg instanceof BGPNotificationMessage);
175 assertEquals(BGPError.AS_PATH_MALFORMED, ((BGPNotificationMessage) msg).getError());
176 assertNull(((BGPNotificationMessage) msg).getData());
180 public void testBGPOpenMessage() {
181 final Notification msg = new BGPOpenMessage(new AsNumber((long) 58), (short) 5, null, null);
182 assertNull(((BGPOpenMessage) msg).getOptParams());
186 public void testToString() {
187 final Notification o = new BGPOpenMessage(new AsNumber((long) 58), (short) 5, null, null);
188 final Notification n = new BGPNotificationMessage(BGPError.ATTR_FLAGS_MISSING);
189 assertNotSame(o.toString(), n.toString());