blob: dec3804b983113557989c999fbff9c84f4608e4e [file] [log] [blame]
Thejaswi N Kd94c7542015-10-14 23:54:40 +05301/*
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 */
16package org.onosproject.bgpio.types.attr;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.types.BgpErrorType;
23import org.onosproject.bgpio.types.BgpValueType;
Thejaswi N Kd94c7542015-10-14 23:54:40 +053024import org.onosproject.bgpio.util.Validation;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import com.google.common.base.MoreObjects;
29
30/**
31 * Implements BGP prefix IGP Flag attribute.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public final class BgpPrefixAttrIgpFlags implements BgpValueType {
Thejaswi N Kd94c7542015-10-14 23:54:40 +053034
35 protected static final Logger log = LoggerFactory
Thejaswi N K2e542522015-11-02 20:52:17 +053036 .getLogger(BgpPrefixAttrIgpFlags.class);
Thejaswi N Kd94c7542015-10-14 23:54:40 +053037
38 public static final int ATTR_PREFIX_FLAGBIT = 1152;
39 public static final int ATTR_PREFIX_FLAG_LEN = 1;
40
Thejaswi N K2e542522015-11-02 20:52:17 +053041 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 Kd94c7542015-10-14 23:54:40 +053045
46 /* Prefix IGP flag bit TLV */
Thejaswi N K2e542522015-11-02 20:52:17 +053047 private final boolean bisisUpDownBit;
48 private final boolean bOspfNoUnicastBit;
49 private final boolean bOspfLclAddrBit;
50 private final boolean bOspfNSSABit;
Thejaswi N Kd94c7542015-10-14 23:54:40 +053051
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 K2e542522015-11-02 20:52:17 +053060 BgpPrefixAttrIgpFlags(boolean bisisUpDownBit,
61 boolean bOspfNoUnicastBit,
Thejaswi N Kd94c7542015-10-14 23:54:40 +053062 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 K2e542522015-11-02 20:52:17 +053070 * 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 Kd94c7542015-10-14 23:54:40 +053087 * Reads the IGP Flags.
88 *
89 * @param cb ChannelBuffer
90 * @return object of BgpPrefixAttrIGPFlags
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053091 * @throws BgpParseException while parsing BgpPrefixAttrIGPFlags
Thejaswi N Kd94c7542015-10-14 23:54:40 +053092 */
Thejaswi N K2e542522015-11-02 20:52:17 +053093 public static BgpPrefixAttrIgpFlags read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053094 throws BgpParseException {
Thejaswi N Kd94c7542015-10-14 23:54:40 +053095 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)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530104 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
105 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530106 lsAttrLength);
107 }
108
109 byte nodeFlagBits = cb.readByte();
110
Thejaswi N K2e542522015-11-02 20:52:17 +0530111 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 Kd94c7542015-10-14 23:54:40 +0530115
Thejaswi N K2e542522015-11-02 20:52:17 +0530116 return BgpPrefixAttrIgpFlags.of(bisisUpDownBit, bOspfNoUnicastBit,
117 bOspfLclAddrBit, bOspfNSSABit);
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530118 }
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 K2e542522015-11-02 20:52:17 +0530125 public boolean isisUpDownBit() {
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530126 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 K2e542522015-11-02 20:52:17 +0530134 public boolean ospfNoUnicastBit() {
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530135 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 K2e542522015-11-02 20:52:17 +0530143 public boolean ospfLclAddrBit() {
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530144 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 K2e542522015-11-02 20:52:17 +0530152 public boolean ospfNSSABit() {
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530153 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 K2e542522015-11-02 20:52:17 +0530179 if (obj instanceof BgpPrefixAttrIgpFlags) {
180 BgpPrefixAttrIgpFlags other = (BgpPrefixAttrIgpFlags) obj;
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530181 return Objects.equals(bisisUpDownBit, other.bisisUpDownBit)
182 && Objects.equals(bOspfNoUnicastBit,
183 other.bOspfNoUnicastBit)
Thejaswi N K2e542522015-11-02 20:52:17 +0530184 && Objects.equals(bOspfLclAddrBit, other.bOspfLclAddrBit)
185 && Objects.equals(bOspfNSSABit, other.bOspfNSSABit);
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530186 }
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}