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