blob: 04c2cfc48ba5e11bd17743b2f6c636dc1e9882da [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
Jonathan Hart51539b82015-10-29 09:53:04 -070018import com.google.common.base.MoreObjects;
Thejaswi N Kd94c7542015-10-14 23:54:40 +053019import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053020import org.onosproject.bgpio.exceptions.BgpParseException;
21import org.onosproject.bgpio.types.BgpErrorType;
22import org.onosproject.bgpio.types.BgpValueType;
Thejaswi N Kd94c7542015-10-14 23:54:40 +053023import org.onosproject.bgpio.util.Validation;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
Jonathan Hart51539b82015-10-29 09:53:04 -070027import java.util.Objects;
Thejaswi N Kd94c7542015-10-14 23:54:40 +053028
29/**
30 * Implements BGP prefix IGP Flag attribute.
31 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053032public final class BgpPrefixAttrIgpFlags implements BgpValueType {
Thejaswi N Kd94c7542015-10-14 23:54:40 +053033
34 protected static final Logger log = LoggerFactory
Thejaswi N K2e542522015-11-02 20:52:17 +053035 .getLogger(BgpPrefixAttrIgpFlags.class);
Thejaswi N Kd94c7542015-10-14 23:54:40 +053036
37 public static final int ATTR_PREFIX_FLAGBIT = 1152;
38 public static final int ATTR_PREFIX_FLAG_LEN = 1;
39
Thejaswi N K2e542522015-11-02 20:52:17 +053040 public static final byte FIRST_BIT = (byte) 0x80;
41 public static final byte SECOND_BIT = 0x40;
42 public static final byte THIRD_BIT = 0x20;
43 public static final byte FOURTH_BIT = 0x01;
Thejaswi N Kd94c7542015-10-14 23:54:40 +053044
45 /* Prefix IGP flag bit TLV */
Thejaswi N K2e542522015-11-02 20:52:17 +053046 private final boolean bisisUpDownBit;
47 private final boolean bOspfNoUnicastBit;
48 private final boolean bOspfLclAddrBit;
49 private final boolean bOspfNSSABit;
Thejaswi N Kd94c7542015-10-14 23:54:40 +053050
51 /**
52 * Constructor to initialize the value.
53 *
54 * @param bisisUpDownBit IS-IS Up/Down Bit
55 * @param bOspfNoUnicastBit OSPF no unicast Bit
56 * @param bOspfLclAddrBit OSPF local address Bit
Jonathan Hart51539b82015-10-29 09:53:04 -070057 * @param bOspfNssaBit OSPF propagate NSSA Bit
Thejaswi N Kd94c7542015-10-14 23:54:40 +053058 */
Priyanka B0ab34b92015-12-03 21:13:52 +053059 public BgpPrefixAttrIgpFlags(boolean bisisUpDownBit,
Thejaswi N K2e542522015-11-02 20:52:17 +053060 boolean bOspfNoUnicastBit,
Jonathan Hart51539b82015-10-29 09:53:04 -070061 boolean bOspfLclAddrBit, boolean bOspfNssaBit) {
Thejaswi N Kd94c7542015-10-14 23:54:40 +053062 this.bisisUpDownBit = bisisUpDownBit;
63 this.bOspfNoUnicastBit = bOspfNoUnicastBit;
64 this.bOspfLclAddrBit = bOspfLclAddrBit;
Jonathan Hart51539b82015-10-29 09:53:04 -070065 this.bOspfNSSABit = bOspfNssaBit;
Thejaswi N Kd94c7542015-10-14 23:54:40 +053066 }
67
68 /**
Thejaswi N K2e542522015-11-02 20:52:17 +053069 * Returns object of this class with specified values.
70 *
71 * @param bisisUpDownBit IS-IS Up/Down Bit
72 * @param bOspfNoUnicastBit OSPF no unicast Bit
73 * @param bOspfLclAddrBit OSPF local address Bit
Jonathan Hart51539b82015-10-29 09:53:04 -070074 * @param bOspfNssaBit OSPF propagate NSSA Bit
Thejaswi N K2e542522015-11-02 20:52:17 +053075 * @return object of BgpPrefixAttrIGPFlags
76 */
77 public static BgpPrefixAttrIgpFlags of(final boolean bisisUpDownBit,
78 final boolean bOspfNoUnicastBit,
79 final boolean bOspfLclAddrBit,
Jonathan Hart51539b82015-10-29 09:53:04 -070080 final boolean bOspfNssaBit) {
Thejaswi N K2e542522015-11-02 20:52:17 +053081 return new BgpPrefixAttrIgpFlags(bisisUpDownBit, bOspfNoUnicastBit,
Jonathan Hart51539b82015-10-29 09:53:04 -070082 bOspfLclAddrBit, bOspfNssaBit);
Thejaswi N K2e542522015-11-02 20:52:17 +053083 }
84
85 /**
Thejaswi N Kd94c7542015-10-14 23:54:40 +053086 * Reads the IGP Flags.
87 *
88 * @param cb ChannelBuffer
89 * @return object of BgpPrefixAttrIGPFlags
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053090 * @throws BgpParseException while parsing BgpPrefixAttrIGPFlags
Thejaswi N Kd94c7542015-10-14 23:54:40 +053091 */
Thejaswi N K2e542522015-11-02 20:52:17 +053092 public static BgpPrefixAttrIgpFlags read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053093 throws BgpParseException {
Thejaswi N Kd94c7542015-10-14 23:54:40 +053094 boolean bisisUpDownBit = false;
95 boolean bOspfNoUnicastBit = false;
96 boolean bOspfLclAddrBit = false;
Jonathan Hart51539b82015-10-29 09:53:04 -070097 boolean bOspfNssaBit = false;
Thejaswi N Kd94c7542015-10-14 23:54:40 +053098
99 short lsAttrLength = cb.readShort();
100
101 if ((lsAttrLength != ATTR_PREFIX_FLAG_LEN)
102 || (cb.readableBytes() < lsAttrLength)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530103 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
104 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530105 lsAttrLength);
106 }
107
108 byte nodeFlagBits = cb.readByte();
109
Thejaswi N K2e542522015-11-02 20:52:17 +0530110 bisisUpDownBit = ((nodeFlagBits & FIRST_BIT) == FIRST_BIT);
111 bOspfNoUnicastBit = ((nodeFlagBits & SECOND_BIT) == SECOND_BIT);
112 bOspfLclAddrBit = ((nodeFlagBits & THIRD_BIT) == THIRD_BIT);
Jonathan Hart51539b82015-10-29 09:53:04 -0700113 bOspfNssaBit = ((nodeFlagBits & FOURTH_BIT) == FOURTH_BIT);
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530114
Thejaswi N K2e542522015-11-02 20:52:17 +0530115 return BgpPrefixAttrIgpFlags.of(bisisUpDownBit, bOspfNoUnicastBit,
Jonathan Hart51539b82015-10-29 09:53:04 -0700116 bOspfLclAddrBit, bOspfNssaBit);
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530117 }
118
119 /**
120 * Returns the IS-IS Up/Down Bit set or not.
121 *
122 * @return IS-IS Up/Down Bit set or not
123 */
Thejaswi N K2e542522015-11-02 20:52:17 +0530124 public boolean isisUpDownBit() {
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530125 return bisisUpDownBit;
126 }
127
128 /**
129 * Returns the OSPF no unicast Bit set or not.
130 *
131 * @return OSPF no unicast Bit set or not
132 */
Thejaswi N K2e542522015-11-02 20:52:17 +0530133 public boolean ospfNoUnicastBit() {
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530134 return bOspfNoUnicastBit;
135 }
136
137 /**
138 * Returns the OSPF local address Bit set or not.
139 *
140 * @return OSPF local address Bit set or not
141 */
Thejaswi N K2e542522015-11-02 20:52:17 +0530142 public boolean ospfLclAddrBit() {
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530143 return bOspfLclAddrBit;
144 }
145
146 /**
147 * Returns the OSPF propagate NSSA Bit set or not.
148 *
149 * @return OSPF propagate NSSA Bit set or not
150 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700151 public boolean ospfNssaBit() {
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530152 return bOspfNSSABit;
153 }
154
155 @Override
156 public short getType() {
157 return ATTR_PREFIX_FLAGBIT;
158 }
159
160 @Override
161 public int write(ChannelBuffer cb) {
162 // TODO This will be implemented in the next version
163 return 0;
164 }
165
166 @Override
167 public int hashCode() {
168 return Objects.hash(bisisUpDownBit, bOspfNoUnicastBit, bOspfLclAddrBit,
169 bOspfNSSABit);
170 }
171
172 @Override
173 public boolean equals(Object obj) {
174 if (this == obj) {
175 return true;
176 }
177
Thejaswi N K2e542522015-11-02 20:52:17 +0530178 if (obj instanceof BgpPrefixAttrIgpFlags) {
179 BgpPrefixAttrIgpFlags other = (BgpPrefixAttrIgpFlags) obj;
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530180 return Objects.equals(bisisUpDownBit, other.bisisUpDownBit)
181 && Objects.equals(bOspfNoUnicastBit,
182 other.bOspfNoUnicastBit)
Thejaswi N K2e542522015-11-02 20:52:17 +0530183 && Objects.equals(bOspfLclAddrBit, other.bOspfLclAddrBit)
184 && Objects.equals(bOspfNSSABit, other.bOspfNSSABit);
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530185 }
186 return false;
187 }
188
189 @Override
190 public String toString() {
191 return MoreObjects.toStringHelper(getClass())
192 .add("bisisUpDownBit", bisisUpDownBit)
193 .add("bOspfNoUnicastBit", bOspfNoUnicastBit)
194 .add("bOspfLclAddrBit", bOspfLclAddrBit)
195 .add("bOspfNSSABit", bOspfNSSABit).toString();
196 }
Priyanka B02040732015-11-29 11:30:29 +0530197
198 @Override
199 public int compareTo(Object o) {
200 // TODO Auto-generated method stub
201 return 0;
202 }
Thejaswi N Kd94c7542015-10-14 23:54:40 +0530203}