Priyanka B | 8f58729 | 2015-09-29 15:28:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.bgpio.types; |
| 17 | |
| 18 | import java.net.InetAddress; |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onosproject.bgpio.exceptions.BGPParseException; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import com.google.common.base.MoreObjects; |
| 28 | import com.google.common.base.Preconditions; |
| 29 | |
| 30 | /** |
| 31 | * Provides Implementation of IPv4AddressTlv. |
| 32 | */ |
| 33 | public class IPv4AddressTlv implements BGPValueType { |
| 34 | private static final Logger log = LoggerFactory.getLogger(IPv4AddressTlv.class); |
| 35 | private static final int LENGTH = 4; |
| 36 | |
| 37 | private Ip4Address address; |
| 38 | private short type; |
| 39 | |
| 40 | /** |
| 41 | * Constructor to initialize parameters. |
| 42 | * |
| 43 | * @param address Ipv4 address of interface/neighbor |
| 44 | * @param type address type |
| 45 | */ |
| 46 | public IPv4AddressTlv(Ip4Address address, short type) { |
| 47 | this.address = Preconditions.checkNotNull(address); |
| 48 | this.type = type; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns Ipv4 address of interface/neighbor. |
| 53 | * |
| 54 | * @return Ipv4 address of interface/neighbor |
| 55 | */ |
| 56 | public Ip4Address getValue() { |
| 57 | return address; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public short getType() { |
| 62 | return this.type; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public int hashCode() { |
| 67 | return Objects.hash(address); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean equals(Object obj) { |
| 72 | if (this == obj) { |
| 73 | return true; |
| 74 | } |
| 75 | if (obj instanceof IPv4AddressTlv) { |
| 76 | IPv4AddressTlv other = (IPv4AddressTlv) obj; |
| 77 | return Objects.equals(this.address, other.address) && Objects.equals(this.type, other.type); |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public int write(ChannelBuffer cb) { |
| 84 | int iLenStartIndex = cb.writerIndex(); |
| 85 | cb.writeShort(type); |
| 86 | cb.writeShort(LENGTH); |
| 87 | cb.writeInt(address.toInt()); |
| 88 | return cb.writerIndex() - iLenStartIndex; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Reads the channel buffer and returns object of IPv4AddressTlv. |
| 93 | * |
| 94 | * @param cb channelBuffer |
| 95 | * @param type address type |
| 96 | * @return object of IPv4AddressTlv |
| 97 | * @throws BGPParseException while parsing IPv4AddressTlv |
| 98 | */ |
| 99 | public static IPv4AddressTlv read(ChannelBuffer cb, short type) throws BGPParseException { |
| 100 | //TODO: use Validation.toInetAddress once Validation is merged |
| 101 | InetAddress ipAddress = (InetAddress) cb.readBytes(LENGTH); |
| 102 | if (ipAddress.isMulticastAddress()) { |
| 103 | throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null); |
| 104 | } |
| 105 | Ip4Address address = Ip4Address.valueOf(ipAddress); |
| 106 | return IPv4AddressTlv.of(address, type); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns object of this class with specified values. |
| 111 | * |
| 112 | * @param address Ipv4 interface/neighbor Address |
| 113 | * @param type says Ipv4 address of interface/neighbor tlv type |
| 114 | * @return object of this class |
| 115 | */ |
| 116 | public static IPv4AddressTlv of(final Ip4Address address , final short type) { |
| 117 | return new IPv4AddressTlv(address, type); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public String toString() { |
| 122 | return MoreObjects.toStringHelper(getClass()) |
| 123 | .add("type", type) |
| 124 | .add("LENGTH", LENGTH) |
| 125 | .add("address", address) |
| 126 | .toString(); |
| 127 | } |
| 128 | } |