blob: 72eb3a986af55dcebabf07a58f92632a205ec29c [file] [log] [blame]
sunish vk7bdf4d42016-06-24 12:29:43 +05301/*
2* Copyright 2016-present 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*/
16package org.onosproject.isis.io.isispacket.tlv.subtlv;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.base.Objects;
20import com.google.common.primitives.Bytes;
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onlab.packet.Ip4Address;
23import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
24import org.onosproject.isis.io.util.IsisUtil;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import java.util.ArrayList;
29import java.util.List;
30
31/**
32 * Representation of neighbor ip address TE value.
33 */
34public class NeighborIpAddress extends TlvHeader implements TrafficEngineeringSubTlv {
35 private static final Logger log =
36 LoggerFactory.getLogger(NeighborIpAddress.class);
37 private Ip4Address neighborIPAddress;
38
39 /**
40 * Creates an instance of neighbor ip address.
41 *
42 * @param header tlv header instance
43 */
44 public NeighborIpAddress(TlvHeader header) {
45 this.setTlvType(header.tlvType());
46 this.setTlvLength(header.tlvLength());
47 }
48
49 /**
50 * Sets the neighbor ip address.
51 *
52 * @param neighborIPAddress ip address
53 */
54 public void setIpAddress(Ip4Address neighborIPAddress) {
55 this.neighborIPAddress = neighborIPAddress;
56 }
57
58 /**
59 * Gets the neighbor ip address.
60 *
61 * @return neighbor ip address
62 */
63 public Ip4Address neighborIPAddress() {
64 return neighborIPAddress;
65 }
66
67 /**
68 * Reads bytes from channel buffer.
69 *
70 * @param channelBuffer channel buffer instance
71 */
72 public void readFrom(ChannelBuffer channelBuffer) {
73 while (channelBuffer.readableBytes() >= IsisUtil.FOUR_BYTES) {
74 byte[] tempByteArray = new byte[IsisUtil.FOUR_BYTES];
75 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES);
76 this.setIpAddress(Ip4Address.valueOf(tempByteArray));
77
78 }
79 }
80
81 /**
82 * Gets the neighbor ip address as byte array.
83 *
84 * @return neighbor ip address as byte array
85 */
86 public byte[] asBytes() {
87 byte[] linkSubType = null;
88
89 byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
90 byte[] linkSubTlvBody = tlvBodyAsBytes();
91 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
92
93 return linkSubType;
94 }
95
96 /**
97 * Gets byte array of neighborIPAddress.
98 *
99 * @return byte array of neighborIPAddress
100 */
101 public byte[] tlvBodyAsBytes() {
102
103 List<Byte> linkSubTypeBody = new ArrayList<>();
104
105 linkSubTypeBody.addAll(Bytes.asList(this.neighborIPAddress.toOctets()));
106
107
108 return Bytes.toArray(linkSubTypeBody);
109 }
110
111 @Override
112 public boolean equals(Object o) {
113 if (this == o) {
114 return true;
115 }
116 if (o == null || getClass() != o.getClass()) {
117 return false;
118 }
119 NeighborIpAddress that = (NeighborIpAddress) o;
120 return Objects.equal(neighborIPAddress, that.neighborIPAddress);
121 }
122
123 @Override
124 public int hashCode() {
125 return Objects.hashCode(neighborIPAddress);
126 }
127
128 @Override
129 public String toString() {
130 return MoreObjects.toStringHelper(getClass())
131 .omitNullValues()
132 .add("localInterfaceIPAddress", neighborIPAddress)
133 .toString();
134 }
135}