blob: f56a3f90ba61a3a190635309e1640bff2de512ee [file] [log] [blame]
Thejaswi N K65800e12015-10-15 17:31:30 +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
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 K65800e12015-10-15 17:31:30 +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 link protection type attribute.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public final class BgpLinkAttrProtectionType implements BgpValueType {
Thejaswi N K65800e12015-10-15 17:31:30 +053034
35 protected static final Logger log = LoggerFactory
36 .getLogger(BgpLinkAttrProtectionType.class);
37
38 public static final int ATTRLINK_PROTECTIONTYPE = 1093;
39 public static final int LINK_PROTECTION_LEN = 2;
40
41 public static final int EXTRA_TRAFFIC = 0x01;
42 public static final int UNPROTECTED = 0x02;
43 public static final int SHARED = 0x04;
44 public static final int DEDICATED_ONE_ISTO_ONE = 0x08;
45 public static final int DEDICATED_ONE_PLUS_ONE = 0x10;
46 public static final int ENHANCED = 0x20;
47
48 /* Link Protection type flags */
49 private final boolean bExtraTraffic;
50 private final boolean bUnprotected;
51 private final boolean bShared;
52 private final boolean bDedOneIstoOne;
53 private final boolean bDedOnePlusOne;
54 private final boolean bEnhanced;
55
56 /**
57 * Constructor to initialize the value.
58 *
59 * @param bExtraTraffic Extra Traffic
60 * @param bUnprotected Unprotected
61 * @param bShared Shared
62 * @param bDedOneIstoOne Dedicated 1:1
63 * @param bDedOnePlusOne Dedicated 1+1
64 * @param bEnhanced Enhanced
65 */
66 private BgpLinkAttrProtectionType(boolean bExtraTraffic,
67 boolean bUnprotected,
68 boolean bShared, boolean bDedOneIstoOne,
69 boolean bDedOnePlusOne, boolean bEnhanced) {
70 this.bExtraTraffic = bExtraTraffic;
71 this.bUnprotected = bUnprotected;
72 this.bShared = bShared;
73 this.bDedOneIstoOne = bDedOneIstoOne;
74 this.bDedOnePlusOne = bDedOnePlusOne;
75 this.bEnhanced = bEnhanced;
76 }
77
78 /**
79 * Returns object of this class with specified values.
80 *
81 * @param bExtraTraffic Extra Traffic
82 * @param bUnprotected Unprotected
83 * @param bShared Shared
84 * @param bDedOneIstoOne Dedicated 1:1
85 * @param bDedOnePlusOne Dedicated 1+1
86 * @param bEnhanced Enhanced
87 * @return object of BgpLinkAttrProtectionType
88 */
89 public static BgpLinkAttrProtectionType of(boolean bExtraTraffic,
90 boolean bUnprotected,
91 boolean bShared,
92 boolean bDedOneIstoOne,
93 boolean bDedOnePlusOne,
94 boolean bEnhanced) {
95 return new BgpLinkAttrProtectionType(bExtraTraffic, bUnprotected,
96 bShared, bDedOneIstoOne,
97 bDedOnePlusOne, bEnhanced);
98 }
99
100 /**
101 * Reads the BGP link attributes protection type.
102 *
103 * @param cb Channel buffer
104 * @return object of type BgpLinkAttrProtectionType
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530105 * @throws BgpParseException while parsing BgpLinkAttrProtectionType
Thejaswi N K65800e12015-10-15 17:31:30 +0530106 */
107 public static BgpLinkAttrProtectionType read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530108 throws BgpParseException {
Thejaswi N K65800e12015-10-15 17:31:30 +0530109 short linkProtectionType;
110 byte higherByte;
111 short lsAttrLength = cb.readShort();
112
113 boolean bExtraTraffic;
114 boolean bUnprotected;
115 boolean bShared;
116 boolean bDedOneIstoOne;
117 boolean bDedOnePlusOne;
118 boolean bEnhanced;
119
120 if ((lsAttrLength != LINK_PROTECTION_LEN)
121 || (cb.readableBytes() < lsAttrLength)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530122 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
123 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N K65800e12015-10-15 17:31:30 +0530124 lsAttrLength);
125 }
126
127 linkProtectionType = cb.readShort();
128 higherByte = (byte) (linkProtectionType >> 8);
129
130 bExtraTraffic = ((higherByte & (byte) EXTRA_TRAFFIC) == EXTRA_TRAFFIC);
131 bUnprotected = ((higherByte & (byte) UNPROTECTED) == UNPROTECTED);
132 bShared = ((higherByte & (byte) SHARED) == SHARED);
133 bDedOneIstoOne = ((higherByte & (byte) DEDICATED_ONE_ISTO_ONE) == DEDICATED_ONE_ISTO_ONE);
134 bDedOnePlusOne = ((higherByte & (byte) DEDICATED_ONE_PLUS_ONE) == DEDICATED_ONE_PLUS_ONE);
135 bEnhanced = ((higherByte & (byte) ENHANCED) == ENHANCED);
136
137 return BgpLinkAttrProtectionType.of(bExtraTraffic, bUnprotected,
138 bShared, bDedOneIstoOne,
139 bDedOnePlusOne, bEnhanced);
140 }
141
142 /**
143 * Returns ExtraTraffic Bit.
144 *
145 * @return ExtraTraffic Bit
146 */
147 public boolean extraTraffic() {
148 return bExtraTraffic;
149 }
150
151 /**
152 * Returns Unprotected Bit.
153 *
154 * @return Unprotected Bit
155 */
156 public boolean unprotected() {
157 return bUnprotected;
158 }
159
160 /**
161 * Returns Shared Bit.
162 *
163 * @return Shared Bit
164 */
165 public boolean shared() {
166 return bShared;
167 }
168
169 /**
170 * Returns DedOneIstoOne Bit.
171 *
172 * @return DedOneIstoOne Bit
173 */
174 public boolean dedOneIstoOne() {
175 return bDedOneIstoOne;
176 }
177
178 /**
179 * Returns DedOnePlusOne Bit.
180 *
181 * @return DedOnePlusOne Bit
182 */
183 public boolean dedOnePlusOne() {
184 return bDedOnePlusOne;
185 }
186
187 /**
188 * Returns Enhanced Bit.
189 *
190 * @return Enhanced Bit
191 */
192 public boolean enhanced() {
193 return bEnhanced;
194 }
195
196 @Override
197 public short getType() {
198 return ATTRLINK_PROTECTIONTYPE;
199 }
200
201 @Override
202 public int hashCode() {
203 return Objects.hash(bExtraTraffic, bUnprotected, bShared,
204 bDedOneIstoOne, bDedOnePlusOne, bEnhanced);
205 }
206
207 @Override
208 public boolean equals(Object obj) {
209 if (this == obj) {
210 return true;
211 }
212
213 if (obj instanceof BgpLinkAttrProtectionType) {
214 BgpLinkAttrProtectionType other = (BgpLinkAttrProtectionType) obj;
215 return Objects.equals(bExtraTraffic, other.bExtraTraffic)
216 && Objects.equals(bUnprotected, other.bUnprotected)
217 && Objects.equals(bShared, other.bShared)
218 && Objects.equals(bDedOneIstoOne, other.bDedOneIstoOne)
219 && Objects.equals(bDedOnePlusOne, other.bDedOnePlusOne)
220 && Objects.equals(bEnhanced, other.bEnhanced);
221 }
222 return false;
223 }
224
225 @Override
226 public int write(ChannelBuffer cb) {
227 // TODO This will be implemented in the next version
228 return 0;
229 }
230
231 @Override
232 public String toString() {
233 return MoreObjects.toStringHelper(getClass())
234 .add("bExtraTraffic", bExtraTraffic)
235 .add("bUnprotected", bUnprotected).add("bShared", bShared)
236 .add("bDedOneIstoOne", bDedOneIstoOne)
237 .add("bDedOnePlusOne", bDedOnePlusOne)
238 .add("bEnhanced", bEnhanced).toString();
239 }
240}