YANG revision dates mass-update
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / LocalPreferenceAttributeParser.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.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;
28
29 public final class LocalPreferenceAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
30     private static final Logger LOG = LoggerFactory.getLogger(LocalPreferenceAttributeParser.class);
31
32     public static final int TYPE = 5;
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.7
40             LOG.debug("Discarded LOCAL_PREF attribute from external peer");
41             return;
42         }
43
44         final int readable = buffer.readableBytes();
45         if (readable != 4) {
46             throw errorHandling.reportError(BGPError.ATTR_LENGTH_ERROR, "Expected 4 bytes, have %s", readable);
47         }
48
49         builder.setLocalPref(new LocalPrefBuilder().setPref(ByteBufUtils.readUint32(buffer)).build());
50     }
51
52     @Override
53     public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
54         final LocalPref lp = attribute.getLocalPref();
55         if (lp != null) {
56             final Uint32 pref = lp.getPref();
57             if (pref != null) {
58                 AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, Unpooled.copyInt(pref.intValue()),
59                     byteAggregator);
60             }
61         }
62     }
63 }