BUG-4568: Introduce BGP Extended Community registry 26/29526/4
authorMilos Fabian <milfabia@cisco.com>
Thu, 5 Nov 2015 09:16:58 +0000 (10:16 +0100)
committerMilos Fabian <milfabia@cisco.com>
Wed, 11 Nov 2015 22:01:39 +0000 (23:01 +0100)
-new Extended Community registry
-parsers registration is based on type/subtype
-serializers registration is based on "extened community case"
-added new "transitive" field for extened communities
-other "transitive" fileds are deprecated in yang data model
-deprecated type/sub-type value from yang data model:
-values are not filled in during a parsing
-values are ignored during a serialization
-make the extended commnity attribute parser non-extensible:
-BGP path attribute serializers are registred in
a specific order, an extending of the extended community
handler breaks the contract
-added missing IPv4 Transitive/non-transitive Route Target/Route Origin
extened communities
-updated Flowspec extended communities

Change-Id: I2818b20c069da362f60a716862421bb185bc8571
Signed-off-by: Milos Fabian <milfabia@cisco.com>
50 files changed:
bgp/concepts/src/main/yang/bgp-types.yang
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/BGPActivator.java
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSExtendedCommunitiesAttributeParser.java [deleted file]
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/RedirectAsTwoOctetEcHandler.java [new file with mode: 0644]
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/TrafficActionEcHandler.java [new file with mode: 0644]
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/TrafficMarkingEcHandler.java [new file with mode: 0644]
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/TrafficRateEcHandler.java [new file with mode: 0644]
bgp/flowspec/src/main/yang/bgp-flowspec.yang
bgp/flowspec/src/test/java/org/opendaylight/protocol/bgp/flowspec/FSExtendedCommunitiesAttributeParserTest.java [deleted file]
bgp/flowspec/src/test/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/FSExtendedCommunitiesTest.java [new file with mode: 0644]
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/update/ExtendedCommunitiesAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/AsTwoOctetSpecificEcHandler.java [new file with mode: 0644]
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/Ipv4SpecificEcHandler.java [new file with mode: 0644]
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/OpaqueEcHandler.java [new file with mode: 0644]
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginAsTwoOctetEcHandler.java [new file with mode: 0644]
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginIpv4EcHandler.java [new file with mode: 0644]
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetAsTwoOctetEcHandler.java [new file with mode: 0644]
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetIpv4EcHandler.java [new file with mode: 0644]
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/BGPParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/RouteOriginCommunityTest.java [deleted file]
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/RouteTargetCommunityTest.java [deleted file]
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/ExtendedCommunitiesAttributeParserTest.java [new file with mode: 0644]
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/AsTwoOctetSpecificEcHandlerTest.java [new file with mode: 0644]
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/Ipv4SpecificEcHandlerTest.java [new file with mode: 0644]
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/OpaqueEcHandlerTest.java [new file with mode: 0644]
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginAsTwoOctetEcHandlerTest.java [new file with mode: 0644]
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginIpv4EcHandlerTest.java [new file with mode: 0644]
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetAsTwoOctetEcHandlerTest.java [new file with mode: 0644]
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetIpv4EcHandlerTest.java [new file with mode: 0644]
bgp/parser-impl/src/test/resources/up4.bin
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/BGPExtensionConsumerContext.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/BGPExtensionProviderContext.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractIpv4ExtendedCommunity.java [new file with mode: 0644]
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractOpaqueExtendedCommunity.java [new file with mode: 0644]
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractTwoOctetAsExtendedCommunity.java [new file with mode: 0644]
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityParser.java [new file with mode: 0644]
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityRegistry.java [new file with mode: 0644]
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunitySerializer.java [new file with mode: 0644]
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityUtil.java [new file with mode: 0644]
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleBGPExtensionConsumerContext.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleBGPExtensionProviderContext.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleExtendedCommunityRegistry.java [new file with mode: 0644]
bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractIpv4ExtendedCommunityTest.java [new file with mode: 0644]
bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractOpaqueExtendedCommunityTest.java [new file with mode: 0644]
bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractTwoOctetAsExtendedCommunityTest.java [new file with mode: 0644]
bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityUtilTest.java [new file with mode: 0644]
bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/pojo/BgpTestActivator.java
bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleExtendedCommunityRegistryTest.java [new file with mode: 0644]

index 1e5c2ad10cf039bd509e7c235e89f9abd0f7d32f..41be05c363fdbb7b4adcee1fb3c498c767cbb6f9 100644 (file)
@@ -102,18 +102,33 @@ module bgp-types {
         }
     }
 
+    grouping route-ipv4-extended-community {
+        reference "http://tools.ietf.org/html/rfc4360";
+        leaf global-administrator {
+            type inet:ipv4-address;
+        }
+        leaf local-administrator {
+            type uint16;
+        }
+    }
+
     grouping extended-community {
         leaf comm-type {
+            status deprecated;
             type uint8;
         }
         leaf comm-sub-type {
+            status deprecated;
             type uint8;
         }
+        leaf transitive {
+            type boolean;
+        }
         choice extended-community {
             case as-specific-extended-community-case {
                 container as-specific-extended-community {
-                    when "../comm-type = 0 and ../comm-sub-type = 0 or ../comm-type = 40 and ../comm-sub-type = 0";
                     leaf transitive {
+                        status deprecated;
                         type boolean;
                     }
                     leaf global-administrator {
@@ -128,8 +143,8 @@ module bgp-types {
             }
             case inet4-specific-extended-community-case {
                 container inet4-specific-extended-community {
-                    when "../comm-type = 1 and ../comm-sub-type = 0 or ../comm-type = 41 and ../comm-sub-type = 0";
                     leaf transitive {
+                        status deprecated;
                         type boolean;
                     }
                     leaf global-administrator {
@@ -144,8 +159,8 @@ module bgp-types {
             }
             case opaque-extended-community-case {
                 container opaque-extended-community {
-                    when "../comm-type = 3 and ../comm-sub-type = 0 or ../comm-type = 43 and ../comm-sub-type = 0";
                     leaf transitive {
+                        status deprecated;
                         type boolean;
                     }
                     leaf value {
@@ -157,7 +172,6 @@ module bgp-types {
             }
             case route-target-extended-community-case {
                 container route-target-extended-community {
-                    when "../comm-type = 1 and ../comm-sub-type = 2 or ../comm-type = 2 and ../comm-sub-type = 2 or ../comm-type = 3 and ../comm-sub-type = 2";
                     leaf global-administrator {
                         type short-as-number;
                     }
@@ -170,7 +184,6 @@ module bgp-types {
             }
             case route-origin-extended-community-case {
                 container route-origin-extended-community {
-                    when "../comm-type = 1 and ../comm-sub-type = 3 or ../comm-type = 2 and ../comm-sub-type = 3 or ../comm-type = 3 and ../comm-sub-type = 3";
                     leaf global-administrator {
                         type short-as-number;
                     }
@@ -181,6 +194,18 @@ module bgp-types {
                     }
                 }
             }
+            case route-target-ipv4-case {
+                container route-target-ipv4 {
+                    reference "http://tools.ietf.org/html/rfc4360#section-4";
+                    uses route-ipv4-extended-community;
+                }
+            }
+            case route-origin-ipv4-case {
+                container route-origin-ipv4 {
+                    reference "http://tools.ietf.org/html/rfc4360#section-5";
+                    uses route-ipv4-extended-community;
+                }
+            }
         }
     }
 
index 0ac8111cc7048a2a6e83642453d7d299c96ce8e9..8e52f26bda134aea422ee9d575776f4a271d9db2 100644 (file)
@@ -9,7 +9,10 @@ package org.opendaylight.protocol.bgp.flowspec;
 
 import java.util.ArrayList;
 import java.util.List;
-import org.opendaylight.protocol.bgp.parser.impl.message.update.ExtendedCommunitiesAttributeParser;
+import org.opendaylight.protocol.bgp.flowspec.extended.communities.RedirectAsTwoOctetEcHandler;
+import org.opendaylight.protocol.bgp.flowspec.extended.communities.TrafficActionEcHandler;
+import org.opendaylight.protocol.bgp.flowspec.extended.communities.TrafficMarkingEcHandler;
+import org.opendaylight.protocol.bgp.flowspec.extended.communities.TrafficRateEcHandler;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.next.hop.Ipv4NextHopParserSerializer;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.next.hop.Ipv6NextHopParserSerializer;
 import org.opendaylight.protocol.bgp.parser.spi.AbstractBGPExtensionProviderActivator;
@@ -17,7 +20,10 @@ import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.FlowspecSubsequentAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.FlowspecIpv6Routes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.routes.FlowspecRoutes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.routes.flowspec.routes.flowspec.route.attributes.extended.communities.extended.community.RedirectExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.routes.flowspec.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.routes.flowspec.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.routes.flowspec.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
@@ -35,8 +41,8 @@ public final class BGPActivator extends AbstractBGPExtensionProviderActivator {
 
         final FSIpv4NlriParser ipv4Handler = new FSIpv4NlriParser();
         final FSIpv6NlriParser ipv6Handler = new FSIpv6NlriParser();
-        Ipv4NextHopParserSerializer ipv4NextHopParser = new Ipv4NextHopParserSerializer();
-        Ipv6NextHopParserSerializer ipv6NextHopParser = new Ipv6NextHopParserSerializer();
+        final Ipv4NextHopParserSerializer ipv4NextHopParser = new Ipv4NextHopParserSerializer();
+        final Ipv6NextHopParserSerializer ipv6NextHopParser = new Ipv6NextHopParserSerializer();
         regs.add(context.registerNlriParser(Ipv4AddressFamily.class, FlowspecSubsequentAddressFamily.class,
             ipv4Handler, ipv4NextHopParser, Ipv4NextHopCase.class));
         regs.add(context.registerNlriParser(Ipv6AddressFamily.class, FlowspecSubsequentAddressFamily.class,
@@ -44,9 +50,25 @@ public final class BGPActivator extends AbstractBGPExtensionProviderActivator {
         regs.add(context.registerNlriSerializer(FlowspecRoutes.class, ipv4Handler));
         regs.add(context.registerNlriSerializer(FlowspecIpv6Routes.class, ipv6Handler));
 
-        final ExtendedCommunitiesAttributeParser extendedCommunitiesAttributeParser = new FSExtendedCommunitiesAttributeParser(context.getReferenceCache());
-        regs.add(context.registerAttributeSerializer(ExtendedCommunities.class, extendedCommunitiesAttributeParser));
-        regs.add(context.registerAttributeParser(ExtendedCommunitiesAttributeParser.TYPE, extendedCommunitiesAttributeParser));
+        final RedirectAsTwoOctetEcHandler redirect2bHandler = new RedirectAsTwoOctetEcHandler();
+        regs.add(context.registerExtendedCommunityParser(redirect2bHandler.getType(true), redirect2bHandler.getSubType(), redirect2bHandler));
+        regs.add(context.registerExtendedCommunitySerializer(RedirectExtendedCommunityCase.class, redirect2bHandler));
+        regs.add(context.registerExtendedCommunitySerializer(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.flowspec.ipv6.routes.flowspec.route.attributes.extended.communities.extended.community.RedirectExtendedCommunityCase.class, redirect2bHandler));
+
+        final TrafficActionEcHandler trafficActionHandler = new TrafficActionEcHandler();
+        regs.add(context.registerExtendedCommunityParser(trafficActionHandler.getType(true), trafficActionHandler.getSubType(), trafficActionHandler));
+        regs.add(context.registerExtendedCommunitySerializer(TrafficActionExtendedCommunityCase.class, trafficActionHandler));
+        regs.add(context.registerExtendedCommunitySerializer(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.flowspec.ipv6.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCase.class, trafficActionHandler));
+
+        final TrafficMarkingEcHandler trafficMarkingHandler = new TrafficMarkingEcHandler();
+        regs.add(context.registerExtendedCommunityParser(trafficMarkingHandler.getType(true), trafficMarkingHandler.getSubType(), trafficMarkingHandler));
+        regs.add(context.registerExtendedCommunitySerializer(TrafficMarkingExtendedCommunityCase.class, trafficMarkingHandler));
+        regs.add(context.registerExtendedCommunitySerializer(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.flowspec.ipv6.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCase.class, trafficMarkingHandler));
+
+        final TrafficRateEcHandler trafficRateEcHandler = new TrafficRateEcHandler();
+        regs.add(context.registerExtendedCommunityParser(trafficRateEcHandler.getType(true), trafficRateEcHandler.getSubType(), trafficRateEcHandler));
+        regs.add(context.registerExtendedCommunitySerializer(TrafficRateExtendedCommunityCase.class, trafficRateEcHandler));
+        regs.add(context.registerExtendedCommunitySerializer(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.flowspec.ipv6.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCase.class, trafficRateEcHandler));
 
         return regs;
     }
diff --git a/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSExtendedCommunitiesAttributeParser.java b/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSExtendedCommunitiesAttributeParser.java
deleted file mode 100644 (file)
index b217b99..0000000
+++ /dev/null
@@ -1,138 +0,0 @@
-/*
- * Copyright (c) 2015 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.flowspec;
-
-import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
-import org.opendaylight.protocol.bgp.parser.BGPError;
-import org.opendaylight.protocol.bgp.parser.impl.message.update.ExtendedCommunitiesAttributeParser;
-import org.opendaylight.protocol.util.BitArray;
-import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
-import org.opendaylight.protocol.util.ReferenceCache;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.Dscp;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.redirect.extended.community.RedirectExtendedCommunity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.redirect.extended.community.RedirectExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.action.extended.community.TrafficActionExtendedCommunity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.action.extended.community.TrafficActionExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.marking.extended.community.TrafficMarkingExtendedCommunity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.marking.extended.community.TrafficMarkingExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.rate.extended.community.TrafficRateExtendedCommunity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.rate.extended.community.TrafficRateExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.RedirectExtendedCommunityCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.RedirectExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
-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.network.concepts.rev131125.Bandwidth;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class FSExtendedCommunitiesAttributeParser extends ExtendedCommunitiesAttributeParser {
-
-    private static final Logger LOG = LoggerFactory.getLogger(FSExtendedCommunitiesAttributeParser.class);
-
-    private static final short FS_TYPE = 128;
-
-    private static final short TRAFFIC_RATE_SUBTYPE = 6;
-
-    private static final short TRAFFIC_ACTION_SUBTYPE = 7;
-
-    private static final short REDIRECT_SUBTYPE = 8;
-
-    private static final short TRAFFIC_MARKING_SUBTYPE = 9;
-
-    private static final int TRAFFIC_RATE_SIZE = 4;
-
-    private static final int RESERVED = 5;
-
-    private static final int FLAGS_SIZE = 8;
-
-    private static final int SAMPLE_BIT = 6;
-
-    private static final int TERMINAL_BIT = 7;
-
-    public FSExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
-        super(refCache);
-    }
-
-    @Override
-    public ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final ExtendedCommunitiesBuilder communitiesBuilder, final ByteBuf buffer) throws BGPDocumentedException {
-        ExtendedCommunity c = null;
-        if (communitiesBuilder.getCommType().equals(FS_TYPE)) {
-            switch (communitiesBuilder.getCommSubType()) {
-            case TRAFFIC_RATE_SUBTYPE:
-                final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
-                final Bandwidth value = new Bandwidth(ByteArray.readBytes(buffer, TRAFFIC_RATE_SIZE));
-                c = new TrafficRateExtendedCommunityCaseBuilder().setTrafficRateExtendedCommunity(new TrafficRateExtendedCommunityBuilder().setInformativeAs(as).setLocalAdministrator(value).build()).build();
-                break;
-            case TRAFFIC_ACTION_SUBTYPE:
-                buffer.skipBytes(RESERVED);
-                final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
-                final boolean sample = flags.get(SAMPLE_BIT);
-                final boolean terminal = flags.get(TERMINAL_BIT);
-                c = new TrafficActionExtendedCommunityCaseBuilder().setTrafficActionExtendedCommunity(new TrafficActionExtendedCommunityBuilder().setSample(sample).setTerminalAction(terminal).build()).build();
-                break;
-            case REDIRECT_SUBTYPE:
-                final ShortAsNumber as1 = new ShortAsNumber((long) buffer.readUnsignedShort());
-                final byte[] byteValue = ByteArray.readBytes(buffer, TRAFFIC_RATE_SIZE);
-                c = new RedirectExtendedCommunityCaseBuilder().setRedirectExtendedCommunity(new RedirectExtendedCommunityBuilder().setGlobalAdministrator(as1).setLocalAdministrator(byteValue).build()).build();
-                break;
-            case TRAFFIC_MARKING_SUBTYPE:
-                buffer.skipBytes(RESERVED);
-                final Dscp dscp = new Dscp(buffer.readUnsignedByte());
-                c = new TrafficMarkingExtendedCommunityCaseBuilder().setTrafficMarkingExtendedCommunity(new TrafficMarkingExtendedCommunityBuilder().setGlobalAdministrator(dscp).build()).build();
-                break;
-            default:
-                throw new BGPDocumentedException("Could not parse Flowspec Extended Community type: " + communitiesBuilder.getCommSubType(), BGPError.OPT_ATTR_ERROR);
-            }
-        }
-        if (c == null) {
-            LOG.debug("Extended community is not from Flowspec, fallback to original communities.");
-            return super.parseExtendedCommunity(refCache, communitiesBuilder, buffer);
-        }
-        return communitiesBuilder.setExtendedCommunity(c).build();
-    }
-
-    @Override
-    public void serializeExtendedCommunity(final ExtendedCommunities exCommunities, final ByteBuf buffer) {
-        final ExtendedCommunity ex = exCommunities.getExtendedCommunity();
-        if (ex instanceof TrafficRateExtendedCommunityCase) {
-            final TrafficRateExtendedCommunity trafficRate = ((TrafficRateExtendedCommunityCase) ex).getTrafficRateExtendedCommunity();
-            ByteBufWriteUtil.writeShort(trafficRate.getInformativeAs().getValue().shortValue(), buffer);
-            buffer.writeBytes(trafficRate.getLocalAdministrator().getValue());
-        }
-        else if (ex instanceof TrafficActionExtendedCommunityCase) {
-            final TrafficActionExtendedCommunity trafficAction = ((TrafficActionExtendedCommunityCase) ex).getTrafficActionExtendedCommunity();
-            buffer.writeZero(RESERVED);
-            final BitArray flags = new BitArray(FLAGS_SIZE);
-            flags.set(SAMPLE_BIT, trafficAction.isSample());
-            flags.set(TERMINAL_BIT, trafficAction.isTerminalAction());
-            flags.toByteBuf(buffer);
-        }
-        else if (ex instanceof RedirectExtendedCommunityCase) {
-            final RedirectExtendedCommunity redirect = ((RedirectExtendedCommunityCase) ex).getRedirectExtendedCommunity();
-            ByteBufWriteUtil.writeUnsignedShort(redirect.getGlobalAdministrator().getValue().intValue(), buffer);
-            buffer.writeBytes(redirect.getLocalAdministrator());
-        }
-        else if (ex instanceof TrafficMarkingExtendedCommunityCase) {
-            final TrafficMarkingExtendedCommunity trafficMarking = ((TrafficMarkingExtendedCommunityCase) ex).getTrafficMarkingExtendedCommunity();
-            buffer.writeZero(RESERVED);
-            ByteBufWriteUtil.writeUnsignedByte(trafficMarking.getGlobalAdministrator().getValue().shortValue(), buffer);
-        } else {
-            super.serializeExtendedCommunity(exCommunities, buffer);
-        }
-    }
-}
diff --git a/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/RedirectAsTwoOctetEcHandler.java b/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/RedirectAsTwoOctetEcHandler.java
new file mode 100644 (file)
index 0000000..5f797ad
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2015 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.flowspec.extended.communities;
+
+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.flowspec.rev150807.redirect.extended.community.RedirectExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.redirect.extended.community.RedirectExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.RedirectExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
+
+public class RedirectAsTwoOctetEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
+
+    private static final int TYPE = 128;
+
+    private static final int SUBTYPE = 8;
+
+    private static final int TRAFFIC_RATE_SIZE = 4;
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.RedirectExtendedCommunity,
+                "The extended community %s is not RedirectExtendedCommunityCase type.", extendedCommunity);
+        final RedirectExtendedCommunity redirect = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.RedirectExtendedCommunity) extendedCommunity).getRedirectExtendedCommunity();
+        ByteBufWriteUtil.writeUnsignedShort(redirect.getGlobalAdministrator().getValue().intValue(), byteAggregator);
+        byteAggregator.writeBytes(redirect.getLocalAdministrator());
+    }
+
+    @Override
+    public int getType(final boolean isTransitive) {
+        //redirect is transitive only
+        return TYPE;
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        final ShortAsNumber as1 = new ShortAsNumber((long) buffer.readUnsignedShort());
+        final byte[] byteValue = ByteArray.readBytes(buffer, TRAFFIC_RATE_SIZE);
+        return new RedirectExtendedCommunityCaseBuilder().setRedirectExtendedCommunity(
+                new RedirectExtendedCommunityBuilder()
+                    .setGlobalAdministrator(as1)
+                    .setLocalAdministrator(byteValue)
+                    .build()).build();
+    }
+}
diff --git a/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/TrafficActionEcHandler.java b/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/TrafficActionEcHandler.java
new file mode 100644 (file)
index 0000000..dd1e1a4
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2015 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.flowspec.extended.communities;
+
+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.BitArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.action.extended.community.TrafficActionExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.action.extended.community.TrafficActionExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
+
+public class TrafficActionEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
+
+    private static final int TYPE = 128;
+
+    private static final int SUBTYPE = 7;
+
+    private static final int RESERVED = 5;
+
+    private static final int FLAGS_SIZE = 8;
+
+    private static final int SAMPLE_BIT = 6;
+
+    private static final int TERMINAL_BIT = 7;
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.TrafficActionExtendedCommunity,
+                "The extended community %s is not TrafficActionExtendedCommunityCase type.", extendedCommunity);
+        final TrafficActionExtendedCommunity trafficAction = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.TrafficActionExtendedCommunity) extendedCommunity).getTrafficActionExtendedCommunity();
+        byteAggregator.writeZero(RESERVED);
+        final BitArray flags = new BitArray(FLAGS_SIZE);
+        flags.set(SAMPLE_BIT, trafficAction.isSample());
+        flags.set(TERMINAL_BIT, trafficAction.isTerminalAction());
+        flags.toByteBuf(byteAggregator);
+    }
+
+    @Override
+    public int getType(final boolean isTransitive) {
+        //traffic-action is transitive only
+        return TYPE;
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        buffer.skipBytes(RESERVED);
+        final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
+        final boolean sample = flags.get(SAMPLE_BIT);
+        final boolean terminal = flags.get(TERMINAL_BIT);
+        return new TrafficActionExtendedCommunityCaseBuilder().setTrafficActionExtendedCommunity(
+                new TrafficActionExtendedCommunityBuilder()
+                    .setSample(sample)
+                    .setTerminalAction(terminal)
+                    .build()).build();
+    }
+
+}
diff --git a/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/TrafficMarkingEcHandler.java b/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/TrafficMarkingEcHandler.java
new file mode 100644 (file)
index 0000000..52f8319
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2015 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.flowspec.extended.communities;
+
+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.ByteBufWriteUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.Dscp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.marking.extended.community.TrafficMarkingExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.marking.extended.community.TrafficMarkingExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
+
+public class TrafficMarkingEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
+
+    private static final int TYPE = 128;
+
+    private static final int SUBTYPE = 9;
+
+    private static final int RESERVED = 5;
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.TrafficMarkingExtendedCommunity,
+                "The extended community %s is not TrafficMarkingExtendedCommunity type.", extendedCommunity);
+        final TrafficMarkingExtendedCommunity trafficMarking = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.TrafficMarkingExtendedCommunity) extendedCommunity).getTrafficMarkingExtendedCommunity();
+        byteAggregator.writeZero(RESERVED);
+        ByteBufWriteUtil.writeUnsignedByte(trafficMarking.getGlobalAdministrator().getValue().shortValue(), byteAggregator);
+    }
+
+    @Override
+    public int getType(final boolean isTransitive) {
+        //traffic-rate is transitive only
+        return TYPE;
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        buffer.skipBytes(RESERVED);
+        final Dscp dscp = new Dscp(buffer.readUnsignedByte());
+        return new TrafficMarkingExtendedCommunityCaseBuilder().setTrafficMarkingExtendedCommunity(
+                new TrafficMarkingExtendedCommunityBuilder()
+                    .setGlobalAdministrator(dscp).build()).build();
+    }
+
+}
diff --git a/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/TrafficRateEcHandler.java b/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/TrafficRateEcHandler.java
new file mode 100644 (file)
index 0000000..ecc66f9
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2015 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.flowspec.extended.communities;
+
+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.flowspec.rev150807.traffic.rate.extended.community.TrafficRateExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.rate.extended.community.TrafficRateExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
+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.network.concepts.rev131125.Bandwidth;
+
+public class TrafficRateEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
+
+    private static final int TYPE = 128;
+
+    private static final int SUBTYPE = 6;
+
+    private static final int TRAFFIC_RATE_SIZE = 4;
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.TrafficRateExtendedCommunity,
+                "The extended community %s is not TrafficRateExtendedCommunity type.", extendedCommunity);
+        final TrafficRateExtendedCommunity trafficRate = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.TrafficRateExtendedCommunity) extendedCommunity).getTrafficRateExtendedCommunity();
+        ByteBufWriteUtil.writeShort(trafficRate.getInformativeAs().getValue().shortValue(), byteAggregator);
+        byteAggregator.writeBytes(trafficRate.getLocalAdministrator().getValue());
+    }
+
+    @Override
+    public int getType(final boolean isTransitive) {
+        //traffic-rate is transitive only
+        return TYPE;
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
+        final Bandwidth value = new Bandwidth(ByteArray.readBytes(buffer, TRAFFIC_RATE_SIZE));
+        return new TrafficRateExtendedCommunityCaseBuilder().setTrafficRateExtendedCommunity(
+                new TrafficRateExtendedCommunityBuilder()
+                    .setInformativeAs(as)
+                    .setLocalAdministrator(value)
+                    .build()).build();
+    }
+
+}
index 5bfec87285cbf43b8b486dd6f2088a7ff7a1551c..590f055bf5eec4c4c4b484620a39baafb4b87da4 100644 (file)
@@ -299,7 +299,6 @@ module bgp-flowspec {
 
     grouping traffic-rate-extended-community {
         container traffic-rate-extended-community {
-            when "../comm-type = 128 and ../comm-sub-type = 6";
             leaf informative-as {
                 type bgp-t:short-as-number;
             }
@@ -311,7 +310,6 @@ module bgp-flowspec {
 
     grouping traffic-action-extended-community {
         container traffic-action-extended-community {
-            when "../comm-type = 128 and ../comm-sub-type = 7";
             leaf sample {
                 type boolean;
                 default false;
@@ -325,7 +323,6 @@ module bgp-flowspec {
 
     grouping redirect-extended-community {
         container redirect-extended-community {
-            when "../comm-type = 128 and ../comm-sub-type = 8";
             leaf global-administrator {
                 type bgp-t:short-as-number;
             }
@@ -339,7 +336,6 @@ module bgp-flowspec {
 
     grouping traffic-marking-extended-community {
         container traffic-marking-extended-community {
-            when "../comm-type = 128 and ../comm-sub-type = 9";
             leaf global-administrator {
                 type dscp;
             }
diff --git a/bgp/flowspec/src/test/java/org/opendaylight/protocol/bgp/flowspec/FSExtendedCommunitiesAttributeParserTest.java b/bgp/flowspec/src/test/java/org/opendaylight/protocol/bgp/flowspec/FSExtendedCommunitiesAttributeParserTest.java
deleted file mode 100644 (file)
index 4b8339e..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright (c) 2015 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.flowspec;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
-import org.junit.Test;
-import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
-import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.util.NoopReferenceCache;
-import org.opendaylight.protocol.util.ReferenceCache;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.Dscp;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.redirect.extended.community.RedirectExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.action.extended.community.TrafficActionExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.marking.extended.community.TrafficMarkingExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.rate.extended.community.TrafficRateExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.RedirectExtendedCommunityCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.RedirectExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
-
-public class FSExtendedCommunitiesAttributeParserTest {
-
-    private final ReferenceCache ref = NoopReferenceCache.getInstance();
-
-    private static final byte[] communitiesBytes = { (byte)128, 6, 0, 72, 0, 1, 2, 3,
-        (byte)128, 7, 0, 0, 0, 0, 0, 3,
-        (byte)128, 8, 0, 35, 4, 2, 8, 7,
-        (byte)128, 9, 0, 0, 0, 0, 0, 63 };
-
-    @Test
-    public void testExtendedCommunitiesParser() {
-        final FSExtendedCommunitiesAttributeParser parser = new FSExtendedCommunitiesAttributeParser(this.ref);
-        final AttributesBuilder pa = new AttributesBuilder();
-        try {
-            parser.parseAttribute(Unpooled.copiedBuffer(communitiesBytes), pa);
-        } catch (final BGPDocumentedException e1) {
-            fail("Not expected exception: " + e1);
-        }
-
-        final TrafficRateExtendedCommunityCase expected = new TrafficRateExtendedCommunityCaseBuilder().setTrafficRateExtendedCommunity(
-                new TrafficRateExtendedCommunityBuilder().setInformativeAs(new ShortAsNumber(72L))
-                    .setLocalAdministrator(new Bandwidth(new byte[] { 0, 1, 2, 3 })).build()).build();
-        ExtendedCommunities ex = pa.getExtendedCommunities().get(0);
-        final TrafficRateExtendedCommunityCase result = (TrafficRateExtendedCommunityCase) ex.getExtendedCommunity();
-        assertEquals(expected.getTrafficRateExtendedCommunity().getInformativeAs(), result.getTrafficRateExtendedCommunity().getInformativeAs());
-        assertArrayEquals(expected.getTrafficRateExtendedCommunity().getLocalAdministrator().getValue(),
-                result.getTrafficRateExtendedCommunity().getLocalAdministrator().getValue());
-
-        final TrafficActionExtendedCommunityCase expected1 = new TrafficActionExtendedCommunityCaseBuilder().setTrafficActionExtendedCommunity(
-            new TrafficActionExtendedCommunityBuilder().setSample(true).setTerminalAction(true).build()).build();
-        ex = pa.getExtendedCommunities().get(1);
-        final TrafficActionExtendedCommunityCase result1 = (TrafficActionExtendedCommunityCase) ex.getExtendedCommunity();
-        assertEquals(expected1.getTrafficActionExtendedCommunity().isSample(), result1.getTrafficActionExtendedCommunity().isSample());
-        assertEquals(expected1.getTrafficActionExtendedCommunity().isTerminalAction(), result1.getTrafficActionExtendedCommunity().isTerminalAction());
-
-        final RedirectExtendedCommunityCase expected2 = new RedirectExtendedCommunityCaseBuilder().setRedirectExtendedCommunity(
-                new RedirectExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(35L)).setLocalAdministrator(
-                        new byte[] { 4, 2, 8, 7 }).build()).build();
-        ex = pa.getExtendedCommunities().get(2);
-        final RedirectExtendedCommunityCase result2 = (RedirectExtendedCommunityCase) ex.getExtendedCommunity();
-        assertEquals(expected2.getRedirectExtendedCommunity().getGlobalAdministrator(),
-                result2.getRedirectExtendedCommunity().getGlobalAdministrator());
-        assertArrayEquals(expected2.getRedirectExtendedCommunity().getLocalAdministrator(),
-                result2.getRedirectExtendedCommunity().getLocalAdministrator());
-
-        final TrafficMarkingExtendedCommunityCase expected3 = new TrafficMarkingExtendedCommunityCaseBuilder().setTrafficMarkingExtendedCommunity(
-                new TrafficMarkingExtendedCommunityBuilder().setGlobalAdministrator(new Dscp((short) 63)).build()).build();
-        ex = pa.getExtendedCommunities().get(3);
-        final TrafficMarkingExtendedCommunityCase result3 = (TrafficMarkingExtendedCommunityCase) ex.getExtendedCommunity();
-        assertEquals(expected3.getTrafficMarkingExtendedCommunity().getGlobalAdministrator(),
-                result3.getTrafficMarkingExtendedCommunity().getGlobalAdministrator());
-
-        final ByteBuf serializedBuffer = Unpooled.buffer();
-        parser.serializeAttribute(pa.build(), serializedBuffer);
-        assertArrayEquals(new byte[]{ (byte)192, 16, 32, (byte)128, 6, 0, 72, 0, 1, 2, 3,
-            (byte)128, 7, 0, 0, 0, 0, 0, 3,
-            (byte)128, 8, 0, 35, 4, 2, 8, 7,
-            (byte)128, 9, 0, 0, 0, 0, 0, 63 }, ByteArray.readAllBytes(serializedBuffer));
-
-        try {
-            parser.parseAttribute(Unpooled.copiedBuffer(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 }), pa);
-            fail("Exception should have occured.");
-        } catch (final BGPDocumentedException e) {
-            assertEquals("Could not parse Extended Community type: 11", e.getMessage());
-        }
-    }
-}
diff --git a/bgp/flowspec/src/test/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/FSExtendedCommunitiesTest.java b/bgp/flowspec/src/test/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/FSExtendedCommunitiesTest.java
new file mode 100644 (file)
index 0000000..8e62381
--- /dev/null
@@ -0,0 +1,155 @@
+/*
+ * Copyright (c) 2015 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.flowspec.extended.communities;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.flowspec.BGPActivator;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
+import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.Dscp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.redirect.extended.community.RedirectExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.action.extended.community.TrafficActionExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.marking.extended.community.TrafficMarkingExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.rate.extended.community.TrafficRateExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.RedirectExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.RedirectExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
+
+public class FSExtendedCommunitiesTest {
+
+    private static final byte[] TRAFFIC_RATE = {(byte)128, 6, 0, 72, 0, 1, 2, 3};
+
+    private static final byte[] TRAFFIC_ACTION = {(byte)128, 7, 0, 0, 0, 0, 0, 3};
+
+    private static final byte[] REDIRECT_AS_2BYTES = {(byte)128, 8, 0, 35, 4, 2, 8, 7};
+
+    private static final byte[] TRAFFIC_MARKING = {(byte)128, 9, 0, 0, 0, 0, 0, 63};
+
+    private ExtendedCommunityRegistry registry;
+    private BGPActivator act;
+
+    @Before
+    public void setUp() throws Exception {
+        this.act = new BGPActivator();
+        final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
+        act.start(context);
+        registry = context.getExtendedCommunityReistry();
+    }
+
+    @After
+    public void tearDown() {
+        this.act.close();
+    }
+
+    @Test
+    public void testTrafficRateParser() throws BGPDocumentedException, BGPParsingException {
+        final TrafficRateExtendedCommunityCase trafficRate = new TrafficRateExtendedCommunityCaseBuilder().setTrafficRateExtendedCommunity(
+                new TrafficRateExtendedCommunityBuilder().setInformativeAs(new ShortAsNumber(72L))
+                    .setLocalAdministrator(new Bandwidth(new byte[] { 0, 1, 2, 3 })).build()).build();
+        final ExtendedCommunities expected = new ExtendedCommunitiesBuilder().setExtendedCommunity(trafficRate).setTransitive(true).build();
+
+        final ExtendedCommunities parsed = registry.parseExtendedCommunity(Unpooled.copiedBuffer(TRAFFIC_RATE));
+        Assert.assertEquals(expected, parsed);
+    }
+
+    @Test
+    public void testTrafficRateSerializer() throws BGPDocumentedException, BGPParsingException {
+        final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.flowspec.ipv6.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCase trafficRate = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.flowspec.ipv6.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCaseBuilder().setTrafficRateExtendedCommunity(
+                new TrafficRateExtendedCommunityBuilder().setInformativeAs(new ShortAsNumber(72L))
+                    .setLocalAdministrator(new Bandwidth(new byte[] { 0, 1, 2, 3 })).build()).build();
+        final ExtendedCommunities expected = new ExtendedCommunitiesBuilder().setExtendedCommunity(trafficRate).setTransitive(true).build();
+
+        final ByteBuf output = Unpooled.buffer(TRAFFIC_RATE.length);
+        registry.serializeExtendedCommunity(expected, output);
+        Assert.assertArrayEquals(TRAFFIC_RATE, output.array());
+    }
+
+    @Test
+    public void testTrafficActionParser() throws BGPDocumentedException, BGPParsingException {
+        final TrafficActionExtendedCommunityCase trafficAction = new TrafficActionExtendedCommunityCaseBuilder().setTrafficActionExtendedCommunity(
+                new TrafficActionExtendedCommunityBuilder().setSample(true).setTerminalAction(true).build()).build();
+        final ExtendedCommunities expected = new ExtendedCommunitiesBuilder().setExtendedCommunity(trafficAction).setTransitive(true).build();
+
+        final ExtendedCommunities parsed = registry.parseExtendedCommunity(Unpooled.copiedBuffer(TRAFFIC_ACTION));
+        Assert.assertEquals(expected, parsed);
+    }
+
+    @Test
+    public void testTrafficActionSerializer() throws BGPDocumentedException, BGPParsingException {
+        final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.routes.flowspec.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCase trafficAction = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.routes.flowspec.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCaseBuilder().setTrafficActionExtendedCommunity(
+                new TrafficActionExtendedCommunityBuilder().setSample(true).setTerminalAction(true).build()).build();
+        final ExtendedCommunities expected = new ExtendedCommunitiesBuilder().setExtendedCommunity(trafficAction).setTransitive(true).build();
+
+        final ByteBuf output = Unpooled.buffer(TRAFFIC_ACTION.length);
+        registry.serializeExtendedCommunity(expected, output);
+        Assert.assertArrayEquals(TRAFFIC_ACTION, output.array());
+    }
+
+    @Test
+    public void testTrafficMarkingParser() throws BGPDocumentedException, BGPParsingException {
+        final TrafficMarkingExtendedCommunityCase trafficMarking = new TrafficMarkingExtendedCommunityCaseBuilder().setTrafficMarkingExtendedCommunity(
+                new TrafficMarkingExtendedCommunityBuilder().setGlobalAdministrator(new Dscp((short) 63)).build()).build();
+        final ExtendedCommunities expected = new ExtendedCommunitiesBuilder().setExtendedCommunity(trafficMarking).setTransitive(true).build();
+
+        final ExtendedCommunities parsed = registry.parseExtendedCommunity(Unpooled.copiedBuffer(TRAFFIC_MARKING));
+        Assert.assertEquals(expected, parsed);
+    }
+
+    @Test
+    public void testTrafficMarkingSerializer() throws BGPDocumentedException, BGPParsingException {
+        final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.flowspec.ipv6.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCase trafficMarking = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.flowspec.ipv6.routes.flowspec.route.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCaseBuilder().setTrafficMarkingExtendedCommunity(
+                new TrafficMarkingExtendedCommunityBuilder().setGlobalAdministrator(new Dscp((short) 63)).build()).build();
+        final ExtendedCommunities expected = new ExtendedCommunitiesBuilder().setExtendedCommunity(trafficMarking).setTransitive(true).build();
+
+        final ByteBuf output = Unpooled.buffer(TRAFFIC_MARKING.length);
+        registry.serializeExtendedCommunity(expected, output);
+        Assert.assertArrayEquals(TRAFFIC_MARKING, output.array());
+    }
+
+    @Test
+    public void testRedirect2bParser() throws BGPDocumentedException, BGPParsingException {
+        final RedirectExtendedCommunityCase redirect = new RedirectExtendedCommunityCaseBuilder().setRedirectExtendedCommunity(
+                new RedirectExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(35L)).setLocalAdministrator(
+                        new byte[] { 4, 2, 8, 7 }).build()).build();
+        final ExtendedCommunities expected = new ExtendedCommunitiesBuilder().setExtendedCommunity(redirect).setTransitive(true).build();
+
+        final ExtendedCommunities parsed = registry.parseExtendedCommunity(Unpooled.copiedBuffer(REDIRECT_AS_2BYTES));
+        Assert.assertEquals(expected, parsed);
+    }
+
+    @Test
+    public void testredirect2bSerializer() throws BGPDocumentedException, BGPParsingException {
+        final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.flowspec.ipv6.routes.flowspec.route.attributes.extended.communities.extended.community.RedirectExtendedCommunityCase redirect = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.flowspec.ipv6.routes.flowspec.route.attributes.extended.communities.extended.community.RedirectExtendedCommunityCaseBuilder().setRedirectExtendedCommunity(
+                new RedirectExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(35L)).setLocalAdministrator(
+                        new byte[] { 4, 2, 8, 7 }).build()).build();
+        final ExtendedCommunities expected = new ExtendedCommunitiesBuilder().setExtendedCommunity(redirect).setTransitive(true).build();
+
+        final ByteBuf output = Unpooled.buffer(REDIRECT_AS_2BYTES.length);
+        registry.serializeExtendedCommunity(expected, output);
+        Assert.assertArrayEquals(REDIRECT_AS_2BYTES, output.array());
+    }
+
+}
index 0d84ca2644e828c699da6a7c8e1ee0f88f408170..eb1808e5630dade28b611b0286e83952830a294e 100644 (file)
@@ -37,6 +37,13 @@ import org.opendaylight.protocol.bgp.parser.impl.message.update.NextHopAttribute
 import org.opendaylight.protocol.bgp.parser.impl.message.update.OriginAttributeParser;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.OriginatorIdAttributeParser;
 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.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;
+import org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities.RouteTargetAsTwoOctetEcHandler;
+import org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities.RouteTargetIpv4EcHandler;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.next.hop.Ipv4NextHopParserSerializer;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.next.hop.Ipv6NextHopParserSerializer;
 import org.opendaylight.protocol.bgp.parser.spi.AbstractBGPExtensionProviderActivator;
@@ -72,6 +79,13 @@ 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.MplsLabeledVpnSubsequentAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.NextHop;
 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.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;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetIpv4Case;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
 
@@ -96,14 +110,15 @@ public final class BGPActivator extends AbstractBGPExtensionProviderActivator {
         regs.add(context.registerSubsequentAddressFamily(UnicastSubsequentAddressFamily.class, UNICAST_SAFI));
         regs.add(context.registerSubsequentAddressFamily(MplsLabeledVpnSubsequentAddressFamily.class, VPN_SAFI));
 
-        Ipv4NextHopParserSerializer ipv4NextHopParser = new Ipv4NextHopParserSerializer();
+        final Ipv4NextHopParserSerializer ipv4NextHopParser = new Ipv4NextHopParserSerializer();
         regs.add(context.registerNlriParser(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class,
             new Ipv4NlriParser(), ipv4NextHopParser, Ipv4NextHopCase.class));
 
-        Ipv6NextHopParserSerializer ipv6NextHopParser = new Ipv6NextHopParserSerializer();
+        final Ipv6NextHopParserSerializer ipv6NextHopParser = new Ipv6NextHopParserSerializer();
         regs.add(context.registerNlriParser(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class, new
             Ipv6NlriParser(), ipv6NextHopParser, Ipv6NextHopCase.class));
 
+        this.registerExtendedCommunities(regs, context);
         this.registerCapabilityParsers(regs, context);
         this.registerAttributeParsers(regs, context);
         this.registerMessageParsers(regs, context);
@@ -186,7 +201,7 @@ public final class BGPActivator extends AbstractBGPExtensionProviderActivator {
         regs.add(context.registerAttributeSerializer(MpUnreachNlri.class, mpUnreachAttributeParser));
         regs.add(context.registerAttributeParser(MPUnreachAttributeParser.TYPE, mpUnreachAttributeParser));
 
-        final ExtendedCommunitiesAttributeParser extendedCommunitiesAttributeParser = new ExtendedCommunitiesAttributeParser(context.getReferenceCache());
+        final ExtendedCommunitiesAttributeParser extendedCommunitiesAttributeParser = new ExtendedCommunitiesAttributeParser(context.getExtendedCommunityReistry());
         regs.add(context.registerAttributeSerializer(ExtendedCommunities.class, extendedCommunitiesAttributeParser));
         regs.add(context.registerAttributeParser(ExtendedCommunitiesAttributeParser.TYPE, extendedCommunitiesAttributeParser));
 
@@ -211,4 +226,55 @@ public final class BGPActivator extends AbstractBGPExtensionProviderActivator {
         regs.add(context.registerMessageParser(BGPKeepAliveMessageParser.TYPE, kamp));
         regs.add(context.registerMessageSerializer(Keepalive.class, kamp));
     }
+
+    private void registerExtendedCommunities(final List<AutoCloseable> regs, final BGPExtensionProviderContext context) {
+        final AsTwoOctetSpecificEcHandler twoOctetSpecificEcHandler = new AsTwoOctetSpecificEcHandler();
+        regs.add(context.registerExtendedCommunityParser(twoOctetSpecificEcHandler.getType(true), twoOctetSpecificEcHandler.getSubType(),
+                twoOctetSpecificEcHandler));
+        regs.add(context.registerExtendedCommunityParser(twoOctetSpecificEcHandler.getType(false), twoOctetSpecificEcHandler.getSubType(),
+                twoOctetSpecificEcHandler));
+        regs.add(context.registerExtendedCommunitySerializer(AsSpecificExtendedCommunityCase.class, twoOctetSpecificEcHandler));
+
+        final Ipv4SpecificEcHandler ipv4SpecificEcHandler = new Ipv4SpecificEcHandler();
+        regs.add(context.registerExtendedCommunityParser(ipv4SpecificEcHandler.getType(true), ipv4SpecificEcHandler.getSubType(),
+                ipv4SpecificEcHandler));
+        regs.add(context.registerExtendedCommunityParser(ipv4SpecificEcHandler.getType(false), ipv4SpecificEcHandler.getSubType(),
+                ipv4SpecificEcHandler));
+        regs.add(context.registerExtendedCommunitySerializer(Inet4SpecificExtendedCommunityCase.class, ipv4SpecificEcHandler));
+
+        final OpaqueEcHandler opaqueEcHandler = new OpaqueEcHandler();
+        regs.add(context.registerExtendedCommunityParser(opaqueEcHandler.getType(true), opaqueEcHandler.getSubType(),
+                opaqueEcHandler));
+        regs.add(context.registerExtendedCommunityParser(opaqueEcHandler.getType(false), opaqueEcHandler.getSubType(),
+                opaqueEcHandler));
+        regs.add(context.registerExtendedCommunitySerializer(OpaqueExtendedCommunityCase.class, opaqueEcHandler));
+
+        final RouteOriginAsTwoOctetEcHandler routeOriginAS2bEcHandler = new RouteOriginAsTwoOctetEcHandler();
+        regs.add(context.registerExtendedCommunityParser(routeOriginAS2bEcHandler.getType(true), routeOriginAS2bEcHandler.getSubType(),
+                routeOriginAS2bEcHandler));
+        regs.add(context.registerExtendedCommunityParser(routeOriginAS2bEcHandler.getType(false), routeOriginAS2bEcHandler.getSubType(),
+                routeOriginAS2bEcHandler));
+        regs.add(context.registerExtendedCommunitySerializer(RouteOriginExtendedCommunityCase.class, routeOriginAS2bEcHandler));
+
+        final RouteTargetAsTwoOctetEcHandler routeTargetAS2bEcHandler = new RouteTargetAsTwoOctetEcHandler();
+        regs.add(context.registerExtendedCommunityParser(routeTargetAS2bEcHandler.getType(true), routeTargetAS2bEcHandler.getSubType(),
+                routeTargetAS2bEcHandler));
+        regs.add(context.registerExtendedCommunityParser(routeTargetAS2bEcHandler.getType(false), routeTargetAS2bEcHandler.getSubType(),
+                routeTargetAS2bEcHandler));
+        regs.add(context.registerExtendedCommunitySerializer(RouteTargetExtendedCommunityCase.class, routeTargetAS2bEcHandler));
+
+        final RouteOriginIpv4EcHandler routeOriginIpv4EcHandler = new RouteOriginIpv4EcHandler();
+        regs.add(context.registerExtendedCommunityParser(routeOriginIpv4EcHandler.getType(true), routeOriginIpv4EcHandler.getSubType(),
+                routeOriginIpv4EcHandler));
+        regs.add(context.registerExtendedCommunityParser(routeOriginIpv4EcHandler.getType(false), routeOriginIpv4EcHandler.getSubType(),
+                routeOriginIpv4EcHandler));
+        regs.add(context.registerExtendedCommunitySerializer(RouteOriginIpv4Case.class, routeOriginIpv4EcHandler));
+
+        final RouteTargetIpv4EcHandler routeTargetIpv4EcHandler = new RouteTargetIpv4EcHandler();
+        regs.add(context.registerExtendedCommunityParser(routeTargetIpv4EcHandler.getType(true), routeTargetIpv4EcHandler.getSubType(),
+                routeTargetIpv4EcHandler));
+        regs.add(context.registerExtendedCommunityParser(routeTargetIpv4EcHandler.getType(false), routeTargetIpv4EcHandler.getSubType(),
+                routeTargetIpv4EcHandler));
+        regs.add(context.registerExtendedCommunitySerializer(RouteTargetIpv4Case.class, routeTargetIpv4EcHandler));
+    }
 }
index b6ce40e3c0e1e30f70dcdcef75d1266aa570e278..3ed0eed0939f5b190f730639e291fda892aa42f4 100644 (file)
@@ -1,10 +1,11 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 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;
 
 import com.google.common.base.Preconditions;
@@ -13,183 +14,38 @@ import io.netty.buffer.Unpooled;
 import java.util.ArrayList;
 import java.util.List;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
-import org.opendaylight.protocol.bgp.parser.BGPError;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
-import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
-import org.opendaylight.protocol.util.Ipv4Util;
-import org.opendaylight.protocol.util.ReferenceCache;
+import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityRegistry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
-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.AsSpecificExtendedCommunityCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCaseBuilder;
-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.Inet4SpecificExtendedCommunityCaseBuilder;
-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.OpaqueExtendedCommunityCaseBuilder;
-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.RouteOriginExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunityBuilder;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 
-public class ExtendedCommunitiesAttributeParser implements AttributeParser,AttributeSerializer {
+public final class ExtendedCommunitiesAttributeParser implements AttributeParser, AttributeSerializer {
 
     public static final int TYPE = 16;
 
-    private static final int EXTENDED_COMMUNITY_LENGTH = 6;
-
-    private static final int AS_LOCAL_ADMIN_LENGTH = 4;
-
-    private static final int INET_LOCAL_ADMIN_LENGTH = 2;
-
-    private static final short AS_TYPE_TRANS = 0;
-
-    private static final short AS_TYPE_NON_TRANS = 40;
-
-    private static final short INET_TYPE_TRANS = 1;
-
-    private static final short INET_TYPE_NON_TRANS = 41;
-
-    private static final short OPAQUE_TYPE_TRANS = 3;
-
-    private static final short OPAQUE_TYPE_NON_TRANS = 43;
+    private final ExtendedCommunityRegistry ecReg;
 
-    private static final short ROUTE_TYPE_ONLY = 2;
-
-    private static final short ROUTE_TARGET_SUBTYPE = 2;
-
-    private static final short ROUTE_ORIGIN_SUBTYPE = 3;
-
-    private final ReferenceCache refCache;
-
-    public ExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
-        this.refCache = Preconditions.checkNotNull(refCache);
+    public ExtendedCommunitiesAttributeParser(final ExtendedCommunityRegistry ecReg) {
+        this.ecReg = Preconditions.checkNotNull(ecReg);
     }
 
     @Override
-    public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
+    public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
         final List<ExtendedCommunities> set = new ArrayList<>();
         while (buffer.isReadable()) {
-            final ExtendedCommunitiesBuilder exBuilder = new ExtendedCommunitiesBuilder();
-            parseHeader(exBuilder, buffer);
-            final ExtendedCommunities comm = parseExtendedCommunity(this.refCache, exBuilder, buffer.readSlice(EXTENDED_COMMUNITY_LENGTH));
-            set.add(comm);
+            final ExtendedCommunities exComm = ecReg.parseExtendedCommunity(buffer);
+            if (exComm != null) {
+                set.add(exComm);
+            }
         }
         builder.setExtendedCommunities(set);
     }
 
-    protected void parseHeader(final ExtendedCommunitiesBuilder exBuilder, final ByteBuf buffer) {
-        exBuilder.setCommType(buffer.readUnsignedByte());
-        exBuilder.setCommSubType(buffer.readUnsignedByte());
-    }
-
-    /**
-     * Parse Extended Community according to their type.
-     *
-     * @param refCache
-     * @param comm ExtendedCommunitiesBuilder based on which community type will be new ExtendedCommunity created
-     * @param buffer byte array to be parsed
-     * @return new Specific Extended Community
-     * @throws BGPDocumentedException if the type is not recognized
-     */
-    public ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final ExtendedCommunitiesBuilder comm, final ByteBuf buffer)
-            throws BGPDocumentedException {
-        ExtendedCommunity c = null;
-        switch (comm.getCommType()) {
-        case AS_TYPE_TRANS:
-            c = parseAsTransCommunity(comm, buffer);
-            break;
-        case AS_TYPE_NON_TRANS:
-            final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
-            final byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
-            c = new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
-                new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
-            break;
-        case ROUTE_TYPE_ONLY:
-            c =  parseRouteTypeOnlyCommunity(buffer, comm.getCommSubType());
-            break;
-        case INET_TYPE_TRANS:
-            c = parseInetTypeCommunity(comm, buffer);
-            break;
-        case INET_TYPE_NON_TRANS:
-            c = new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
-                new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
-                        Ipv4Util.addressForByteBuf(buffer)).setLocalAdministrator(
-                        ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build();
-            break;
-        case OPAQUE_TYPE_TRANS:
-            c = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(ByteArray.readAllBytes(buffer)).build()).build();
-            break;
-        case OPAQUE_TYPE_NON_TRANS:
-            c = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(ByteArray.readAllBytes(buffer)).build()).build();
-            break;
-        default:
-            throw new BGPDocumentedException("Could not parse Extended Community type: " + comm.getCommType(), BGPError.OPT_ATTR_ERROR);
-        }
-        return comm.setExtendedCommunity(c).build();
-    }
-
-    private static ExtendedCommunity parseRouteTypeOnlyCommunity(final ByteBuf buffer, final int subtype) throws BGPDocumentedException {
-        final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
-        final byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
-        if (subtype == ROUTE_TARGET_SUBTYPE) {
-            return new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
-        } else if (subtype == ROUTE_ORIGIN_SUBTYPE) {
-            return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
-        } else {
-            throw new BGPDocumentedException("Could not parse Extended Community subtype: " + subtype, BGPError.OPT_ATTR_ERROR);
-        }
-    }
-
-    private static ExtendedCommunity parseAsTransCommunity(final ExtendedCommunitiesBuilder comm, final ByteBuf buffer) {
-        final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
-        final byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
-        if (comm.getCommSubType() == ROUTE_TARGET_SUBTYPE) {
-            return new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
-                new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
-        }
-        if (comm.getCommSubType() == ROUTE_ORIGIN_SUBTYPE) {
-            return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
-                new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
-        }
-        return new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
-            new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
-    }
-
-    private static ExtendedCommunity parseInetTypeCommunity(final ExtendedCommunitiesBuilder comm, final ByteBuf buffer) {
-        if (comm.getCommSubType() == ROUTE_TARGET_SUBTYPE) {
-            return new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
-                new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
-                    .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build();
-        }
-        if (comm.getCommSubType() == ROUTE_ORIGIN_SUBTYPE) {
-            return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
-                new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
-                    .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build();
-        }
-        return new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
-            new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
-                    Ipv4Util.addressForByteBuf(buffer)).setLocalAdministrator(
-                    ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build();
-    }
-
     @Override
     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
@@ -199,38 +55,10 @@ public class ExtendedCommunitiesAttributeParser implements AttributeParser,Attri
         }
         final ByteBuf extendedCommunitiesBuffer = Unpooled.buffer();
         for (final ExtendedCommunities extendedCommunities : communitiesList) {
-            serializeHeader(extendedCommunities, extendedCommunitiesBuffer);
-            serializeExtendedCommunity(extendedCommunities, extendedCommunitiesBuffer);
+            this.ecReg.serializeExtendedCommunity(extendedCommunities, extendedCommunitiesBuffer);
         }
-        AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, extendedCommunitiesBuffer, byteAggregator);
-    }
-
-    protected void serializeHeader(final ExtendedCommunities extendedCommunities, final ByteBuf extendedCommunitiesBuffer) {
-        ByteBufWriteUtil.writeUnsignedByte(extendedCommunities.getCommType(), extendedCommunitiesBuffer);
-        ByteBufWriteUtil.writeUnsignedByte(extendedCommunities.getCommSubType(), extendedCommunitiesBuffer);
-    }
-
-    public void serializeExtendedCommunity(final ExtendedCommunities extendedCommunities, final ByteBuf buffer) {
-        final ExtendedCommunity ex = extendedCommunities.getExtendedCommunity();
-        if (ex instanceof AsSpecificExtendedCommunityCase) {
-            final AsSpecificExtendedCommunity asSpecificExtendedCommunity = ((AsSpecificExtendedCommunityCase) ex).getAsSpecificExtendedCommunity();
-            ByteBufWriteUtil.writeUnsignedShort(asSpecificExtendedCommunity.getGlobalAdministrator().getValue().intValue(), buffer);
-            buffer.writeBytes(asSpecificExtendedCommunity.getLocalAdministrator());
-        } else if (ex instanceof Inet4SpecificExtendedCommunityCase) {
-            final Inet4SpecificExtendedCommunity inet4SpecificExtendedCommunity = ((Inet4SpecificExtendedCommunityCase) ex).getInet4SpecificExtendedCommunity();
-            ByteBufWriteUtil.writeIpv4Address(inet4SpecificExtendedCommunity.getGlobalAdministrator(), buffer);
-            buffer.writeBytes(inet4SpecificExtendedCommunity.getLocalAdministrator());
-        } else if (ex instanceof OpaqueExtendedCommunityCase) {
-            final OpaqueExtendedCommunity opaqueExtendedCommunity = ((OpaqueExtendedCommunityCase) ex).getOpaqueExtendedCommunity();
-            buffer.writeBytes(opaqueExtendedCommunity.getValue());
-        } else if (ex instanceof RouteTargetExtendedCommunityCase) {
-            final RouteTargetExtendedCommunity routeTarget = ((RouteTargetExtendedCommunityCase) ex).getRouteTargetExtendedCommunity();
-            ByteBufWriteUtil.writeUnsignedShort(routeTarget.getGlobalAdministrator().getValue().intValue(), buffer);
-            buffer.writeBytes(routeTarget.getLocalAdministrator());
-        } else if (ex instanceof RouteOriginExtendedCommunityCase) {
-            final RouteOriginExtendedCommunity routeOriginExtendedCommunity = ((RouteOriginExtendedCommunityCase) ex).getRouteOriginExtendedCommunity();
-            ByteBufWriteUtil.writeUnsignedShort(routeOriginExtendedCommunity.getGlobalAdministrator().getValue().intValue(), buffer);
-            buffer.writeBytes(routeOriginExtendedCommunity.getLocalAdministrator());
+        if (extendedCommunitiesBuffer.readableBytes() > 0) {
+            AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, extendedCommunitiesBuffer, byteAggregator);
         }
     }
 }
diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/AsTwoOctetSpecificEcHandler.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/AsTwoOctetSpecificEcHandler.java
new file mode 100644 (file)
index 0000000..439e158
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2015 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 com.google.common.base.Preconditions;
+import com.google.common.primitives.Ints;
+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.AbstractTwoOctetAsExtendedCommunity;
+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.ShortAsNumber;
+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.AsSpecificExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunityBuilder;
+
+public class AsTwoOctetSpecificEcHandler extends AbstractTwoOctetAsExtendedCommunity {
+
+    private static final int SUBTYPE = 0;
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        final AsSpecificExtendedCommunity asSpecific = new AsSpecificExtendedCommunityBuilder()
+            .setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
+            .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH))
+            .build();
+        return new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(asSpecific).build();
+    }
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(extendedCommunity instanceof AsSpecificExtendedCommunityCase,
+                "The extended community %s is not AsSpecificExtendedCommunity type.", extendedCommunity);
+        final AsSpecificExtendedCommunity asSpecific = ((AsSpecificExtendedCommunityCase) extendedCommunity).getAsSpecificExtendedCommunity();
+        ByteBufWriteUtil.writeUnsignedShort(Ints.checkedCast(asSpecific.getGlobalAdministrator().getValue()), byteAggregator);
+        byteAggregator.writeBytes(asSpecific.getLocalAdministrator());
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+
+}
diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/Ipv4SpecificEcHandler.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/Ipv4SpecificEcHandler.java
new file mode 100644 (file)
index 0000000..08a5332
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2015 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 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.AbstractIpv4ExtendedCommunity;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
+import org.opendaylight.protocol.util.Ipv4Util;
+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.Inet4SpecificExtendedCommunityCase;
+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.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
+
+public final class Ipv4SpecificEcHandler extends AbstractIpv4ExtendedCommunity {
+
+    private static final int SUBTYPE = 0;
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        return new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
+                new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
+                        Ipv4Util.addressForByteBuf(buffer)).setLocalAdministrator(
+                        ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build();
+    }
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(extendedCommunity instanceof Inet4SpecificExtendedCommunityCase,
+                "The extended community %s is not Inet4SpecificExtendedCommunityCase type.", extendedCommunity);
+        final Inet4SpecificExtendedCommunity inet4SpecificExtendedCommunity = ((Inet4SpecificExtendedCommunityCase) extendedCommunity).getInet4SpecificExtendedCommunity();
+        ByteBufWriteUtil.writeIpv4Address(inet4SpecificExtendedCommunity.getGlobalAdministrator(), byteAggregator);
+        byteAggregator.writeBytes(inet4SpecificExtendedCommunity.getLocalAdministrator());
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+
+}
diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/OpaqueEcHandler.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/OpaqueEcHandler.java
new file mode 100644 (file)
index 0000000..b12c7f5
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2015 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 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.AbstractOpaqueExtendedCommunity;
+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.OpaqueExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunityBuilder;
+
+public final class OpaqueEcHandler extends AbstractOpaqueExtendedCommunity {
+
+    private static final int SUBTYPE = 0;
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        return new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
+                new OpaqueExtendedCommunityBuilder()
+                    .setValue(ByteArray.readAllBytes(buffer))
+                    .build()).build();
+    }
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(extendedCommunity instanceof OpaqueExtendedCommunityCase,
+                "The extended community %s is not OpaqueExtendedCommunityCase type.", extendedCommunity);
+        final OpaqueExtendedCommunity opaqueExtendedCommunity = ((OpaqueExtendedCommunityCase) extendedCommunity).getOpaqueExtendedCommunity();
+        byteAggregator.writeBytes(opaqueExtendedCommunity.getValue());
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+
+}
diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginAsTwoOctetEcHandler.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginAsTwoOctetEcHandler.java
new file mode 100644 (file)
index 0000000..8179a39
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2015 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 com.google.common.base.Preconditions;
+import com.google.common.primitives.Ints;
+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.AbstractTwoOctetAsExtendedCommunity;
+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.ShortAsNumber;
+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.RouteOriginExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
+
+public final class RouteOriginAsTwoOctetEcHandler extends AbstractTwoOctetAsExtendedCommunity {
+
+    private static final int SUBTYPE = 3;
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        final RouteOriginExtendedCommunity targetOrigin = new RouteOriginExtendedCommunityBuilder()
+            .setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
+            .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH))
+            .build();
+        return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(targetOrigin).build();
+    }
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(extendedCommunity instanceof RouteOriginExtendedCommunityCase,
+                "The extended community %s is not RouteOriginExtendedCommunity type.", extendedCommunity);
+        final RouteOriginExtendedCommunity routeOrigin = ((RouteOriginExtendedCommunityCase) extendedCommunity).getRouteOriginExtendedCommunity();
+        ByteBufWriteUtil.writeUnsignedShort(Ints.checkedCast(routeOrigin.getGlobalAdministrator().getValue()), byteAggregator);
+        byteAggregator.writeBytes(routeOrigin.getLocalAdministrator());
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+}
diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginIpv4EcHandler.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginIpv4EcHandler.java
new file mode 100644 (file)
index 0000000..34b9ce1
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2015 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 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.AbstractIpv4ExtendedCommunity;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
+import org.opendaylight.protocol.util.Ipv4Util;
+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.RouteOriginIpv4Case;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginIpv4CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.ipv4._case.RouteOriginIpv4;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.ipv4._case.RouteOriginIpv4Builder;
+
+public final class RouteOriginIpv4EcHandler extends AbstractIpv4ExtendedCommunity {
+
+    private static final int SUBTYPE = 3;
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(extendedCommunity instanceof RouteOriginIpv4Case,
+                "The extended community %s is not RouteOriginIpv4Case type.", extendedCommunity);
+        final RouteOriginIpv4 routeTarget = ((RouteOriginIpv4Case) extendedCommunity).getRouteOriginIpv4();
+        ByteBufWriteUtil.writeIpv4Address(routeTarget.getGlobalAdministrator(), byteAggregator);
+        ByteBufWriteUtil.writeUnsignedShort(routeTarget.getLocalAdministrator(), byteAggregator);
+    }
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        final RouteOriginIpv4 routeTarget = new RouteOriginIpv4Builder()
+            .setGlobalAdministrator(Ipv4Util.addressForByteBuf(buffer))
+            .setLocalAdministrator(buffer.readUnsignedShort())
+            .build();
+        return new RouteOriginIpv4CaseBuilder().setRouteOriginIpv4(routeTarget).build();
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+
+}
diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetAsTwoOctetEcHandler.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetAsTwoOctetEcHandler.java
new file mode 100644 (file)
index 0000000..563a697
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2015 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 com.google.common.base.Preconditions;
+import com.google.common.primitives.Ints;
+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.AbstractTwoOctetAsExtendedCommunity;
+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.ShortAsNumber;
+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.RouteTargetExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunityBuilder;
+
+public final class RouteTargetAsTwoOctetEcHandler extends AbstractTwoOctetAsExtendedCommunity {
+
+    private static final int SUBTYPE = 2;
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        final RouteTargetExtendedCommunity targetTarget = new RouteTargetExtendedCommunityBuilder()
+            .setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
+            .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH))
+            .build();
+        return new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(targetTarget).build();
+    }
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(extendedCommunity instanceof RouteTargetExtendedCommunityCase,
+                "The extended community %s is not RouteTargetExtendedCommunityCase type.", extendedCommunity);
+        final RouteTargetExtendedCommunity routeTarget = ((RouteTargetExtendedCommunityCase) extendedCommunity).getRouteTargetExtendedCommunity();
+        ByteBufWriteUtil.writeUnsignedShort(Ints.checkedCast(routeTarget.getGlobalAdministrator().getValue()), byteAggregator);
+        byteAggregator.writeBytes(routeTarget.getLocalAdministrator());
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+
+}
diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetIpv4EcHandler.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetIpv4EcHandler.java
new file mode 100644 (file)
index 0000000..a9229bc
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2015 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 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.AbstractIpv4ExtendedCommunity;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
+import org.opendaylight.protocol.util.Ipv4Util;
+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.RouteTargetIpv4Case;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.ipv4._case.RouteTargetIpv4;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.ipv4._case.RouteTargetIpv4Builder;
+
+public final class RouteTargetIpv4EcHandler extends AbstractIpv4ExtendedCommunity {
+
+    private static final int SUBTYPE = 2;
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(extendedCommunity instanceof RouteTargetIpv4Case,
+                "The extended community %s is not RouteTargetAsTwoOctetCase type.", extendedCommunity);
+        final RouteTargetIpv4 routeTarget = ((RouteTargetIpv4Case) extendedCommunity).getRouteTargetIpv4();
+        ByteBufWriteUtil.writeIpv4Address(routeTarget.getGlobalAdministrator(), byteAggregator);
+        ByteBufWriteUtil.writeUnsignedShort(routeTarget.getLocalAdministrator(), byteAggregator);
+    }
+
+    @Override
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+        final RouteTargetIpv4 routeTarget = new RouteTargetIpv4Builder()
+            .setGlobalAdministrator(Ipv4Util.addressForByteBuf(buffer))
+            .setLocalAdministrator(buffer.readUnsignedShort())
+            .build();
+        return new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(routeTarget).build();
+    }
+
+    @Override
+    public int getSubType() {
+        return SUBTYPE;
+    }
+}
index 06f73993467078fb24c6162e5a2b0aa20bba5ca7..946834b8c2f12fca0d93ea997e5aa13bf9a88dc1 100644 (file)
@@ -11,6 +11,7 @@ import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+
 import com.google.common.collect.Lists;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
@@ -70,8 +71,8 @@ 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.Ipv6AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
 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.Inet4SpecificExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.ipv4._case.RouteTargetIpv4Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
@@ -529,10 +530,10 @@ public class BGPParserTest {
             new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
 
         final List<ExtendedCommunities> comms = Lists.newArrayList();
-        comms.add(new ExtendedCommunitiesBuilder().setCommType((short) 1).setCommSubType((short) 4).setExtendedCommunity(
-            new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
-                new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
-                    new Ipv4Address("192.168.1.0")).setLocalAdministrator(new byte[] { 0x12, 0x34 }).build()).build()).build());
+        comms.add(new ExtendedCommunitiesBuilder().setTransitive(true).setExtendedCommunity(
+            new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(
+                new RouteTargetIpv4Builder().setGlobalAdministrator(
+                    new Ipv4Address("192.168.1.0")).setLocalAdministrator(4660).build()).build()).build());
 
         // check path attributes
         final Attributes attrs = message.getAttributes();
index 3abf89d797a6984699891ef24de617cc22819ef1..70e258e39d1e563e74dd34ba26979d8fb73a143b 100644 (file)
@@ -7,32 +7,22 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl;
 
-import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.fail;
 
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.List;
 import org.junit.Test;
-import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser;
-import org.opendaylight.protocol.bgp.parser.impl.message.update.ExtendedCommunitiesAttributeParser;
-import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.util.NoopReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AggregatorBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.GracefulRestartCapability;
@@ -45,23 +35,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mult
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpAggregator;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
 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.AsSpecificExtendedCommunityCaseBuilder;
-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.Inet4SpecificExtendedCommunityCaseBuilder;
-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.OpaqueExtendedCommunityCaseBuilder;
-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.RouteOriginExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunityBuilder;
 
 public class ComplementaryTest {
 
@@ -121,115 +95,6 @@ public class ComplementaryTest {
         assertEquals(ipv4.getNetworkAddress(), ipv4i.getNetworkAddress());
     }
 
-    private static final byte[] communitiesBytes = { 0, 5, 0, 54, 0, 0, 1, 76,
-        40, 5, 0, 54, 0, 0, 1, 76,
-        1, 2, 0, 35, 4, 2, 8, 7,
-        0, 3, 0, 24, 4, 2, 8, 7,
-        41, 6, 12, 51, 2, 5, 21, 45,
-        3, 6, 21, 45, 5, 4, 3, 1,
-        43, 6, 21, 45, 5, 4, 3, 1 };
-
-    @Test
-    public void testExtendedCommunitiesParser() {
-        final ExtendedCommunitiesAttributeParser parser = new ExtendedCommunitiesAttributeParser(NoopReferenceCache.getInstance());
-        final AttributesBuilder pa = new AttributesBuilder();
-        try {
-            parser.parseAttribute(Unpooled.copiedBuffer(communitiesBytes), pa);
-        } catch (final BGPDocumentedException e1) {
-            fail("Not expected exception: " + e1);
-        }
-        AsSpecificExtendedCommunityCase expected = new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
-                new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(new ShortAsNumber(54L)).setLocalAdministrator(
-                        new byte[] { 0, 0, 1, 76 }).build()).build();
-        ExtendedCommunities ex = pa.getExtendedCommunities().get(0);
-        AsSpecificExtendedCommunityCase result = (AsSpecificExtendedCommunityCase) ex.getExtendedCommunity();
-        assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), result.getAsSpecificExtendedCommunity().isTransitive());
-        assertEquals(expected.getAsSpecificExtendedCommunity().getGlobalAdministrator(),
-                result.getAsSpecificExtendedCommunity().getGlobalAdministrator());
-        assertArrayEquals(expected.getAsSpecificExtendedCommunity().getLocalAdministrator(),
-                result.getAsSpecificExtendedCommunity().getLocalAdministrator());
-        assertEquals(0, ex.getCommType().intValue());
-
-        expected = new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
-                new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new ShortAsNumber(54L)).setLocalAdministrator(
-                        new byte[] { 0, 0, 1, 76 }).build()).build();
-        ex = pa.getExtendedCommunities().get(1);
-        result = (AsSpecificExtendedCommunityCase) ex.getExtendedCommunity();
-        assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), result.getAsSpecificExtendedCommunity().isTransitive());
-        assertEquals(40, ex.getCommType().intValue());
-
-        final RouteTargetExtendedCommunityCase rexpected = new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
-                new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(35L)).setLocalAdministrator(
-                        new byte[] { 4, 2, 8, 7 }).build()).build();
-        ex = pa.getExtendedCommunities().get(2);
-        final RouteTargetExtendedCommunityCase rresult = (RouteTargetExtendedCommunityCase) ex.getExtendedCommunity();
-        assertEquals(rexpected.getRouteTargetExtendedCommunity().getGlobalAdministrator(),
-                rresult.getRouteTargetExtendedCommunity().getGlobalAdministrator());
-        assertArrayEquals(rexpected.getRouteTargetExtendedCommunity().getLocalAdministrator(),
-                rresult.getRouteTargetExtendedCommunity().getLocalAdministrator());
-        assertEquals(1, ex.getCommType().intValue());
-        assertEquals(2, ex.getCommSubType().intValue());
-
-        final RouteOriginExtendedCommunityCase oexpected = new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
-                new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(24L)).setLocalAdministrator(
-                        new byte[] { 4, 2, 8, 7 }).build()).build();
-        ex = pa.getExtendedCommunities().get(3);
-        final RouteOriginExtendedCommunityCase oresult = (RouteOriginExtendedCommunityCase) ex.getExtendedCommunity();
-        assertEquals(oexpected.getRouteOriginExtendedCommunity().getGlobalAdministrator(),
-                oresult.getRouteOriginExtendedCommunity().getGlobalAdministrator());
-        assertArrayEquals(oexpected.getRouteOriginExtendedCommunity().getLocalAdministrator(),
-                oresult.getRouteOriginExtendedCommunity().getLocalAdministrator());
-        assertEquals(0, ex.getCommType().intValue());
-        assertEquals(3, ex.getCommSubType().intValue());
-
-        final Inet4SpecificExtendedCommunityCase iexpected = new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
-                new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new Ipv4Address("12.51.2.5")).setLocalAdministrator(
-                        new byte[] { 21, 45 }).build()).build();
-        ex = pa.getExtendedCommunities().get(4);
-        final Inet4SpecificExtendedCommunityCase iresult = (Inet4SpecificExtendedCommunityCase) ex.getExtendedCommunity();
-        assertEquals(iexpected.getInet4SpecificExtendedCommunity().isTransitive(),
-                iresult.getInet4SpecificExtendedCommunity().isTransitive());
-        assertEquals(iexpected.getInet4SpecificExtendedCommunity().getGlobalAdministrator(),
-                iresult.getInet4SpecificExtendedCommunity().getGlobalAdministrator());
-        assertArrayEquals(iexpected.getInet4SpecificExtendedCommunity().getLocalAdministrator(),
-                iresult.getInet4SpecificExtendedCommunity().getLocalAdministrator());
-        assertEquals(41, ex.getCommType().intValue());
-
-        final OpaqueExtendedCommunityCase oeexpected = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
-                new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
-        ex = pa.getExtendedCommunities().get(5);
-        final OpaqueExtendedCommunityCase oeresult = (OpaqueExtendedCommunityCase) ex.getExtendedCommunity();
-        assertEquals(oeexpected.getOpaqueExtendedCommunity().isTransitive(), oeresult.getOpaqueExtendedCommunity().isTransitive());
-        assertArrayEquals(oeexpected.getOpaqueExtendedCommunity().getValue(), oeresult.getOpaqueExtendedCommunity().getValue());
-        assertEquals(3, ex.getCommType().intValue());
-
-        final OpaqueExtendedCommunityCase oeexpected1 = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
-                new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
-        ex = pa.getExtendedCommunities().get(6);
-        final OpaqueExtendedCommunityCase oeresult1 = (OpaqueExtendedCommunityCase) ex.getExtendedCommunity();
-        assertEquals(oeexpected1.getOpaqueExtendedCommunity().isTransitive(), oeresult1.getOpaqueExtendedCommunity().isTransitive());
-        assertArrayEquals(oeexpected1.getOpaqueExtendedCommunity().getValue(), oeresult1.getOpaqueExtendedCommunity().getValue());
-        assertEquals(43, ex.getCommType().intValue());
-
-        final ByteBuf serializedBuffer = Unpooled.buffer();
-        parser.serializeAttribute(pa.build(), serializedBuffer);
-        assertArrayEquals(new byte[]{ (byte)192, 16, 56, 0, 5, 0, 54, 0, 0, 1, 76,
-            40, 5, 0, 54, 0, 0, 1, 76,
-            1, 2, 0, 35, 4, 2, 8, 7,
-            0, 3, 0, 24, 4, 2, 8, 7,
-            41, 6, 12, 51, 2, 5, 21, 45,
-            3, 6, 21, 45, 5, 4, 3, 1,
-            43, 6, 21, 45, 5, 4, 3, 1 }, ByteArray.readAllBytes(serializedBuffer));
-
-        try {
-            parser.parseAttribute(Unpooled.copiedBuffer(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 }), pa);
-            fail("Exception should have occured.");
-        } catch (final BGPDocumentedException e) {
-            assertEquals("Could not parse Extended Community type: 11", e.getMessage());
-        }
-    }
-
-
     @Test(expected=UnsupportedOperationException.class)
     public void testAsPathSegmentParserPrivateConstructor() throws Throwable {
         final Constructor<AsPathSegmentParser> c = AsPathSegmentParser.class.getDeclaredConstructor();
diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/RouteOriginCommunityTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/RouteOriginCommunityTest.java
deleted file mode 100644 (file)
index 07ff856..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2013 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;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
-
-public class RouteOriginCommunityTest {
-
-    private RouteOriginExtendedCommunity community;
-
-    @Before
-    public void init() {
-        final ShortAsNumber globalAdmin = new ShortAsNumber(65535L);
-        final byte[] localAdmin = new byte[] { 10, 0, 0, 1 };
-        this.community = new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(globalAdmin).setLocalAdministrator(localAdmin).build();
-    }
-
-    @Test
-    public void testGetGlobalAdmin() {
-        final ShortAsNumber testAsn = new ShortAsNumber(65535L);
-        assertEquals(this.community.getGlobalAdministrator(), testAsn);
-    }
-
-    @Test
-    public void testGetLocalAdmin() {
-        assertArrayEquals(new byte[] { 10, 0, 0, 1 }, this.community.getLocalAdministrator());
-    }
-}
diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/RouteTargetCommunityTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/RouteTargetCommunityTest.java
deleted file mode 100644 (file)
index fea301a..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2013 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;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunityBuilder;
-
-public class RouteTargetCommunityTest {
-
-    private RouteTargetExtendedCommunity community;
-
-    @Before
-    public void init() {
-        final ShortAsNumber globalAdmin = new ShortAsNumber(65535L);
-        final byte[] localAdmin = new byte[] { 10, 0, 0, 1 };
-        this.community = new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(globalAdmin).setLocalAdministrator(localAdmin).build();
-    }
-
-    @Test
-    public void testGetGlobalAdmin() {
-        final ShortAsNumber testAsn = new ShortAsNumber(65535L);
-        assertEquals(this.community.getGlobalAdministrator(), testAsn);
-    }
-
-    @Test
-    public void testGetLocalAdmin() {
-        assertArrayEquals(new byte[] { 10, 0, 0, 1 }, this.community.getLocalAdministrator());
-    }
-}
diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/ExtendedCommunitiesAttributeParserTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/ExtendedCommunitiesAttributeParserTest.java
new file mode 100644 (file)
index 0000000..0025c8d
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2015 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;
+
+import com.google.common.primitives.Bytes;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Assert;
+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.bgp.parser.spi.extended.community.ExtendedCommunityRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
+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.RouteOriginExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
+
+public class ExtendedCommunitiesAttributeParserTest {
+
+    private static final byte[] INPUT = {
+        0x40, 0x03, 0x00, 54, 0, 0, 1, 76
+    };
+
+    private static final byte[] UNKOWN = {
+        0x40, 0x05, 0x00, 54, 0, 0, 1, 76
+    };
+
+    private ExtendedCommunitiesAttributeParser handler;
+
+    @Before
+    public void setUp() {
+        final ExtendedCommunityRegistry exReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getExtendedCommunityReistry();
+        handler = new ExtendedCommunitiesAttributeParser(exReg);
+    }
+
+    @Test
+    public void testExtendedCommunityAttributeParser() throws BGPDocumentedException, BGPParsingException {
+        final RouteOriginExtendedCommunityCase routeOrigin = new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
+                new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(54L)).setLocalAdministrator(
+                        new byte[] { 0, 0, 1, 76 }).build()).build();
+        final ExtendedCommunities expected = new ExtendedCommunitiesBuilder().setTransitive(false).setExtendedCommunity(routeOrigin).build();
+        final AttributesBuilder attBuilder = new AttributesBuilder();
+
+        handler.parseAttribute(Unpooled.copiedBuffer(INPUT), attBuilder);
+        final ExtendedCommunities parsed = attBuilder.getExtendedCommunities().get(0);
+        Assert.assertEquals(expected, parsed);
+
+        final ByteBuf output = Unpooled.buffer(INPUT.length);
+        handler.serializeAttribute(attBuilder.build(), output);
+        Assert.assertArrayEquals(Bytes.concat(new byte[] {(byte)192, 16, 8}, INPUT), ByteArray.readAllBytes(output));
+    }
+
+    @Test
+    public void testExtendedCommunityAttributeParserUnknown() throws BGPDocumentedException, BGPParsingException {
+        final AttributesBuilder attBuilder = new AttributesBuilder();
+        handler.parseAttribute(Unpooled.copiedBuffer(UNKOWN), attBuilder);
+        Assert.assertTrue(attBuilder.getExtendedCommunities().isEmpty());
+    }
+
+}
diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/AsTwoOctetSpecificEcHandlerTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/AsTwoOctetSpecificEcHandlerTest.java
new file mode 100644 (file)
index 0000000..2d15e63
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2015 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 io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
+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.AsSpecificExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunityBuilder;
+
+public class AsTwoOctetSpecificEcHandlerTest {
+
+    private static final byte[] INPUT = {
+        0, 54, 0, 0, 1, 76
+    };
+
+    @Test
+    public void testHandler() throws BGPDocumentedException, BGPParsingException {
+        final AsTwoOctetSpecificEcHandler handler = new AsTwoOctetSpecificEcHandler();
+        final AsSpecificExtendedCommunityCase expected = new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
+                new AsSpecificExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(54L)).setLocalAdministrator(
+                        new byte[] { 0, 0, 1, 76 }).build()).build();
+
+        final ExtendedCommunity exComm = handler.parseExtendedCommunity(Unpooled.copiedBuffer(INPUT));
+        Assert.assertEquals(expected, exComm);
+
+        final ByteBuf output = Unpooled.buffer(INPUT.length);
+        handler.serializeExtendedCommunity(expected, output);
+        Assert.assertArrayEquals(INPUT, output.array());
+    }
+
+}
diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/Ipv4SpecificEcHandlerTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/Ipv4SpecificEcHandlerTest.java
new file mode 100644 (file)
index 0000000..3e2315d
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2015 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 io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+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.Inet4SpecificExtendedCommunityCase;
+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.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
+
+public class Ipv4SpecificEcHandlerTest {
+
+    private static final byte[] INPUT = {
+        12, 51, 2, 5, 21, 45
+    };
+
+    @Test
+    public void testHandler() throws BGPDocumentedException, BGPParsingException {
+        final Ipv4SpecificEcHandler handler = new Ipv4SpecificEcHandler();
+        final Inet4SpecificExtendedCommunityCase expected = new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
+                new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new Ipv4Address("12.51.2.5")).setLocalAdministrator(
+                        new byte[] { 21, 45 }).build()).build();
+
+        final ExtendedCommunity exComm = handler.parseExtendedCommunity(Unpooled.copiedBuffer(INPUT));
+        Assert.assertEquals(expected, exComm);
+
+        final ByteBuf output = Unpooled.buffer(INPUT.length);
+        handler.serializeExtendedCommunity(expected, output);
+        Assert.assertArrayEquals(INPUT, output.array());
+    }
+
+}
diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/OpaqueEcHandlerTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/OpaqueEcHandlerTest.java
new file mode 100644 (file)
index 0000000..95c6280
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2015 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 io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+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.OpaqueExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunityBuilder;
+
+public class OpaqueEcHandlerTest {
+
+    private static final byte[] INPUT = {
+        21, 45, 5, 4, 3, 1
+    };
+
+    @Test
+    public void testHandler() throws BGPDocumentedException, BGPParsingException {
+        final OpaqueEcHandler handler = new OpaqueEcHandler();
+        final OpaqueExtendedCommunityCase expected = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
+                new OpaqueExtendedCommunityBuilder().setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
+
+        final ExtendedCommunity exComm = handler.parseExtendedCommunity(Unpooled.copiedBuffer(INPUT));
+        Assert.assertEquals(expected, exComm);
+
+        final ByteBuf output = Unpooled.buffer(INPUT.length);
+        handler.serializeExtendedCommunity(expected, output);
+        Assert.assertArrayEquals(INPUT, output.array());
+    }
+
+}
diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginAsTwoOctetEcHandlerTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginAsTwoOctetEcHandlerTest.java
new file mode 100644 (file)
index 0000000..8534d5b
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2015 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 io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
+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.RouteOriginExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
+
+public class RouteOriginAsTwoOctetEcHandlerTest {
+
+    private static final byte[] INPUT = {
+        0, 24, 4, 2, 8, 7
+    };
+
+    @Test
+    public void testHandler() throws BGPDocumentedException, BGPParsingException {
+        final RouteOriginAsTwoOctetEcHandler handler = new RouteOriginAsTwoOctetEcHandler();
+        final RouteOriginExtendedCommunityCase expected = new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
+                new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(24L)).setLocalAdministrator(
+                        new byte[] { 4, 2, 8, 7 }).build()).build();
+
+        final ExtendedCommunity exComm = handler.parseExtendedCommunity(Unpooled.copiedBuffer(INPUT));
+        Assert.assertEquals(expected, exComm);
+
+        final ByteBuf output = Unpooled.buffer(INPUT.length);
+        handler.serializeExtendedCommunity(expected, output);
+        Assert.assertArrayEquals(INPUT, output.array());
+    }
+
+}
diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginIpv4EcHandlerTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteOriginIpv4EcHandlerTest.java
new file mode 100644 (file)
index 0000000..4414926
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2015 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 io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+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.RouteOriginIpv4Case;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginIpv4CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.ipv4._case.RouteOriginIpv4Builder;
+
+public class RouteOriginIpv4EcHandlerTest {
+
+    private static final byte[] INPUT = {
+        12, 51, 2, 5, 0x15, 0x2d
+    };
+
+    @Test
+    public void testHandler() throws BGPDocumentedException, BGPParsingException {
+        final RouteOriginIpv4EcHandler handler = new RouteOriginIpv4EcHandler();
+        final RouteOriginIpv4Case expected = new RouteOriginIpv4CaseBuilder().setRouteOriginIpv4(
+                new RouteOriginIpv4Builder()
+                    .setGlobalAdministrator(new Ipv4Address("12.51.2.5"))
+                    .setLocalAdministrator(5421)
+                    .build()).build();
+
+        final ExtendedCommunity exComm = handler.parseExtendedCommunity(Unpooled.copiedBuffer(INPUT));
+        Assert.assertEquals(expected, exComm);
+
+        final ByteBuf output = Unpooled.buffer(INPUT.length);
+        handler.serializeExtendedCommunity(expected, output);
+        Assert.assertArrayEquals(INPUT, output.array());
+    }
+
+}
diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetAsTwoOctetEcHandlerTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetAsTwoOctetEcHandlerTest.java
new file mode 100644 (file)
index 0000000..d79077c
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2015 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 io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
+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.RouteTargetExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunityBuilder;
+
+public class RouteTargetAsTwoOctetEcHandlerTest {
+
+    private static final byte[] INPUT = {
+        0, 35, 4, 2, 8, 7
+    };
+
+    @Test
+    public void testHandler() throws BGPDocumentedException, BGPParsingException {
+        final RouteTargetAsTwoOctetEcHandler handler = new RouteTargetAsTwoOctetEcHandler();
+        final RouteTargetExtendedCommunityCase expected = new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
+                new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(35L)).setLocalAdministrator(
+                        new byte[] { 4, 2, 8, 7 }).build()).build();
+
+        final ExtendedCommunity exComm = handler.parseExtendedCommunity(Unpooled.copiedBuffer(INPUT));
+        Assert.assertEquals(expected, exComm);
+
+        final ByteBuf output = Unpooled.buffer(INPUT.length);
+        handler.serializeExtendedCommunity(expected, output);
+        Assert.assertArrayEquals(INPUT, output.array());
+    }
+
+}
diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetIpv4EcHandlerTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/extended/communities/RouteTargetIpv4EcHandlerTest.java
new file mode 100644 (file)
index 0000000..6131f99
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2015 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 io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+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.RouteTargetIpv4Case;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.ipv4._case.RouteTargetIpv4Builder;
+
+public class RouteTargetIpv4EcHandlerTest {
+
+    private static final byte[] INPUT = {
+        12, 51, 2, 5, 0x15, 0x2d
+    };
+
+    @Test
+    public void testHandler() throws BGPDocumentedException, BGPParsingException {
+        final RouteTargetIpv4EcHandler handler = new RouteTargetIpv4EcHandler();
+        final RouteTargetIpv4Case expected = new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(
+                new RouteTargetIpv4Builder()
+                    .setGlobalAdministrator(new Ipv4Address("12.51.2.5"))
+                    .setLocalAdministrator(5421)
+                    .build()).build();
+
+        final ExtendedCommunity exComm = handler.parseExtendedCommunity(Unpooled.copiedBuffer(INPUT));
+        Assert.assertEquals(expected, exComm);
+
+        final ByteBuf output = Unpooled.buffer(INPUT.length);
+        handler.serializeExtendedCommunity(expected, output);
+        Assert.assertArrayEquals(INPUT, output.array());
+    }
+
+}
index 28d9461f9ac6164ac7147c31ebb18654a14ebfc0..01eb5a01586be506f6172a46bd147c7a66d2a2f8 100644 (file)
Binary files a/bgp/parser-impl/src/test/resources/up4.bin and b/bgp/parser-impl/src/test/resources/up4.bin differ
index 13631fffe4465571000acd8a81021cd7f551694b..ee8b58b7756423e699d54339913284d1ba316c72 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityRegistry;
+
 /**
  * A single instance of a collection of extensions for use by consumers. This provides access to the various BGP-related
  * registries. The registries are read-only and are populated by extension producers.
@@ -25,4 +27,6 @@ public interface BGPExtensionConsumerContext {
     ParameterRegistry getParameterRegistry();
 
     SubsequentAddressFamilyRegistry getSubsequentAddressFamilyRegistry();
+
+    ExtendedCommunityRegistry getExtendedCommunityReistry();
 }
index 19e137c375b89773cc93b98a6c40f956e7c6f286..2f998c61be7f88e7ca936f440a3205bc07004cbd 100644 (file)
@@ -7,10 +7,14 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+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.ReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
+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.next.hop.CNextHop;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.Notification;
@@ -54,6 +58,11 @@ public interface BGPExtensionProviderContext extends BGPExtensionConsumerContext
 
     AutoCloseable registerParameterSerializer(Class<? extends BgpParameters> paramClass, ParameterSerializer serializer);
 
+    AutoCloseable registerExtendedCommunitySerializer(final Class<? extends ExtendedCommunity> extendedCommunityClass,
+            final ExtendedCommunitySerializer serializer);
+
+    AutoCloseable registerExtendedCommunityParser(final int type, final int subtype, final ExtendedCommunityParser parser);
+
     /**
      * Get the context-wide cache for a particular object type.
      *
diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractIpv4ExtendedCommunity.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractIpv4ExtendedCommunity.java
new file mode 100644 (file)
index 0000000..d8bfc95
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2015 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.spi.extended.community;
+
+
+public abstract class AbstractIpv4ExtendedCommunity implements ExtendedCommunityParser, ExtendedCommunitySerializer {
+
+    private static final int TYPE = 1;
+
+    protected static final int INET_LOCAL_ADMIN_LENGTH = 2;
+
+    @Override
+    public final int getType(final boolean isTransitive) {
+        return ExtendedCommunityUtil.setTransitivity(TYPE, isTransitive);
+    }
+
+}
diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractOpaqueExtendedCommunity.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractOpaqueExtendedCommunity.java
new file mode 100644 (file)
index 0000000..a5d96dd
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2015 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.spi.extended.community;
+
+public abstract class AbstractOpaqueExtendedCommunity implements ExtendedCommunityParser, ExtendedCommunitySerializer {
+
+    private static final int TYPE = 3;
+
+    @Override
+    public int getType(final boolean isTransitive) {
+        return ExtendedCommunityUtil.setTransitivity(TYPE, isTransitive);
+    }
+
+}
diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractTwoOctetAsExtendedCommunity.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractTwoOctetAsExtendedCommunity.java
new file mode 100644 (file)
index 0000000..bc8acfd
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2015 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.spi.extended.community;
+
+
+public abstract class AbstractTwoOctetAsExtendedCommunity implements ExtendedCommunityParser, ExtendedCommunitySerializer {
+
+    private static final int TYPE = 0;
+
+    protected static final int AS_LOCAL_ADMIN_LENGTH = 4;
+
+    @Override
+    public final int getType(final boolean isTransitive) {
+        return ExtendedCommunityUtil.setTransitivity(TYPE, isTransitive);
+    }
+}
diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityParser.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityParser.java
new file mode 100644 (file)
index 0000000..5d5aac9
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2015 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.spi.extended.community;
+
+import io.netty.buffer.ByteBuf;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
+
+/**
+ * The Extended Community value parser (ByteBuf to ExtendedCommunity)
+ *
+ */
+public interface ExtendedCommunityParser {
+
+    /**
+     * Parses the Extended Community value encoded in the buffer.
+     * @param buffer ByteBuf
+     * @return Decoded Extended Community value.
+     * @throws BGPDocumentedException
+     * @throws BGPParsingException
+     */
+    ExtendedCommunity parseExtendedCommunity(ByteBuf buffer) throws BGPDocumentedException, BGPParsingException;
+
+}
diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityRegistry.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityRegistry.java
new file mode 100644 (file)
index 0000000..ddbb942
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2015 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.spi.extended.community;
+
+import io.netty.buffer.ByteBuf;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
+
+/**
+ * The Extended Communities registry.
+ *
+ */
+public interface ExtendedCommunityRegistry {
+
+    /**
+     * Parses the Extended Community.
+     * @param buffer ByteBuf
+     * @return Decoded ExtendedCommunity or null if the parser for such Extended Community type/sub-type is not present.
+     * @throws BGPDocumentedException
+     * @throws BGPParsingException
+     */
+    ExtendedCommunities parseExtendedCommunity(ByteBuf buffer) throws BGPDocumentedException, BGPParsingException;
+
+    /**
+     * Serializes the Extended Community.
+     * @param extendedCommunity ExtendedCommunity to be encoded.
+     * @param byteAggregator ByteBuf, where the Extended Community is serialized,
+     *  if a serialized is not present for such Extended Community type, no bytes are written into output buffer.
+     */
+    void serializeExtendedCommunity(ExtendedCommunities extendedCommunity, ByteBuf byteAggregator);
+
+}
diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunitySerializer.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunitySerializer.java
new file mode 100644 (file)
index 0000000..522080b
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2015 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.spi.extended.community;
+
+import io.netty.buffer.ByteBuf;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
+
+/**
+ * The Extended Community serializer (ExtendedCommunity to ByteBuf).
+ *
+ */
+public interface ExtendedCommunitySerializer {
+
+    /**
+     * Serializes Extended Community value to the buffer.
+     * @param extendedCommunity ExtendedCommuity to be encoded.
+     * @param byteAggregator The output buffer where the extended community is written.
+     */
+    void serializeExtendedCommunity(ExtendedCommunity extendedCommunity, ByteBuf byteAggregator);
+
+    /**
+     * Provides a type of the extended community for which the serializer is registered.
+     * @param isTransitive Transitivity of the extended community.
+     * @return A type of the extended community.
+     */
+    int getType(boolean isTransitive);
+
+    /**
+     * Provides a sub-type of the extended community for which the serializer is registered.
+     * @return A sub-type of the extended community.
+     */
+    int getSubType();
+
+}
diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityUtil.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityUtil.java
new file mode 100644 (file)
index 0000000..6546f8b
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2015 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.spi.extended.community;
+
+/**
+ * The utility functions related to the extended communities.
+ *
+ */
+public final class ExtendedCommunityUtil {
+
+    private ExtendedCommunityUtil() {
+        throw new UnsupportedOperationException();
+    }
+
+    private static final byte NON_TRANS = 0x40;
+
+    /**
+     * Sets transitivity flag for the Extended Community type.
+     * @param type Extended Community Type
+     * @param isTransitive Extended Community transitivity
+     * @return Extended Community type with a transitivity flag set if isTransitive false, otherwise returns unchanged type.
+     */
+    public static int setTransitivity(final int type, final boolean isTransitive) {
+        return isTransitive ? type : ExtendedCommunityUtil.toNonTransitiveType(type);
+    }
+
+    /**
+     * Check the Extended Community type for transitivity.
+     * @param type Extended Community Type
+     * @return True if input type is transitive, false if the type is non-transitive
+     */
+    public static boolean isTransitive(final int type) {
+        return (type & NON_TRANS) == 0 ? true : false;
+    }
+
+    private static int toNonTransitiveType(final int type) {
+        return type | NON_TRANS;
+    }
+
+}
index e134af00072167713c5dce26127fcd9395825252..8da7008bdc6dbd7e01d895af76c1b8d1c6ee6da0 100644 (file)
@@ -17,6 +17,7 @@ class SimpleBGPExtensionConsumerContext implements BGPExtensionConsumerContext {
     private final SimpleSubsequentAddressFamilyRegistry safiReg = new SimpleSubsequentAddressFamilyRegistry();
     private final SimpleParameterRegistry paramReg = new SimpleParameterRegistry();
     private final SimpleNlriRegistry nlriReg = new SimpleNlriRegistry(this.afiReg, this.safiReg);
+    private final SimpleExtendedCommunityRegistry ecReg = new SimpleExtendedCommunityRegistry();
 
     @Override
     public final SimpleAddressFamilyRegistry getAddressFamilyRegistry() {
@@ -52,4 +53,9 @@ class SimpleBGPExtensionConsumerContext implements BGPExtensionConsumerContext {
     public final SimpleSubsequentAddressFamilyRegistry getSubsequentAddressFamilyRegistry() {
         return this.safiReg;
     }
+
+    @Override
+    public SimpleExtendedCommunityRegistry getExtendedCommunityReistry() {
+        return this.ecReg;
+    }
 }
index c973b14c8626aa3779f7266a5aa52af05eba7a9f..d1d503cc38c4305e004e7af024c8bb2bbb1e7543 100644 (file)
@@ -23,10 +23,13 @@ import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
 import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
 import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
+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.ReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
+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.next.hop.CNextHop;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.Notification;
@@ -105,7 +108,7 @@ public class SimpleBGPExtensionProviderContext extends SimpleBGPExtensionConsume
      */
     @Deprecated
     @Override
-    public AutoCloseable registerNlriParser(Class<? extends AddressFamily> afi, Class<? extends SubsequentAddressFamily> safi, NlriParser parser) {
+    public AutoCloseable registerNlriParser(final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi, final NlriParser parser) {
         return this.getNlriRegistry().registerNlriParser(afi, safi, parser, null, null);
     }
 
@@ -148,8 +151,19 @@ public class SimpleBGPExtensionProviderContext extends SimpleBGPExtensionConsume
     public final synchronized void setMaximumCachedObjects(final int maximumCachedObjects) {
         Preconditions.checkArgument(maximumCachedObjects >= 0);
 
-        Cache<Object, Object> newCache = CacheBuilder.newBuilder().maximumSize(maximumCachedObjects).build();
+        final Cache<Object, Object> newCache = CacheBuilder.newBuilder().maximumSize(maximumCachedObjects).build();
         newCache.putAll(this.cacheRef.get().asMap());
         this.cacheRef.set(newCache);
     }
+
+    @Override
+    public AutoCloseable registerExtendedCommunitySerializer(final Class<? extends ExtendedCommunity> extendedCommunityClass,
+            final ExtendedCommunitySerializer serializer) {
+        return this.getExtendedCommunityReistry().registerExtendedCommunitySerializer(extendedCommunityClass, serializer);
+    }
+
+    @Override
+    public AutoCloseable registerExtendedCommunityParser(final int type, final int subtype, final ExtendedCommunityParser parser) {
+        return this.getExtendedCommunityReistry().registerExtendedCommunityParser(type, subtype, parser);
+    }
 }
diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleExtendedCommunityRegistry.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleExtendedCommunityRegistry.java
new file mode 100644 (file)
index 0000000..7b144f9
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2015 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.spi.pojo;
+
+import static org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityUtil.isTransitive;
+import static org.opendaylight.protocol.util.Values.UNSIGNED_BYTE_MAX_VALUE;
+
+import com.google.common.base.Preconditions;
+import com.google.common.primitives.Shorts;
+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.ExtendedCommunityRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
+import org.opendaylight.protocol.concepts.HandlerRegistry;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
+import org.opendaylight.yangtools.yang.binding.DataContainer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class SimpleExtendedCommunityRegistry implements ExtendedCommunityRegistry {
+
+    private static final Logger LOG = LoggerFactory.getLogger(SimpleExtendedCommunityRegistry.class);
+
+    private static final int EXTENDED_COMMUNITY_LENGTH = 6;
+
+    private final HandlerRegistry<DataContainer, ExtendedCommunityParser, ExtendedCommunitySerializer> handlers = new HandlerRegistry<>();
+
+    private static int createKey(final int type, final int subtype) {
+        return (type << UNSIGNED_BYTE_MAX_VALUE) | subtype;
+    }
+
+    @Override
+    public ExtendedCommunities parseExtendedCommunity(final ByteBuf buffer)
+            throws BGPDocumentedException, BGPParsingException {
+        final short type = buffer.readUnsignedByte();
+        final short subtype = buffer.readUnsignedByte();
+        final ExtendedCommunityParser parser = this.handlers.getParser(createKey(type, subtype));
+        if (parser == null) {
+            buffer.skipBytes(EXTENDED_COMMUNITY_LENGTH);
+            LOG.info("Skipping unknown extended-community type/sub-type {}/{}.", type, subtype);
+            return null;
+        }
+        return new ExtendedCommunitiesBuilder()
+            .setTransitive(isTransitive(type))
+            .setExtendedCommunity(parser.parseExtendedCommunity(buffer))
+            .build();
+    }
+
+    @Override
+    public void serializeExtendedCommunity(final ExtendedCommunities extendedCommunity, final ByteBuf byteAggregator) {
+        final ExtendedCommunitySerializer serializer = this.handlers.getSerializer(extendedCommunity.getExtendedCommunity().getImplementedInterface());
+        if (serializer == null) {
+            return;
+        }
+        ByteBufWriteUtil.writeUnsignedByte(Shorts.checkedCast(serializer.getType(extendedCommunity.isTransitive())), byteAggregator);
+        ByteBufWriteUtil.writeUnsignedByte(Shorts.checkedCast(serializer.getSubType()), byteAggregator);
+        serializer.serializeExtendedCommunity(extendedCommunity.getExtendedCommunity(), byteAggregator);
+    }
+
+    synchronized AutoCloseable registerExtendedCommunitySerializer(
+            final Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity> extendedCommunityClass,
+            final ExtendedCommunitySerializer serializer) {
+        return handlers.registerSerializer(extendedCommunityClass, serializer);
+    }
+
+    synchronized AutoCloseable registerExtendedCommunityParser(final int type, final int subtype, final ExtendedCommunityParser parser) {
+        checkTypes(type, subtype);
+        return handlers.registerParser(createKey(type, subtype), parser);
+    }
+
+    private static void checkTypes(final int type, final int subtype) {
+        Preconditions.checkArgument(type >= 0 && type <= UNSIGNED_BYTE_MAX_VALUE, "Illegal extended-community type %s",
+                type);
+        Preconditions.checkArgument(subtype >= 0 && subtype <= UNSIGNED_BYTE_MAX_VALUE, "Illegal extended-community sub-type %s", subtype);
+    }
+
+}
diff --git a/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractIpv4ExtendedCommunityTest.java b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractIpv4ExtendedCommunityTest.java
new file mode 100644 (file)
index 0000000..55f15f2
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2015 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.spi.extended.community;
+
+import io.netty.buffer.ByteBuf;
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
+
+public class AbstractIpv4ExtendedCommunityTest {
+
+    @Test
+    public void testGetType() {
+        final AbstractIpv4ExtendedCommunity abstractIpv4ExtendedCommunity = new AbstractIpv4ExtendedCommunity() {
+            @Override
+            public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+            }
+            @Override
+            public int getSubType() {
+                return 0;
+            }
+            @Override
+            public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+                return null;
+            }
+        };
+        Assert.assertEquals(1, abstractIpv4ExtendedCommunity.getType(true));
+        Assert.assertEquals(65, abstractIpv4ExtendedCommunity.getType(false));
+    }
+
+}
diff --git a/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractOpaqueExtendedCommunityTest.java b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractOpaqueExtendedCommunityTest.java
new file mode 100644 (file)
index 0000000..dd88a03
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2015 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.spi.extended.community;
+
+import io.netty.buffer.ByteBuf;
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
+
+public class AbstractOpaqueExtendedCommunityTest {
+
+    @Test
+    public void testGetType() {
+        final AbstractOpaqueExtendedCommunity abstractOpaqueExtendedCommunity = new AbstractOpaqueExtendedCommunity() {
+
+            @Override
+            public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+            }
+            @Override
+            public int getSubType() {
+                return 0;
+            }
+
+            @Override
+            public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+                return null;
+            }
+        };
+        Assert.assertEquals(3, abstractOpaqueExtendedCommunity.getType(true));
+        Assert.assertEquals(67, abstractOpaqueExtendedCommunity.getType(false));
+    }
+
+}
diff --git a/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractTwoOctetAsExtendedCommunityTest.java b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/AbstractTwoOctetAsExtendedCommunityTest.java
new file mode 100644 (file)
index 0000000..beb2145
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2015 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.spi.extended.community;
+
+import io.netty.buffer.ByteBuf;
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
+
+public class AbstractTwoOctetAsExtendedCommunityTest {
+
+    @Test
+    public void testGetType() {
+        final AbstractTwoOctetAsExtendedCommunity abstractTwoOctetAsExtendedCommunity = new AbstractTwoOctetAsExtendedCommunity() {
+            @Override
+            public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
+            }
+            @Override
+            public int getSubType() {
+                return 0;
+            }
+            @Override
+            public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+                return null;
+            }
+        };
+        Assert.assertEquals(0, abstractTwoOctetAsExtendedCommunity.getType(true));
+        Assert.assertEquals(64, abstractTwoOctetAsExtendedCommunity.getType(false));
+    }
+
+}
diff --git a/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityUtilTest.java b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityUtilTest.java
new file mode 100644 (file)
index 0000000..1620c2d
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2015 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.spi.extended.community;
+
+import static org.junit.Assert.assertEquals;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ExtendedCommunityUtilTest {
+
+    @Test(expected=UnsupportedOperationException.class)
+    public void testPrivateConstructor() throws Throwable {
+        final Constructor<ExtendedCommunityUtil> c = ExtendedCommunityUtil.class.getDeclaredConstructor();
+        c.setAccessible(true);
+        try {
+            c.newInstance();
+        } catch (final InvocationTargetException e) {
+            throw e.getCause();
+        }
+    }
+
+    @Test
+    public void testGetType() {
+        assertEquals(1, ExtendedCommunityUtil.setTransitivity(1, true));
+        assertEquals(65, ExtendedCommunityUtil.setTransitivity(1, false));
+    }
+
+    @Test
+    public void testIsTransitiveType() {
+        Assert.assertTrue(ExtendedCommunityUtil.isTransitive(2));
+        Assert.assertFalse(ExtendedCommunityUtil.isTransitive(66));
+    }
+
+}
index b8009197b936cf9871b6ba4abf71152dcf9a7650..39aa04e1a589bce7eb9e22467510a55cf01c0471 100644 (file)
@@ -30,6 +30,8 @@ import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
 import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
 import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
+import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
+import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
@@ -39,6 +41,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mult
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
 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.RouteTargetIpv4Case;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
@@ -77,6 +80,11 @@ public class BgpTestActivator extends AbstractBGPExtensionProviderActivator {
     @Mock
     protected NlriSerializer nlriSerializer;
 
+    @Mock
+    protected ExtendedCommunityParser exParser;
+    @Mock
+    protected ExtendedCommunitySerializer exSerializer;
+
     protected NextHopParserSerializer nextHopParserSerializer;
 
     @Override
@@ -119,6 +127,9 @@ public class BgpTestActivator extends AbstractBGPExtensionProviderActivator {
             .nlriParser, this.nextHopParserSerializer, Ipv6NextHopCase.class));
         regs.add(context.registerNlriSerializer(DataObject.class, this.nlriSerializer));
 
+        regs.add(context.registerExtendedCommunityParser(0, 0, this.exParser));
+        regs.add(context.registerExtendedCommunitySerializer(RouteTargetIpv4Case.class, this.exSerializer));
+
         return regs;
     }
 
diff --git a/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleExtendedCommunityRegistryTest.java b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleExtendedCommunityRegistryTest.java
new file mode 100644 (file)
index 0000000..bd7939f
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2015 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.spi.pojo;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
+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.RouteOriginIpv4CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetIpv4Case;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.ipv4._case.RouteTargetIpv4Builder;
+
+public class SimpleExtendedCommunityRegistryTest {
+
+    private SimpleExtendedCommunityRegistry register;
+
+    private final ExtendedCommunityParser parser = Mockito.mock(ExtendedCommunityParser.class);
+    private final ExtendedCommunitySerializer serializer = Mockito.mock(ExtendedCommunitySerializer.class);
+
+    @Before
+    public void setup() throws BGPDocumentedException, BGPParsingException {
+        register = new SimpleExtendedCommunityRegistry();
+        register.registerExtendedCommunityParser(0, 0, parser);
+        register.registerExtendedCommunitySerializer(RouteTargetIpv4Case.class, serializer);
+        Mockito.doReturn(0).when(serializer).getType(Mockito.anyBoolean());
+        Mockito.doReturn(0).when(serializer).getSubType();
+        Mockito.doNothing().when(serializer).serializeExtendedCommunity(Mockito.any(ExtendedCommunity.class), Mockito.any(ByteBuf.class));
+        Mockito.doReturn(null).when(parser).parseExtendedCommunity(Mockito.any(ByteBuf.class));
+
+    }
+
+    @Test
+    public void testExtendedCommunityRegistry() throws BGPDocumentedException, BGPParsingException {
+        final ByteBuf output = Unpooled.buffer();
+        register.serializeExtendedCommunity(
+                new ExtendedCommunitiesBuilder().setTransitive(true).setExtendedCommunity(new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(new RouteTargetIpv4Builder().build()).build()).build(), output);
+        Mockito.verify(serializer).serializeExtendedCommunity(Mockito.any(ExtendedCommunity.class), Mockito.any(ByteBuf.class));
+        //no value serialized, just header
+        Assert.assertEquals(2, output.readableBytes());
+
+        final ExtendedCommunities parsedExtendedCommunity = register.parseExtendedCommunity(Unpooled.copiedBuffer(new byte[] {0, 0, 0, 0, 0, 0, 0, 0}));
+        Mockito.verify(parser).parseExtendedCommunity(Mockito.any(ByteBuf.class));
+        Assert.assertTrue(parsedExtendedCommunity.isTransitive());
+        //no value parser
+        Assert.assertNull(parsedExtendedCommunity.getExtendedCommunity());
+    }
+
+    @Test
+    public void testExtendedCommunityRegistryUnknown() throws BGPDocumentedException, BGPParsingException {
+        final ByteBuf output = Unpooled.buffer();
+        register.serializeExtendedCommunity(
+                new ExtendedCommunitiesBuilder().setTransitive(false).setExtendedCommunity(new RouteOriginIpv4CaseBuilder().build()).build(), output);
+        //no ex. community was serialized
+        Assert.assertEquals(0, output.readableBytes());
+        Mockito.verify(serializer, Mockito.never()).serializeExtendedCommunity(Mockito.any(ExtendedCommunity.class), Mockito.any(ByteBuf.class));
+
+        final ExtendedCommunities noExtCommunity = register.parseExtendedCommunity(Unpooled.copiedBuffer(new byte[] {0, 1, 0, 0, 0, 0, 0, 0}));
+        //no ext. community was parsed
+        Assert.assertNull(noExtCommunity);
+        Mockito.verify(parser, Mockito.never()).parseExtendedCommunity(Mockito.any(ByteBuf.class));
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testRegisterParserOutOfRangeType() {
+        register.registerExtendedCommunityParser(1234, 0, parser);
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testRegisterParserOutOfRangeSubType() {
+        register.registerExtendedCommunityParser(0, 1234, parser);
+    }
+
+}