035d706ccc7958f8245aff6a70ede5b9b5dd65d6
[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 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 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;
45
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;
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, boolean bOspfNoUnicastBit,
61                           boolean bOspfLclAddrBit, boolean bOspfNSSABit) {
62         this.bisisUpDownBit = bisisUpDownBit;
63         this.bOspfNoUnicastBit = bOspfNoUnicastBit;
64         this.bOspfLclAddrBit = bOspfLclAddrBit;
65         this.bOspfNSSABit = bOspfNSSABit;
66     }
67
68     /**
69      * Reads the IGP Flags.
70      *
71      * @param cb ChannelBuffer
72      * @return object of BgpPrefixAttrIGPFlags
73      * @throws BGPParseException while parsing BgpPrefixAttrIGPFlags
74      */
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;
81
82         short lsAttrLength = cb.readShort();
83
84         if ((lsAttrLength != ATTR_PREFIX_FLAG_LEN)
85                 || (cb.readableBytes() < lsAttrLength)) {
86             Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
87                                    BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
88                                    lsAttrLength);
89         }
90
91         byte nodeFlagBits = cb.readByte();
92
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);
97
98         return new BgpPrefixAttrIGPFlags(bisisUpDownBit, bOspfNoUnicastBit,
99                                          bOspfLclAddrBit, bOspfNSSABit);
100     }
101
102     /**
103      * Returns the IS-IS Up/Down Bit set or not.
104      *
105      * @return IS-IS Up/Down Bit set or not
106      */
107     boolean getisisUpDownBit() {
108         return bisisUpDownBit;
109     }
110
111     /**
112      * Returns the OSPF no unicast Bit set or not.
113      *
114      * @return OSPF no unicast Bit set or not
115      */
116     boolean getOspfNoUnicastBit() {
117         return bOspfNoUnicastBit;
118     }
119
120     /**
121      * Returns the OSPF local address Bit set or not.
122      *
123      * @return OSPF local address Bit set or not
124      */
125     boolean getOspfLclAddrBit() {
126         return bOspfLclAddrBit;
127     }
128
129     /**
130      * Returns the OSPF propagate NSSA Bit set or not.
131      *
132      * @return OSPF propagate NSSA Bit set or not
133      */
134     boolean getOspfNSSABit() {
135         return bOspfNSSABit;
136     }
137
138     @Override
139     public short getType() {
140         return ATTR_PREFIX_FLAGBIT;
141     }
142
143     @Override
144     public int write(ChannelBuffer cb) {
145         // TODO This will be implemented in the next version
146         return 0;
147     }
148
149     @Override
150     public int hashCode() {
151         return Objects.hash(bisisUpDownBit, bOspfNoUnicastBit, bOspfLclAddrBit,
152                             bOspfNSSABit);
153     }
154
155     @Override
156     public boolean equals(Object obj) {
157         if (this == obj) {
158             return true;
159         }
160
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);
168         }
169         return false;
170     }
171
172     @Override
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();
179     }
180 }