Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [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; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import com.google.common.base.MoreObjects; |
| 25 | |
| 26 | /** |
| 27 | * Provides FourOctetAsNumCapabilityTlv Capability Tlv. |
| 28 | */ |
| 29 | public class FourOctetAsNumCapabilityTlv implements BgpValueType { |
| 30 | |
| 31 | /** |
| 32 | * support to indicate its support for four-octet AS numbers -CAPABILITY TLV format. |
| 33 | */ |
| 34 | protected static final Logger log = LoggerFactory |
| 35 | .getLogger(FourOctetAsNumCapabilityTlv.class); |
| 36 | |
| 37 | public static final byte TYPE = 65; |
| 38 | public static final byte LENGTH = 4; |
| 39 | |
| 40 | private final int rawValue; |
| 41 | |
| 42 | /** |
| 43 | * constructor to initialize rawValue. |
| 44 | * @param rawValue FourOctetAsNumCapabilityTlv |
| 45 | */ |
| 46 | public FourOctetAsNumCapabilityTlv(int rawValue) { |
| 47 | this.rawValue = rawValue; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * constructor to initialize raw. |
| 52 | * @param raw AS number |
| 53 | * @return object of FourOctetAsNumCapabilityTlv |
| 54 | */ |
| 55 | public static FourOctetAsNumCapabilityTlv of(final int raw) { |
| 56 | return new FourOctetAsNumCapabilityTlv(raw); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns value of TLV. |
| 61 | * @return int value of rawValue |
| 62 | */ |
| 63 | public int getInt() { |
| 64 | return rawValue; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public short getType() { |
| 69 | return TYPE; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public int hashCode() { |
| 74 | return Objects.hash(rawValue); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean equals(Object obj) { |
| 79 | if (this == obj) { |
| 80 | return true; |
| 81 | } |
| 82 | if (obj instanceof FourOctetAsNumCapabilityTlv) { |
| 83 | FourOctetAsNumCapabilityTlv other = (FourOctetAsNumCapabilityTlv) obj; |
| 84 | return Objects.equals(rawValue, other.rawValue); |
| 85 | } |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public int write(ChannelBuffer cb) { |
| 91 | int iLenStartIndex = cb.writerIndex(); |
| 92 | cb.writeByte(TYPE); |
| 93 | cb.writeByte(LENGTH); |
| 94 | cb.writeInt(rawValue); |
| 95 | return cb.writerIndex() - iLenStartIndex; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Reads the channel buffer and returns object of FourOctetAsNumCapabilityTlv. |
| 100 | * @param cb type of channel buffer |
| 101 | * @return object of FourOctetAsNumCapabilityTlv |
| 102 | */ |
| 103 | public static FourOctetAsNumCapabilityTlv read(ChannelBuffer cb) { |
| 104 | return FourOctetAsNumCapabilityTlv.of(cb.readInt()); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return MoreObjects.toStringHelper(getClass()) |
| 110 | .add("Type", TYPE) |
| 111 | .add("Length", LENGTH) |
| 112 | .add("Value", rawValue).toString(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public int compareTo(Object o) { |
| 117 | // TODO Auto-generated method stub |
| 118 | return 0; |
| 119 | } |
| 120 | } |