blob: 68bda54deed210dd9df5fcf9dc375d1fb3e03507 [file] [log] [blame]
mohamed rahile04626f2016-04-05 20:42:53 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
mohamed rahile04626f2016-04-05 20:42:53 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.isis.io.isispacket.tlv;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.primitives.Bytes;
sunishvka1dfc3e2016-04-16 12:24:47 +053020import org.jboss.netty.buffer.ChannelBuffer;
mohamed rahile04626f2016-04-05 20:42:53 +053021
22import java.util.ArrayList;
23import java.util.List;
24
25/**
sunishvka1dfc3e2016-04-16 12:24:47 +053026 * Representation of TLV header.
mohamed rahile04626f2016-04-05 20:42:53 +053027 */
28public class TlvHeader implements IsisTlv {
29 private int tlvType;
30 private int tlvLength;
31
32 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053033 * Returns TLV length of TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053034 *
sunishvka1dfc3e2016-04-16 12:24:47 +053035 * @return TLV length
mohamed rahile04626f2016-04-05 20:42:53 +053036 */
37 public int tlvLength() {
38 return tlvLength;
39 }
40
41 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053042 * Sets TLV length for TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053043 *
44 * @param tlvLength TLV length
45 */
46 public void setTlvLength(int tlvLength) {
47 this.tlvLength = tlvLength;
48 }
49
50 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053051 * Returns TLV type of TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053052 *
sunishvka1dfc3e2016-04-16 12:24:47 +053053 * @return TLV type
mohamed rahile04626f2016-04-05 20:42:53 +053054 */
55 public int tlvType() {
56 return tlvType;
57 }
58
59 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053060 * Sets TLV type for TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053061 *
62 * @param tlvType TLV type
63 */
64 public void setTlvType(int tlvType) {
65 this.tlvType = tlvType;
66 }
67
68 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053069 * Sets TLV values from channel buffer.
mohamed rahile04626f2016-04-05 20:42:53 +053070 *
sunishvka1dfc3e2016-04-16 12:24:47 +053071 * @param channelBuffer channel Buffer instance
mohamed rahile04626f2016-04-05 20:42:53 +053072 */
sunishvka1dfc3e2016-04-16 12:24:47 +053073 public void readFrom(ChannelBuffer channelBuffer) {
mohamed rahile04626f2016-04-05 20:42:53 +053074 //implemented in sub classes
75 }
76
77
78 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053079 * Returns TLV as byte array.
mohamed rahile04626f2016-04-05 20:42:53 +053080 *
sunishvka1dfc3e2016-04-16 12:24:47 +053081 * @return byteArray TLV body of area address TLV
mohamed rahile04626f2016-04-05 20:42:53 +053082 */
83 public byte[] asBytes() {
mohamed rahile04626f2016-04-05 20:42:53 +053084 return null;
85 }
86
87 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053088 * Returns TLV header of TLV as bytes.
mohamed rahile04626f2016-04-05 20:42:53 +053089 *
sunishvka1dfc3e2016-04-16 12:24:47 +053090 * @return TLV header as bytes
mohamed rahile04626f2016-04-05 20:42:53 +053091 */
92 public byte[] tlvHeaderAsByteArray() {
sunishvka1dfc3e2016-04-16 12:24:47 +053093 List<Byte> headerLst = new ArrayList<>();
mohamed rahile04626f2016-04-05 20:42:53 +053094 headerLst.add((byte) this.tlvType);
95 headerLst.add((byte) this.tlvLength);
96 return Bytes.toArray(headerLst);
97 }
98
99 @Override
100 public String toString() {
101 return MoreObjects.toStringHelper(getClass())
102 .omitNullValues()
103 .add("tlvType", tlvType)
104 .add("tlvLength", tlvLength)
105 .toString();
106 }
107}