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 | |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 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 protocol supported TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 27 | */ |
| 28 | public class ProtocolSupportedTlv extends TlvHeader implements IsisTlv { |
| 29 | |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 30 | private List<Byte> protocolSupported = new ArrayList<>(); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 31 | |
| 32 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 33 | * Creates an instance of protocol supported TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 34 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 35 | * @param tlvHeader TLV header |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 36 | */ |
| 37 | public ProtocolSupportedTlv(TlvHeader tlvHeader) { |
| 38 | |
| 39 | this.setTlvType(tlvHeader.tlvType()); |
| 40 | this.setTlvLength(tlvHeader.tlvLength()); |
| 41 | |
| 42 | } |
| 43 | |
| 44 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 45 | * Adds the protocol supported to protocol supported TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 46 | * |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 47 | * @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 rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 57 | */ |
| 58 | public List<Byte> protocolSupported() { |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 59 | return this.protocolSupported; |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 63 | public void readFrom(ChannelBuffer channelBuffer) { |
| 64 | while (channelBuffer.readableBytes() > 0) { |
| 65 | this.protocolSupported.add(channelBuffer.readByte()); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public byte[] asBytes() { |
| 71 | byte[] bytes = null; |
| 72 | |
| 73 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 74 | byte[] tlvBody = tlvBodyAsBytes(); |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 75 | tlvHeader[1] = (byte) tlvBody.length; |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 76 | bytes = Bytes.concat(tlvHeader, tlvBody); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 77 | return bytes; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Gets TLV body of protocol supported TLV. |
| 82 | * |
| 83 | * @return byteArray TLV body of protocol supported TLV |
| 84 | */ |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 85 | private byte[] tlvBodyAsBytes() { |
| 86 | List<Byte> bytes = new ArrayList<>(); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 87 | 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 | } |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 97 | |
| 98 | @Override |
| 99 | public String toString() { |
| 100 | return MoreObjects.toStringHelper(getClass()) |
| 101 | .omitNullValues() |
| 102 | .add("protocolSupported", protocolSupported) |
| 103 | .toString(); |
| 104 | } |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 105 | } |