BUG-2982 : moved path-attributes container to grouping
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / BGPUpdateMessageParser.java
index ebdf468258c15d02e24229a5e993425d4f916690..8f76c4e01320b93619c01f047f23720722f0975f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * 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,
@@ -12,25 +12,22 @@ import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufUtil;
 import io.netty.buffer.Unpooled;
-import java.util.Arrays;
 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.impl.message.update.ClusterIdAttributeParser;
-import org.opendaylight.protocol.bgp.parser.impl.message.update.OriginatorIdAttributeParser;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
-import org.opendaylight.protocol.concepts.Ipv4Util;
 import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.Ipv4Util;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
+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.update.Nlri;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.NlriBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutesBuilder;
 import org.opendaylight.yangtools.yang.binding.Notification;
@@ -68,7 +65,7 @@ public class BGPUpdateMessageParser implements MessageParser, MessageSerializer
     @Override
     public Update parseMessageBody(final ByteBuf buffer, final int messageLength) throws BGPDocumentedException {
         Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
-        LOG.trace("Started parsing of update message: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
+        LOG.trace("Started parsing of update message: {}", ByteBufUtil.hexDump(buffer));
 
         final int withdrawnRoutesLength = buffer.readUnsignedShort();
         final UpdateBuilder eventBuilder = new UpdateBuilder();
@@ -84,9 +81,8 @@ public class BGPUpdateMessageParser implements MessageParser, MessageSerializer
         }
         if (totalPathAttrLength > 0) {
             try {
-                final PathAttributes pathAttributes = this.reg.parseAttributes(buffer.slice(buffer.readerIndex(), totalPathAttrLength));
-                buffer.skipBytes(totalPathAttrLength);
-                eventBuilder.setPathAttributes(pathAttributes);
+                final Attributes pathAttributes = this.reg.parseAttributes(buffer.readSlice(totalPathAttrLength));
+                eventBuilder.setAttributes(pathAttributes);
             } catch (final BGPParsingException | RuntimeException e) {
                 // Catch everything else and turn it into a BGPDocumentedException
                 LOG.warn("Could not parse BGP attributes", e);
@@ -97,62 +93,44 @@ public class BGPUpdateMessageParser implements MessageParser, MessageSerializer
         if (nlri != null && !nlri.isEmpty()) {
             eventBuilder.setNlri(new NlriBuilder().setNlri(nlri).build());
         }
-        Update msg = eventBuilder.build();
+        final Update msg = eventBuilder.build();
         LOG.debug("BGP Update message was parsed {}.", msg);
         return msg;
     }
 
     @Override
-    public void serializeMessage(Notification message, ByteBuf bytes) {
-        if (message == null) {
-            throw new IllegalArgumentException("BGPUpdate message cannot be null");
-        }
+    public void serializeMessage(final Notification message, final ByteBuf bytes) {
+        Preconditions.checkArgument(message instanceof Update, "BGPUpdate message cannot be null");
         LOG.trace("Started serializing update message: {}", message);
         final Update update = (Update) message;
 
-        ByteBuf messageBody = Unpooled.buffer();
-        WithdrawnRoutes withdrawnRoutes = update.getWithdrawnRoutes();
+        final ByteBuf messageBody = Unpooled.buffer();
+        final WithdrawnRoutes withdrawnRoutes = update.getWithdrawnRoutes();
         if (withdrawnRoutes != null) {
-            ByteBuf withdrawnRoutesBuf = Unpooled.buffer();
-            for (Ipv4Prefix withdrawnRoutePrefix : withdrawnRoutes.getWithdrawnRoutes()) {
-                int prefixBits = Ipv4Util.getPrefixLength(withdrawnRoutePrefix.getValue());
-                byte[] prefixBytes = ByteArray.subByte(Ipv4Util.bytesForPrefix(withdrawnRoutePrefix), 0,
-                        Ipv4Util.getPrefixLengthBytes(withdrawnRoutePrefix.getValue()));
-                withdrawnRoutesBuf.writeByte(prefixBits);
-                withdrawnRoutesBuf.writeBytes(prefixBytes);
+            final ByteBuf withdrawnRoutesBuf = Unpooled.buffer();
+            for (final Ipv4Prefix prefix : withdrawnRoutes.getWithdrawnRoutes()) {
+                withdrawnRoutesBuf.writeBytes(Ipv4Util.bytesForPrefixBegin(prefix));
             }
             messageBody.writeShort(withdrawnRoutesBuf.writerIndex());
             messageBody.writeBytes(withdrawnRoutesBuf);
-
         } else {
-            messageBody.writeZero(2);
+            messageBody.writeZero(WITHDRAWN_ROUTES_LENGTH_SIZE);
         }
-        if (update.getPathAttributes() != null) {
-            ByteBuf pathAttributesBuf = Unpooled.buffer();
-            this.reg.serializeAttribute(update.getPathAttributes(), pathAttributesBuf);
-            ClusterIdAttributeParser clusterIdAttributeParser = new ClusterIdAttributeParser();
-            clusterIdAttributeParser.serializeAttribute(update.getPathAttributes(), pathAttributesBuf);
-
-            OriginatorIdAttributeParser originatorIdAttributeParser = new OriginatorIdAttributeParser();
-            originatorIdAttributeParser.serializeAttribute(update.getPathAttributes(), pathAttributesBuf);
-
+        if (update.getAttributes() != null) {
+            final ByteBuf pathAttributesBuf = Unpooled.buffer();
+            this.reg.serializeAttribute(update.getAttributes(), pathAttributesBuf);
             messageBody.writeShort(pathAttributesBuf.writerIndex());
             messageBody.writeBytes(pathAttributesBuf);
         } else {
-            messageBody.writeZero(2);
+            messageBody.writeZero(TOTAL_PATH_ATTR_LENGTH_SIZE);
         }
-        Nlri nlri = update.getNlri();
+        final Nlri nlri = update.getNlri();
         if (nlri != null) {
-            for (Ipv4Prefix ipv4Prefix : nlri.getNlri()) {
-                int prefixBits = Ipv4Util.getPrefixLength(ipv4Prefix.getValue());
-                byte[] prefixBytes = ByteArray.subByte(Ipv4Util.bytesForPrefix(ipv4Prefix), 0,
-                        Ipv4Util.getPrefixLengthBytes(ipv4Prefix.getValue()));
-                messageBody.writeByte(prefixBits);
-                messageBody.writeBytes(prefixBytes);
+            for (final Ipv4Prefix prefix : nlri.getNlri()) {
+                messageBody.writeBytes(Ipv4Util.bytesForPrefixBegin(prefix));
             }
         }
         LOG.trace("Update message serialized to {}", ByteBufUtil.hexDump(messageBody));
-        //FIXME: switch to ByteBuf
-        bytes.writeBytes(MessageUtil.formatMessage(TYPE, ByteArray.getAllBytes(messageBody)));
+        MessageUtil.formatMessage(TYPE, messageBody, bytes);
     }
 }