Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +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 prefix IGP Flag attribute. |
| 32 | */ |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 33 | public final class BgpPrefixAttrIgpFlags implements BGPValueType { |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 34 | |
| 35 | protected static final Logger log = LoggerFactory |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 36 | .getLogger(BgpPrefixAttrIgpFlags.class); |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 37 | |
| 38 | public static final int ATTR_PREFIX_FLAGBIT = 1152; |
| 39 | public static final int ATTR_PREFIX_FLAG_LEN = 1; |
| 40 | |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 41 | public static final byte FIRST_BIT = (byte) 0x80; |
| 42 | public static final byte SECOND_BIT = 0x40; |
| 43 | public static final byte THIRD_BIT = 0x20; |
| 44 | public static final byte FOURTH_BIT = 0x01; |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 45 | |
| 46 | /* Prefix IGP flag bit TLV */ |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 47 | private final boolean bisisUpDownBit; |
| 48 | private final boolean bOspfNoUnicastBit; |
| 49 | private final boolean bOspfLclAddrBit; |
| 50 | private final boolean bOspfNSSABit; |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Constructor to initialize the value. |
| 54 | * |
| 55 | * @param bisisUpDownBit IS-IS Up/Down Bit |
| 56 | * @param bOspfNoUnicastBit OSPF no unicast Bit |
| 57 | * @param bOspfLclAddrBit OSPF local address Bit |
| 58 | * @param bOspfNSSABit OSPF propagate NSSA Bit |
| 59 | */ |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 60 | BgpPrefixAttrIgpFlags(boolean bisisUpDownBit, |
| 61 | boolean bOspfNoUnicastBit, |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 62 | boolean bOspfLclAddrBit, boolean bOspfNSSABit) { |
| 63 | this.bisisUpDownBit = bisisUpDownBit; |
| 64 | this.bOspfNoUnicastBit = bOspfNoUnicastBit; |
| 65 | this.bOspfLclAddrBit = bOspfLclAddrBit; |
| 66 | this.bOspfNSSABit = bOspfNSSABit; |
| 67 | } |
| 68 | |
| 69 | /** |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 70 | * Returns object of this class with specified values. |
| 71 | * |
| 72 | * @param bisisUpDownBit IS-IS Up/Down Bit |
| 73 | * @param bOspfNoUnicastBit OSPF no unicast Bit |
| 74 | * @param bOspfLclAddrBit OSPF local address Bit |
| 75 | * @param bOspfNSSABit OSPF propagate NSSA Bit |
| 76 | * @return object of BgpPrefixAttrIGPFlags |
| 77 | */ |
| 78 | public static BgpPrefixAttrIgpFlags of(final boolean bisisUpDownBit, |
| 79 | final boolean bOspfNoUnicastBit, |
| 80 | final boolean bOspfLclAddrBit, |
| 81 | final boolean bOspfNSSABit) { |
| 82 | return new BgpPrefixAttrIgpFlags(bisisUpDownBit, bOspfNoUnicastBit, |
| 83 | bOspfLclAddrBit, bOspfNSSABit); |
| 84 | } |
| 85 | |
| 86 | /** |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 87 | * Reads the IGP Flags. |
| 88 | * |
| 89 | * @param cb ChannelBuffer |
| 90 | * @return object of BgpPrefixAttrIGPFlags |
| 91 | * @throws BGPParseException while parsing BgpPrefixAttrIGPFlags |
| 92 | */ |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 93 | public static BgpPrefixAttrIgpFlags read(ChannelBuffer cb) |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 94 | throws BGPParseException { |
| 95 | boolean bisisUpDownBit = false; |
| 96 | boolean bOspfNoUnicastBit = false; |
| 97 | boolean bOspfLclAddrBit = false; |
| 98 | boolean bOspfNSSABit = false; |
| 99 | |
| 100 | short lsAttrLength = cb.readShort(); |
| 101 | |
| 102 | if ((lsAttrLength != ATTR_PREFIX_FLAG_LEN) |
| 103 | || (cb.readableBytes() < lsAttrLength)) { |
| 104 | Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, |
| 105 | BGPErrorType.ATTRIBUTE_LENGTH_ERROR, |
| 106 | lsAttrLength); |
| 107 | } |
| 108 | |
| 109 | byte nodeFlagBits = cb.readByte(); |
| 110 | |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 111 | bisisUpDownBit = ((nodeFlagBits & FIRST_BIT) == FIRST_BIT); |
| 112 | bOspfNoUnicastBit = ((nodeFlagBits & SECOND_BIT) == SECOND_BIT); |
| 113 | bOspfLclAddrBit = ((nodeFlagBits & THIRD_BIT) == THIRD_BIT); |
| 114 | bOspfNSSABit = ((nodeFlagBits & FOURTH_BIT) == FOURTH_BIT); |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 115 | |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 116 | return BgpPrefixAttrIgpFlags.of(bisisUpDownBit, bOspfNoUnicastBit, |
| 117 | bOspfLclAddrBit, bOspfNSSABit); |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns the IS-IS Up/Down Bit set or not. |
| 122 | * |
| 123 | * @return IS-IS Up/Down Bit set or not |
| 124 | */ |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 125 | public boolean isisUpDownBit() { |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 126 | return bisisUpDownBit; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the OSPF no unicast Bit set or not. |
| 131 | * |
| 132 | * @return OSPF no unicast Bit set or not |
| 133 | */ |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 134 | public boolean ospfNoUnicastBit() { |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 135 | return bOspfNoUnicastBit; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns the OSPF local address Bit set or not. |
| 140 | * |
| 141 | * @return OSPF local address Bit set or not |
| 142 | */ |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 143 | public boolean ospfLclAddrBit() { |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 144 | return bOspfLclAddrBit; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns the OSPF propagate NSSA Bit set or not. |
| 149 | * |
| 150 | * @return OSPF propagate NSSA Bit set or not |
| 151 | */ |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 152 | public boolean ospfNSSABit() { |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 153 | return bOspfNSSABit; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public short getType() { |
| 158 | return ATTR_PREFIX_FLAGBIT; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public int write(ChannelBuffer cb) { |
| 163 | // TODO This will be implemented in the next version |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public int hashCode() { |
| 169 | return Objects.hash(bisisUpDownBit, bOspfNoUnicastBit, bOspfLclAddrBit, |
| 170 | bOspfNSSABit); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public boolean equals(Object obj) { |
| 175 | if (this == obj) { |
| 176 | return true; |
| 177 | } |
| 178 | |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 179 | if (obj instanceof BgpPrefixAttrIgpFlags) { |
| 180 | BgpPrefixAttrIgpFlags other = (BgpPrefixAttrIgpFlags) obj; |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 181 | return Objects.equals(bisisUpDownBit, other.bisisUpDownBit) |
| 182 | && Objects.equals(bOspfNoUnicastBit, |
| 183 | other.bOspfNoUnicastBit) |
Thejaswi N K | 2e54252 | 2015-11-02 20:52:17 +0530 | [diff] [blame] | 184 | && Objects.equals(bOspfLclAddrBit, other.bOspfLclAddrBit) |
| 185 | && Objects.equals(bOspfNSSABit, other.bOspfNSSABit); |
Thejaswi N K | d94c754 | 2015-10-14 23:54:40 +0530 | [diff] [blame] | 186 | } |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public String toString() { |
| 192 | return MoreObjects.toStringHelper(getClass()) |
| 193 | .add("bisisUpDownBit", bisisUpDownBit) |
| 194 | .add("bOspfNoUnicastBit", bOspfNoUnicastBit) |
| 195 | .add("bOspfLclAddrBit", bOspfLclAddrBit) |
| 196 | .add("bOspfNSSABit", bOspfNSSABit).toString(); |
| 197 | } |
| 198 | } |