Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.ospf.protocol.lsa.linksubtype; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.primitives.Bytes; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onosproject.ospf.exceptions.OspfErrorType; |
| 22 | import org.onosproject.ospf.exceptions.OspfParseException; |
| 23 | import org.onosproject.ospf.protocol.lsa.TlvHeader; |
| 24 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.net.InetAddress; |
Ray Milkey | 019fba4 | 2018-01-31 14:07:47 -0800 | [diff] [blame^] | 29 | import java.net.UnknownHostException; |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * Representation of local interface ip address TE value. |
| 35 | */ |
| 36 | public 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 Milkey | 019fba4 | 2018-01-31 14:07:47 -0800 | [diff] [blame^] | 73 | * @throws OspfParseException might throws exception while parsing buffer |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 74 | */ |
Ray Milkey | 019fba4 | 2018-01-31 14:07:47 -0800 | [diff] [blame^] | 75 | public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 76 | 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 Milkey | 019fba4 | 2018-01-31 14:07:47 -0800 | [diff] [blame^] | 81 | } catch (UnknownHostException e) { |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 82 | 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 Milkey | 019fba4 | 2018-01-31 14:07:47 -0800 | [diff] [blame^] | 93 | * @throws OspfParseException might throws exception while parsing packet |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 94 | */ |
Ray Milkey | 019fba4 | 2018-01-31 14:07:47 -0800 | [diff] [blame^] | 95 | public byte[] asBytes() throws OspfParseException { |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 96 | 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 Milkey | 019fba4 | 2018-01-31 14:07:47 -0800 | [diff] [blame^] | 109 | * @throws OspfParseException might throws exception while parsing packet |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 110 | */ |
Ray Milkey | 019fba4 | 2018-01-31 14:07:47 -0800 | [diff] [blame^] | 111 | public byte[] getLinkSubTypeTlvBodyAsByteArray() throws OspfParseException { |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 112 | |
| 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 | } |