blob: fe46345898f9936462bcd855e34e59740e002875 [file] [log] [blame]
Kiran Ramachandra959353a2016-02-16 22:12:07 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
29import java.util.ArrayList;
30import java.util.List;
31
32/**
33 * Representation of local interface ip address TE value.
34 */
35public class LocalInterfaceIpAddress extends TlvHeader implements LinkSubType {
36 private static final Logger log =
37 LoggerFactory.getLogger(RemoteInterfaceIpAddress.class);
38 private List<String> localInterfaceIPAddress = new ArrayList<>();
39
40 /**
41 * Creates an instance of local interface ip address.
42 *
43 * @param header tlv header instance
44 */
45 public LocalInterfaceIpAddress(TlvHeader header) {
46 this.setTlvType(header.tlvType());
47 this.setTlvLength(header.tlvLength());
48 }
49
50 /**
51 * Adds local interface ip address.
52 *
53 * @param localAddress ip address
54 */
55 public void addLocalInterfaceIPAddress(String localAddress) {
56 localInterfaceIPAddress.add(localAddress);
57 }
58
59 /**
60 * Gets local interface ip address.
61 *
62 * @return localAddress ip address
63 */
64 public List<String> getLocalInterfaceIPAddress() {
65 return localInterfaceIPAddress;
66 }
67
68 /**
69 * Reads bytes from channel buffer.
70 *
71 * @param channelBuffer channel buffer instance
72 * @throws Exception might throws exception while parsing buffer
73 */
74 public void readFrom(ChannelBuffer channelBuffer) throws Exception {
75 while (channelBuffer.readableBytes() >= OspfUtil.FOUR_BYTES) {
76 try {
77 byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
78 channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
79 this.addLocalInterfaceIPAddress(InetAddress.getByAddress(tempByteArray).getHostName());
80 } catch (Exception e) {
81 log.debug("Error::readFrom:: {}", e.getMessage());
82 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
83 OspfErrorType.BAD_MESSAGE);
84 }
85 }
86 }
87
88 /**
89 * Gets local interface ip address as byte array.
90 *
91 * @return local interface ip address as byte array
92 * @throws Exception might throws exception while parsing packet
93 */
94 public byte[] asBytes() throws Exception {
95 byte[] linkSubType = null;
96
97 byte[] linkSubTlvHeader = getTlvHeaderAsByteArray();
98 byte[] linkSubTlvBody = getLinkSubTypeTlvBodyAsByteArray();
99 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
100
101 return linkSubType;
102 }
103
104 /**
105 * Gets byte array of local interface ip address.
106 *
107 * @return byte array of local interface ip address
108 * @throws Exception might throws exception while parsing packet
109 */
110 public byte[] getLinkSubTypeTlvBodyAsByteArray() throws Exception {
111
112 List<Byte> linkSubTypeBody = new ArrayList<>();
113
114 for (String remoteAddress : this.localInterfaceIPAddress) {
115 try {
116 linkSubTypeBody.addAll(Bytes.asList(InetAddress.getByName(remoteAddress).getAddress()));
117 } catch (Exception e) {
118 log.debug("Error::getLinkSubTypeTlvBodyAsByteArray:: {}", e.getMessage());
119 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
120 OspfErrorType.BAD_MESSAGE);
121 }
122 }
123
124 return Bytes.toArray(linkSubTypeBody);
125 }
126
127 @Override
128 public String toString() {
129 return MoreObjects.toStringHelper(getClass())
130 .omitNullValues()
131 .add("localInterfaceIPAddress", localInterfaceIPAddress)
132 .toString();
133 }
134}