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