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 final 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 final boolean bOverloadBit;
42 private final boolean bAttachedBit;
43 private final boolean bExternalBit;
44 private final boolean bAbrBit;
46 public static final byte FIRST_BIT = (byte) 0x80;
47 public static final byte SECOND_BIT = 0x40;
48 public static final byte THIRD_BIT = 0x20;
49 public static final byte FOURTH_BIT = 0x01;
52 * Constructor to initialize parameters.
54 * @param bOverloadBit Overload bit
55 * @param bAttachedBit Attached bit
56 * @param bExternalBit External bit
57 * @param bAbrBit ABR Bit
59 private BgpAttrNodeFlagBitTlv(boolean bOverloadBit, boolean bAttachedBit,
60 boolean bExternalBit, boolean bAbrBit) {
61 this.bOverloadBit = bOverloadBit;
62 this.bAttachedBit = bAttachedBit;
63 this.bExternalBit = bExternalBit;
64 this.bAbrBit = bAbrBit;
68 * Returns object of this class with specified values.
70 * @param bOverloadBit Overload bit
71 * @param bAttachedBit Attached bit
72 * @param bExternalBit External bit
73 * @param bAbrBit ABR Bit
74 * @return object of BgpAttrNodeFlagBitTlv
76 public static BgpAttrNodeFlagBitTlv of(final boolean bOverloadBit,
77 final boolean bAttachedBit,
78 final boolean bExternalBit,
79 final boolean bAbrBit) {
80 return new BgpAttrNodeFlagBitTlv(bOverloadBit, bAttachedBit,
81 bExternalBit, bAbrBit);
85 * Reads the Node Flag Bits.
87 * @param cb ChannelBuffer
88 * @return attribute node flag bit tlv
89 * @throws BgpParseException while parsing BgpAttrNodeFlagBitTlv
91 public static BgpAttrNodeFlagBitTlv read(ChannelBuffer cb)
92 throws BgpParseException {
93 boolean bOverloadBit = false;
94 boolean bAttachedBit = false;
95 boolean bExternalBit = false;
96 boolean bAbrBit = false;
98 short lsAttrLength = cb.readShort();
100 if ((lsAttrLength != 1) || (cb.readableBytes() < lsAttrLength)) {
101 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
102 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
106 byte nodeFlagBits = cb.readByte();
108 bOverloadBit = ((nodeFlagBits & FIRST_BIT) == FIRST_BIT);
109 bAttachedBit = ((nodeFlagBits & SECOND_BIT) == SECOND_BIT);
110 bExternalBit = ((nodeFlagBits & THIRD_BIT) == THIRD_BIT);
111 bAbrBit = ((nodeFlagBits & FOURTH_BIT) == FOURTH_BIT);
113 return BgpAttrNodeFlagBitTlv.of(bOverloadBit, bAttachedBit,
114 bExternalBit, bAbrBit);
118 * Returns Overload Bit.
120 * @return Overload Bit
122 public boolean overLoadBit() {
127 * Returns Attached Bit.
129 * @return Attached Bit
131 public boolean attachedBit() {
136 * Returns External Bit.
138 * @return External Bit
140 public boolean externalBit() {
149 public boolean abrBit() {
154 public short getType() {
155 return ATTRNODE_FLAGBIT;
159 public int write(ChannelBuffer cb) {
160 // TODO This will be implemented in the next version
165 public int hashCode() {
166 return Objects.hash(bOverloadBit, bAttachedBit, bExternalBit, bAbrBit);
170 public boolean equals(Object obj) {
175 if (obj instanceof BgpAttrNodeFlagBitTlv) {
176 BgpAttrNodeFlagBitTlv other = (BgpAttrNodeFlagBitTlv) obj;
177 return Objects.equals(bOverloadBit, other.bOverloadBit)
178 && Objects.equals(bAttachedBit, other.bAttachedBit)
179 && Objects.equals(bExternalBit, other.bExternalBit)
180 && Objects.equals(bAbrBit, other.bAbrBit);
186 public String toString() {
187 return MoreObjects.toStringHelper(getClass())
188 .add("bOverloadBit", bOverloadBit)
189 .add("bAttachedBit", bAttachedBit)
190 .add("bExternalBit", bExternalBit).add("bAbrBit", bAbrBit)
195 public int compareTo(Object o) {
196 // TODO Auto-generated method stub