blob: 517e6fc0141a09bc92a976990627ee62ea94af6f [file] [log] [blame]
Thejaswi N K53beb002015-10-13 20:21:34 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thejaswi N K53beb002015-10-13 20:21:34 +05303 *
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.attr;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onlab.packet.Ip6Address;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053022import org.onosproject.bgpio.exceptions.BgpParseException;
23import org.onosproject.bgpio.types.BgpErrorType;
24import org.onosproject.bgpio.types.BgpValueType;
Thejaswi N K53beb002015-10-13 20:21:34 +053025import org.onosproject.bgpio.util.Validation;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import com.google.common.base.MoreObjects;
30
31/**
32 * Implements BGP attribute IPv6 router ID.
33 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053034public final class BgpAttrRouterIdV6 implements BgpValueType {
Thejaswi N K53beb002015-10-13 20:21:34 +053035
Ray Milkey9c9cde42018-01-12 14:22:06 -080036 private static final Logger log = LoggerFactory
Thejaswi N K53beb002015-10-13 20:21:34 +053037 .getLogger(BgpAttrRouterIdV6.class);
38
Thejaswi N Ke05c9bb2015-11-02 19:37:31 +053039 private final short sType;
Thejaswi N K53beb002015-10-13 20:21:34 +053040
41 /* IPv4 Router-ID of Node */
Thejaswi N Ke05c9bb2015-11-02 19:37:31 +053042 private final Ip6Address ip6RouterId;
Thejaswi N K53beb002015-10-13 20:21:34 +053043
44 /**
45 * Constructor to initialize the value.
46 *
47 * @param ip6RouterId IPV6 address of the router ID
48 * @param sType TLV type
49 */
Thejaswi N Ke05c9bb2015-11-02 19:37:31 +053050 private BgpAttrRouterIdV6(Ip6Address ip6RouterId, short sType) {
Thejaswi N K53beb002015-10-13 20:21:34 +053051 this.ip6RouterId = ip6RouterId;
52 this.sType = sType;
53 }
54
55 /**
Thejaswi N Ke05c9bb2015-11-02 19:37:31 +053056 * Returns object of this class with specified values.
57 *
58 * @param ip6RouterId IPV6 address of the router ID
59 * @param sType TLV type
60 * @return object of BgpAttrRouterIdV6
61 */
62 public static BgpAttrRouterIdV6 of(final Ip6Address ip6RouterId,
63 final short sType) {
64 return new BgpAttrRouterIdV6(ip6RouterId, sType);
65 }
66
67 /**
Thejaswi N K53beb002015-10-13 20:21:34 +053068 * Reads the IPv6 Router-ID.
69 *
70 * @param cb ChannelBuffer
Thejaswi N Ke05c9bb2015-11-02 19:37:31 +053071 * @param sType TLV type
Thejaswi N K53beb002015-10-13 20:21:34 +053072 * @return object of BgpAttrRouterIdV6
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053073 * @throws BgpParseException while parsing BgpAttrRouterIdV6
Thejaswi N K53beb002015-10-13 20:21:34 +053074 */
75 public static BgpAttrRouterIdV6 read(ChannelBuffer cb, short sType)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053076 throws BgpParseException {
Thejaswi N K53beb002015-10-13 20:21:34 +053077 byte[] ipBytes;
78 Ip6Address ip6RouterId;
79
80 short lsAttrLength = cb.readShort();
81
Thejaswi N Ke05c9bb2015-11-02 19:37:31 +053082 if ((lsAttrLength != 16) || (cb.readableBytes() < lsAttrLength)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053083 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
84 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N K53beb002015-10-13 20:21:34 +053085 lsAttrLength);
86 }
87
88 ipBytes = new byte[lsAttrLength];
89 cb.readBytes(ipBytes);
90 ip6RouterId = Ip6Address.valueOf(ipBytes);
Thejaswi N Ke05c9bb2015-11-02 19:37:31 +053091 return BgpAttrRouterIdV6.of(ip6RouterId, sType);
Thejaswi N K53beb002015-10-13 20:21:34 +053092 }
93
94 /**
95 * Returns IPV6 router ID.
96 *
97 * @return Router ID
98 */
Thejaswi N Ke05c9bb2015-11-02 19:37:31 +053099 public Ip6Address attrRouterId() {
Thejaswi N K53beb002015-10-13 20:21:34 +0530100 return ip6RouterId;
101 }
102
103 @Override
104 public short getType() {
105 return sType;
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash(ip6RouterId);
111 }
112
113 @Override
114 public boolean equals(Object obj) {
115 if (this == obj) {
116 return true;
117 }
118
119 if (obj instanceof BgpAttrRouterIdV6) {
120 BgpAttrRouterIdV6 other = (BgpAttrRouterIdV6) obj;
121 return Objects.equals(ip6RouterId, other.ip6RouterId);
122 }
123 return false;
124 }
125
126 @Override
127 public int write(ChannelBuffer cb) {
Thejaswi N Ke05c9bb2015-11-02 19:37:31 +0530128 // TODO This will be implemented in the next version
Thejaswi N K53beb002015-10-13 20:21:34 +0530129 return 0;
130 }
131
132 @Override
133 public String toString() {
134 return MoreObjects.toStringHelper(getClass()).omitNullValues()
135 .add("ip6RouterId", ip6RouterId).toString();
136 }
Priyanka B02040732015-11-29 11:30:29 +0530137
138 @Override
139 public int compareTo(Object o) {
140 // TODO Auto-generated method stub
141 return 0;
142 }
Thejaswi N K53beb002015-10-13 20:21:34 +0530143}