Bug 611 - BGP Update message serialization
[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
12 import io.netty.buffer.ByteBuf;
13
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Origin;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.OriginBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24
25 public final class OriginAttributeParser implements AttributeParser, AttributeSerializer {
26
27     public static final int TYPE = 1;
28
29     @Override
30     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
31         byte rawOrigin = buffer.readByte();
32         final BgpOrigin borigin = BgpOrigin.forValue(UnsignedBytes.toInt(rawOrigin));
33         if (borigin == null) {
34             throw new BGPDocumentedException("Unknown Origin type.", BGPError.ORIGIN_ATTR_NOT_VALID, new byte[] { (byte) 0x01, (byte) 0x01, rawOrigin} );
35         }
36         builder.setOrigin(new OriginBuilder().setValue(borigin).build());
37     }
38
39     @Override
40     public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
41         PathAttributes pathAttributes = (PathAttributes) attribute;
42         Origin origin = pathAttributes.getOrigin();
43         if (origin == null) {
44             return;
45         }
46         byteAggregator.writeByte(origin.getValue().getIntValue());
47     }
48 }