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 | */ |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 16 | package org.onosproject.isis.io.isispacket.tlv; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 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 | import org.onosproject.isis.io.util.IsisUtil; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | |
| 26 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 27 | * Representation of area address TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 28 | */ |
| 29 | public class AreaAddressTlv extends TlvHeader implements IsisTlv { |
| 30 | |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 31 | private List<String> areaAddress = new ArrayList<>(); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 32 | |
| 33 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 34 | * Creates an instance of area address TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 35 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 36 | * @param tlvHeader TLV header |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 37 | */ |
| 38 | public AreaAddressTlv(TlvHeader tlvHeader) { |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 39 | this.setTlvType(tlvHeader.tlvType()); |
| 40 | this.setTlvLength(tlvHeader.tlvLength()); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 44 | * Adds the area address to the area address TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 45 | * |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 46 | * @param areaAddress area address |
| 47 | */ |
| 48 | public void addAddress(String areaAddress) { |
| 49 | this.areaAddress.add(areaAddress); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the area address of area address TLV. |
| 54 | * |
| 55 | * @return areaAddress area address |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 56 | */ |
| 57 | public List<String> areaAddress() { |
| 58 | return this.areaAddress; |
| 59 | } |
| 60 | |
| 61 | @Override |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 62 | public void readFrom(ChannelBuffer channelBuffer) { |
| 63 | while (channelBuffer.readableBytes() > 0) { |
| 64 | int addressLength = channelBuffer.readByte(); |
| 65 | byte[] addressbytes = new byte[addressLength]; |
| 66 | channelBuffer.readBytes(addressbytes, 0, addressLength); |
| 67 | String areaAddress = IsisUtil.areaAddres(addressbytes); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 68 | this.areaAddress.add(areaAddress); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public byte[] asBytes() { |
| 74 | byte[] bytes = null; |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 75 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 76 | byte[] tlvBody = tlvBodyAsBytes(); |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 77 | tlvHeader[1] = (byte) tlvBody.length; |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 78 | bytes = Bytes.concat(tlvHeader, tlvBody); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 79 | return bytes; |
| 80 | } |
| 81 | |
| 82 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 83 | * Returns TLV body of area address TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 84 | * |
| 85 | * @return byteArray TLV body of area address TLV |
| 86 | */ |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 87 | private byte[] tlvBodyAsBytes() { |
| 88 | List<Byte> bytes = new ArrayList<>(); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 89 | for (String areaAddress : this.areaAddress) { |
| 90 | bytes.add((byte) (areaAddress.length() / 2)); |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 91 | bytes.addAll(IsisUtil.areaAddressToBytes(areaAddress)); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 92 | } |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 93 | return Bytes.toArray(bytes); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public String toString() { |
| 98 | return MoreObjects.toStringHelper(getClass()) |
| 99 | .omitNullValues() |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 100 | .add("areaAddress", areaAddress) |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 101 | .toString(); |
| 102 | } |
| 103 | } |