blob: a87825fc7ee9579717caac10981eda2787e7b0a6 [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 +053025import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import com.google.common.base.MoreObjects;
29import com.google.common.base.Preconditions;
30
31/**
32 * Provides Implementation of IPv6AddressTlv.
33 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053034public class IPv6AddressTlv implements BgpValueType {
Priyanka B8f587292015-09-29 15:28:00 +053035 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 Bf6942c62015-11-23 10:55:09 +053057 public Ip6Address address() {
Priyanka B8f587292015-09-29 15:28:00 +053058 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 VH5dd8dbe2015-11-26 13:22:18 +053098 * @throws BgpParseException while parsing IPv6AddressTlv
Priyanka B8f587292015-09-29 15:28:00 +053099 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530100 public static IPv6AddressTlv read(ChannelBuffer cb, short type) throws BgpParseException {
Priyanka Bf6942c62015-11-23 10:55:09 +0530101 InetAddress ipAddress = Validation.toInetAddress(LENGTH, cb);
Priyanka B8f587292015-09-29 15:28:00 +0530102 if (ipAddress.isMulticastAddress()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530103 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
Priyanka B8f587292015-09-29 15:28:00 +0530104 }
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}