2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
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;
29 public final class OriginatorIdAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
30 private static final Logger LOG = LoggerFactory.getLogger(OriginatorIdAttributeParser.class);
32 public static final int TYPE = 9;
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");
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);
49 builder.setOriginatorId(new OriginatorIdBuilder().setOriginator(Ipv4Util.addressForByteBuf(buffer)).build());
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);