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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.LocalPref;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.LocalPrefBuilder;
24 import org.opendaylight.yangtools.yang.common.Uint32;
25 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public final class LocalPreferenceAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
30 private static final Logger LOG = LoggerFactory.getLogger(LocalPreferenceAttributeParser.class);
32 public static final int TYPE = 5;
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.7
40 LOG.debug("Discarded LOCAL_PREF attribute from external peer");
44 final int readable = buffer.readableBytes();
46 throw errorHandling.reportError(BGPError.ATTR_LENGTH_ERROR, "Expected 4 bytes, have %s", readable);
49 builder.setLocalPref(new LocalPrefBuilder().setPref(ByteBufUtils.readUint32(buffer)).build());
53 public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
54 final LocalPref lp = attribute.getLocalPref();
56 final Uint32 pref = lp.getPref();
58 AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, Unpooled.copyInt(pref.intValue()),