blob: 65b7c16d2061e3f0de064262a3a78cb3874a25f8 [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;
23import org.onosproject.bgpio.exceptions.BGPParseException;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import com.google.common.base.MoreObjects;
28import com.google.common.base.Preconditions;
29
30/**
31 * Provides Implementation of IPv6AddressTlv.
32 */
33public class IPv6AddressTlv implements BGPValueType {
34 private static final Logger log = LoggerFactory.getLogger(IPv6AddressTlv.class);
35 private static final int LENGTH = 16;
36
37 private final Ip6Address address;
38 private short type;
39
40 /**
41 * Constructor to initialize parameters.
42 *
43 * @param address Ipv6 address of interface/neighbor
44 * @param type address type
45 */
46 public IPv6AddressTlv(Ip6Address address, short type) {
47 this.address = Preconditions.checkNotNull(address);
48 this.type = type;
49 }
50
51 /**
52 * Returns Ipv6 address of interface/neighbor.
53 *
54 * @return Ipv6 address of interface/neighbor
55 */
56 public Ip6Address getValue() {
57 return address;
58 }
59
60 @Override
61 public short getType() {
62 return 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 IPv6AddressTlv) {
76 IPv6AddressTlv other = (IPv6AddressTlv) 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.writeBytes(address.toOctets());
88 return cb.writerIndex() - iLenStartIndex;
89 }
90
91 /**
92 * Reads the channel buffer and returns object of IPv6AddressTlv.
93 *
94 * @param cb channelBuffer
95 * @param type address type
96 * @return object of IPv6AddressTlv
97 * @throws BGPParseException while parsing IPv6AddressTlv
98 */
99 public static IPv6AddressTlv 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 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}