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 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;
27 public final class OriginAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
29 public static final int TYPE = 1;
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();
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();
41 throw errorHandling.reportError(BGPError.ATTR_LENGTH_ERROR,
42 "ORIGIN attribute is expected to have size 1, but has %s", readable);
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);
52 builder.setOrigin(EGP);
55 builder.setOrigin(IGP);
58 builder.setOrigin(INC);
65 public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
66 final Origin origin = attribute.getOrigin();
68 AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE,
69 Unpooled.wrappedBuffer(new byte[]{ UnsignedBytes.checkedCast(origin.getValue().getIntValue()) }),