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