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.Ip4Address; |
| 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 IP interface address TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 29 | */ |
| 30 | public class IpInterfaceAddressTlv extends TlvHeader implements IsisTlv { |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 31 | |
| 32 | private List<Ip4Address> interfaceAddress = 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 IP interface address 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 IpInterfaceAddressTlv(TlvHeader tlvHeader) { |
| 40 | |
| 41 | this.setTlvType(tlvHeader.tlvType()); |
| 42 | this.setTlvLength(tlvHeader.tlvLength()); |
| 43 | |
| 44 | } |
| 45 | |
| 46 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 47 | * Adds the interface address to IP interface address TLV. |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 48 | * |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 49 | * @param interfaceAddress interface address |
| 50 | */ |
| 51 | public void addInterfaceAddres(Ip4Address interfaceAddress) { |
| 52 | this.interfaceAddress.add(interfaceAddress); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the interface address of IP interface address TLV. |
| 57 | * |
| 58 | * @return interface address |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 59 | */ |
| 60 | public List<Ip4Address> interfaceAddress() { |
| 61 | return interfaceAddress; |
| 62 | } |
| 63 | |
| 64 | @Override |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 65 | public void readFrom(ChannelBuffer channelBuffer) { |
| 66 | while (channelBuffer.readableBytes() >= 4) { |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 67 | byte[] addressbytes = new byte[IsisUtil.FOUR_BYTES]; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 68 | channelBuffer.readBytes(addressbytes, 0, IsisUtil.FOUR_BYTES); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 69 | this.interfaceAddress.add(Ip4Address.valueOf(addressbytes)); |
| 70 | } |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public byte[] asBytes() { |
| 75 | byte[] bytes = null; |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 76 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 77 | byte[] tlvBody = tlvBodyAsBytes(); |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 78 | tlvHeader[1] = (byte) tlvBody.length; |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 79 | bytes = Bytes.concat(tlvHeader, tlvBody); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 80 | return bytes; |
| 81 | } |
| 82 | |
| 83 | /** |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 84 | * Returns TLV body of IP interface 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 | * @return byteArray TLV body of IP interface address TLV |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 87 | */ |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 88 | private byte[] tlvBodyAsBytes() { |
| 89 | List<Byte> bytes = new ArrayList<>(); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 90 | for (Ip4Address ip4Address : this.interfaceAddress) { |
| 91 | bytes.addAll(Bytes.asList(ip4Address.toOctets())); |
| 92 | } |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 93 | return Bytes.toArray(bytes); |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public String toString() { |
| 98 | return MoreObjects.toStringHelper(getClass()) |
| 99 | .omitNullValues() |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 100 | .add("interfaceAddress", interfaceAddress) |
mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 101 | .toString(); |
| 102 | } |
| 103 | } |