blob: cc6174f6d3f542ebb40cc0a717c04dbc833ab7a2 [file] [log] [blame]
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +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.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 =
35 LoggerFactory.getLogger(InterfaceIpAddress.class);
36 private List<Ip4Address> localInterfaceIPAddress = new ArrayList<>();
37
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 */
53 public void addLocalInterfaceIPAddress(Ip4Address localAddress) {
54 localInterfaceIPAddress.add(localAddress);
55 }
56
57 /**
58 * Returns local interface ip address.
59 *
60 * @return localAddress ip address
61 */
62 public List<Ip4Address> getLocalInterfaceIPAddress() {
63 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);
75 this.addLocalInterfaceIPAddress(Ip4Address.valueOf(tempByteArray));
76 }
77 }
78
79 /**
80 * Returns local interface ip address as byte array.
81 *
82 * @return local interface ip address as byte array
83 */
84 public byte[] asBytes() {
85 byte[] linkSubType = null;
86 byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
87 byte[] linkSubTlvBody = tlvBodyAsBytes();
88 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
89
90 return linkSubType;
91 }
92
93 /**
94 * Returns byte array of local interface ip address.
95 *
96 * @return byte array of local interface ip address
97 */
98 public byte[] tlvBodyAsBytes() {
99 List<Byte> linkSubTypeBody = new ArrayList<>();
100 for (Ip4Address remoteAddress : this.localInterfaceIPAddress) {
101 linkSubTypeBody.addAll(Bytes.asList(remoteAddress.toOctets()));
102 }
103
104 return Bytes.toArray(linkSubTypeBody);
105 }
106
107 @Override
108 public String toString() {
109 return MoreObjects.toStringHelper(getClass())
110 .omitNullValues()
111 .add("localInterfaceIPAddress", localInterfaceIPAddress)
112 .toString();
113 }
114}