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