Code clean up
[bgpcep.git] / bgp / labeled-unicast / src / main / java / org / opendaylight / protocol / bgp / labeled / unicast / LabelIndexTlvParser.java
1 /*
2  * Copyright (c) 2016 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.labeled.unicast;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvParser;
13 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvSerializer;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.bgp.prefix.sid.tlv.LuLabelIndexTlv;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.bgp.prefix.sid.tlv.LuLabelIndexTlvBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv;
17
18 final class LabelIndexTlvParser implements BgpPrefixSidTlvParser, BgpPrefixSidTlvSerializer {
19
20     private static final int LABEL_INDEX_TYPE = 1;
21     private static final int RESERVED = 1;
22     private static final int LABEL_INDEX_FLAGS_BYTES = 2;
23
24     @Override
25     public void serializeBgpPrefixSidTlv(final BgpPrefixSidTlv tlv, final ByteBuf valueBuf) {
26         Preconditions.checkArgument(tlv instanceof LuLabelIndexTlv, "Incoming TLV is not LuLabelIndexTlv");
27         valueBuf.writeZero(RESERVED);
28         valueBuf.writeZero(LABEL_INDEX_FLAGS_BYTES);
29         valueBuf.writeInt(((LuLabelIndexTlv) tlv).getLabelIndexTlv().intValue());
30     }
31
32     @Override
33     public LuLabelIndexTlv parseBgpPrefixSidTlv(final ByteBuf buffer) {
34         buffer.readBytes(RESERVED);
35         buffer.readBytes(LABEL_INDEX_FLAGS_BYTES);
36         final Long value = buffer.readUnsignedInt();
37         return new LuLabelIndexTlvBuilder().setLabelIndexTlv(value).build();
38     }
39
40     @Override
41     public int getType() {
42         return LABEL_INDEX_TYPE;
43     }
44 }