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 padding TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 27 | */ |
| 28 | public class PaddingTlv extends TlvHeader implements IsisTlv { |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 29 | private List<Byte> paddings = new ArrayList<>(); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 30 | |
| 31 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 32 | * Creates an instance of padding TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 33 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 34 | * @param tlvHeader TLV header |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 35 | */ |
| 36 | public PaddingTlv(TlvHeader tlvHeader) { |
| 37 | this.setTlvType(tlvHeader.tlvType()); |
| 38 | this.setTlvLength(tlvHeader.tlvLength()); |
| 39 | } |
| 40 | |
| 41 | @Override |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 42 | public void readFrom(ChannelBuffer channelBuffer) { |
| 43 | while (channelBuffer.readableBytes() > 0) { |
| 44 | this.paddings.add(channelBuffer.readByte()); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public byte[] asBytes() { |
| 50 | byte[] bytes = null; |
| 51 | |
| 52 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 53 | byte[] tlvBody = tlvBodyAsBytes(); |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 54 | tlvHeader[1] = (byte) tlvBody.length; |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 55 | bytes = Bytes.concat(tlvHeader, tlvBody); |
| 56 | |
| 57 | return bytes; |
| 58 | } |
| 59 | |
| 60 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 61 | * Returns TLV body of padding TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 62 | * |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 63 | * @return byteArray TLV body of padding TLV |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 64 | */ |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 65 | private byte[] tlvBodyAsBytes() { |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 66 | byte[] areaArea = new byte[this.tlvLength()]; |
| 67 | return areaArea; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String toString() { |
| 72 | return MoreObjects.toStringHelper(getClass()) |
| 73 | .omitNullValues() |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 74 | .add("paddings", paddings) |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 75 | .toString(); |
| 76 | } |
| 77 | } |