mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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.primitives.Bytes; |
| 19 | import io.netty.buffer.ByteBuf; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
| 23 | |
| 24 | /** |
| 25 | * Represents Protocol supported TLV. |
| 26 | */ |
| 27 | public class ProtocolSupportedTlv extends TlvHeader implements IsisTlv { |
| 28 | |
| 29 | private List<Byte> protocolSupported = new ArrayList(); |
| 30 | |
| 31 | /** |
| 32 | * Sets TLV type and TLV length of protocol supported TLV. |
| 33 | * |
| 34 | * @param tlvHeader tlvHeader. |
| 35 | */ |
| 36 | public ProtocolSupportedTlv(TlvHeader tlvHeader) { |
| 37 | |
| 38 | this.setTlvType(tlvHeader.tlvType()); |
| 39 | this.setTlvLength(tlvHeader.tlvLength()); |
| 40 | |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Gets the Protocol Supported by the TLV. |
| 45 | * |
| 46 | * @return Protocol Supported |
| 47 | */ |
| 48 | public List<Byte> protocolSupported() { |
| 49 | |
| 50 | return this.protocolSupported; |
| 51 | |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void readFrom(ByteBuf byteBuf) { |
| 56 | |
| 57 | while (byteBuf.readableBytes() > 0) { |
| 58 | this.protocolSupported.add(byteBuf.readByte()); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public byte[] asBytes() { |
| 64 | byte[] bytes = null; |
| 65 | |
| 66 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 67 | byte[] tlvBody = tlvBodyAsBytes(); |
| 68 | bytes = Bytes.concat(tlvHeader, tlvBody); |
| 69 | |
| 70 | return bytes; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Gets TLV body of protocol supported TLV. |
| 75 | * |
| 76 | * @return byteArray TLV body of protocol supported TLV |
| 77 | */ |
| 78 | public byte[] tlvBodyAsBytes() { |
| 79 | |
| 80 | List<Byte> bytes = new ArrayList(); |
| 81 | for (byte byt : this.protocolSupported) { |
| 82 | bytes.add(byt); |
| 83 | } |
| 84 | byte[] byteArray = new byte[bytes.size()]; |
| 85 | int i = 0; |
| 86 | for (byte byt : bytes) { |
| 87 | byteArray[i++] = byt; |
| 88 | } |
| 89 | return byteArray; |
| 90 | } |
| 91 | } |