YANG revision dates mass-update
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / MultiExitDiscriminatorAttributeParser.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.MultiExitDisc;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.MultiExitDiscBuilder;
24 import org.opendaylight.yangtools.yang.common.Uint32;
25 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
26
27 public final class MultiExitDiscriminatorAttributeParser extends AbstractAttributeParser
28         implements AttributeSerializer {
29     public static final int TYPE = 4;
30
31     @Override
32     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
33             final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint)
34                     throws BGPDocumentedException, BGPTreatAsWithdrawException {
35         final int readable = buffer.readableBytes();
36         if (readable != 4) {
37             throw errorHandling.reportError(BGPError.ATTR_LENGTH_ERROR,
38                 "MULTI_EXIT_DISC has to have length 4, but has %s", readable);
39         }
40         builder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(ByteBufUtils.readUint32(buffer)).build());
41     }
42
43     @Override
44     public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
45         final MultiExitDisc multiExitDisc = attribute.getMultiExitDisc();
46         if (multiExitDisc != null) {
47             final Uint32 med = multiExitDisc.getMed();
48             if (med != null) {
49                 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, Unpooled.copyInt(med.intValue()),
50                     byteAggregator);
51             }
52         }
53     }
54 }