blob: add4127d9a46264161db11fc9b7589b92e479b7a [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 */
16
17package org.onosproject.isis.io.isispacket.tlv;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.primitives.Bytes;
21import io.netty.buffer.ByteBuf;
22import org.onosproject.isis.io.util.IsisUtil;
23
24import java.util.ArrayList;
25import java.util.List;
26
27/**
28 * Represents the area address TLV.
29 */
30public 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}