e0fef7c84fd0731dee9779bd5d358c46ceb50b19
[onosfw.git] /
1 /*
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  */
16 package org.onosproject.bgpio.types.attr;
17
18 import java.util.Objects;
19
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;
27
28 import com.google.common.base.MoreObjects;
29
30 /**
31  * Implements BGP attribute node flag.
32  */
33 public final class BgpAttrNodeFlagBitTlv implements BgpValueType {
34
35     protected static final Logger log = LoggerFactory
36             .getLogger(BgpAttrNodeFlagBitTlv.class);
37
38     public static final int ATTRNODE_FLAGBIT = 1024;
39
40     /* Node flag bit TLV */
41     private final boolean bOverloadBit;
42     private final boolean bAttachedBit;
43     private final boolean bExternalBit;
44     private final boolean bAbrBit;
45
46     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;
50
51     /**
52      * Constructor to initialize parameters.
53      *
54      * @param bOverloadBit Overload bit
55      * @param bAttachedBit Attached bit
56      * @param bExternalBit External bit
57      * @param bAbrBit ABR Bit
58      */
59     private BgpAttrNodeFlagBitTlv(boolean bOverloadBit, boolean bAttachedBit,
60                                   boolean bExternalBit, boolean bAbrBit) {
61         this.bOverloadBit = bOverloadBit;
62         this.bAttachedBit = bAttachedBit;
63         this.bExternalBit = bExternalBit;
64         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);
82     }
83
84     /**
85      * Reads the Node Flag Bits.
86      *
87      * @param cb ChannelBuffer
88      * @return attribute node flag bit tlv
89      * @throws BgpParseException while parsing BgpAttrNodeFlagBitTlv
90      */
91     public static BgpAttrNodeFlagBitTlv read(ChannelBuffer cb)
92             throws BgpParseException {
93         boolean bOverloadBit = false;
94         boolean bAttachedBit = false;
95         boolean bExternalBit = false;
96         boolean bAbrBit = false;
97
98         short lsAttrLength = cb.readShort();
99
100         if ((lsAttrLength != 1) || (cb.readableBytes() < lsAttrLength)) {
101             Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
102                                    BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
103                                    lsAttrLength);
104         }
105
106         byte nodeFlagBits = cb.readByte();
107
108         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);
112
113         return BgpAttrNodeFlagBitTlv.of(bOverloadBit, bAttachedBit,
114                                         bExternalBit, bAbrBit);
115     }
116
117     /**
118      * Returns Overload Bit.
119      *
120      * @return Overload Bit
121      */
122     public boolean overLoadBit() {
123         return bOverloadBit;
124     }
125
126     /**
127      * Returns Attached Bit.
128      *
129      * @return Attached Bit
130      */
131     public boolean attachedBit() {
132         return bAttachedBit;
133     }
134
135     /**
136      * Returns External Bit.
137      *
138      * @return External Bit
139      */
140     public boolean externalBit() {
141         return bExternalBit;
142     }
143
144     /**
145      * Returns ABR Bit.
146      *
147      * @return ABR Bit
148      */
149     public boolean abrBit() {
150         return bAbrBit;
151     }
152
153     @Override
154     public short getType() {
155         return ATTRNODE_FLAGBIT;
156     }
157
158     @Override
159     public int write(ChannelBuffer cb) {
160         // TODO This will be implemented in the next version
161         return 0;
162     }
163
164     @Override
165     public int hashCode() {
166         return Objects.hash(bOverloadBit, bAttachedBit, bExternalBit, bAbrBit);
167     }
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)
180                     && Objects.equals(bAbrBit, other.bAbrBit);
181         }
182         return false;
183     }
184
185     @Override
186     public String toString() {
187         return MoreObjects.toStringHelper(getClass())
188                 .add("bOverloadBit", bOverloadBit)
189                 .add("bAttachedBit", bAttachedBit)
190                 .add("bExternalBit", bExternalBit).add("bAbrBit", bAbrBit)
191                 .toString();
192     }
193
194     @Override
195     public int compareTo(Object o) {
196         // TODO Auto-generated method stub
197         return 0;
198     }
199 }