Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +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.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; |
| 27 | |
| 28 | import com.google.common.base.MoreObjects; |
| 29 | |
| 30 | /** |
| 31 | * Implements BGP attribute node flag. |
| 32 | */ |
| 33 | public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
| 34 | |
| 35 | protected static final Logger log = LoggerFactory |
| 36 | .getLogger(BgpAttrNodeFlagBitTlv.class); |
| 37 | |
| 38 | public static final int ATTRNODE_FLAGBIT = 1024; |
| 39 | |
| 40 | /* Node flag bit TLV */ |
| 41 | private boolean bOverloadBit; |
| 42 | private boolean bAttachedBit; |
| 43 | private boolean bExternalBit; |
| 44 | private boolean bABRBit; |
| 45 | |
| 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; |
| 51 | |
| 52 | /** |
| 53 | * Constructor to initialize parameters. |
| 54 | * |
| 55 | * @param bOverloadBit Overload bit |
| 56 | * @param bAttachedBit Attached bit |
| 57 | * @param bExternalBit External bit |
| 58 | * @param bABRBit ABR Bit |
| 59 | */ |
| 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; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Reads the Node Flag Bits. |
| 70 | * |
| 71 | * @param cb ChannelBuffer |
| 72 | * @return attribute node flag bit tlv |
| 73 | * @throws BGPParseException while parsing BgpAttrNodeFlagBitTlv |
| 74 | */ |
| 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; |
| 81 | |
| 82 | short lsAttrLength = cb.readShort(); |
| 83 | |
| 84 | if (lsAttrLength != 1) { |
| 85 | Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, |
| 86 | BGPErrorType.ATTRIBUTE_LENGTH_ERROR, |
| 87 | lsAttrLength); |
| 88 | } |
| 89 | |
| 90 | byte nodeFlagBits = cb.readByte(); |
| 91 | |
| 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); |
| 96 | |
| 97 | return new BgpAttrNodeFlagBitTlv(bOverloadBit, bAttachedBit, |
| 98 | bExternalBit, bABRBit); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns Overload Bit. |
| 103 | * |
| 104 | * @return Overload Bit |
| 105 | */ |
| 106 | boolean getOverLoadBit() { |
| 107 | return bOverloadBit; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns Attached Bit. |
| 112 | * |
| 113 | * @return Attached Bit |
| 114 | */ |
| 115 | boolean getAttachedBit() { |
| 116 | return bAttachedBit; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns External Bit. |
| 121 | * |
| 122 | * @return External Bit |
| 123 | */ |
| 124 | boolean getExternalBit() { |
| 125 | return bExternalBit; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns ABR Bit. |
| 130 | * |
| 131 | * @return ABR Bit |
| 132 | */ |
| 133 | boolean getABRBit() { |
| 134 | return bABRBit; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public short getType() { |
| 139 | return ATTRNODE_FLAGBIT; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public int write(ChannelBuffer cb) { |
| 144 | // TODO will be implementing it later |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public int hashCode() { |
| 150 | return Objects.hash(bOverloadBit, bAttachedBit, bExternalBit, bABRBit); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public boolean equals(Object obj) { |
| 155 | if (this == obj) { |
| 156 | return true; |
| 157 | } |
| 158 | |
| 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); |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public String toString() { |
| 171 | return MoreObjects.toStringHelper(getClass()) |
| 172 | .add("bOverloadBit", bOverloadBit) |
| 173 | .add("bAttachedBit", bAttachedBit) |
| 174 | .add("bExternalBit", bExternalBit).add("bABRBit", bABRBit) |
| 175 | .toString(); |
| 176 | } |
| 177 | } |