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.onosproject.bgpio.exceptions.BGPParseException;
22 import org.onosproject.bgpio.types.BGPErrorType;
23 import org.onosproject.bgpio.types.BGPValueType;
24 import org.onosproject.bgpio.util.Validation;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.MoreObjects;
31 * Implements BGP attribute node flag.
33 public class BgpAttrNodeFlagBitTlv implements BGPValueType {
35 protected static final Logger log = LoggerFactory
36 .getLogger(BgpAttrNodeFlagBitTlv.class);
38 public static final int ATTRNODE_FLAGBIT = 1024;
40 /* Node flag bit TLV */
41 private boolean bOverloadBit;
42 private boolean bAttachedBit;
43 private boolean bExternalBit;
44 private boolean bABRBit;
46 public static final int BIT_SET = 1;
47 public static final int FIRST_BIT = 0x80;
48 public static final int SECOND_BIT = 0x40;
49 public static final int THIRD_BIT = 0x20;
50 public static final int FOURTH_BIT = 0x01;
53 * Constructor to initialize parameters.
55 * @param bOverloadBit Overload bit
56 * @param bAttachedBit Attached bit
57 * @param bExternalBit External bit
58 * @param bABRBit ABR Bit
60 BgpAttrNodeFlagBitTlv(boolean bOverloadBit, boolean bAttachedBit,
61 boolean bExternalBit, boolean bABRBit) {
62 this.bOverloadBit = bOverloadBit;
63 this.bAttachedBit = bAttachedBit;
64 this.bExternalBit = bExternalBit;
65 this.bABRBit = bABRBit;
69 * Reads the Node Flag Bits.
71 * @param cb ChannelBuffer
72 * @return attribute node flag bit tlv
73 * @throws BGPParseException while parsing BgpAttrNodeFlagBitTlv
75 public static BgpAttrNodeFlagBitTlv read(ChannelBuffer cb)
76 throws BGPParseException {
77 boolean bOverloadBit = false;
78 boolean bAttachedBit = false;
79 boolean bExternalBit = false;
80 boolean bABRBit = false;
82 short lsAttrLength = cb.readShort();
84 if (lsAttrLength != 1) {
85 Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
86 BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
90 byte nodeFlagBits = cb.readByte();
92 bOverloadBit = ((nodeFlagBits & (byte) FIRST_BIT) == FIRST_BIT);
93 bAttachedBit = ((nodeFlagBits & (byte) SECOND_BIT) == SECOND_BIT);
94 bExternalBit = ((nodeFlagBits & (byte) THIRD_BIT) == THIRD_BIT);
95 bABRBit = ((nodeFlagBits & (byte) FOURTH_BIT) == FOURTH_BIT);
97 return new BgpAttrNodeFlagBitTlv(bOverloadBit, bAttachedBit,
98 bExternalBit, bABRBit);
102 * Returns Overload Bit.
104 * @return Overload Bit
106 boolean getOverLoadBit() {
111 * Returns Attached Bit.
113 * @return Attached Bit
115 boolean getAttachedBit() {
120 * Returns External Bit.
122 * @return External Bit
124 boolean getExternalBit() {
133 boolean getABRBit() {
138 public short getType() {
139 return ATTRNODE_FLAGBIT;
143 public int write(ChannelBuffer cb) {
144 // TODO will be implementing it later
149 public int hashCode() {
150 return Objects.hash(bOverloadBit, bAttachedBit, bExternalBit, bABRBit);
154 public boolean equals(Object obj) {
159 if (obj instanceof BgpAttrNodeFlagBitTlv) {
160 BgpAttrNodeFlagBitTlv other = (BgpAttrNodeFlagBitTlv) obj;
161 return Objects.equals(bOverloadBit, other.bOverloadBit)
162 && Objects.equals(bAttachedBit, other.bAttachedBit)
163 && Objects.equals(bExternalBit, other.bExternalBit)
164 && Objects.equals(bABRBit, other.bABRBit);
170 public String toString() {
171 return MoreObjects.toStringHelper(getClass())
172 .add("bOverloadBit", bOverloadBit)
173 .add("bAttachedBit", bAttachedBit)
174 .add("bExternalBit", bExternalBit).add("bABRBit", bABRBit)