mohamed rahil | e04626f | 2016-04-05 20:42:53 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.isis.io.isispacket.tlv; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.primitives.Bytes; |
| 20 | import io.netty.buffer.ByteBuf; |
| 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 | /** |
| 28 | * Represents IP interface address TLV. |
| 29 | */ |
| 30 | public class IpInterfaceAddressTlv extends TlvHeader implements IsisTlv { |
| 31 | private List<Ip4Address> interfaceAddress = new ArrayList(); |
| 32 | |
| 33 | /** |
| 34 | * Sets TLV type and TLV length of IP interface address TLV. |
| 35 | * |
| 36 | * @param tlvHeader tlvHeader. |
| 37 | */ |
| 38 | public IpInterfaceAddressTlv(TlvHeader tlvHeader) { |
| 39 | |
| 40 | this.setTlvType(tlvHeader.tlvType()); |
| 41 | this.setTlvLength(tlvHeader.tlvLength()); |
| 42 | |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Gets interface address of interface address TLV. |
| 47 | * |
| 48 | * @return interfaceAddress interface address |
| 49 | */ |
| 50 | public List<Ip4Address> interfaceAddress() { |
| 51 | return interfaceAddress; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void readFrom(ByteBuf byteBuf) { |
| 56 | while (byteBuf.readableBytes() >= 4) { |
| 57 | byte[] addressbytes = new byte[IsisUtil.FOUR_BYTES]; |
| 58 | byteBuf.readBytes(addressbytes, 0, IsisUtil.FOUR_BYTES); |
| 59 | this.interfaceAddress.add(Ip4Address.valueOf(addressbytes)); |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public byte[] asBytes() { |
| 66 | byte[] bytes = null; |
| 67 | |
| 68 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 69 | byte[] tlvBody = tlvBodyAsBytes(); |
| 70 | bytes = Bytes.concat(tlvHeader, tlvBody); |
| 71 | |
| 72 | return bytes; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Gets TLV body of interface address TLV. |
| 77 | * |
| 78 | * @return byteArray TLV body of interface address TLV. |
| 79 | */ |
| 80 | public byte[] tlvBodyAsBytes() { |
| 81 | |
| 82 | List<Byte> bytes = new ArrayList(); |
| 83 | for (Ip4Address ip4Address : this.interfaceAddress) { |
| 84 | bytes.addAll(Bytes.asList(ip4Address.toOctets())); |
| 85 | } |
| 86 | byte[] byteArray = new byte[bytes.size()]; |
| 87 | int i = 0; |
| 88 | for (byte byt : bytes) { |
| 89 | byteArray[i++] = byt; |
| 90 | } |
| 91 | return byteArray; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public String toString() { |
| 96 | return MoreObjects.toStringHelper(getClass()) |
| 97 | .omitNullValues() |
| 98 | .toString(); |
| 99 | } |
| 100 | } |