4e84191a66e7836c530abd79d6af5d5fe7446aae
[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 prefix IGP Flag attribute.
32  */
33 public final class BgpPrefixAttrIgpFlags implements BgpValueType {
34
35     protected static final Logger log = LoggerFactory
36             .getLogger(BgpPrefixAttrIgpFlags.class);
37
38     public static final int ATTR_PREFIX_FLAGBIT = 1152;
39     public static final int ATTR_PREFIX_FLAG_LEN = 1;
40
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;
45
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;
51
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      */
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;
67     }
68
69     /**
70      * 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     /**
87      * Reads the IGP Flags.
88      *
89      * @param cb ChannelBuffer
90      * @return object of BgpPrefixAttrIGPFlags
91      * @throws BgpParseException while parsing BgpPrefixAttrIGPFlags
92      */
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;
99
100         short lsAttrLength = cb.readShort();
101
102         if ((lsAttrLength != ATTR_PREFIX_FLAG_LEN)
103                 || (cb.readableBytes() < lsAttrLength)) {
104             Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
105                                    BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
106                                    lsAttrLength);
107         }
108
109         byte nodeFlagBits = cb.readByte();
110
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);
115
116         return BgpPrefixAttrIgpFlags.of(bisisUpDownBit, bOspfNoUnicastBit,
117                                         bOspfLclAddrBit, bOspfNSSABit);
118     }
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      */
125     public boolean isisUpDownBit() {
126         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      */
134     public boolean ospfNoUnicastBit() {
135         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      */
143     public boolean ospfLclAddrBit() {
144         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      */
152     public boolean ospfNSSABit() {
153         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
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);
186         }
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
199     @Override
200     public int compareTo(Object o) {
201         // TODO Auto-generated method stub
202         return 0;
203     }
204 }