blob: a202c44ef29869dacb11a85e1cfa84b947d4ed77 [file] [log] [blame]
Thejaswi N Kc00c4d32015-10-09 17:19:04 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thejaswi N Kc00c4d32015-10-09 17:19:04 +05303 *
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 Kc00c4d32015-10-09 17:19:04 +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 attribute node flag.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public final class BgpAttrNodeFlagBitTlv implements BgpValueType {
Thejaswi N Kc00c4d32015-10-09 17:19:04 +053034
Ray Milkey9c9cde42018-01-12 14:22:06 -080035 private static final Logger log = LoggerFactory
Thejaswi N Kc00c4d32015-10-09 17:19:04 +053036 .getLogger(BgpAttrNodeFlagBitTlv.class);
37
38 public static final int ATTRNODE_FLAGBIT = 1024;
39
40 /* Node flag bit TLV */
Thejaswi N K54177832015-11-02 16:15:06 +053041 private final boolean bOverloadBit;
42 private final boolean bAttachedBit;
43 private final boolean bExternalBit;
44 private final boolean bAbrBit;
Thejaswi N Kc00c4d32015-10-09 17:19:04 +053045
Thejaswi N K54177832015-11-02 16:15:06 +053046 public static final byte FIRST_BIT = (byte) 0x80;
47 public static final byte SECOND_BIT = 0x40;
48 public static final byte THIRD_BIT = 0x20;
49 public static final byte FOURTH_BIT = 0x01;
Thejaswi N Kc00c4d32015-10-09 17:19:04 +053050
51 /**
52 * Constructor to initialize parameters.
53 *
54 * @param bOverloadBit Overload bit
55 * @param bAttachedBit Attached bit
56 * @param bExternalBit External bit
Thejaswi N K54177832015-11-02 16:15:06 +053057 * @param bAbrBit ABR Bit
Thejaswi N Kc00c4d32015-10-09 17:19:04 +053058 */
Priyanka B0ab34b92015-12-03 21:13:52 +053059 public BgpAttrNodeFlagBitTlv(boolean bOverloadBit, boolean bAttachedBit,
Thejaswi N K54177832015-11-02 16:15:06 +053060 boolean bExternalBit, boolean bAbrBit) {
Thejaswi N Kc00c4d32015-10-09 17:19:04 +053061 this.bOverloadBit = bOverloadBit;
62 this.bAttachedBit = bAttachedBit;
63 this.bExternalBit = bExternalBit;
Thejaswi N K54177832015-11-02 16:15:06 +053064 this.bAbrBit = bAbrBit;
65 }
66
67 /**
68 * Returns object of this class with specified values.
69 *
70 * @param bOverloadBit Overload bit
71 * @param bAttachedBit Attached bit
72 * @param bExternalBit External bit
73 * @param bAbrBit ABR Bit
74 * @return object of BgpAttrNodeFlagBitTlv
75 */
76 public static BgpAttrNodeFlagBitTlv of(final boolean bOverloadBit,
77 final boolean bAttachedBit,
78 final boolean bExternalBit,
79 final boolean bAbrBit) {
80 return new BgpAttrNodeFlagBitTlv(bOverloadBit, bAttachedBit,
81 bExternalBit, bAbrBit);
Thejaswi N Kc00c4d32015-10-09 17:19:04 +053082 }
83
84 /**
85 * Reads the Node Flag Bits.
86 *
87 * @param cb ChannelBuffer
88 * @return attribute node flag bit tlv
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053089 * @throws BgpParseException while parsing BgpAttrNodeFlagBitTlv
Thejaswi N Kc00c4d32015-10-09 17:19:04 +053090 */
91 public static BgpAttrNodeFlagBitTlv read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053092 throws BgpParseException {
Thejaswi N Kc00c4d32015-10-09 17:19:04 +053093 boolean bOverloadBit = false;
94 boolean bAttachedBit = false;
95 boolean bExternalBit = false;
Thejaswi N K54177832015-11-02 16:15:06 +053096 boolean bAbrBit = false;
Thejaswi N Kc00c4d32015-10-09 17:19:04 +053097
98 short lsAttrLength = cb.readShort();
99
Thejaswi N K54177832015-11-02 16:15:06 +0530100 if ((lsAttrLength != 1) || (cb.readableBytes() < lsAttrLength)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530101 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
102 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530103 lsAttrLength);
104 }
105
106 byte nodeFlagBits = cb.readByte();
107
Thejaswi N K54177832015-11-02 16:15:06 +0530108 bOverloadBit = ((nodeFlagBits & FIRST_BIT) == FIRST_BIT);
109 bAttachedBit = ((nodeFlagBits & SECOND_BIT) == SECOND_BIT);
110 bExternalBit = ((nodeFlagBits & THIRD_BIT) == THIRD_BIT);
111 bAbrBit = ((nodeFlagBits & FOURTH_BIT) == FOURTH_BIT);
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530112
Thejaswi N K54177832015-11-02 16:15:06 +0530113 return BgpAttrNodeFlagBitTlv.of(bOverloadBit, bAttachedBit,
114 bExternalBit, bAbrBit);
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530115 }
116
117 /**
118 * Returns Overload Bit.
119 *
120 * @return Overload Bit
121 */
Thejaswi N K54177832015-11-02 16:15:06 +0530122 public boolean overLoadBit() {
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530123 return bOverloadBit;
124 }
125
126 /**
127 * Returns Attached Bit.
128 *
129 * @return Attached Bit
130 */
Thejaswi N K54177832015-11-02 16:15:06 +0530131 public boolean attachedBit() {
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530132 return bAttachedBit;
133 }
134
135 /**
136 * Returns External Bit.
137 *
138 * @return External Bit
139 */
Thejaswi N K54177832015-11-02 16:15:06 +0530140 public boolean externalBit() {
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530141 return bExternalBit;
142 }
143
144 /**
145 * Returns ABR Bit.
146 *
147 * @return ABR Bit
148 */
Thejaswi N K54177832015-11-02 16:15:06 +0530149 public boolean abrBit() {
150 return bAbrBit;
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530151 }
152
153 @Override
154 public short getType() {
155 return ATTRNODE_FLAGBIT;
156 }
157
158 @Override
159 public int write(ChannelBuffer cb) {
Thejaswi N K54177832015-11-02 16:15:06 +0530160 // TODO This will be implemented in the next version
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530161 return 0;
162 }
163
164 @Override
165 public int hashCode() {
Thejaswi N K54177832015-11-02 16:15:06 +0530166 return Objects.hash(bOverloadBit, bAttachedBit, bExternalBit, bAbrBit);
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530167 }
168
169 @Override
170 public boolean equals(Object obj) {
171 if (this == obj) {
172 return true;
173 }
174
175 if (obj instanceof BgpAttrNodeFlagBitTlv) {
176 BgpAttrNodeFlagBitTlv other = (BgpAttrNodeFlagBitTlv) obj;
177 return Objects.equals(bOverloadBit, other.bOverloadBit)
178 && Objects.equals(bAttachedBit, other.bAttachedBit)
179 && Objects.equals(bExternalBit, other.bExternalBit)
Thejaswi N K54177832015-11-02 16:15:06 +0530180 && Objects.equals(bAbrBit, other.bAbrBit);
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530181 }
182 return false;
183 }
184
185 @Override
186 public String toString() {
187 return MoreObjects.toStringHelper(getClass())
188 .add("bOverloadBit", bOverloadBit)
189 .add("bAttachedBit", bAttachedBit)
Thejaswi N K54177832015-11-02 16:15:06 +0530190 .add("bExternalBit", bExternalBit).add("bAbrBit", bAbrBit)
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530191 .toString();
192 }
Priyanka B02040732015-11-29 11:30:29 +0530193
194 @Override
195 public int compareTo(Object o) {
196 // TODO Auto-generated method stub
197 return 0;
198 }
Thejaswi N Kc00c4d32015-10-09 17:19:04 +0530199}