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; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 21 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 22 | import org.onosproject.bgpio.types.BgpErrorType; |
| 23 | import org.onosproject.bgpio.types.BgpValueType; |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 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 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 33 | public final class BgpAttrNodeFlagBitTlv implements BgpValueType { |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 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 */ |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 41 | private final boolean bOverloadBit; |
| 42 | private final boolean bAttachedBit; |
| 43 | private final boolean bExternalBit; |
| 44 | private final boolean bAbrBit; |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 45 | |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 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; |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Constructor to initialize parameters. |
| 53 | * |
| 54 | * @param bOverloadBit Overload bit |
| 55 | * @param bAttachedBit Attached bit |
| 56 | * @param bExternalBit External bit |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 57 | * @param bAbrBit ABR Bit |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 58 | */ |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 59 | private BgpAttrNodeFlagBitTlv(boolean bOverloadBit, boolean bAttachedBit, |
| 60 | boolean bExternalBit, boolean bAbrBit) { |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 61 | this.bOverloadBit = bOverloadBit; |
| 62 | this.bAttachedBit = bAttachedBit; |
| 63 | this.bExternalBit = bExternalBit; |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 64 | this.bAbrBit = bAbrBit; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns object of this class with specified values. |
| 69 | * |
| 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 |
| 75 | */ |
| 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); |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Reads the Node Flag Bits. |
| 86 | * |
| 87 | * @param cb ChannelBuffer |
| 88 | * @return attribute node flag bit tlv |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 89 | * @throws BgpParseException while parsing BgpAttrNodeFlagBitTlv |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 90 | */ |
| 91 | public static BgpAttrNodeFlagBitTlv read(ChannelBuffer cb) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 92 | throws BgpParseException { |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 93 | boolean bOverloadBit = false; |
| 94 | boolean bAttachedBit = false; |
| 95 | boolean bExternalBit = false; |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 96 | boolean bAbrBit = false; |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 97 | |
| 98 | short lsAttrLength = cb.readShort(); |
| 99 | |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 100 | if ((lsAttrLength != 1) || (cb.readableBytes() < lsAttrLength)) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 101 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 102 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 103 | lsAttrLength); |
| 104 | } |
| 105 | |
| 106 | byte nodeFlagBits = cb.readByte(); |
| 107 | |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 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); |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 112 | |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 113 | return BgpAttrNodeFlagBitTlv.of(bOverloadBit, bAttachedBit, |
| 114 | bExternalBit, bAbrBit); |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns Overload Bit. |
| 119 | * |
| 120 | * @return Overload Bit |
| 121 | */ |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 122 | public boolean overLoadBit() { |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 123 | return bOverloadBit; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns Attached Bit. |
| 128 | * |
| 129 | * @return Attached Bit |
| 130 | */ |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 131 | public boolean attachedBit() { |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 132 | return bAttachedBit; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns External Bit. |
| 137 | * |
| 138 | * @return External Bit |
| 139 | */ |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 140 | public boolean externalBit() { |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 141 | return bExternalBit; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns ABR Bit. |
| 146 | * |
| 147 | * @return ABR Bit |
| 148 | */ |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 149 | public boolean abrBit() { |
| 150 | return bAbrBit; |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public short getType() { |
| 155 | return ATTRNODE_FLAGBIT; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public int write(ChannelBuffer cb) { |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 160 | // TODO This will be implemented in the next version |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public int hashCode() { |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 166 | return Objects.hash(bOverloadBit, bAttachedBit, bExternalBit, bAbrBit); |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public boolean equals(Object obj) { |
| 171 | if (this == obj) { |
| 172 | return true; |
| 173 | } |
| 174 | |
| 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) |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 180 | && Objects.equals(bAbrBit, other.bAbrBit); |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 181 | } |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public String toString() { |
| 187 | return MoreObjects.toStringHelper(getClass()) |
| 188 | .add("bOverloadBit", bOverloadBit) |
| 189 | .add("bAttachedBit", bAttachedBit) |
Thejaswi N K | 5417783 | 2015-11-02 16:15:06 +0530 | [diff] [blame] | 190 | .add("bExternalBit", bExternalBit).add("bAbrBit", bAbrBit) |
Thejaswi N K | c00c4d3 | 2015-10-09 17:19:04 +0530 | [diff] [blame] | 191 | .toString(); |
| 192 | } |
| 193 | } |