blob: ded4ae1d67b9e204fa9bd5eb1520fd8a633cea45 [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
sunishvka1dfc3e2016-04-16 12:24:47 +053018import com.google.common.base.MoreObjects;
mohamed rahile04626f2016-04-05 20:42:53 +053019import 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 protocol supported TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053027 */
28public class ProtocolSupportedTlv extends TlvHeader implements IsisTlv {
29
sunishvka1dfc3e2016-04-16 12:24:47 +053030 private List<Byte> protocolSupported = new ArrayList<>();
mohamed rahile04626f2016-04-05 20:42:53 +053031
32 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053033 * Creates an instance of protocol supported TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053034 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053035 * @param tlvHeader TLV header
mohamed rahile04626f2016-04-05 20:42:53 +053036 */
37 public ProtocolSupportedTlv(TlvHeader tlvHeader) {
38
39 this.setTlvType(tlvHeader.tlvType());
40 this.setTlvLength(tlvHeader.tlvLength());
41
42 }
43
44 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053045 * Adds the protocol supported to protocol supported TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053046 *
sunishvka1dfc3e2016-04-16 12:24:47 +053047 * @param protocolValue protocol supported
48 */
49 public void addProtocolSupported(byte protocolValue) {
50 protocolSupported.add(protocolValue);
51 }
52
53 /**
54 * Returns protocols supported of protocol supported TLV.
55 *
56 * @return protocol supported
mohamed rahile04626f2016-04-05 20:42:53 +053057 */
58 public List<Byte> protocolSupported() {
mohamed rahile04626f2016-04-05 20:42:53 +053059 return this.protocolSupported;
mohamed rahile04626f2016-04-05 20:42:53 +053060 }
61
62 @Override
sunishvka1dfc3e2016-04-16 12:24:47 +053063 public void readFrom(ChannelBuffer channelBuffer) {
64 while (channelBuffer.readableBytes() > 0) {
65 this.protocolSupported.add(channelBuffer.readByte());
mohamed rahile04626f2016-04-05 20:42:53 +053066 }
67 }
68
69 @Override
70 public byte[] asBytes() {
71 byte[] bytes = null;
72
73 byte[] tlvHeader = tlvHeaderAsByteArray();
74 byte[] tlvBody = tlvBodyAsBytes();
sunishvka1dfc3e2016-04-16 12:24:47 +053075 tlvHeader[1] = (byte) tlvBody.length;
mohamed rahile04626f2016-04-05 20:42:53 +053076 bytes = Bytes.concat(tlvHeader, tlvBody);
mohamed rahile04626f2016-04-05 20:42:53 +053077 return bytes;
78 }
79
80 /**
81 * Gets TLV body of protocol supported TLV.
82 *
83 * @return byteArray TLV body of protocol supported TLV
84 */
sunishvka1dfc3e2016-04-16 12:24:47 +053085 private byte[] tlvBodyAsBytes() {
86 List<Byte> bytes = new ArrayList<>();
mohamed rahile04626f2016-04-05 20:42:53 +053087 for (byte byt : this.protocolSupported) {
88 bytes.add(byt);
89 }
90 byte[] byteArray = new byte[bytes.size()];
91 int i = 0;
92 for (byte byt : bytes) {
93 byteArray[i++] = byt;
94 }
95 return byteArray;
96 }
sunishvka1dfc3e2016-04-16 12:24:47 +053097
98 @Override
99 public String toString() {
100 return MoreObjects.toStringHelper(getClass())
101 .omitNullValues()
102 .add("protocolSupported", protocolSupported)
103 .toString();
104 }
mohamed rahile04626f2016-04-05 20:42:53 +0530105}