blob: 0214ccc2bd567525ad297ab3b5c80feec6014b7a [file] [log] [blame]
Kiran Ramachandra959353a2016-02-16 22:12:07 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Kiran Ramachandra959353a2016-02-16 22:12:07 +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.ospf.protocol.lsa.linksubtype;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.primitives.Bytes;
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.ospf.exceptions.OspfErrorType;
22import org.onosproject.ospf.exceptions.OspfParseException;
23import org.onosproject.ospf.protocol.lsa.TlvHeader;
24import org.onosproject.ospf.protocol.util.OspfUtil;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import java.net.InetAddress;
Ray Milkey019fba42018-01-31 14:07:47 -080029import java.net.UnknownHostException;
Kiran Ramachandra959353a2016-02-16 22:12:07 +053030import java.util.ArrayList;
31import java.util.List;
32
33/**
34 * Representation of local interface ip address TE value.
35 */
36public class LocalInterfaceIpAddress extends TlvHeader implements LinkSubType {
37 private static final Logger log =
38 LoggerFactory.getLogger(RemoteInterfaceIpAddress.class);
39 private List<String> localInterfaceIPAddress = new ArrayList<>();
40
41 /**
42 * Creates an instance of local interface ip address.
43 *
44 * @param header tlv header instance
45 */
46 public LocalInterfaceIpAddress(TlvHeader header) {
47 this.setTlvType(header.tlvType());
48 this.setTlvLength(header.tlvLength());
49 }
50
51 /**
52 * Adds local interface ip address.
53 *
54 * @param localAddress ip address
55 */
56 public void addLocalInterfaceIPAddress(String localAddress) {
57 localInterfaceIPAddress.add(localAddress);
58 }
59
60 /**
61 * Gets local interface ip address.
62 *
63 * @return localAddress ip address
64 */
65 public List<String> getLocalInterfaceIPAddress() {
66 return localInterfaceIPAddress;
67 }
68
69 /**
70 * Reads bytes from channel buffer.
71 *
72 * @param channelBuffer channel buffer instance
Ray Milkey019fba42018-01-31 14:07:47 -080073 * @throws OspfParseException might throws exception while parsing buffer
Kiran Ramachandra959353a2016-02-16 22:12:07 +053074 */
Ray Milkey019fba42018-01-31 14:07:47 -080075 public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
Kiran Ramachandra959353a2016-02-16 22:12:07 +053076 while (channelBuffer.readableBytes() >= OspfUtil.FOUR_BYTES) {
77 try {
78 byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
79 channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
80 this.addLocalInterfaceIPAddress(InetAddress.getByAddress(tempByteArray).getHostName());
Ray Milkey019fba42018-01-31 14:07:47 -080081 } catch (UnknownHostException e) {
Kiran Ramachandra959353a2016-02-16 22:12:07 +053082 log.debug("Error::readFrom:: {}", e.getMessage());
83 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
84 OspfErrorType.BAD_MESSAGE);
85 }
86 }
87 }
88
89 /**
90 * Gets local interface ip address as byte array.
91 *
92 * @return local interface ip address as byte array
Ray Milkey019fba42018-01-31 14:07:47 -080093 * @throws OspfParseException might throws exception while parsing packet
Kiran Ramachandra959353a2016-02-16 22:12:07 +053094 */
Ray Milkey019fba42018-01-31 14:07:47 -080095 public byte[] asBytes() throws OspfParseException {
Kiran Ramachandra959353a2016-02-16 22:12:07 +053096 byte[] linkSubType = null;
97
98 byte[] linkSubTlvHeader = getTlvHeaderAsByteArray();
99 byte[] linkSubTlvBody = getLinkSubTypeTlvBodyAsByteArray();
100 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
101
102 return linkSubType;
103 }
104
105 /**
106 * Gets byte array of local interface ip address.
107 *
108 * @return byte array of local interface ip address
Ray Milkey019fba42018-01-31 14:07:47 -0800109 * @throws OspfParseException might throws exception while parsing packet
Kiran Ramachandra959353a2016-02-16 22:12:07 +0530110 */
Ray Milkey019fba42018-01-31 14:07:47 -0800111 public byte[] getLinkSubTypeTlvBodyAsByteArray() throws OspfParseException {
Kiran Ramachandra959353a2016-02-16 22:12:07 +0530112
113 List<Byte> linkSubTypeBody = new ArrayList<>();
114
115 for (String remoteAddress : this.localInterfaceIPAddress) {
116 try {
117 linkSubTypeBody.addAll(Bytes.asList(InetAddress.getByName(remoteAddress).getAddress()));
118 } catch (Exception e) {
119 log.debug("Error::getLinkSubTypeTlvBodyAsByteArray:: {}", e.getMessage());
120 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
121 OspfErrorType.BAD_MESSAGE);
122 }
123 }
124
125 return Bytes.toArray(linkSubTypeBody);
126 }
127
128 @Override
129 public String toString() {
130 return MoreObjects.toStringHelper(getClass())
131 .omitNullValues()
132 .add("localInterfaceIPAddress", localInterfaceIPAddress)
133 .toString();
134 }
135}