Search in sources :

Example 21 with Ip4Prefix

use of org.onlab.packet.Ip4Prefix in project onos by opennetworkinglab.

the class TestBgpPeerChannelHandler method encodePackedPrefixes.

/**
 * Encodes a collection of IPv4 network prefixes in a packed format.
 * <p>
 * The IPv4 prefixes are encoded in the form:
 * <Length, Prefix> where Length is the length in bits of the IPv4 prefix,
 * and Prefix is the IPv4 prefix (padded with trailing bits to the end
 * of an octet).
 *
 * @param prefixes the prefixes to encode
 * @return the buffer with the encoded prefixes
 */
private ChannelBuffer encodePackedPrefixes(Collection<Ip4Prefix> prefixes) {
    ChannelBuffer message = ChannelBuffers.buffer(BgpConstants.BGP_MESSAGE_MAX_LENGTH);
    // Write each of the prefixes
    for (Ip4Prefix prefix : prefixes) {
        int prefixBitlen = prefix.prefixLength();
        // Round-up
        int prefixBytelen = (prefixBitlen + 7) / 8;
        message.writeByte(prefixBitlen);
        Ip4Address address = prefix.address();
        long value = address.toInt() & 0xffffffffL;
        for (int i = 0; i < Ip4Address.BYTE_LENGTH; i++) {
            if (prefixBytelen-- == 0) {
                break;
            }
            long nextByte = (value >> ((Ip4Address.BYTE_LENGTH - i - 1) * 8)) & 0xff;
            message.writeByte((int) nextByte);
        }
    }
    return message;
}
Also used : Ip4Address(org.onlab.packet.Ip4Address) Ip4Prefix(org.onlab.packet.Ip4Prefix) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 22 with Ip4Prefix

use of org.onlab.packet.Ip4Prefix in project onos by opennetworkinglab.

the class BgpSessionManagerTest method testProcessedBgpUpdateMessages.

/**
 * Tests that the BGP UPDATE messages have been received and processed.
 */
@Test
public void testProcessedBgpUpdateMessages() throws InterruptedException {
    ChannelBuffer message;
    BgpRouteEntry bgpRouteEntry;
    Collection<BgpRouteEntry> bgpRibIn1;
    Collection<BgpRouteEntry> bgpRibIn2;
    Collection<BgpRouteEntry> bgpRibIn3;
    Collection<BgpRouteEntry> bgpRoutes;
    // Initiate the connections
    peer1.connect(connectToSocket);
    peer2.connect(connectToSocket);
    peer3.connect(connectToSocket);
    // Prepare routes to add/delete
    Collection<Ip4Prefix> addedRoutes = new LinkedList<>();
    Collection<Ip4Prefix> withdrawnRoutes = new LinkedList<>();
    // 
    // Add and delete some routes
    // 
    addedRoutes.add(Ip4Prefix.valueOf("0.0.0.0/0"));
    addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8"));
    addedRoutes.add(Ip4Prefix.valueOf("30.0.0.0/16"));
    addedRoutes.add(Ip4Prefix.valueOf("40.0.0.0/24"));
    addedRoutes.add(Ip4Prefix.valueOf("50.0.0.0/32"));
    withdrawnRoutes.add(Ip4Prefix.valueOf("60.0.0.0/8"));
    withdrawnRoutes.add(Ip4Prefix.valueOf("70.0.0.0/16"));
    withdrawnRoutes.add(Ip4Prefix.valueOf("80.0.0.0/24"));
    withdrawnRoutes.add(Ip4Prefix.valueOf("90.0.0.0/32"));
    // Write the routes
    message = peer1.peerChannelHandler.prepareBgpUpdate(NEXT_HOP1_ROUTER, DEFAULT_LOCAL_PREF, DEFAULT_MULTI_EXIT_DISC, asPathLong, addedRoutes, withdrawnRoutes);
    peer1.peerChannelHandler.savedCtx.getChannel().write(message);
    // 
    // Check that the routes have been received, processed and stored
    // 
    bgpRibIn1 = waitForBgpRibIn(bgpSession1, 5);
    assertThat(bgpRibIn1, hasSize(5));
    bgpRoutes = waitForBgpRoutes(5);
    assertThat(bgpRoutes, hasSize(5));
    // 
    bgpRouteEntry = new BgpRouteEntry(bgpSession1, Ip4Prefix.valueOf("0.0.0.0/0"), NEXT_HOP1_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathLong, DEFAULT_LOCAL_PREF);
    bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC);
    assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry));
    assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
    // 
    bgpRouteEntry = new BgpRouteEntry(bgpSession1, Ip4Prefix.valueOf("20.0.0.0/8"), NEXT_HOP1_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathLong, DEFAULT_LOCAL_PREF);
    bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC);
    assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry));
    assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
    // 
    bgpRouteEntry = new BgpRouteEntry(bgpSession1, Ip4Prefix.valueOf("30.0.0.0/16"), NEXT_HOP1_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathLong, DEFAULT_LOCAL_PREF);
    bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC);
    assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry));
    assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
    // 
    bgpRouteEntry = new BgpRouteEntry(bgpSession1, Ip4Prefix.valueOf("40.0.0.0/24"), NEXT_HOP1_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathLong, DEFAULT_LOCAL_PREF);
    bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC);
    assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry));
    assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
    // 
    bgpRouteEntry = new BgpRouteEntry(bgpSession1, Ip4Prefix.valueOf("50.0.0.0/32"), NEXT_HOP1_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathLong, DEFAULT_LOCAL_PREF);
    bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC);
    assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry));
    assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
    // 
    // Delete some routes
    // 
    addedRoutes = new LinkedList<>();
    withdrawnRoutes = new LinkedList<>();
    withdrawnRoutes.add(Ip4Prefix.valueOf("0.0.0.0/0"));
    withdrawnRoutes.add(Ip4Prefix.valueOf("50.0.0.0/32"));
    // Write the routes
    message = peer1.peerChannelHandler.prepareBgpUpdate(NEXT_HOP1_ROUTER, DEFAULT_LOCAL_PREF, DEFAULT_MULTI_EXIT_DISC, asPathLong, addedRoutes, withdrawnRoutes);
    peer1.peerChannelHandler.savedCtx.getChannel().write(message);
    // 
    // Check that the routes have been received, processed and stored
    // 
    bgpRibIn1 = waitForBgpRibIn(bgpSession1, 3);
    assertThat(bgpRibIn1, hasSize(3));
    bgpRoutes = waitForBgpRoutes(3);
    assertThat(bgpRoutes, hasSize(3));
    // 
    bgpRouteEntry = new BgpRouteEntry(bgpSession1, Ip4Prefix.valueOf("20.0.0.0/8"), NEXT_HOP1_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathLong, DEFAULT_LOCAL_PREF);
    bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC);
    assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry));
    assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
    // 
    bgpRouteEntry = new BgpRouteEntry(bgpSession1, Ip4Prefix.valueOf("30.0.0.0/16"), NEXT_HOP1_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathLong, DEFAULT_LOCAL_PREF);
    bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC);
    assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry));
    assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
    // 
    bgpRouteEntry = new BgpRouteEntry(bgpSession1, Ip4Prefix.valueOf("40.0.0.0/24"), NEXT_HOP1_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathLong, DEFAULT_LOCAL_PREF);
    bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC);
    assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry));
    assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
    // Close the channels and test there are no routes
    peer1.peerChannelHandler.closeChannel();
    peer2.peerChannelHandler.closeChannel();
    peer3.peerChannelHandler.closeChannel();
    bgpRoutes = waitForBgpRoutes(0);
    assertThat(bgpRoutes, hasSize(0));
}
Also used : Ip4Prefix(org.onlab.packet.Ip4Prefix) LinkedList(java.util.LinkedList) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Test(org.junit.Test)

Example 23 with Ip4Prefix

use of org.onlab.packet.Ip4Prefix in project onos by opennetworkinglab.

the class BgpRouteEntryTest method testInvalidConstructorNullAsPath.

/**
 * Tests invalid class constructor for null AS Path.
 */
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullAsPath() {
    Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
    Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
    byte origin = BgpConstants.Update.Origin.IGP;
    BgpRouteEntry.AsPath asPath = null;
    long localPref = 100;
    new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath, localPref);
}
Also used : Ip4Address(org.onlab.packet.Ip4Address) Ip4Prefix(org.onlab.packet.Ip4Prefix) Test(org.junit.Test)

Example 24 with Ip4Prefix

use of org.onlab.packet.Ip4Prefix in project onos by opennetworkinglab.

the class BgpRouteEntryTest method testIsLocalRoute.

/**
 * Tests whether a BGP route entry is a local route.
 */
@Test
public void testIsLocalRoute() {
    // 
    // Test non-local route
    // 
    BgpRouteEntry bgpRouteEntry = generateBgpRouteEntry();
    assertThat(bgpRouteEntry.isLocalRoute(), is(false));
    // 
    // Test local route with AS Path that begins with AS_SET
    // 
    Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
    Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
    byte origin = BgpConstants.Update.Origin.IGP;
    // Setup the AS Path
    ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
    byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SET;
    ArrayList<Long> segmentAsNumbers1 = new ArrayList<>();
    segmentAsNumbers1.add(1L);
    segmentAsNumbers1.add(2L);
    segmentAsNumbers1.add(3L);
    BgpRouteEntry.PathSegment pathSegment1 = new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1);
    pathSegments.add(pathSegment1);
    // 
    byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE;
    ArrayList<Long> segmentAsNumbers2 = new ArrayList<>();
    segmentAsNumbers2.add(4L);
    segmentAsNumbers2.add(5L);
    segmentAsNumbers2.add(6L);
    BgpRouteEntry.PathSegment pathSegment2 = new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2);
    pathSegments.add(pathSegment2);
    // 
    BgpRouteEntry.AsPath asPath = new BgpRouteEntry.AsPath(pathSegments);
    // 
    long localPref = 100;
    long multiExitDisc = 20;
    // 
    bgpRouteEntry = new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath, localPref);
    bgpRouteEntry.setMultiExitDisc(multiExitDisc);
    assertThat(bgpRouteEntry.isLocalRoute(), is(true));
    // 
    // Test local route with empty AS Path
    // 
    pathSegments = new ArrayList<>();
    asPath = new BgpRouteEntry.AsPath(pathSegments);
    bgpRouteEntry = new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath, localPref);
    bgpRouteEntry.setMultiExitDisc(multiExitDisc);
    assertThat(bgpRouteEntry.isLocalRoute(), is(true));
}
Also used : Ip4Address(org.onlab.packet.Ip4Address) ArrayList(java.util.ArrayList) Ip4Prefix(org.onlab.packet.Ip4Prefix) Test(org.junit.Test)

Example 25 with Ip4Prefix

use of org.onlab.packet.Ip4Prefix in project onos by opennetworkinglab.

the class RouteEntryTest method testToString.

/**
 * Tests object string representation.
 */
@Test
public void testToString() {
    Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
    Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
    RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
    assertThat(routeEntry.toString(), is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}"));
    Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64");
    Ip6Address nextHop6 = Ip6Address.valueOf("2000::1");
    RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6);
    assertThat(routeEntry6.toString(), is("RouteEntry{prefix=1000::/64, nextHop=2000::1}"));
}
Also used : Ip6Prefix(org.onlab.packet.Ip6Prefix) Ip6Address(org.onlab.packet.Ip6Address) Ip4Address(org.onlab.packet.Ip4Address) Ip4Prefix(org.onlab.packet.Ip4Prefix) Test(org.junit.Test)

Aggregations

Ip4Prefix (org.onlab.packet.Ip4Prefix)32 Ip4Address (org.onlab.packet.Ip4Address)20 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)11 Ip6Prefix (org.onlab.packet.Ip6Prefix)10 Ip6Address (org.onlab.packet.Ip6Address)6 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)4 LinkedList (java.util.LinkedList)2 Up4AdminService (org.omecproject.up4.impl.Up4AdminService)2 OduSignalId (org.onosproject.net.OduSignalId)2 UpfInterface (org.onosproject.net.behaviour.upf.UpfInterface)2 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)2 TrafficSelector (org.onosproject.net.flow.TrafficSelector)2 OFOxm (org.projectfloodlight.openflow.protocol.oxm.OFOxm)2 CircuitSignalID (org.projectfloodlight.openflow.types.CircuitSignalID)2 IPv4Address (org.projectfloodlight.openflow.types.IPv4Address)2 IPv6Address (org.projectfloodlight.openflow.types.IPv6Address)2 OFVlanVidMatch (org.projectfloodlight.openflow.types.OFVlanVidMatch)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1