Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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.Ip4Address; |
| 22 | import org.onosproject.bgpio.exceptions.BGPParseException; |
| 23 | import org.onosproject.bgpio.types.BGPErrorType; |
| 24 | import org.onosproject.bgpio.types.BGPValueType; |
| 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 node router ID. |
| 33 | */ |
Thejaswi N K | 76c892a | 2015-11-02 16:54:31 +0530 | [diff] [blame] | 34 | public final class BgpAttrRouterIdV4 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(BgpAttrRouterIdV4.class); |
| 38 | |
Thejaswi N K | 76c892a | 2015-11-02 16:54: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 | 76c892a | 2015-11-02 16:54:31 +0530 | [diff] [blame] | 42 | private final Ip4Address ip4RouterId; |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Constructor to initialize the value. |
| 46 | * |
| 47 | * @param ip4RouterId IPV4 address of router |
| 48 | * @param sType TLV type |
| 49 | */ |
Thejaswi N K | 76c892a | 2015-11-02 16:54:31 +0530 | [diff] [blame] | 50 | private BgpAttrRouterIdV4(Ip4Address ip4RouterId, short sType) { |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 51 | this.ip4RouterId = ip4RouterId; |
| 52 | this.sType = sType; |
| 53 | } |
| 54 | |
| 55 | /** |
Thejaswi N K | 76c892a | 2015-11-02 16:54:31 +0530 | [diff] [blame] | 56 | * Returns object of this class with specified values. |
| 57 | * |
| 58 | * @param ip4RouterId IPv4 address |
| 59 | * @param sType Type of this TLV |
| 60 | * @return object of BgpAttrRouterIdV4 |
| 61 | */ |
| 62 | public static BgpAttrRouterIdV4 of(final Ip4Address ip4RouterId, |
| 63 | final short sType) { |
| 64 | return new BgpAttrRouterIdV4(ip4RouterId, sType); |
| 65 | } |
| 66 | |
| 67 | /** |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 68 | * Reads the IPv4 Router-ID. |
| 69 | * |
| 70 | * @param cb ChannelBuffer |
Thejaswi N K | 76c892a | 2015-11-02 16:54:31 +0530 | [diff] [blame] | 71 | * @param sType tag type |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 72 | * @return object of BgpAttrRouterIdV4 |
Thejaswi N K | 76c892a | 2015-11-02 16:54:31 +0530 | [diff] [blame] | 73 | * @throws BGPParseException while parsing BgpAttrRouterIdV4 |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 74 | */ |
| 75 | public static BgpAttrRouterIdV4 read(ChannelBuffer cb, short sType) |
| 76 | throws BGPParseException { |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 77 | short lsAttrLength = cb.readShort(); |
| 78 | |
Thejaswi N K | 76c892a | 2015-11-02 16:54:31 +0530 | [diff] [blame] | 79 | if ((lsAttrLength != 4) || (cb.readableBytes() < lsAttrLength)) { |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 80 | Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, |
| 81 | BGPErrorType.ATTRIBUTE_LENGTH_ERROR, |
| 82 | lsAttrLength); |
| 83 | } |
| 84 | |
Thejaswi N K | 76c892a | 2015-11-02 16:54:31 +0530 | [diff] [blame] | 85 | byte[] ipBytes = new byte[lsAttrLength]; |
| 86 | cb.readBytes(ipBytes, 0, lsAttrLength); |
| 87 | Ip4Address ip4RouterId = Ip4Address.valueOf(ipBytes); |
| 88 | return BgpAttrRouterIdV4.of(ip4RouterId, sType); |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the IPV4 router ID. |
| 93 | * |
| 94 | * @return Router ID |
| 95 | */ |
Thejaswi N K | 76c892a | 2015-11-02 16:54:31 +0530 | [diff] [blame] | 96 | public Ip4Address attrRouterId() { |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 97 | return ip4RouterId; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public short getType() { |
| 102 | return sType; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public int hashCode() { |
| 107 | return Objects.hash(ip4RouterId); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean equals(Object obj) { |
| 112 | if (this == obj) { |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | if (obj instanceof BgpAttrRouterIdV4) { |
| 117 | BgpAttrRouterIdV4 other = (BgpAttrRouterIdV4) obj; |
| 118 | return Objects.equals(ip4RouterId, other.ip4RouterId); |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public int write(ChannelBuffer cb) { |
Thejaswi N K | 76c892a | 2015-11-02 16:54:31 +0530 | [diff] [blame] | 125 | // TODO This will be implemented in the next version |
Thejaswi N K | 53beb00 | 2015-10-13 20:21:34 +0530 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String toString() { |
| 131 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 132 | .add("ip4RouterId", ip4RouterId).toString(); |
| 133 | } |
| 134 | } |