Bug 611 - serialization of path attributes
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / OriginatorIdAttributeParser.java
index d031ce6aa44cf8029b5a52015281e45e6d9d73df..b6318a993d8a5406a8becb12f4a3d4a7fa4d4c59 100644 (file)
@@ -7,18 +7,44 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import com.google.common.base.Preconditions;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+
+import org.opendaylight.protocol.bgp.parser.AttributeFlags;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributesBuilder;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
+import org.opendaylight.protocol.concepts.Ipv4Util;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+
+
+public final class OriginatorIdAttributeParser implements AttributeParser,AttributeSerializer {
+
+    public static final int TYPE = 9;
+    public static final int ATTR_LENGTH = 4;
 
-public final class OriginatorIdAttributeParser implements AttributeParser {
-       public static final int TYPE = 9;
+    @Override
+    public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
+        Preconditions.checkArgument(buffer.readableBytes() == ATTR_LENGTH, "Length of byte array for ORIGINATOR_ID should be %s, but is %s", ATTR_LENGTH, buffer.readableBytes());
+        builder.setOriginatorId(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, ATTR_LENGTH)));
+    }
 
-       @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) {
-               if (bytes.length != 4) {
-                       throw new IllegalArgumentException("Length of byte array for ORIGINATOR_ID should be 4, but is " + bytes.length);
-               }
+    @Override
+    public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
+        PathAttributes pathAttributes = (PathAttributes) attribute;
+        if (pathAttributes.getOriginatorId() == null) {
+            return;
+        }
+        ByteBuf originatorIdBuf = Unpooled.buffer();
+        originatorIdBuf.writeBytes(Ipv4Util.bytesForAddress(pathAttributes.getOriginatorId()));
+        byteAggregator.writeByte(AttributeFlags.OPTIONAL);
+        byteAggregator.writeByte(TYPE);
+        byteAggregator.writeByte(originatorIdBuf.writerIndex());
+        byteAggregator.writeBytes(originatorIdBuf);
 
-               builder.setOriginatorId(bytes);
-       }
-}
\ No newline at end of file
+    }
+}