5a706d8259abf379e3f15bcd06e0cf033557f189
[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.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;
57
58 import com.google.common.collect.Maps;
59 import com.google.common.collect.Sets;
60
61 public class APITest {
62
63         BaseBGPObjectState objState = new BaseBGPObjectState(BgpOrigin.Egp, null);
64         NetworkObjectState netObjState = new NetworkObjectState(null, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet());
65
66         @Test
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()));
72
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);
79
80                 final BGPParsingException e = new BGPParsingException("Some error message.");
81                 assertEquals("Some error message.", e.getError());
82
83                 final BGPParsingException e2 = new BGPParsingException("Some error message.", new IllegalAccessException());
84                 assertEquals("Some error message.", e2.getError());
85                 assertTrue(e2.getCause() instanceof IllegalAccessException);
86         }
87
88         @Test
89         public void testPrefixes() throws UnknownHostException {
90                 final NetworkPrefixState state = new NetworkPrefixState(this.netObjState, Sets.<RouteTag> newTreeSet(), null);
91
92                 final BGPPrefixState ipv4 = new BGPPrefixState(this.objState, state);
93                 final BGPPrefixState ipv6 = new BGPPrefixState(new BaseBGPObjectState(BgpOrigin.Egp, null), state);
94
95                 assertEquals(ipv4.toString(), ipv4.newInstance().toString());
96                 assertNotSame(ipv4, new BGPPrefixState(ipv6));
97         }
98
99         @Test
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));
104         }
105
106         @Test
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));
113         }
114
115         @Test
116         public void testBGPParameter() {
117
118                 final BGPTableType t = new BGPTableType(LinkstateAddressFamily.class, UnicastSubsequentAddressFamily.class);
119                 final BGPTableType t1 = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
120
121                 final BGPParameter tlv1 = new MultiprotocolCapability(t);
122
123                 final BGPParameter tlv2 = new MultiprotocolCapability(t1);
124
125                 final Map<BGPTableType, Boolean> tt = Maps.newHashMap();
126                 tt.put(t, true);
127                 tt.put(t1, false);
128
129                 final BGPParameter tlv3 = new GracefulCapability(false, 0, tt);
130
131                 final BGPParameter tlv4 = new AS4BytesCapability(new AsNumber((long) 40));
132
133                 assertFalse(((GracefulCapability) tlv3).isRestartFlag());
134
135                 assertEquals(0, ((GracefulCapability) tlv3).getRestartTimerValue());
136
137                 assertEquals(tlv1.getType(), tlv2.getType());
138
139                 assertFalse(tlv1.equals(tlv2));
140
141                 assertNotSame(tlv1.hashCode(), tlv3.hashCode());
142
143                 assertNotSame(tlv2.toString(), tlv3.toString());
144
145                 assertEquals(((GracefulCapability) tlv3).getTableTypes(), tt);
146
147                 assertNotSame(((CapabilityParameter) tlv1).getCode(), ((CapabilityParameter) tlv3).getCode());
148
149                 assertEquals(((MultiprotocolCapability) tlv1).getSafi(), ((MultiprotocolCapability) tlv2).getSafi());
150
151                 assertNotSame(((MultiprotocolCapability) tlv1).getAfi(), ((MultiprotocolCapability) tlv2).getAfi());
152
153                 assertEquals(40, ((AS4BytesCapability) tlv4).getASNumber().getValue().longValue());
154
155                 assertEquals(new AS4BytesCapability(new AsNumber((long) 40)).toString(), tlv4.toString());
156         }
157
158         @Test
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());
164         }
165
166         @Test
167         public void testBGPKeepAliveMessage() {
168                 final Notification msg = new KeepaliveBuilder().build();
169                 assertTrue(msg instanceof Keepalive);
170         }
171
172         @Test
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());
178         }
179
180         @Test
181         public void testBGPOpenMessage() {
182                 final Notification msg = new BGPOpenMessage(new AsNumber((long) 58), (short) 5, null, null);
183                 assertNull(((BGPOpenMessage) msg).getOptParams());
184         }
185
186         @Test
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());
191         }
192 }