Mass-convert all compontents to use -no-zone addresses
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / OriginatorIdAttributeParser.java
index d977a4e9095e69f6537a039ae273d3c7cb49fe3f..e7e76d806b7bb7db8455cb1024fede00371bedda 100644 (file)
@@ -7,39 +7,57 @@
  */
 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.spi.AttributeParser;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPError;
+import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
+import org.opendaylight.protocol.bgp.parser.spi.AbstractAttributeParser;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
+import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
+import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
 import org.opendaylight.protocol.util.Ipv4Util;
-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.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;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OriginatorId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginatorIdBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-
-public final class OriginatorIdAttributeParser implements AttributeParser,AttributeSerializer {
+public final class OriginatorIdAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
+    private static final Logger LOG = LoggerFactory.getLogger(OriginatorIdAttributeParser.class);
 
     public static final int TYPE = 9;
 
     @Override
-    public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
-        Preconditions.checkArgument(buffer.readableBytes() == Ipv4Util.IP4_LENGTH, "Length of byte array for ORIGINATOR_ID should be %s, but is %s", Ipv4Util.IP4_LENGTH, buffer.readableBytes());
-        builder.setOriginatorId(Ipv4Util.addressForByteBuf(buffer));
+    public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
+            final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint)
+                    throws BGPDocumentedException, BGPTreatAsWithdrawException {
+        if (errorHandling == RevisedErrorHandling.EXTERNAL) {
+            // RFC7606 section 7.9
+            LOG.debug("Discarded ORIGINATOR_ID attribute from external peer");
+            return;
+        }
+
+        final int readable = buffer.readableBytes();
+        if (readable != Ipv4Util.IP4_LENGTH) {
+            throw errorHandling.reportError(BGPError.ATTR_LENGTH_ERROR,
+                "Length of byte array for ORIGINATOR_ID should be 4, but is %s", readable);
+        }
+        builder.setOriginatorId(new OriginatorIdBuilder().setOriginator(Ipv4Util.addressForByteBuf(buffer)).build());
     }
 
     @Override
-    public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
-        final Ipv4Address originator = ((PathAttributes) attribute).getOriginatorId();
-        if (originator == null) {
-            return;
+    public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
+        final OriginatorId originator = attribute.getOriginatorId();
+        if (originator != null) {
+            final Ipv4AddressNoZone address = originator.getOriginator();
+            if (address != null) {
+                AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE,
+                    Unpooled.wrappedBuffer(Ipv4Util.bytesForAddress(address)), byteAggregator);
+            }
         }
-        final ByteBuf originatorIdBuf = Unpooled.buffer();
-        originatorIdBuf.writeBytes(Ipv4Util.bytesForAddress(originator));
-        AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, originatorIdBuf, byteAggregator);
     }
 }