mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 17 | package org.onosproject.isis.io.isispacket.tlv; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.primitives.Bytes; |
| 21 | import io.netty.buffer.ByteBuf; |
| 22 | import org.onosproject.isis.io.util.IsisUtil; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | |
| 27 | /** |
| 28 | * Represents the area address TLV. |
| 29 | */ |
| 30 | public class AreaAddressTlv extends TlvHeader implements IsisTlv { |
| 31 | |
| 32 | private List<String> areaAddress = new ArrayList(); |
| 33 | |
| 34 | /** |
| 35 | * Sets TLV type and TLV length of area address TLV. |
| 36 | * |
| 37 | * @param tlvHeader tlvHeader. |
| 38 | */ |
| 39 | public AreaAddressTlv(TlvHeader tlvHeader) { |
| 40 | |
| 41 | this.setTlvType(tlvHeader.tlvType()); |
| 42 | this.setTlvLength(tlvHeader.tlvLength()); |
| 43 | |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Gets the area address of area address TLV. |
| 48 | * |
| 49 | * @return area address |
| 50 | */ |
| 51 | public List<String> areaAddress() { |
| 52 | return this.areaAddress; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void readFrom(ByteBuf byteBuf) { |
| 57 | while (byteBuf.readableBytes() > 0) { |
| 58 | int addressLength = byteBuf.readByte(); |
| 59 | byte[] addressBytes = new byte[IsisUtil.THREE_BYTES]; |
| 60 | byteBuf.readBytes(addressBytes, 0, IsisUtil.THREE_BYTES); |
| 61 | String areaAddress = IsisUtil.areaAddres(addressBytes); |
| 62 | this.areaAddress.add(areaAddress); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public byte[] asBytes() { |
| 68 | byte[] bytes = null; |
| 69 | |
| 70 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 71 | byte[] tlvBody = tlvBodyAsBytes(); |
| 72 | bytes = Bytes.concat(tlvHeader, tlvBody); |
| 73 | |
| 74 | return bytes; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Gets TLV body of area address TLV. |
| 79 | * |
| 80 | * @return byteArray TLV body of area address TLV |
| 81 | */ |
| 82 | public byte[] tlvBodyAsBytes() { |
| 83 | |
| 84 | List<Byte> bytes = new ArrayList(); |
| 85 | for (String areaAddress : this.areaAddress) { |
| 86 | bytes.add((byte) (areaAddress.length() / 2)); |
| 87 | bytes.addAll(IsisUtil.areaAddresToBytes(areaAddress)); |
| 88 | } |
| 89 | byte[] byteArray = new byte[bytes.size()]; |
| 90 | int i = 0; |
| 91 | for (byte byt : bytes) { |
| 92 | byteArray[i++] = byt; |
| 93 | } |
| 94 | return byteArray; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String toString() { |
| 99 | return MoreObjects.toStringHelper(getClass()) |
| 100 | .omitNullValues() |
| 101 | .toString(); |
| 102 | } |
| 103 | } |