Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
| 31 | |
| 32 | /** |
| 33 | * Representation of remote interface ip address TE value. |
| 34 | */ |
| 35 | public class RemoteInterfaceIpAddress extends TlvHeader implements LinkSubType { |
| 36 | private static final Logger log = |
| 37 | LoggerFactory.getLogger(RemoteInterfaceIpAddress.class); |
| 38 | private List<String> remoteInterfaceAddress = new ArrayList<>(); |
| 39 | |
| 40 | /** |
| 41 | * Creates an instance of remote interface ip address. |
| 42 | * |
| 43 | * @param header tlv header instance |
| 44 | */ |
| 45 | public RemoteInterfaceIpAddress(TlvHeader header) { |
| 46 | this.setTlvType(header.tlvType()); |
| 47 | this.setTlvLength(header.tlvLength()); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Adds remote interface ip address. |
| 52 | * |
| 53 | * @param remoteAddress ip address |
| 54 | */ |
| 55 | public void addRemoteInterfaceAddress(String remoteAddress) { |
| 56 | remoteInterfaceAddress.add(remoteAddress); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Gets remote interface ip address. |
| 61 | * |
| 62 | * @return remoteAddress ip address |
| 63 | */ |
| 64 | public List<String> getRemoteInterfaceAddress() { |
| 65 | return remoteInterfaceAddress; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Reads bytes from channel buffer . |
| 70 | * |
| 71 | * @param channelBuffer channel buffer instance |
| 72 | * @throws Exception might throws exception while parsing packet |
| 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.addRemoteInterfaceAddress(InetAddress.getByAddress(tempByteArray).getHostName()); |
| 80 | } catch (Exception e) { |
| 81 | log.debug("Error::RemoteInterfaceIPAddress:: {}", e.getMessage()); |
| 82 | throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, |
| 83 | OspfErrorType.BAD_MESSAGE); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Gets byte array of remote interface ip address . |
| 90 | * |
| 91 | * @return byte array of remote interface ip address |
| 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 remote interface ip address. |
| 106 | * |
| 107 | * @return byte array of remote interface ip address |
| 108 | * @throws Exception might throws exception while parsing packet |
| 109 | */ |
| 110 | public byte[] getLinkSubTypeTlvBodyAsByteArray() throws Exception { |
| 111 | List<Byte> linkSubTypeBody = new ArrayList<>(); |
| 112 | |
| 113 | for (String remoteAddress : this.remoteInterfaceAddress) { |
| 114 | try { |
| 115 | linkSubTypeBody.addAll(Bytes.asList(InetAddress.getByName(remoteAddress).getAddress())); |
| 116 | } catch (Exception e) { |
| 117 | log.debug("Error::RemoteInterfaceIPAddress:: {}", e.getMessage()); |
| 118 | throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, |
| 119 | OspfErrorType.BAD_MESSAGE); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return Bytes.toArray(linkSubTypeBody); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns instance as string. |
| 128 | * |
| 129 | * @return instance as string |
| 130 | */ |
| 131 | public String toString() { |
| 132 | return MoreObjects.toStringHelper(getClass()) |
| 133 | .add("RemoteInterfaceIPAddress", remoteInterfaceAddress) |
| 134 | .toString(); |
| 135 | } |
| 136 | |
| 137 | } |