blob: bc249103266ca5bdfff735f1fdf22f7fef255658 [file] [log] [blame]
mohamed rahile04626f2016-04-05 20:42:53 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
mohamed rahile04626f2016-04-05 20:42:53 +05303 *
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 rahile04626f2016-04-05 20:42:53 +053016package org.onosproject.isis.io.isispacket.tlv;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.primitives.Bytes;
sunishvka1dfc3e2016-04-16 12:24:47 +053020import org.jboss.netty.buffer.ChannelBuffer;
mohamed rahile04626f2016-04-05 20:42:53 +053021import org.onosproject.isis.io.util.IsisUtil;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
sunishvka1dfc3e2016-04-16 12:24:47 +053027 * Representation of area address TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053028 */
29public class AreaAddressTlv extends TlvHeader implements IsisTlv {
30
sunishvka1dfc3e2016-04-16 12:24:47 +053031 private List<String> areaAddress = new ArrayList<>();
mohamed rahile04626f2016-04-05 20:42:53 +053032
33 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053034 * Creates an instance of area address TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053035 *
sunishvka1dfc3e2016-04-16 12:24:47 +053036 * @param tlvHeader tlvHeader
mohamed rahile04626f2016-04-05 20:42:53 +053037 */
38 public AreaAddressTlv(TlvHeader tlvHeader) {
mohamed rahile04626f2016-04-05 20:42:53 +053039 this.setTlvType(tlvHeader.tlvType());
40 this.setTlvLength(tlvHeader.tlvLength());
mohamed rahile04626f2016-04-05 20:42:53 +053041 }
42
43 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053044 * Adds the area address to the area address TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053045 *
sunishvka1dfc3e2016-04-16 12:24:47 +053046 * @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 rahile04626f2016-04-05 20:42:53 +053056 */
57 public List<String> areaAddress() {
58 return this.areaAddress;
59 }
60
61 @Override
sunishvka1dfc3e2016-04-16 12:24:47 +053062 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 rahile04626f2016-04-05 20:42:53 +053068 this.areaAddress.add(areaAddress);
69 }
70 }
71
72 @Override
73 public byte[] asBytes() {
74 byte[] bytes = null;
mohamed rahile04626f2016-04-05 20:42:53 +053075 byte[] tlvHeader = tlvHeaderAsByteArray();
76 byte[] tlvBody = tlvBodyAsBytes();
sunishvka1dfc3e2016-04-16 12:24:47 +053077 tlvHeader[1] = (byte) tlvBody.length;
mohamed rahile04626f2016-04-05 20:42:53 +053078 bytes = Bytes.concat(tlvHeader, tlvBody);
mohamed rahile04626f2016-04-05 20:42:53 +053079 return bytes;
80 }
81
82 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053083 * Returns TLV body of area address TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053084 *
85 * @return byteArray TLV body of area address TLV
86 */
sunishvka1dfc3e2016-04-16 12:24:47 +053087 private byte[] tlvBodyAsBytes() {
88 List<Byte> bytes = new ArrayList<>();
mohamed rahile04626f2016-04-05 20:42:53 +053089 for (String areaAddress : this.areaAddress) {
90 bytes.add((byte) (areaAddress.length() / 2));
sunishvka1dfc3e2016-04-16 12:24:47 +053091 bytes.addAll(IsisUtil.areaAddressToBytes(areaAddress));
mohamed rahile04626f2016-04-05 20:42:53 +053092 }
sunishvka1dfc3e2016-04-16 12:24:47 +053093 return Bytes.toArray(bytes);
mohamed rahile04626f2016-04-05 20:42:53 +053094 }
95
96 @Override
97 public String toString() {
98 return MoreObjects.toStringHelper(getClass())
99 .omitNullValues()
sunishvka1dfc3e2016-04-16 12:24:47 +0530100 .add("areaAddress", areaAddress)
mohamed rahile04626f2016-04-05 20:42:53 +0530101 .toString();
102 }
103}