d39e03062c66b1b1e402d234055ebf10147fbc3c
[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.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPError;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.Origin;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.OriginBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25
26 public final class OriginAttributeParser implements AttributeParser, AttributeSerializer {
27
28     public static final int TYPE = 1;
29
30     private static final Origin IGP = new OriginBuilder().setValue(BgpOrigin.Igp).build();
31     private static final Origin EGP = new OriginBuilder().setValue(BgpOrigin.Egp).build();
32     private static final Origin INC = new OriginBuilder().setValue(BgpOrigin.Incomplete).build();
33
34     @Override
35     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
36         final byte rawOrigin = buffer.readByte();
37         final BgpOrigin borigin = BgpOrigin.forValue(UnsignedBytes.toInt(rawOrigin));
38         if (borigin == null) {
39             throw new BGPDocumentedException("Unknown Origin type.", BGPError.ORIGIN_ATTR_NOT_VALID, new byte[] { (byte) 0x01, (byte) 0x01, rawOrigin} );
40         }
41         switch (borigin) {
42         case Egp:
43             builder.setOrigin(EGP);
44             return;
45         case Igp:
46             builder.setOrigin(IGP);
47             return;
48         case Incomplete:
49             builder.setOrigin(INC);
50             return;
51         default:
52             return;
53         }
54     }
55
56     @Override
57     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
58         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
59         final Origin origin = ((Attributes) attribute).getOrigin();
60         if (origin == null) {
61             return;
62         }
63         AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, Unpooled.wrappedBuffer(new byte[] {UnsignedBytes.checkedCast(origin.getValue().getIntValue())}), byteAggregator);
64     }
65 }