blob: 9ad875999b8841f054671ddeb8445690279d24c7 [file] [log] [blame]
Priyanka B8f587292015-09-29 15:28:00 +05301/*
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 */
16package org.onosproject.bgpio.types;
17
18import java.net.InetAddress;
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onlab.packet.Ip6Address;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka Bf6942c62015-11-23 10:55:09 +053024import org.onosproject.bgpio.util.Validation;
Priyanka B8f587292015-09-29 15:28:00 +053025
26import com.google.common.base.MoreObjects;
27import com.google.common.base.Preconditions;
28
29/**
30 * Provides Implementation of IPv6AddressTlv.
31 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053032public class IPv6AddressTlv implements BgpValueType {
Priyanka B8f587292015-09-29 15:28:00 +053033 private static final int LENGTH = 16;
34
35 private final Ip6Address address;
36 private short type;
37
38 /**
39 * Constructor to initialize parameters.
40 *
41 * @param address Ipv6 address of interface/neighbor
42 * @param type address type
43 */
44 public IPv6AddressTlv(Ip6Address address, short type) {
45 this.address = Preconditions.checkNotNull(address);
46 this.type = type;
47 }
48
49 /**
50 * Returns Ipv6 address of interface/neighbor.
51 *
52 * @return Ipv6 address of interface/neighbor
53 */
Priyanka Bf6942c62015-11-23 10:55:09 +053054 public Ip6Address address() {
Priyanka B8f587292015-09-29 15:28:00 +053055 return address;
56 }
57
58 @Override
59 public short getType() {
60 return 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 IPv6AddressTlv) {
74 IPv6AddressTlv other = (IPv6AddressTlv) 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.writeBytes(address.toOctets());
86 return cb.writerIndex() - iLenStartIndex;
87 }
88
89 /**
90 * Reads the channel buffer and returns object of IPv6AddressTlv.
91 *
92 * @param cb channelBuffer
93 * @param type address type
94 * @return object of IPv6AddressTlv
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053095 * @throws BgpParseException while parsing IPv6AddressTlv
Priyanka B8f587292015-09-29 15:28:00 +053096 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053097 public static IPv6AddressTlv read(ChannelBuffer cb, short type) throws BgpParseException {
Priyanka Bf6942c62015-11-23 10:55:09 +053098 InetAddress ipAddress = Validation.toInetAddress(LENGTH, cb);
Priyanka B8f587292015-09-29 15:28:00 +053099 if (ipAddress.isMulticastAddress()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530100 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
Priyanka B8f587292015-09-29 15:28:00 +0530101 }
102 Ip6Address address = Ip6Address.valueOf(ipAddress);
103 return IPv6AddressTlv.of(address, type);
104 }
105
106 /**
107 * Returns object of this class with specified values.
108 *
109 * @param address Ipv6 interface/neighbor address
110 * @param type says Ipv6 address of interface/neighbor tlv type
111 * @return object of this class
112 */
Jian Li68c4fc42016-01-11 16:07:03 -0800113 public static IPv6AddressTlv of(final Ip6Address address, final short type) {
Priyanka B8f587292015-09-29 15:28:00 +0530114 return new IPv6AddressTlv(address, type);
115 }
116
117 @Override
Priyanka B02040732015-11-29 11:30:29 +0530118 public int compareTo(Object o) {
119 if (this.equals(o)) {
120 return 0;
121 }
122 return ((Ip6Address) (this.address)).compareTo((Ip6Address) (((IPv6AddressTlv) o).address));
123 }
124
125 @Override
Priyanka B8f587292015-09-29 15:28:00 +0530126 public String toString() {
127 return MoreObjects.toStringHelper(getClass())
128 .add("type", type)
129 .add("LENGTH", LENGTH)
130 .add("address", address)
131 .toString();
132 }
133}