sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +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; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | |
| 22 | /** |
| 23 | * Representation of host name TLV. |
| 24 | */ |
| 25 | public class HostNameTlv extends TlvHeader { |
| 26 | private String hostName; |
| 27 | |
| 28 | /** |
| 29 | * Creates an instance of host name TLV. |
| 30 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 31 | * @param tlvHeader TLV header |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 32 | */ |
| 33 | public HostNameTlv(TlvHeader tlvHeader) { |
| 34 | this.setTlvType(tlvHeader.tlvType()); |
| 35 | this.setTlvLength(tlvHeader.tlvLength()); |
| 36 | |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Returns host name of host name TLV. |
| 41 | * |
| 42 | * @return host name |
| 43 | */ |
| 44 | public String hostName() { |
| 45 | return hostName; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Sets host name for host name TLV. |
| 50 | * |
| 51 | * @param hostName host name. |
| 52 | */ |
| 53 | public void setHostName(String hostName) { |
| 54 | this.hostName = hostName; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void readFrom(ChannelBuffer channelBuffer) { |
| 59 | byte[] addressbytes = new byte[this.tlvLength()]; |
| 60 | channelBuffer.readBytes(addressbytes, 0, this.tlvLength()); |
| 61 | this.hostName = new String(addressbytes); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public byte[] asBytes() { |
| 66 | byte[] bytes = null; |
| 67 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 68 | byte[] tlvBody = tlvBodyAsBytes(); |
| 69 | tlvHeader[1] = (byte) tlvBody.length; |
| 70 | bytes = Bytes.concat(tlvHeader, tlvBody); |
| 71 | return bytes; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns TLV body of host name TLV. |
| 76 | * |
| 77 | * @return byteArray TLV body of host name TLV |
| 78 | */ |
| 79 | private byte[] tlvBodyAsBytes() { |
| 80 | byte[] bytes = this.hostName.getBytes(); |
| 81 | return bytes; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public String toString() { |
| 86 | return MoreObjects.toStringHelper(getClass()) |
| 87 | .omitNullValues() |
| 88 | .add("hostName", hostName) |
| 89 | .toString(); |
| 90 | } |
| 91 | } |