Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.bgpio.types.attr; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onlab.packet.Ip6Address; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 22 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 23 | import org.onosproject.bgpio.types.BgpErrorType; |
| 24 | import org.onosproject.bgpio.types.BgpValueType; |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 25 | import org.onosproject.bgpio.util.Validation; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import com.google.common.base.MoreObjects; |
| 30 | |
| 31 | /** |
| 32 | * Implements BGP attribute IPv6 router ID. |
| 33 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 34 | public final class BgpAttrRouterIdV6 implements BgpValueType { |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 35 | |
| 36 | protected static final Logger log = LoggerFactory |
| 37 | .getLogger(BgpAttrRouterIdV6.class); |
| 38 | |
Thejaswi N K | e05c9bb | 2015-11-02 19:37:31 +0530 | [diff] [blame] | 39 | private final short sType; |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 40 | |
| 41 | /* IPv4 Router-ID of Node */ |
Thejaswi N K | e05c9bb | 2015-11-02 19:37:31 +0530 | [diff] [blame] | 42 | private final Ip6Address ip6RouterId; |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 43 | |
| 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 K | e05c9bb | 2015-11-02 19:37:31 +0530 | [diff] [blame] | 50 | private BgpAttrRouterIdV6(Ip6Address ip6RouterId, short sType) { |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 51 | this.ip6RouterId = ip6RouterId; |
| 52 | this.sType = sType; |
| 53 | } |
| 54 | |
| 55 | /** |
Thejaswi N K | e05c9bb | 2015-11-02 19:37:31 +0530 | [diff] [blame] | 56 | * 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 K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 68 | * Reads the IPv6 Router-ID. |
| 69 | * |
| 70 | * @param cb ChannelBuffer |
Thejaswi N K | e05c9bb | 2015-11-02 19:37:31 +0530 | [diff] [blame] | 71 | * @param sType TLV type |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 72 | * @return object of BgpAttrRouterIdV6 |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 73 | * @throws BgpParseException while parsing BgpAttrRouterIdV6 |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 74 | */ |
| 75 | public static BgpAttrRouterIdV6 read(ChannelBuffer cb, short sType) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 76 | throws BgpParseException { |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 77 | byte[] ipBytes; |
| 78 | Ip6Address ip6RouterId; |
| 79 | |
| 80 | short lsAttrLength = cb.readShort(); |
| 81 | |
Thejaswi N K | e05c9bb | 2015-11-02 19:37:31 +0530 | [diff] [blame] | 82 | if ((lsAttrLength != 16) || (cb.readableBytes() < lsAttrLength)) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 83 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 84 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 85 | lsAttrLength); |
| 86 | } |
| 87 | |
| 88 | ipBytes = new byte[lsAttrLength]; |
| 89 | cb.readBytes(ipBytes); |
| 90 | ip6RouterId = Ip6Address.valueOf(ipBytes); |
Thejaswi N K | e05c9bb | 2015-11-02 19:37:31 +0530 | [diff] [blame] | 91 | return BgpAttrRouterIdV6.of(ip6RouterId, sType); |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns IPV6 router ID. |
| 96 | * |
| 97 | * @return Router ID |
| 98 | */ |
Thejaswi N K | e05c9bb | 2015-11-02 19:37:31 +0530 | [diff] [blame] | 99 | public Ip6Address attrRouterId() { |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 100 | 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 K | e05c9bb | 2015-11-02 19:37:31 +0530 | [diff] [blame] | 128 | // TODO This will be implemented in the next version |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String toString() { |
| 134 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 135 | .add("ip6RouterId", ip6RouterId).toString(); |
| 136 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 137 | |
| 138 | @Override |
| 139 | public int compareTo(Object o) { |
| 140 | // TODO Auto-generated method stub |
| 141 | return 0; |
| 142 | } |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 143 | } |