X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=bgp%2Fparser-api%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fbgp%2Fparser%2FAPITest.java;h=90dab89fcf5ccf1daf760263e765f18b5aa7f84c;hb=3860d202e14ae693d515cf40f4795fd086a5d75d;hp=7e029761c5cb69a8d3634a7621fc369f4d39b743;hpb=672a33b8684e050e4c87e366b245b10c62a1cf8f;p=bgpcep.git diff --git a/bgp/parser-api/src/test/java/org/opendaylight/protocol/bgp/parser/APITest.java b/bgp/parser-api/src/test/java/org/opendaylight/protocol/bgp/parser/APITest.java index 7e029761c5..90dab89fcf 100644 --- a/bgp/parser-api/src/test/java/org/opendaylight/protocol/bgp/parser/APITest.java +++ b/bgp/parser-api/src/test/java/org/opendaylight/protocol/bgp/parser/APITest.java @@ -8,180 +8,91 @@ package org.opendaylight.protocol.bgp.parser; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import java.net.UnknownHostException; -import java.util.Collections; -import java.util.Map; - +import java.util.ArrayList; +import java.util.List; import org.junit.Test; -import org.opendaylight.protocol.bgp.concepts.BGPTableType; -import org.opendaylight.protocol.bgp.concepts.BaseBGPObjectState; -import org.opendaylight.protocol.bgp.concepts.Community; -import org.opendaylight.protocol.bgp.concepts.ExtendedCommunity; -import org.opendaylight.protocol.bgp.concepts.IPv4NextHop; -import org.opendaylight.protocol.bgp.linkstate.ISISAreaIdentifier; -import org.opendaylight.protocol.bgp.linkstate.LinkProtectionType; -import org.opendaylight.protocol.bgp.linkstate.NetworkLinkState; -import org.opendaylight.protocol.bgp.linkstate.NetworkNodeState; -import org.opendaylight.protocol.bgp.linkstate.NetworkObjectState; -import org.opendaylight.protocol.bgp.linkstate.NetworkPrefixState; -import org.opendaylight.protocol.bgp.linkstate.NetworkRouteState; -import org.opendaylight.protocol.bgp.linkstate.RouteTag; -import org.opendaylight.protocol.bgp.linkstate.RouterIdentifier; -import org.opendaylight.protocol.bgp.linkstate.TopologyIdentifier; -import org.opendaylight.protocol.bgp.parser.message.BGPKeepAliveMessage; -import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage; -import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage; -import org.opendaylight.protocol.bgp.parser.parameter.AS4BytesCapability; -import org.opendaylight.protocol.bgp.parser.parameter.CapabilityParameter; -import org.opendaylight.protocol.bgp.parser.parameter.GracefulCapability; -import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability; -import org.opendaylight.protocol.concepts.ASNumber; -import org.opendaylight.protocol.concepts.IPv4; -import org.opendaylight.protocol.concepts.IPv4Address; -import org.opendaylight.protocol.concepts.Metric; -import org.opendaylight.protocol.concepts.TEMetric; -import org.opendaylight.protocol.framework.DocumentedException; -import org.opendaylight.protocol.util.DefaultingTypesafeContainer; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpAddressFamily; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpSubsequentAddressFamily; - -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; +import org.opendaylight.protocol.util.ByteArray; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Open; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.OpenBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParametersBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.OptionalCapabilities; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.OptionalCapabilitiesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.optional.capabilities.CParametersBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.MultiprotocolCapabilityBuilder; +import org.opendaylight.yangtools.yang.common.Uint16; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.common.Uint8; public class APITest { - BaseBGPObjectState objState = new BaseBGPObjectState(BgpOrigin.Egp, null); - NetworkObjectState netObjState = new NetworkObjectState(null, Collections. emptySet(), Collections. emptySet()); - - @Test - public void testAPI() { - final BGPRouteState route1 = new BGPRouteState(this.objState, new NetworkRouteState(new IPv4NextHop(IPv4.FAMILY.addressForString("192.168.5.4")))); - final BGPRouteState route2 = new BGPRouteState(new BaseBGPObjectState(BgpOrigin.Igp, null), new NetworkRouteState(new IPv4NextHop(IPv4.FAMILY.addressForString("172.168.5.42")))); - - assertEquals(route1, route1.newInstance()); - assertNotSame(route1.hashCode(), new BGPRouteState(route2).hashCode()); - assertEquals(route1.toString(), route1.toString()); - assertNull(route1.getAggregator()); - assertNotNull(route1.getObjectState().getNextHop()); - assertEquals(route1.getOrigin(), BgpOrigin.Egp); - - final BGPParsingException e = new BGPParsingException("Some error message."); - assertEquals("Some error message.", e.getError()); - - final BGPParsingException e2 = new BGPParsingException("Some error message.", new IllegalAccessException()); - assertEquals("Some error message.", e2.getError()); - assertTrue(e2.getCause() instanceof IllegalAccessException); - } - - @Test - public void testPrefixes() throws UnknownHostException { - final NetworkPrefixState state = new NetworkPrefixState(this.netObjState, Sets. newTreeSet(), null); - - final BGPPrefixState ipv4 = new BGPPrefixState(this.objState, state); - final BGPPrefixState ipv6 = new BGPPrefixState(new BaseBGPObjectState(BgpOrigin.Egp, null), state); - - assertEquals(ipv4.toString(), ipv4.newInstance().toString()); - assertNotSame(ipv4, new BGPPrefixState(ipv6)); - } - - @Test - public void testNodeState() { - final BGPNodeState n1 = new BGPNodeState(this.objState, new NetworkNodeState(this.netObjState, Collections. emptySet(), Collections. emptySet(), false, false, false, false, Collections. emptySet(), "")); - assertEquals(n1, n1.newInstance()); - assertEquals(n1, new BGPNodeState(n1)); - } - - @Test - public void testLinkState() { - final DefaultingTypesafeContainer> m = new DefaultingTypesafeContainer>(); - m.setDefaultEntry(new TEMetric(15L)); - final BGPLinkState l1 = new BGPLinkState(this.objState, new NetworkLinkState(this.netObjState, m, null, null, null, LinkProtectionType.UNPROTECTED, null, null, null, null, Collections. emptySet(), Collections. emptySet())); - assertEquals(l1, l1.newInstance()); - assertEquals(l1, new BGPLinkState(l1)); - } - - @Test - public void testBGPParameter() { - - final BGPTableType t = new BGPTableType(BgpAddressFamily.Linkstate, BgpSubsequentAddressFamily.Unicast); - final BGPTableType t1 = new BGPTableType(BgpAddressFamily.Ipv4, BgpSubsequentAddressFamily.Unicast); - - final BGPParameter tlv1 = new MultiprotocolCapability(t); - - final BGPParameter tlv2 = new MultiprotocolCapability(t1); - - final Map tt = Maps.newHashMap(); - tt.put(t, true); - tt.put(t1, false); - - final BGPParameter tlv3 = new GracefulCapability(false, 0, tt); - - final BGPParameter tlv4 = new AS4BytesCapability(new ASNumber(40)); - - assertFalse(((GracefulCapability) tlv3).isRestartFlag()); - - assertEquals(0, ((GracefulCapability) tlv3).getRestartTimerValue()); - - assertEquals(tlv1.getType(), tlv2.getType()); - - assertFalse(tlv1.equals(tlv2)); - - assertNotSame(tlv1.hashCode(), tlv3.hashCode()); - - assertNotSame(tlv2.toString(), tlv3.toString()); - - assertEquals(((GracefulCapability) tlv3).getTableTypes(), tt); - - assertNotSame(((CapabilityParameter) tlv1).getCode(), ((CapabilityParameter) tlv3).getCode()); - - assertEquals(((MultiprotocolCapability) tlv1).getSafi(), ((MultiprotocolCapability) tlv2).getSafi()); - - assertNotSame(((MultiprotocolCapability) tlv1).getAfi(), ((MultiprotocolCapability) tlv2).getAfi()); - - assertEquals(40, ((AS4BytesCapability) tlv4).getASNumber().getAsn()); - - assertEquals(new AS4BytesCapability(new ASNumber(40)).toString(), tlv4.toString()); - } - - @Test - public void testDocumentedException() { - final DocumentedException de = new BGPDocumentedException("Some message", BGPError.BAD_BGP_ID); - assertEquals("Some message", de.getMessage()); - assertEquals(BGPError.BAD_BGP_ID, ((BGPDocumentedException) de).getError()); - assertNull(((BGPDocumentedException) de).getData()); - } - - @Test - public void testBGPKeepAliveMessage() { - final BGPMessage msg = new BGPKeepAliveMessage(); - assertTrue(msg instanceof BGPKeepAliveMessage); - } - - @Test - public void testBGPNotificationMessage() { - final BGPMessage msg = new BGPNotificationMessage(BGPError.AS_PATH_MALFORMED); - assertTrue(msg instanceof BGPNotificationMessage); - assertEquals(BGPError.AS_PATH_MALFORMED, ((BGPNotificationMessage) msg).getError()); - assertNull(((BGPNotificationMessage) msg).getData()); - } - - @Test - public void testBGPOpenMessage() { - final BGPMessage msg = new BGPOpenMessage(new ASNumber(58), (short) 5, null, null); - assertNull(((BGPOpenMessage) msg).getOptParams()); - } - - @Test - public void testToString() { - final BGPMessage o = new BGPOpenMessage(new ASNumber(58), (short) 5, null, null); - final BGPMessage n = new BGPNotificationMessage(BGPError.ATTR_FLAGS_MISSING); - assertNotSame(o.toString(), n.toString()); - } + @Test + public void testDocumentedException() { + final BGPDocumentedException de = new BGPDocumentedException("Some message", BGPError.BAD_BGP_ID); + assertEquals("Some message", de.getMessage()); + assertEquals(BGPError.BAD_BGP_ID, de.getError()); + assertEquals(0, de.getData().length); + + final BGPDocumentedException doc = BGPDocumentedException.badMessageLength("Wrong length", 5000); + assertEquals(5000, ByteArray.bytesToInt(doc.getData())); + } + + @Test + public void testParsingException() { + final BGPParsingException de = new BGPParsingException("Some message"); + assertEquals("Some message", de.getMessage()); + + final BGPParsingException de1 = new BGPParsingException("Some message", new IllegalArgumentException("text")); + assertEquals("text", de1.getCause().getMessage()); + } + + @Test + public void testBGPError() { + assertEquals(BGPError.BAD_MSG_TYPE, BGPError.forValue(Uint8.ONE, Uint8.valueOf(3))); + } + + @Test + public void testAsNumberUtil() { + final List params = new ArrayList<>(); + final List capas = new ArrayList<>(); + capas.add(new OptionalCapabilitiesBuilder() + .setCParameters(new CParametersBuilder() + .addAugmentation(new CParameters1Builder() + .setMultiprotocolCapability(new MultiprotocolCapabilityBuilder().build()) + .build()) + .build()) + .build()); + capas.add(new OptionalCapabilitiesBuilder().setCParameters( + new CParametersBuilder().setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber( + new AsNumber(Uint32.valueOf(35))).build()).build()).build()); + params.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build()); + final Open open1 = new OpenBuilder().setBgpParameters(params).build(); + assertEquals(35L, AsNumberUtil.advertizedAsNumber(open1).getValue().longValue()); + + final Open open2 = new OpenBuilder().setMyAsNumber(Uint16.valueOf(10)).build(); + assertEquals(10, AsNumberUtil.advertizedAsNumber(open2).getValue().intValue()); + } + + @Test + public void testBgpExtendedMessageUtil() { + final List params = new ArrayList<>(); + final List capas = new ArrayList<>(); + capas.add(new OptionalCapabilitiesBuilder() + .setCParameters(new CParametersBuilder() + .addAugmentation(new CParameters1Builder() + .setMultiprotocolCapability(new MultiprotocolCapabilityBuilder().build()) + .build()) + .build()) + .build()); + capas.add(new OptionalCapabilitiesBuilder().setCParameters( + BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build()); + params.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build()); + final Open open1 = new OpenBuilder().setBgpParameters(params).build(); + assertTrue(BgpExtendedMessageUtil.advertizedBgpExtendedMessageCapability(open1)); + } }