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 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 int FIRST_BIT = 0x80;
42 public static final int SECOND_BIT = 0x40;
43 public static final int THIRD_BIT = 0x20;
44 public static final int FOURTH_BIT = 0x01;
46 /* Prefix IGP flag bit TLV */
47 private boolean bisisUpDownBit = false;
48 private boolean bOspfNoUnicastBit = false;
49 private boolean bOspfLclAddrBit = false;
50 private boolean bOspfNSSABit = false;
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, boolean bOspfNoUnicastBit,
61 boolean bOspfLclAddrBit, boolean bOspfNSSABit) {
62 this.bisisUpDownBit = bisisUpDownBit;
63 this.bOspfNoUnicastBit = bOspfNoUnicastBit;
64 this.bOspfLclAddrBit = bOspfLclAddrBit;
65 this.bOspfNSSABit = bOspfNSSABit;
69 * Reads the IGP Flags.
71 * @param cb ChannelBuffer
72 * @return object of BgpPrefixAttrIGPFlags
73 * @throws BGPParseException while parsing BgpPrefixAttrIGPFlags
75 public static BgpPrefixAttrIGPFlags read(ChannelBuffer cb)
76 throws BGPParseException {
77 boolean bisisUpDownBit = false;
78 boolean bOspfNoUnicastBit = false;
79 boolean bOspfLclAddrBit = false;
80 boolean bOspfNSSABit = false;
82 short lsAttrLength = cb.readShort();
84 if ((lsAttrLength != ATTR_PREFIX_FLAG_LEN)
85 || (cb.readableBytes() < lsAttrLength)) {
86 Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
87 BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
91 byte nodeFlagBits = cb.readByte();
93 bisisUpDownBit = ((nodeFlagBits & (byte) FIRST_BIT) == FIRST_BIT);
94 bOspfNoUnicastBit = ((nodeFlagBits & (byte) SECOND_BIT) == SECOND_BIT);
95 bOspfLclAddrBit = ((nodeFlagBits & (byte) THIRD_BIT) == THIRD_BIT);
96 bOspfNSSABit = ((nodeFlagBits & (byte) FOURTH_BIT) == FOURTH_BIT);
98 return new BgpPrefixAttrIGPFlags(bisisUpDownBit, bOspfNoUnicastBit,
99 bOspfLclAddrBit, bOspfNSSABit);
103 * Returns the IS-IS Up/Down Bit set or not.
105 * @return IS-IS Up/Down Bit set or not
107 boolean getisisUpDownBit() {
108 return bisisUpDownBit;
112 * Returns the OSPF no unicast Bit set or not.
114 * @return OSPF no unicast Bit set or not
116 boolean getOspfNoUnicastBit() {
117 return bOspfNoUnicastBit;
121 * Returns the OSPF local address Bit set or not.
123 * @return OSPF local address Bit set or not
125 boolean getOspfLclAddrBit() {
126 return bOspfLclAddrBit;
130 * Returns the OSPF propagate NSSA Bit set or not.
132 * @return OSPF propagate NSSA Bit set or not
134 boolean getOspfNSSABit() {
139 public short getType() {
140 return ATTR_PREFIX_FLAGBIT;
144 public int write(ChannelBuffer cb) {
145 // TODO This will be implemented in the next version
150 public int hashCode() {
151 return Objects.hash(bisisUpDownBit, bOspfNoUnicastBit, bOspfLclAddrBit,
156 public boolean equals(Object obj) {
161 if (obj instanceof BgpPrefixAttrIGPFlags) {
162 BgpPrefixAttrIGPFlags other = (BgpPrefixAttrIGPFlags) obj;
163 return Objects.equals(bisisUpDownBit, other.bisisUpDownBit)
164 && Objects.equals(bOspfNoUnicastBit,
165 other.bOspfNoUnicastBit)
166 && Objects.equals(bOspfLclAddrBit, other.bOspfLclAddrBit)
167 && Objects.equals(bOspfNSSABit, other.bOspfNSSABit);
173 public String toString() {
174 return MoreObjects.toStringHelper(getClass())
175 .add("bisisUpDownBit", bisisUpDownBit)
176 .add("bOspfNoUnicastBit", bOspfNoUnicastBit)
177 .add("bOspfLclAddrBit", bOspfLclAddrBit)
178 .add("bOspfNSSABit", bOspfNSSABit).toString();