mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.isis.io.isispacket.tlv; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.primitives.Bytes; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 20 | import org.jboss.netty.buffer.ChannelBuffer; |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.List; |
| 24 | |
| 25 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 26 | * Representation of TLV header. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 27 | */ |
| 28 | public class TlvHeader implements IsisTlv { |
| 29 | private int tlvType; |
| 30 | private int tlvLength; |
| 31 | |
| 32 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 33 | * Returns TLV length of TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 34 | * |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 35 | * @return TLV length |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 36 | */ |
| 37 | public int tlvLength() { |
| 38 | return tlvLength; |
| 39 | } |
| 40 | |
| 41 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 42 | * Sets TLV length for TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 43 | * |
| 44 | * @param tlvLength TLV length |
| 45 | */ |
| 46 | public void setTlvLength(int tlvLength) { |
| 47 | this.tlvLength = tlvLength; |
| 48 | } |
| 49 | |
| 50 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 51 | * Returns TLV type of TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 52 | * |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 53 | * @return TLV type |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 54 | */ |
| 55 | public int tlvType() { |
| 56 | return tlvType; |
| 57 | } |
| 58 | |
| 59 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 60 | * Sets TLV type for TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 61 | * |
| 62 | * @param tlvType TLV type |
| 63 | */ |
| 64 | public void setTlvType(int tlvType) { |
| 65 | this.tlvType = tlvType; |
| 66 | } |
| 67 | |
| 68 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 69 | * Sets TLV values from channel buffer. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 70 | * |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 71 | * @param channelBuffer channel Buffer instance |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 72 | */ |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 73 | public void readFrom(ChannelBuffer channelBuffer) { |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 74 | //implemented in sub classes |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 79 | * Returns TLV as byte array. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 80 | * |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 81 | * @return byteArray TLV body of area address TLV |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 82 | */ |
| 83 | public byte[] asBytes() { |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 84 | return null; |
| 85 | } |
| 86 | |
| 87 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 88 | * Returns TLV header of TLV as bytes. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 89 | * |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 90 | * @return TLV header as bytes |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 91 | */ |
| 92 | public byte[] tlvHeaderAsByteArray() { |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 93 | List<Byte> headerLst = new ArrayList<>(); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 94 | 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 | } |