Fix the decoding opf BGP AGGREGATOR Attribute when 4 octets AS is enabled
This fixes ONOS-1318

Change-Id: I5681b574c46626e4092439e28d28831499e9562b
diff --git a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpConstants.java b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpConstants.java
index 96d74e2..05f9866 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpConstants.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpConstants.java
@@ -157,6 +157,12 @@
         private Update() {
         }
 
+        /** BGP AS length. */
+        public static final int AS_LENGTH = 2;
+
+        /** BGP 4 Octet AS length (RFC 6793). */
+        public static final int AS_4OCTET_LENGTH = 4;
+
         /**
          * BGP UPDATE: ORIGIN related constants.
          */
@@ -227,12 +233,6 @@
             /** BGP UPDATE Attributes Type Code AS_PATH. */
             public static final int TYPE = 2;
 
-            /** BGP AS length. */
-            public static final int AS_LENGTH = 2;
-
-            /** BGP 4 Octet AS length (RFC 6793). */
-            public static final int AS_4OCTET_LENGTH = 4;
-
             /** BGP UPDATE AS_PATH Type: AS_SET. */
             public static final int AS_SET = 1;
 
@@ -373,8 +373,11 @@
             /** BGP UPDATE Attributes Type Code AGGREGATOR. */
             public static final int TYPE = 7;
 
-            /** BGP UPDATE Attributes Type Code AGGREGATOR length. */
-            public static final int LENGTH = 6;
+            /** BGP UPDATE Attributes Type Code AGGREGATOR length: 2 octet AS. */
+            public static final int AS2_LENGTH = 6;
+
+            /** BGP UPDATE Attributes Type Code AGGREGATOR length: 4 octet AS. */
+            public static final int AS4_LENGTH = 8;
         }
 
         /**
diff --git a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpUpdate.java b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpUpdate.java
index e9abd9c..d05e9ac 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpUpdate.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpUpdate.java
@@ -758,9 +758,9 @@
             // 4-octet AS number handling.
             int asPathLen;
             if (bgpSession.isAs4OctetCapable()) {
-                asPathLen = BgpConstants.Update.AsPath.AS_4OCTET_LENGTH;
+                asPathLen = BgpConstants.Update.AS_4OCTET_LENGTH;
             } else {
-                asPathLen = BgpConstants.Update.AsPath.AS_LENGTH;
+                asPathLen = BgpConstants.Update.AS_LENGTH;
             }
 
             // Parse the AS numbers
@@ -774,7 +774,7 @@
             ArrayList<Long> segmentAsNumbers = new ArrayList<>();
             while (pathSegmentLength-- > 0) {
                 long asNumber;
-                if (asPathLen == BgpConstants.Update.AsPath.AS_4OCTET_LENGTH) {
+                if (asPathLen == BgpConstants.Update.AS_4OCTET_LENGTH) {
                     asNumber = message.readUnsignedInt();
                 } else {
                     asNumber = message.readUnsignedShort();
@@ -967,9 +967,16 @@
                                                 int attrFlags,
                                                 ChannelBuffer message)
         throws BgpMessage.BgpParseException {
+        int expectedAttrLen;
+
+        if (bgpSession.isAs4OctetCapable()) {
+            expectedAttrLen = BgpConstants.Update.Aggregator.AS4_LENGTH;
+        } else {
+            expectedAttrLen = BgpConstants.Update.Aggregator.AS2_LENGTH;
+        }
 
         // Check the Attribute Length
-        if (attrLen != BgpConstants.Update.Aggregator.LENGTH) {
+        if (attrLen != expectedAttrLen) {
             // ERROR: Attribute Length Error
             actionsBgpUpdateAttributeLengthError(
                 bgpSession, ctx, attrTypeCode, attrLen, attrFlags, message);
@@ -978,7 +985,12 @@
         }
 
         // The AGGREGATOR AS number
-        long aggregatorAsNumber = message.readUnsignedShort();
+        long aggregatorAsNumber;
+        if (bgpSession.isAs4OctetCapable()) {
+            aggregatorAsNumber = message.readUnsignedInt();
+        } else {
+            aggregatorAsNumber = message.readUnsignedShort();
+        }
         // The AGGREGATOR IP address
         Ip4Address aggregatorIpAddress =
             Ip4Address.valueOf((int) message.readUnsignedInt());