95a01781b378ea7ad65cbf6a5c6d95e3924427ea
[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.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;
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 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()));
70
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);
77
78                 final BGPParsingException e = new BGPParsingException("Some error message.");
79                 assertEquals("Some error message.", e.getError());
80
81                 final BGPParsingException e2 = new BGPParsingException("Some error message.", new IllegalAccessException());
82                 assertEquals("Some error message.", e2.getError());
83                 assertTrue(e2.getCause() instanceof IllegalAccessException);
84         }
85
86         @Test
87         public void testPrefixes() throws UnknownHostException {
88                 final NetworkPrefixState state = new NetworkPrefixState(this.netObjState, Sets.<RouteTag> newTreeSet(), null);
89
90                 final BGPPrefixState ipv4 = new BGPPrefixState(this.objState, state);
91                 final BGPPrefixState ipv6 = new BGPPrefixState(new BaseBGPObjectState(BgpOrigin.Egp, null), state);
92
93                 assertEquals(ipv4.toString(), ipv4.newInstance().toString());
94                 assertNotSame(ipv4, new BGPPrefixState(ipv6));
95         }
96
97         @Test
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));
102         }
103
104         @Test
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));
111         }
112
113         @Test
114         public void testBGPParameter() {
115
116                 final BGPTableType t = new BGPTableType(LinkstateAddressFamily.class, UnicastSubsequentAddressFamily.class);
117                 final BGPTableType t1 = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
118
119                 final BGPParameter tlv1 = new MultiprotocolCapability(t);
120
121                 final BGPParameter tlv2 = new MultiprotocolCapability(t1);
122
123                 final Map<BGPTableType, Boolean> tt = Maps.newHashMap();
124                 tt.put(t, true);
125                 tt.put(t1, false);
126
127                 final BGPParameter tlv3 = new GracefulCapability(false, 0, tt);
128
129                 final BGPParameter tlv4 = new AS4BytesCapability(new AsNumber((long) 40));
130
131                 assertFalse(((GracefulCapability) tlv3).isRestartFlag());
132
133                 assertEquals(0, ((GracefulCapability) tlv3).getRestartTimerValue());
134
135                 assertEquals(tlv1.getType(), tlv2.getType());
136
137                 assertFalse(tlv1.equals(tlv2));
138
139                 assertNotSame(tlv1.hashCode(), tlv3.hashCode());
140
141                 assertNotSame(tlv2.toString(), tlv3.toString());
142
143                 assertEquals(((GracefulCapability) tlv3).getTableTypes(), tt);
144
145                 assertNotSame(((CapabilityParameter) tlv1).getCode(), ((CapabilityParameter) tlv3).getCode());
146
147                 assertEquals(((MultiprotocolCapability) tlv1).getSafi(), ((MultiprotocolCapability) tlv2).getSafi());
148
149                 assertNotSame(((MultiprotocolCapability) tlv1).getAfi(), ((MultiprotocolCapability) tlv2).getAfi());
150
151                 assertEquals(40, ((AS4BytesCapability) tlv4).getASNumber().getValue().longValue());
152
153                 assertEquals(new AS4BytesCapability(new AsNumber((long) 40)).toString(), tlv4.toString());
154         }
155
156         @Test
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());
162         }
163
164         @Test
165         public void testBGPKeepAliveMessage() {
166                 final BGPMessage msg = new BGPKeepAliveMessage();
167                 assertTrue(msg instanceof BGPKeepAliveMessage);
168         }
169
170         @Test
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());
176         }
177
178         @Test
179         public void testBGPOpenMessage() {
180                 final BGPMessage msg = new BGPOpenMessage(new AsNumber((long) 58), (short) 5, null, null);
181                 assertNull(((BGPOpenMessage) msg).getOptParams());
182         }
183
184         @Test
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());
189         }
190 }