blob: 2ff97c353239756193251bdf2ade8432fc198f47 [file] [log] [blame]
mohamed rahile04626f2016-04-05 20:42:53 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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 padding TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053027 */
28public class PaddingTlv extends TlvHeader implements IsisTlv {
sunishvka1dfc3e2016-04-16 12:24:47 +053029 private List<Byte> paddings = new ArrayList<>();
mohamed rahile04626f2016-04-05 20:42:53 +053030
31 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053032 * Creates an instance of padding TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053033 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053034 * @param tlvHeader TLV header
mohamed rahile04626f2016-04-05 20:42:53 +053035 */
36 public PaddingTlv(TlvHeader tlvHeader) {
37 this.setTlvType(tlvHeader.tlvType());
38 this.setTlvLength(tlvHeader.tlvLength());
39 }
40
41 @Override
sunishvka1dfc3e2016-04-16 12:24:47 +053042 public void readFrom(ChannelBuffer channelBuffer) {
43 while (channelBuffer.readableBytes() > 0) {
44 this.paddings.add(channelBuffer.readByte());
mohamed rahile04626f2016-04-05 20:42:53 +053045 }
46 }
47
48 @Override
49 public byte[] asBytes() {
50 byte[] bytes = null;
51
52 byte[] tlvHeader = tlvHeaderAsByteArray();
53 byte[] tlvBody = tlvBodyAsBytes();
sunishvka1dfc3e2016-04-16 12:24:47 +053054 tlvHeader[1] = (byte) tlvBody.length;
mohamed rahile04626f2016-04-05 20:42:53 +053055 bytes = Bytes.concat(tlvHeader, tlvBody);
56
57 return bytes;
58 }
59
60 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053061 * Returns TLV body of padding TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053062 *
sunishvka1dfc3e2016-04-16 12:24:47 +053063 * @return byteArray TLV body of padding TLV
mohamed rahile04626f2016-04-05 20:42:53 +053064 */
sunishvka1dfc3e2016-04-16 12:24:47 +053065 private byte[] tlvBodyAsBytes() {
mohamed rahile04626f2016-04-05 20:42:53 +053066 byte[] areaArea = new byte[this.tlvLength()];
67 return areaArea;
68 }
69
70 @Override
71 public String toString() {
72 return MoreObjects.toStringHelper(getClass())
73 .omitNullValues()
sunishvka1dfc3e2016-04-16 12:24:47 +053074 .add("paddings", paddings)
mohamed rahile04626f2016-04-05 20:42:53 +053075 .toString();
76 }
77}