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