blob: 2d7f9e08e39ed24b217e69088e7512a4835a858e [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 */
16package 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.onlab.packet.MacAddress;
22import org.onosproject.isis.io.util.IsisUtil;
23
24import java.util.ArrayList;
25import java.util.List;
26
27/**
sunishvka1dfc3e2016-04-16 12:24:47 +053028 * Representation of ISIS neighbor TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053029 */
30public class IsisNeighborTlv extends TlvHeader implements IsisTlv {
31
sunishvka1dfc3e2016-04-16 12:24:47 +053032 private List<MacAddress> neighbor = new ArrayList<>();
mohamed rahile04626f2016-04-05 20:42:53 +053033
34 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053035 * Creates an instance of ISIS neighbor TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053036 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053037 * @param tlvHeader TLV header
mohamed rahile04626f2016-04-05 20:42:53 +053038 */
39 public IsisNeighborTlv(TlvHeader tlvHeader) {
mohamed rahile04626f2016-04-05 20:42:53 +053040 this.setTlvType(tlvHeader.tlvType());
41 this.setTlvLength(tlvHeader.tlvLength());
mohamed rahile04626f2016-04-05 20:42:53 +053042 }
43
44 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053045 * Adds the MAC address of the neighbor to ISIS neighbor TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053046 *
sunishvka1dfc3e2016-04-16 12:24:47 +053047 * @param macAddress MAC address
48 */
49 public void addNeighbor(MacAddress macAddress) {
50 neighbor.add(macAddress);
51 }
52
53 /**
54 * Returns the MAC address of the ISIS neighbor TLV.
55 *
56 * @return neighbor
mohamed rahile04626f2016-04-05 20:42:53 +053057 */
58 public List<MacAddress> neighbor() {
59 return this.neighbor;
60 }
61
62 @Override
sunishvka1dfc3e2016-04-16 12:24:47 +053063 public void readFrom(ChannelBuffer channelBuffer) {
64 while (channelBuffer.readableBytes() >= 6) {
mohamed rahile04626f2016-04-05 20:42:53 +053065 byte[] addressbytes = new byte[IsisUtil.SIX_BYTES];
sunishvka1dfc3e2016-04-16 12:24:47 +053066 channelBuffer.readBytes(addressbytes, 0, IsisUtil.SIX_BYTES);
mohamed rahile04626f2016-04-05 20:42:53 +053067 this.neighbor.add(MacAddress.valueOf(addressbytes));
68 }
mohamed rahile04626f2016-04-05 20:42:53 +053069 }
70
71 @Override
72 public byte[] asBytes() {
73 byte[] bytes = null;
mohamed rahile04626f2016-04-05 20:42:53 +053074 byte[] tlvHeader = tlvHeaderAsByteArray();
75 byte[] tlvBody = tlvBodyAsBytes();
sunishvka1dfc3e2016-04-16 12:24:47 +053076 tlvHeader[1] = (byte) tlvBody.length;
mohamed rahile04626f2016-04-05 20:42:53 +053077 bytes = Bytes.concat(tlvHeader, tlvBody);
mohamed rahile04626f2016-04-05 20:42:53 +053078 return bytes;
mohamed rahile04626f2016-04-05 20:42:53 +053079 }
80
81 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053082 * Returns TLV body of ISIS neighbor TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053083 *
sunishvka1dfc3e2016-04-16 12:24:47 +053084 * @return byteArray TLV body of area address TLV
mohamed rahile04626f2016-04-05 20:42:53 +053085 */
sunishvka1dfc3e2016-04-16 12:24:47 +053086 private byte[] tlvBodyAsBytes() {
87 List<Byte> bytes = new ArrayList<>();
mohamed rahile04626f2016-04-05 20:42:53 +053088 for (MacAddress macAddress : this.neighbor) {
89 bytes.addAll(Bytes.asList(macAddress.toBytes()));
90 }
sunishvka1dfc3e2016-04-16 12:24:47 +053091 return Bytes.toArray(bytes);
mohamed rahile04626f2016-04-05 20:42:53 +053092 }
93
94 @Override
95 public String toString() {
96 return MoreObjects.toStringHelper(getClass())
97 .omitNullValues()
sunishvka1dfc3e2016-04-16 12:24:47 +053098 .add("neighbor", neighbor)
mohamed rahile04626f2016-04-05 20:42:53 +053099 .toString();
100 }
101}