BUG-608: parsers/serializers for SID Label Binding TLV and sub-tlvs
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / spi / TlvUtil.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.linkstate.spi;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.ByteBufUtil;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 public final class TlvUtil {
16
17     private TlvUtil() {
18         throw new UnsupportedOperationException();
19     }
20
21     private static final Logger LOG = LoggerFactory.getLogger(TlvUtil.class);
22
23     public static final int TOPOLOGY_ID_OFFSET = 0x3fff;
24
25     public static final int MULTI_TOPOLOGY_ID = 263;
26     public static final int LOCAL_IPV4_ROUTER_ID = 1028;
27     public static final int LOCAL_IPV6_ROUTER_ID = 1029;
28
29     /**
30      * Util method for writing TLV header.
31      * @param type TLV type (2B)
32      * @param value TLV value (2B)
33      * @param byteAggregator final ByteBuf where the tlv should be serialized
34      */
35     public static void writeTLV(final int type, final ByteBuf value, final ByteBuf byteAggregator){
36         byteAggregator.writeShort(type);
37         byteAggregator.writeShort(value.writerIndex());
38         byteAggregator.writeBytes(value);
39         value.readerIndex(0);
40         if (LOG.isDebugEnabled()) {
41             LOG.debug("Serialized tlv type {} to: {}", type, ByteBufUtil.hexDump(value));
42         }
43     }
44
45     /**
46      * Util method for writing Segment routing TLV header.
47      * @param type TLV type (1B)
48      * @param value TLV value (1B)
49      * @param byteAggregator final ByteBuf where the tlv should be serialized
50      */
51     public static void writeSrTLV(final int type, final ByteBuf value, final ByteBuf byteAggregator){
52         byteAggregator.writeByte(type);
53         byteAggregator.writeByte(value.writerIndex());
54         byteAggregator.writeBytes(value);
55         value.readerIndex(0);
56         if (LOG.isDebugEnabled()) {
57             LOG.debug("Serialized tlv type {} to: {}", type, ByteBufUtil.hexDump(value));
58         }
59     }
60 }