blob: 3b1e63faeff395a98ddd7bd4c6a8ad1529eafbeb [file] [log] [blame]
mohamed rahile04626f2016-04-05 20:42:53 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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.Ip4Address;
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 IP interface address TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053029 */
30public class IpInterfaceAddressTlv extends TlvHeader implements IsisTlv {
sunishvka1dfc3e2016-04-16 12:24:47 +053031
32 private List<Ip4Address> interfaceAddress = new ArrayList<>();
mohamed rahile04626f2016-04-05 20:42:53 +053033
34 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053035 * Creates an instance of IP interface address 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 IpInterfaceAddressTlv(TlvHeader tlvHeader) {
40
41 this.setTlvType(tlvHeader.tlvType());
42 this.setTlvLength(tlvHeader.tlvLength());
43
44 }
45
46 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053047 * Adds the interface address to IP interface address TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053048 *
sunishvka1dfc3e2016-04-16 12:24:47 +053049 * @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 rahile04626f2016-04-05 20:42:53 +053059 */
60 public List<Ip4Address> interfaceAddress() {
61 return interfaceAddress;
62 }
63
64 @Override
sunishvka1dfc3e2016-04-16 12:24:47 +053065 public void readFrom(ChannelBuffer channelBuffer) {
66 while (channelBuffer.readableBytes() >= 4) {
mohamed rahile04626f2016-04-05 20:42:53 +053067 byte[] addressbytes = new byte[IsisUtil.FOUR_BYTES];
sunishvka1dfc3e2016-04-16 12:24:47 +053068 channelBuffer.readBytes(addressbytes, 0, IsisUtil.FOUR_BYTES);
mohamed rahile04626f2016-04-05 20:42:53 +053069 this.interfaceAddress.add(Ip4Address.valueOf(addressbytes));
70 }
mohamed rahile04626f2016-04-05 20:42:53 +053071 }
72
73 @Override
74 public byte[] asBytes() {
75 byte[] bytes = null;
mohamed rahile04626f2016-04-05 20:42:53 +053076 byte[] tlvHeader = tlvHeaderAsByteArray();
77 byte[] tlvBody = tlvBodyAsBytes();
sunishvka1dfc3e2016-04-16 12:24:47 +053078 tlvHeader[1] = (byte) tlvBody.length;
mohamed rahile04626f2016-04-05 20:42:53 +053079 bytes = Bytes.concat(tlvHeader, tlvBody);
mohamed rahile04626f2016-04-05 20:42:53 +053080 return bytes;
81 }
82
83 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053084 * Returns TLV body of IP interface address TLV.
mohamed rahile04626f2016-04-05 20:42:53 +053085 *
sunishvka1dfc3e2016-04-16 12:24:47 +053086 * @return byteArray TLV body of IP interface address TLV
mohamed rahile04626f2016-04-05 20:42:53 +053087 */
sunishvka1dfc3e2016-04-16 12:24:47 +053088 private byte[] tlvBodyAsBytes() {
89 List<Byte> bytes = new ArrayList<>();
mohamed rahile04626f2016-04-05 20:42:53 +053090 for (Ip4Address ip4Address : this.interfaceAddress) {
91 bytes.addAll(Bytes.asList(ip4Address.toOctets()));
92 }
sunishvka1dfc3e2016-04-16 12:24:47 +053093 return Bytes.toArray(bytes);
mohamed rahile04626f2016-04-05 20:42:53 +053094 }
95
96 @Override
97 public String toString() {
98 return MoreObjects.toStringHelper(getClass())
99 .omitNullValues()
sunishvka1dfc3e2016-04-16 12:24:47 +0530100 .add("interfaceAddress", interfaceAddress)
mohamed rahile04626f2016-04-05 20:42:53 +0530101 .toString();
102 }
103}