YANG revision dates mass-update
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / OriginAttributeParser.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 com.google.common.primitives.UnsignedBytes;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
14 import org.opendaylight.protocol.bgp.parser.BGPError;
15 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
16 import org.opendaylight.protocol.bgp.parser.spi.AbstractAttributeParser;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
19 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
20 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.Origin;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.OriginBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.BgpOrigin;
26
27 public final class OriginAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
28
29     public static final int TYPE = 1;
30
31     private static final Origin IGP = new OriginBuilder().setValue(BgpOrigin.Igp).build();
32     private static final Origin EGP = new OriginBuilder().setValue(BgpOrigin.Egp).build();
33     private static final Origin INC = new OriginBuilder().setValue(BgpOrigin.Incomplete).build();
34
35     @Override
36     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
37             final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint)
38                     throws BGPDocumentedException, BGPTreatAsWithdrawException {
39         final int readable = buffer.readableBytes();
40         if (readable != 1) {
41             throw errorHandling.reportError(BGPError.ATTR_LENGTH_ERROR,
42                 "ORIGIN attribute is expected to have size 1, but has %s", readable);
43         }
44
45         final byte rawOrigin = buffer.readByte();
46         final BgpOrigin borigin = BgpOrigin.forValue(UnsignedBytes.toInt(rawOrigin));
47         if (borigin == null) {
48             throw errorHandling.reportError(BGPError.ORIGIN_ATTR_NOT_VALID, "Unknown ORIGIN type %s", rawOrigin);
49         }
50         switch (borigin) {
51             case Egp:
52                 builder.setOrigin(EGP);
53                 return;
54             case Igp:
55                 builder.setOrigin(IGP);
56                 return;
57             case Incomplete:
58                 builder.setOrigin(INC);
59                 return;
60             default:
61         }
62     }
63
64     @Override
65     public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
66         final Origin origin = attribute.getOrigin();
67         if (origin != null) {
68             AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE,
69                 Unpooled.wrappedBuffer(new byte[]{ UnsignedBytes.checkedCast(origin.getValue().getIntValue()) }),
70                 byteAggregator);
71         }
72     }
73 }