Kalyankumar Asangi | 27728f2 | 2016-02-17 15:46:28 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Kalyankumar Asangi | 27728f2 | 2016-02-17 15:46:28 +0530 | [diff] [blame] | 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.ospf.controller.lsdb; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.base.Objects; |
| 20 | import org.onosproject.ospf.controller.LsaWrapper; |
| 21 | import org.onosproject.ospf.controller.LsdbAge; |
| 22 | import org.onosproject.ospf.controller.OspfInterface; |
| 23 | import org.onosproject.ospf.controller.OspfLsa; |
| 24 | import org.onosproject.ospf.controller.OspfLsaType; |
| 25 | import org.onosproject.ospf.protocol.lsa.LsaHeader; |
| 26 | import org.onosproject.ospf.protocol.util.OspfParameters; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | /** |
| 31 | * Wrapper object to store LSA and associated metadata information. |
| 32 | */ |
| 33 | public class LsaWrapperImpl implements LsaWrapper { |
| 34 | private static final Logger log = LoggerFactory.getLogger(LsaWrapperImpl.class); |
| 35 | private LsaHeader lsaHeader; |
| 36 | private int lsaAgeReceived; |
| 37 | private int ageCounterWhenReceived; |
| 38 | private boolean isSelfOriginated; |
| 39 | private OspfLsaType lsaType; |
| 40 | private OspfLsa ospfLsa; |
| 41 | private int noReTransmissionLists; |
| 42 | private boolean inAnAgeBin; |
| 43 | private boolean changedSinceLastFlood; |
| 44 | private boolean isSequenceRollOver; |
| 45 | private boolean sentReplyForOlderLsa; |
| 46 | private boolean checkAge; // Queued for check sum verification |
| 47 | private boolean isAging; |
| 48 | private String lsaProcessing; //for LSAQueueConsumer processing |
| 49 | private int binNumber = -1; |
| 50 | private OspfInterface ospfInterface; |
| 51 | private LsdbAge lsdbAge; |
| 52 | |
| 53 | /** |
| 54 | * Gets the LSA type. |
| 55 | * |
| 56 | * @return LSA type |
| 57 | */ |
| 58 | public OspfLsaType lsaType() { |
| 59 | return lsaType; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Sets the LSA type. |
| 64 | * |
| 65 | * @param lsaType LSA type |
| 66 | */ |
| 67 | public void setLsaType(OspfLsaType lsaType) { |
| 68 | this.lsaType = lsaType; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Gets if self originated or not. |
| 73 | * |
| 74 | * @return true if self originated else false |
| 75 | */ |
| 76 | public boolean isSelfOriginated() { |
| 77 | return isSelfOriginated; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Sets if self originated or not. |
| 82 | * |
| 83 | * @param isSelfOriginated true if self originated else false |
| 84 | */ |
| 85 | public void setIsSelfOriginated(boolean isSelfOriginated) { |
| 86 | this.isSelfOriginated = isSelfOriginated; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Adds the LSA in the wrapper. |
| 91 | * |
| 92 | * @param lsaType LSA type |
| 93 | * @param ospfLsa LSA instance |
| 94 | */ |
| 95 | public void addLsa(OspfLsaType lsaType, OspfLsa ospfLsa) { |
| 96 | this.lsaType = lsaType; |
| 97 | this.lsaHeader = (LsaHeader) ospfLsa.lsaHeader(); |
| 98 | this.ospfLsa = ospfLsa; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Age of LSA when received. |
| 103 | * |
| 104 | * @return Age of LSA when received |
| 105 | */ |
| 106 | public int lsaAgeReceived() { |
| 107 | return lsaAgeReceived; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Sets the Age of LSA when received. |
| 112 | * |
| 113 | * @param lsaAgeReceived Age of LSA when received |
| 114 | */ |
| 115 | public void setLsaAgeReceived(int lsaAgeReceived) { |
| 116 | this.lsaAgeReceived = lsaAgeReceived; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Gets the LSA header. |
| 121 | * |
| 122 | * @return LSA header instance |
| 123 | */ |
| 124 | public LsaHeader lsaHeader() { |
| 125 | lsaHeader.setAge(currentAge()); |
| 126 | return lsaHeader; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Sets LSA header. |
| 131 | * |
| 132 | * @param lsaHeader LSA header |
| 133 | */ |
| 134 | public void setLsaHeader(LsaHeader lsaHeader) { |
| 135 | this.lsaHeader = lsaHeader; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Gets the LSA. |
| 140 | * |
| 141 | * @return LSA instance |
| 142 | */ |
| 143 | public OspfLsa ospfLsa() { |
| 144 | LsaHeader lsaHeader = (LsaHeader) ospfLsa; |
| 145 | lsaHeader.setAge(currentAge()); |
| 146 | |
| 147 | return lsaHeader; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Sets the LSA. |
| 152 | * |
| 153 | * @param ospfLsa LSA instance |
| 154 | */ |
| 155 | public void setOspfLsa(OspfLsa ospfLsa) { |
| 156 | this.ospfLsa = ospfLsa; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Gets number of LSAs in retransmission list. |
| 161 | * |
| 162 | * @return number of LSAs in retransmission list |
| 163 | */ |
| 164 | public int noReTransmissionLists() { |
| 165 | return noReTransmissionLists; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Sets number of LSAs in retransmission list. |
| 170 | * |
| 171 | * @param noReTransmissionLists number of LSAs in retransmission list |
| 172 | */ |
| 173 | public void setNoReTransmissionLists(int noReTransmissionLists) { |
| 174 | this.noReTransmissionLists = noReTransmissionLists; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * whether LSA in age bin or not. |
| 179 | * |
| 180 | * @return true if LSA in age bin else false |
| 181 | */ |
| 182 | public boolean isInAnAgeBin() { |
| 183 | return inAnAgeBin; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Sets whether LSA in age bin or not. |
| 188 | * |
| 189 | * @param inAnAgeBin whether LSA in age bin or not |
| 190 | */ |
| 191 | public void setInAnAgeBin(boolean inAnAgeBin) { |
| 192 | this.inAnAgeBin = inAnAgeBin; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Gets if LSA is changed since last flood. |
| 197 | * |
| 198 | * @return true if LSA is changed since last flood else false |
| 199 | */ |
| 200 | public boolean isChangedSinceLastFlood() { |
| 201 | return changedSinceLastFlood; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Sets if LSA is changed since last flood. |
| 206 | * |
| 207 | * @param changedSinceLastFlood true if LSA is changed since last flood else false |
| 208 | */ |
| 209 | public void setChangedSinceLastFlood(boolean changedSinceLastFlood) { |
| 210 | this.changedSinceLastFlood = changedSinceLastFlood; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Gets if sequence number rolled over. |
| 215 | * |
| 216 | * @return true if sequence rolled over else false. |
| 217 | */ |
| 218 | public boolean isSequenceRollOver() { |
| 219 | return isSequenceRollOver; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Sets if sequence number rolled over. |
| 224 | * |
| 225 | * @param isSequenceRollOver true if sequence rolled over else false |
| 226 | */ |
| 227 | public void setIsSequenceRollOver(boolean isSequenceRollOver) { |
| 228 | this.isSequenceRollOver = isSequenceRollOver; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Gets is sent reply for older LSA. |
| 233 | * |
| 234 | * @return true if sent reply for old LSA else false |
| 235 | */ |
| 236 | public boolean isSentReplyForOlderLsa() { |
| 237 | return sentReplyForOlderLsa; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Sets is sent reply for older lsa. |
| 242 | * |
| 243 | * @param sentReplyForOlderLsa true if sent reply for older lsa else false |
| 244 | */ |
| 245 | public void setSentReplyForOlderLsa(boolean sentReplyForOlderLsa) { |
| 246 | this.sentReplyForOlderLsa = sentReplyForOlderLsa; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Gets check age flag. |
| 251 | * |
| 252 | * @return true check age flag is set else false |
| 253 | */ |
| 254 | public boolean isCheckAge() { |
| 255 | return checkAge; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Sets check age flag. |
| 260 | * |
| 261 | * @param checkAge check age flag. |
| 262 | */ |
| 263 | public void setCheckAge(boolean checkAge) { |
| 264 | this.checkAge = checkAge; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Gets value of aging flag. |
| 269 | * |
| 270 | * @return is aging flag |
| 271 | */ |
| 272 | public boolean isAging() { |
| 273 | return isAging; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Sets aging flag. |
| 278 | * |
| 279 | * @param isAging is aging flag |
| 280 | */ |
| 281 | public void setIsAging(boolean isAging) { |
| 282 | this.isAging = isAging; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Gets the LSDB age. |
| 287 | * |
| 288 | * @return LSDB age |
| 289 | */ |
| 290 | public LsdbAge getLsdbAge() { |
| 291 | return lsdbAge; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Sets the LSDB age. |
| 296 | * |
| 297 | * @param lsdbAge LSDB age |
| 298 | */ |
| 299 | public void setLsdbAge(LsdbAge lsdbAge) { |
| 300 | this.lsdbAge = lsdbAge; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Gets the current LSA Age. |
| 305 | * |
| 306 | * @return LSA age |
| 307 | */ |
| 308 | public int currentAge() { |
| 309 | |
| 310 | int currentAge = 0; |
| 311 | //ls age received |
| 312 | if (lsdbAge.getAgeCounter() >= ageCounterWhenReceived) { |
| 313 | currentAge = lsaAgeReceived + (lsdbAge.getAgeCounter() - ageCounterWhenReceived); |
| 314 | } else { |
| 315 | currentAge = lsaAgeReceived + ((OspfParameters.MAXAGE + lsdbAge.getAgeCounter()) |
| 316 | - ageCounterWhenReceived); |
| 317 | } |
| 318 | |
| 319 | if (currentAge >= OspfParameters.MAXAGE) { |
| 320 | return OspfParameters.MAXAGE; |
| 321 | } |
| 322 | |
| 323 | return currentAge; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Gets the age counter when received. |
| 328 | * |
| 329 | * @return the age counter when received |
| 330 | */ |
| 331 | public int ageCounterWhenReceived() { |
| 332 | return ageCounterWhenReceived; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Sets the age counter when received. |
| 337 | * |
| 338 | * @param ageCounterWhenReceived the age counter when received |
| 339 | */ |
| 340 | public void setAgeCounterWhenReceived(int ageCounterWhenReceived) { |
| 341 | this.ageCounterWhenReceived = ageCounterWhenReceived; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Gets the LSA process command. |
| 346 | * |
| 347 | * @return LSA process command |
| 348 | */ |
| 349 | public String lsaProcessing() { |
| 350 | return lsaProcessing; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Sets the LSA process command. |
| 355 | * |
| 356 | * @param lsaProcessing LSA process command |
| 357 | */ |
| 358 | public void setLsaProcessing(String lsaProcessing) { |
| 359 | this.lsaProcessing = lsaProcessing; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Gets bin number. |
| 364 | * |
| 365 | * @return bin number |
| 366 | */ |
| 367 | public int binNumber() { |
| 368 | return binNumber; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Sets bin number. |
| 373 | * |
| 374 | * @param binNumber bin number |
| 375 | */ |
| 376 | public void setBinNumber(int binNumber) { |
| 377 | this.binNumber = binNumber; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | /** |
| 382 | * Get the OSPF interface. |
| 383 | * |
| 384 | * @return the OSPF interface. |
| 385 | */ |
| 386 | public OspfInterface ospfInterface() { |
| 387 | return ospfInterface; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Sets the OSPF interface. |
| 392 | * |
| 393 | * @param ospfInterface OSPF interface instance |
| 394 | */ |
| 395 | @Override |
| 396 | public void setOspfInterface(OspfInterface ospfInterface) { |
| 397 | this.ospfInterface = ospfInterface; |
| 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public String toString() { |
| 402 | return MoreObjects.toStringHelper(getClass()) |
| 403 | .omitNullValues() |
| 404 | .add("lsaAgeReceived", lsaAgeReceived) |
| 405 | .add("ageCounterWhenReceived", ageCounterWhenReceived) |
| 406 | .add("isSelfOriginated", isSelfOriginated) |
| 407 | .add("lsaHeader", lsaHeader) |
| 408 | .add("lsaType", lsaType) |
| 409 | .add("ospflsa", ospfLsa) |
| 410 | .add("noReTransmissionLists", noReTransmissionLists) |
| 411 | .add("inAnAgeBin", inAnAgeBin) |
| 412 | .add("changedSinceLasFlood", changedSinceLastFlood) |
| 413 | .add("isSequenceRollOver", isSequenceRollOver) |
| 414 | .add("sentReplyForOlderLSA", sentReplyForOlderLsa) |
| 415 | .add("checkAge", checkAge) |
| 416 | .add("isAging", isAging) |
| 417 | .add("lsaProcessing", lsaProcessing) |
| 418 | .toString(); |
| 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public boolean equals(Object o) { |
| 423 | if (this == o) { |
| 424 | return true; |
| 425 | } |
| 426 | if (o == null || getClass() != o.getClass()) { |
| 427 | return false; |
| 428 | } |
| 429 | LsaWrapperImpl that = (LsaWrapperImpl) o; |
| 430 | return Objects.equal(lsaAgeReceived, that.lsaAgeReceived) && |
| 431 | Objects.equal(ageCounterWhenReceived, that.ageCounterWhenReceived) && |
| 432 | Objects.equal(isSelfOriginated, that.isSelfOriginated) && |
| 433 | Objects.equal(lsaHeader, that.lsaHeader) && |
| 434 | Objects.equal(ospfLsa, that.ospfLsa) && |
| 435 | Objects.equal(noReTransmissionLists, that.noReTransmissionLists) && |
| 436 | Objects.equal(inAnAgeBin, that.inAnAgeBin) && |
| 437 | Objects.equal(changedSinceLastFlood, that.changedSinceLastFlood) && |
| 438 | Objects.equal(isSequenceRollOver, that.isSequenceRollOver) && |
| 439 | Objects.equal(sentReplyForOlderLsa, that.sentReplyForOlderLsa) && |
| 440 | Objects.equal(checkAge, that.checkAge) && |
| 441 | Objects.equal(isAging, that.isAging) && |
| 442 | Objects.equal(lsaProcessing, that.lsaProcessing); |
| 443 | } |
| 444 | |
| 445 | @Override |
| 446 | public int hashCode() { |
| 447 | return Objects.hashCode(lsaAgeReceived, lsaAgeReceived, ageCounterWhenReceived, isSelfOriginated, |
| 448 | lsaHeader, lsaType, ospfLsa, noReTransmissionLists, inAnAgeBin, |
| 449 | changedSinceLastFlood, isSequenceRollOver, sentReplyForOlderLsa, |
| 450 | checkAge, isAging, lsaProcessing); |
| 451 | } |
| 452 | } |