7aaf737f341a52b1a83f4ec698b594728fb56420
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / OriginatorIdAttributeParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
13 import org.opendaylight.protocol.bgp.parser.BGPError;
14 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
15 import org.opendaylight.protocol.bgp.parser.spi.AbstractAttributeParser;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
18 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
19 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
20 import org.opendaylight.protocol.util.Ipv4Util;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OriginatorId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginatorIdBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public final class OriginatorIdAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
30     private static final Logger LOG = LoggerFactory.getLogger(OriginatorIdAttributeParser.class);
31
32     public static final int TYPE = 9;
33
34     @Override
35     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
36             final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint)
37                     throws BGPDocumentedException, BGPTreatAsWithdrawException {
38         if (errorHandling == RevisedErrorHandling.EXTERNAL) {
39             // RFC7606 section 7.9
40             LOG.debug("Discarded ORIGINATOR_ID attribute from external peer");
41             return;
42         }
43
44         final int readable = buffer.readableBytes();
45         if (readable != Ipv4Util.IP4_LENGTH) {
46             throw errorHandling.reportError(BGPError.ATTR_LENGTH_ERROR,
47                 "Length of byte array for ORIGINATOR_ID should be 4, but is %s", readable);
48         }
49         builder.setOriginatorId(new OriginatorIdBuilder().setOriginator(Ipv4Util.addressForByteBuf(buffer)).build());
50     }
51
52     @Override
53     public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
54         final OriginatorId originator = attribute.getOriginatorId();
55         if (originator != null) {
56             final Ipv4Address address = originator.getOriginator();
57             if (address != null) {
58                 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE,
59                     Unpooled.wrappedBuffer(Ipv4Util.bytesForAddress(address)), byteAggregator);
60             }
61         }
62     }
63 }