blob: e9bba29d84735d5a0796f3d8c9b85794c4ead683 [file] [log] [blame]
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002* Copyright 2016-present Open Networking Foundation
sunish vk7bdf4d42016-06-24 12:29:43 +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*/
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053016package org.onosproject.isis.io.isispacket.tlv.subtlv;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.primitives.Bytes;
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onlab.packet.Ip4Address;
22import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
23import org.onosproject.isis.io.util.IsisUtil;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * Representation of interface ip address TE value.
32 */
33public class InterfaceIpAddress extends TlvHeader implements TrafficEngineeringSubTlv {
34 private static final Logger log =
sunish vk7bdf4d42016-06-24 12:29:43 +053035 LoggerFactory.getLogger(NeighborIpAddress.class);
36 private Ip4Address localInterfaceIPAddress;
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053037
38 /**
39 * Creates an instance of local interface ip address.
40 *
41 * @param header tlv header instance
42 */
43 public InterfaceIpAddress(TlvHeader header) {
44 this.setTlvType(header.tlvType());
45 this.setTlvLength(header.tlvLength());
46 }
47
48 /**
49 * Adds local interface ip address.
50 *
51 * @param localAddress ip address
52 */
sunish vk7bdf4d42016-06-24 12:29:43 +053053 public void setIpAddress(Ip4Address localAddress) {
54 this.localInterfaceIPAddress = localAddress;
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053055 }
56
57 /**
sunish vk7bdf4d42016-06-24 12:29:43 +053058 * Gets local interface ip address.
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053059 *
60 * @return localAddress ip address
61 */
sunish vk7bdf4d42016-06-24 12:29:43 +053062 public Ip4Address localInterfaceIPAddress() {
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053063 return localInterfaceIPAddress;
64 }
65
66 /**
67 * Reads bytes from channel buffer.
68 *
69 * @param channelBuffer channel buffer instance
70 */
71 public void readFrom(ChannelBuffer channelBuffer) {
72 while (channelBuffer.readableBytes() >= IsisUtil.FOUR_BYTES) {
73 byte[] tempByteArray = new byte[IsisUtil.FOUR_BYTES];
74 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES);
sunish vk7bdf4d42016-06-24 12:29:43 +053075 this.setIpAddress(Ip4Address.valueOf(tempByteArray));
76
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053077 }
78 }
79
80 /**
sunish vk7bdf4d42016-06-24 12:29:43 +053081 * Gets local interface ip address as byte array.
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053082 *
83 * @return local interface ip address as byte array
84 */
85 public byte[] asBytes() {
86 byte[] linkSubType = null;
sunish vk7bdf4d42016-06-24 12:29:43 +053087
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053088 byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
89 byte[] linkSubTlvBody = tlvBodyAsBytes();
90 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
91
92 return linkSubType;
93 }
94
95 /**
sunish vk7bdf4d42016-06-24 12:29:43 +053096 * Gets byte array of local interface ip address.
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053097 *
98 * @return byte array of local interface ip address
99 */
100 public byte[] tlvBodyAsBytes() {
sunish vk7bdf4d42016-06-24 12:29:43 +0530101
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +0530102 List<Byte> linkSubTypeBody = new ArrayList<>();
sunish vk7bdf4d42016-06-24 12:29:43 +0530103
104 linkSubTypeBody.addAll(Bytes.asList(this.localInterfaceIPAddress.toOctets()));
105
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +0530106
107 return Bytes.toArray(linkSubTypeBody);
108 }
109
110 @Override
111 public String toString() {
112 return MoreObjects.toStringHelper(getClass())
113 .omitNullValues()
114 .add("localInterfaceIPAddress", localInterfaceIPAddress)
115 .toString();
116 }
sunish vk7bdf4d42016-06-24 12:29:43 +0530117}