2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.bgpio.types.attr;
18 import java.util.Objects;
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onlab.packet.Ip4Address;
22 import org.onlab.packet.Ip6Address;
23 import org.onosproject.bgpio.exceptions.BgpParseException;
24 import org.onosproject.bgpio.types.BgpErrorType;
25 import org.onosproject.bgpio.types.BgpValueType;
26 import org.onosproject.bgpio.util.Validation;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.MoreObjects;
33 * Implements BGP prefix OSPF Forwarding address attribute.
35 public class BgpPrefixAttrOspfFwdAddr implements BgpValueType {
37 protected static final Logger log = LoggerFactory
38 .getLogger(BgpPrefixAttrOspfFwdAddr.class);
40 public static final int ATTR_PREFIX_OSPFFWDADDR = 1156;
41 public static final int IPV4_LEN = 4;
42 public static final int IPV6_LEN = 16;
44 /* OSPF Forwarding Address */
45 private final short lsAttrLength;
46 private final Ip4Address ip4RouterId;
47 private final Ip6Address ip6RouterId;
50 * Constructor to initialize the value.
52 * @param lsAttrLength length of the IP address
53 * @param ip4RouterId Valid IPV4 address if length is 4 else null
54 * @param ip6RouterId Valid IPV6 address if length is 16 else null
56 public BgpPrefixAttrOspfFwdAddr(short lsAttrLength, Ip4Address ip4RouterId,
57 Ip6Address ip6RouterId) {
58 this.lsAttrLength = lsAttrLength;
59 this.ip4RouterId = ip4RouterId;
60 this.ip6RouterId = ip6RouterId;
64 * Returns object of this class with specified values.
66 * @param lsAttrLength length of the IP address
67 * @param ip4RouterId Valid IPV4 address if length is 4 else null
68 * @param ip6RouterId Valid IPV6 address if length is 16 else null
69 * @return object of BgpPrefixAttrOspfFwdAddr
71 public static BgpPrefixAttrOspfFwdAddr of(final short lsAttrLength,
72 final Ip4Address ip4RouterId,
73 final Ip6Address ip6RouterId) {
74 return new BgpPrefixAttrOspfFwdAddr(lsAttrLength, ip4RouterId,
79 * Reads the OSPF Forwarding Address.
81 * @param cb ChannelBuffer
82 * @return object of BgpPrefixAttrOSPFFwdAddr
83 * @throws BgpParseException while parsing BgpPrefixAttrOspfFwdAddr
85 public static BgpPrefixAttrOspfFwdAddr read(ChannelBuffer cb)
86 throws BgpParseException {
89 Ip4Address ip4RouterId = null;
90 Ip6Address ip6RouterId = null;
92 lsAttrLength = cb.readShort();
93 ipBytes = new byte[lsAttrLength];
95 if ((cb.readableBytes() < lsAttrLength)) {
96 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
97 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
101 cb.readBytes(ipBytes);
103 if (IPV4_LEN == lsAttrLength) {
104 ip4RouterId = Ip4Address.valueOf(ipBytes);
105 } else if (IPV6_LEN == lsAttrLength) {
106 ip6RouterId = Ip6Address.valueOf(ipBytes);
109 return BgpPrefixAttrOspfFwdAddr.of(lsAttrLength, ip4RouterId,
114 * Returns IPV4 Address of OSPF forwarding address.
116 * @return IPV4 address
118 public Ip4Address ospfv4FwdAddr() {
123 * Returns IPV6 Address of OSPF forwarding address.
125 * @return IPV6 address
127 public Ip6Address ospfv6FwdAddr() {
132 * Returns OSPF forwarding address length.
134 * @return length of the ip address
136 public short ospfFwdAddrLen() {
141 public short getType() {
142 return ATTR_PREFIX_OSPFFWDADDR;
146 public int hashCode() {
147 if (IPV4_LEN == lsAttrLength) {
148 return Objects.hash(ip4RouterId);
150 return Objects.hash(ip6RouterId);
155 public boolean equals(Object obj) {
160 if (obj instanceof BgpPrefixAttrOspfFwdAddr) {
161 BgpPrefixAttrOspfFwdAddr other = (BgpPrefixAttrOspfFwdAddr) obj;
162 if (IPV4_LEN == lsAttrLength) {
163 return Objects.equals(ip4RouterId, other.ip4RouterId);
165 return Objects.equals(ip6RouterId, other.ip6RouterId);
172 public int write(ChannelBuffer cb) {
173 // TODO This will be implemented in the next version
178 public String toString() {
179 if (IPV4_LEN == lsAttrLength) {
180 return MoreObjects.toStringHelper(getClass()).omitNullValues()
181 .add("ip4RouterId", ip4RouterId).toString();
183 return MoreObjects.toStringHelper(getClass()).omitNullValues()
184 .add("ip6RouterId", ip6RouterId).toString();
189 public int compareTo(Object o) {
190 // TODO Auto-generated method stub