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 prefix IGP Flag attribute.
33 public final class BgpPrefixAttrIgpFlags implements BgpValueType {
35 protected static final Logger log = LoggerFactory
36 .getLogger(BgpPrefixAttrIgpFlags.class);
38 public static final int ATTR_PREFIX_FLAGBIT = 1152;
39 public static final int ATTR_PREFIX_FLAG_LEN = 1;
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;
46 /* Prefix IGP flag bit TLV */
47 private final boolean bisisUpDownBit;
48 private final boolean bOspfNoUnicastBit;
49 private final boolean bOspfLclAddrBit;
50 private final boolean bOspfNSSABit;
53 * Constructor to initialize the value.
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
60 BgpPrefixAttrIgpFlags(boolean bisisUpDownBit,
61 boolean bOspfNoUnicastBit,
62 boolean bOspfLclAddrBit, boolean bOspfNSSABit) {
63 this.bisisUpDownBit = bisisUpDownBit;
64 this.bOspfNoUnicastBit = bOspfNoUnicastBit;
65 this.bOspfLclAddrBit = bOspfLclAddrBit;
66 this.bOspfNSSABit = bOspfNSSABit;
70 * Returns object of this class with specified values.
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
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);
87 * Reads the IGP Flags.
89 * @param cb ChannelBuffer
90 * @return object of BgpPrefixAttrIGPFlags
91 * @throws BgpParseException while parsing BgpPrefixAttrIGPFlags
93 public static BgpPrefixAttrIgpFlags read(ChannelBuffer cb)
94 throws BgpParseException {
95 boolean bisisUpDownBit = false;
96 boolean bOspfNoUnicastBit = false;
97 boolean bOspfLclAddrBit = false;
98 boolean bOspfNSSABit = false;
100 short lsAttrLength = cb.readShort();
102 if ((lsAttrLength != ATTR_PREFIX_FLAG_LEN)
103 || (cb.readableBytes() < lsAttrLength)) {
104 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
105 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
109 byte nodeFlagBits = cb.readByte();
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);
116 return BgpPrefixAttrIgpFlags.of(bisisUpDownBit, bOspfNoUnicastBit,
117 bOspfLclAddrBit, bOspfNSSABit);
121 * Returns the IS-IS Up/Down Bit set or not.
123 * @return IS-IS Up/Down Bit set or not
125 public boolean isisUpDownBit() {
126 return bisisUpDownBit;
130 * Returns the OSPF no unicast Bit set or not.
132 * @return OSPF no unicast Bit set or not
134 public boolean ospfNoUnicastBit() {
135 return bOspfNoUnicastBit;
139 * Returns the OSPF local address Bit set or not.
141 * @return OSPF local address Bit set or not
143 public boolean ospfLclAddrBit() {
144 return bOspfLclAddrBit;
148 * Returns the OSPF propagate NSSA Bit set or not.
150 * @return OSPF propagate NSSA Bit set or not
152 public boolean ospfNSSABit() {
157 public short getType() {
158 return ATTR_PREFIX_FLAGBIT;
162 public int write(ChannelBuffer cb) {
163 // TODO This will be implemented in the next version
168 public int hashCode() {
169 return Objects.hash(bisisUpDownBit, bOspfNoUnicastBit, bOspfLclAddrBit,
174 public boolean equals(Object obj) {
179 if (obj instanceof BgpPrefixAttrIgpFlags) {
180 BgpPrefixAttrIgpFlags other = (BgpPrefixAttrIgpFlags) obj;
181 return Objects.equals(bisisUpDownBit, other.bisisUpDownBit)
182 && Objects.equals(bOspfNoUnicastBit,
183 other.bOspfNoUnicastBit)
184 && Objects.equals(bOspfLclAddrBit, other.bOspfLclAddrBit)
185 && Objects.equals(bOspfNSSABit, other.bOspfNSSABit);
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();
200 public int compareTo(Object o) {
201 // TODO Auto-generated method stub