BUG-5766: BGP Link Bandwidth Extended Community 78/37978/3
authorClaudio D. Gasparini <cgaspari@cisco.com>
Wed, 20 Apr 2016 13:19:55 +0000 (15:19 +0200)
committerRobert Varga <nite@hq.sk>
Sun, 24 Apr 2016 14:48:27 +0000 (14:48 +0000)
Implementation of BGP Link Bandwidth Extended Community
ref.: https://tools.ietf.org/html/draft-ietf-idr-link-bandwidth-06#section-2

Change-Id: I5212a71f304035bff5d8f8d23e5b9701d6105261
Signed-off-by: Claudio D. Gasparini <cgaspari@cisco.com>
bgp/concepts/pom.xml
bgp/concepts/src/main/yang/bgp-types.yang
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/BGPActivator.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPOpenMessageParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/LinkBandwidthEC.java [new file with mode: 0644]
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/LinkBandwidthECTest.java [new file with mode: 0644]

index ea5565f25a9ffb198885f977d718fdf3a9f5575a..e78fa76a8ba1718fe1dcb249798dba1a6c54479b 100644 (file)
             <groupId>org.opendaylight.yangtools</groupId>
             <artifactId>concepts</artifactId>
         </dependency>
-
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>concepts</artifactId>
+        </dependency>
         <!--
              FIXME: these are IETF models which are pre-generated in SAL infra.
                     This should not be here, but rather should somehow be
index 0b1f2a4dcdbb87f4b9620aeb81c10abf36ae5039..97c0291d0cb102479186af81c0913c3e0b8c957d 100644 (file)
@@ -4,6 +4,7 @@ module bgp-types {
     prefix "bgp-t";
 
     import ietf-inet-types { prefix inet; revision-date 2010-09-24; }
+    import network-concepts { prefix netc; revision-date 2013-11-25; }
 
     organization "Cisco Systems, Inc.";
     contact "Dana Kutenicsova <dkutenic@cisco.com>";
@@ -150,6 +151,19 @@ module bgp-types {
         }
     }
 
+    grouping link-bandwidth-extended-community {
+        reference "https://tools.ietf.org/html/draft-ietf-idr-link-bandwidth-06#section-2";
+        description
+            "The Link Bandwidth extended community is optional non-transitive";
+
+        leaf bandwidth {
+            description "Bandwidth of the link";
+            mandatory true;
+            type netc:bandwidth;
+            units "bytes per second";
+        }
+    }
+
     grouping extended-community {
         leaf comm-type {
             status deprecated;
@@ -244,6 +258,11 @@ module bgp-types {
                     uses route-ipv4-extended-community;
                 }
             }
+            case link-bandwidth-case {
+                container link-bandwidth-extended-community {
+                    uses link-bandwidth-extended-community;
+                }
+            }
         }
     }
 
index 6a8102ba4becdf1e6590dafac2b8fdb71dd26030..e8ee5cec69e3c5799bf845e1b01ad2d160bd5bfa 100644 (file)
@@ -43,6 +43,7 @@ import org.opendaylight.protocol.bgp.parser.impl.message.update.OriginatorIdAttr
 import org.opendaylight.protocol.bgp.parser.impl.message.update.WithdrawnRoutesSerializer;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities.AsTwoOctetSpecificEcHandler;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities.Ipv4SpecificEcHandler;
+import org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities.LinkBandwidthEC;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities.OpaqueEcHandler;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities.RouteOriginAsTwoOctetEcHandler;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities.RouteOriginIpv4EcHandler;
@@ -89,6 +90,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.type
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.LinkBandwidthCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginIpv4Case;
@@ -302,5 +304,10 @@ public final class BGPActivator extends AbstractBGPExtensionProviderActivator {
         regs.add(context.registerExtendedCommunityParser(routeTargetIpv4EcHandler.getType(false), routeTargetIpv4EcHandler.getSubType(),
             routeTargetIpv4EcHandler));
         regs.add(context.registerExtendedCommunitySerializer(RouteTargetIpv4Case.class, routeTargetIpv4EcHandler));
+
+        final LinkBandwidthEC linkBandwidthECHandler = new LinkBandwidthEC();
+        regs.add(context.registerExtendedCommunityParser(linkBandwidthECHandler.getType(false), linkBandwidthECHandler.getSubType(),
+            linkBandwidthECHandler));
+        regs.add(context.registerExtendedCommunitySerializer(LinkBandwidthCase.class, linkBandwidthECHandler));
     }
 }
index 07ca965866ef3c1061628ed55c16de458a2221a6..e441eac4f5b18766cdb195f3a96a4a2363c9943c 100644 (file)
@@ -50,7 +50,7 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali
 
     private static final int BGP_VERSION = 4;
 
-    private static final int AS_TRANS = 2345;
+    public static final int AS_TRANS = 23456;
 
     private final ParameterRegistry reg;
 
diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/LinkBandwidthEC.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/LinkBandwidthEC.java
new file mode 100644 (file)
index 0000000..b08fe83
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities;
+
+import static org.opendaylight.protocol.bgp.parser.impl.message.BGPOpenMessageParser.AS_TRANS;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
+import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.LinkBandwidthCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.LinkBandwidthCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.link.bandwidth._case.LinkBandwidthExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.link.bandwidth._case.LinkBandwidthExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
+
+public class LinkBandwidthEC implements ExtendedCommunityParser, ExtendedCommunitySerializer {
+    private static final int TYPE = 64;
+    private static final int SUBTYPE = 4;
+    private static final int BANDWIDTH_SIZE = 4;
+    private static final int AS_TRANS_LENGTH = 2;
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        buffer.skipBytes(AS_TRANS_LENGTH);
+        final LinkBandwidthExtendedCommunity lb = new LinkBandwidthExtendedCommunityBuilder()
+            .setBandwidth(new Bandwidth(ByteArray.readBytes(buffer, BANDWIDTH_SIZE)))
+            .build();
+        return new LinkBandwidthCaseBuilder().setLinkBandwidthExtendedCommunity(lb).build();
+    }
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf body) {
+        Preconditions.checkArgument(extendedCommunity instanceof LinkBandwidthCase,
+            "The extended community %s is not LinkBandwidthCase type.", extendedCommunity);
+        final LinkBandwidthExtendedCommunity lb = ((LinkBandwidthCase) extendedCommunity).getLinkBandwidthExtendedCommunity();
+        body.writeShort(AS_TRANS);
+        ByteBufWriteUtil.writeFloat32(lb.getBandwidth(), body);
+    }
+
+    @Override
+    public int getType(final boolean isTransitive) {
+        return TYPE;
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+}
diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/LinkBandwidthECTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/LinkBandwidthECTest.java
new file mode 100644 (file)
index 0000000..905c57d
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.LinkBandwidthCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.LinkBandwidthCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.link.bandwidth._case.LinkBandwidthExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
+
+public class LinkBandwidthECTest {
+    private static final byte[] RESULT = {(byte) 0x5b, (byte) 0xa0, 0x00, 0x00, (byte) 0xff, (byte) 0xff};
+    private static final int COMMUNITY_VALUE_SIZE = 6;
+    private LinkBandwidthEC parser;
+
+    @Before
+    public void setUp() {
+        this.parser = new LinkBandwidthEC();
+    }
+
+    @Test
+    public void parserTest() throws BGPParsingException, BGPDocumentedException {
+        final ByteBuf buff = Unpooled.buffer(COMMUNITY_VALUE_SIZE);
+
+        final LinkBandwidthCase expected = new LinkBandwidthCaseBuilder().setLinkBandwidthExtendedCommunity(new LinkBandwidthExtendedCommunityBuilder()
+            .setBandwidth(new Bandwidth(new byte[]{0x00, 0x00, (byte) 0xff, (byte) 0xff}))
+            .build()).build();
+        this.parser.serializeExtendedCommunity(expected, buff);
+        assertArrayEquals(RESULT, ByteArray.getAllBytes(buff));
+
+        final ExtendedCommunity result = this.parser.parseExtendedCommunity(Unpooled.wrappedBuffer(RESULT));
+        assertEquals(expected, result);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void wrongCaseTest() {
+        this.parser.serializeExtendedCommunity(new Inet4SpecificExtendedCommunityCaseBuilder().build(), null);
+    }
+
+    @Test
+    public void testSubtype() {
+        assertEquals(4, this.parser.getSubType());
+    }
+}
\ No newline at end of file