Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.incubator.net.l2monitoring.cfm; |
| 17 | |
| 18 | import java.time.Duration; |
| 19 | |
| 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 22 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | |
| 28 | /** |
| 29 | * The default implementation of {@link org.onosproject.incubator.net.l2monitoring.cfm.Mep}. |
| 30 | */ |
| 31 | public class DefaultMep implements Mep { |
| 32 | |
| 33 | protected final MepId mepId; |
| 34 | protected final DeviceId deviceId; |
| 35 | protected final PortNumber port; |
| 36 | protected final MepDirection direction; |
| 37 | protected final MdId mdId; |
| 38 | protected final MaIdShort maId; |
| 39 | protected final VlanId primaryVid; |
| 40 | protected final boolean administrativeState; |
| 41 | protected final boolean cciEnabled; |
| 42 | protected final Priority ccmLtmPriority; |
| 43 | protected final FngAddress fngAddress; |
| 44 | protected final LowestFaultDefect lowestFaultPriorityDefect; |
| 45 | protected final Duration defectPresentTime; |
| 46 | protected final Duration defectAbsentTime; |
| 47 | |
| 48 | protected DefaultMep(DefaultMepBuilder mepBuilder) { |
| 49 | this.mepId = mepBuilder.mepId; |
| 50 | this.deviceId = mepBuilder.deviceId; |
| 51 | this.port = mepBuilder.port; |
| 52 | this.direction = mepBuilder.direction; |
| 53 | this.mdId = mepBuilder.mdId; |
| 54 | this.maId = mepBuilder.maId; |
| 55 | this.primaryVid = mepBuilder.primaryVid; |
| 56 | this.administrativeState = mepBuilder.administrativeState; |
| 57 | this.cciEnabled = mepBuilder.cciEnabled; |
| 58 | this.ccmLtmPriority = mepBuilder.ccmLtmPriority; |
| 59 | this.fngAddress = mepBuilder.fngAddress; |
| 60 | this.lowestFaultPriorityDefect = mepBuilder.lowestFaultPriorityDefect; |
| 61 | this.defectPresentTime = mepBuilder.defectPresentTime; |
| 62 | this.defectAbsentTime = mepBuilder.defectAbsentTime; |
| 63 | } |
| 64 | |
| 65 | protected DefaultMep(DefaultMep mep, AttributeName attrName, Object attrValue) { |
| 66 | this.mepId = mep.mepId; |
| 67 | this.deviceId = mep.deviceId; |
| 68 | this.port = mep.port; |
| 69 | this.direction = mep.direction; |
| 70 | this.mdId = mep.mdId; |
| 71 | this.maId = mep.maId; |
| 72 | if (attrName == AttributeName.PRIMARY_VID) { |
| 73 | this.primaryVid = (VlanId) attrValue; |
| 74 | } else { |
| 75 | this.primaryVid = mep.primaryVid; |
| 76 | } |
| 77 | if (attrName == AttributeName.ADMINISTRATIVE_STATE) { |
| 78 | this.administrativeState = (boolean) attrValue; |
| 79 | } else { |
| 80 | this.administrativeState = mep.administrativeState; |
| 81 | } |
| 82 | |
| 83 | if (attrName == AttributeName.CCI_ENABLED) { |
| 84 | this.cciEnabled = (boolean) attrValue; |
| 85 | } else { |
| 86 | this.cciEnabled = mep.cciEnabled; |
| 87 | } |
| 88 | |
| 89 | if (attrName == AttributeName.CCM_LTM_PRIORITY) { |
| 90 | this.ccmLtmPriority = (Priority) attrValue; |
| 91 | } else { |
| 92 | this.ccmLtmPriority = mep.ccmLtmPriority; |
| 93 | } |
| 94 | |
| 95 | if (attrName == AttributeName.FNG_ADDRESS) { |
| 96 | this.fngAddress = (FngAddress) attrValue; |
| 97 | } else { |
| 98 | this.fngAddress = mep.fngAddress; |
| 99 | } |
| 100 | |
| 101 | if (attrName == AttributeName.LOWEST_FAULT_PRIORITY_DEFECT) { |
| 102 | this.lowestFaultPriorityDefect = (LowestFaultDefect) attrValue; |
| 103 | } else { |
| 104 | this.lowestFaultPriorityDefect = mep.lowestFaultPriorityDefect; |
| 105 | } |
| 106 | |
| 107 | if (attrName == AttributeName.DEFECT_PRESENT_TIME) { |
| 108 | this.defectPresentTime = (Duration) attrValue; |
| 109 | } else { |
| 110 | this.defectPresentTime = mep.defectPresentTime; |
| 111 | } |
| 112 | |
| 113 | if (attrName == AttributeName.DEFECT_ABSENT_TIME) { |
| 114 | this.defectAbsentTime = (Duration) attrValue; |
| 115 | } else { |
| 116 | this.defectAbsentTime = mep.defectAbsentTime; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public MepId mepId() { |
| 122 | return mepId; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public DeviceId deviceId() { |
| 127 | return deviceId; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public PortNumber port() { |
| 132 | return port; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public MepDirection direction() { |
| 137 | return direction; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public MdId mdId() { |
| 142 | return mdId; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public MaIdShort maId() { |
| 147 | return maId; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public VlanId primaryVid() { |
| 152 | return primaryVid; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Mep withPrimaryVid(VlanId primaryVid) { |
| 157 | return new DefaultMep(this, AttributeName.PRIMARY_VID, primaryVid); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public boolean administrativeState() { |
| 162 | return administrativeState; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public Mep withAdministrativeState(boolean adminState) { |
| 167 | return new DefaultMep(this, AttributeName.ADMINISTRATIVE_STATE, adminState); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public Boolean cciEnabled() { |
| 172 | return cciEnabled; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public Mep withCciEnabled(boolean cciEnabled) { |
| 177 | return new DefaultMep(this, AttributeName.CCI_ENABLED, cciEnabled); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Priority ccmLtmPriority() { |
| 182 | return ccmLtmPriority; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public Mep withCcmLtmPriority(Priority priority) { |
| 187 | return new DefaultMep(this, AttributeName.CCM_LTM_PRIORITY, priority); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public Mep withFngAddress(FngAddress fngAddress) { |
| 192 | return new DefaultMep(this, AttributeName.FNG_ADDRESS, fngAddress); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public FngAddress fngAddress() { |
| 197 | return fngAddress; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public LowestFaultDefect lowestFaultPriorityDefect() { |
| 202 | return lowestFaultPriorityDefect; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Mep withLowestFaultPriorityDefect(LowestFaultDefect lowestFdType) { |
| 207 | return new DefaultMep(this, AttributeName.LOWEST_FAULT_PRIORITY_DEFECT, lowestFdType); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public Duration defectPresentTime() { |
| 212 | return defectPresentTime; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public Mep withDefectPresentTime(Duration duration) { |
| 217 | return new DefaultMep(this, AttributeName.DEFECT_PRESENT_TIME, duration); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Duration defectAbsentTime() { |
| 222 | return defectAbsentTime; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public Mep withDefectAbsentTime(Duration duration) { |
| 227 | return new DefaultMep(this, AttributeName.DEFECT_ABSENT_TIME, duration); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | @Override |
| 232 | public boolean equals(Object o) { |
| 233 | if (this == o) { |
| 234 | return true; |
| 235 | } |
| 236 | if (o == null || getClass() != o.getClass()) { |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | DefaultMep that = (DefaultMep) o; |
| 241 | |
| 242 | if (administrativeState != that.administrativeState) { |
| 243 | return false; |
| 244 | } |
| 245 | if (cciEnabled != that.cciEnabled) { |
| 246 | return false; |
| 247 | } |
| 248 | if (!mepId.equals(that.mepId)) { |
| 249 | return false; |
| 250 | } |
| 251 | if (!deviceId.equals(that.deviceId)) { |
| 252 | return false; |
| 253 | } |
| 254 | if (!port.equals(that.port)) { |
| 255 | return false; |
| 256 | } |
| 257 | if (direction != that.direction) { |
| 258 | return false; |
| 259 | } |
| 260 | if (!mdId.equals(that.mdId)) { |
| 261 | return false; |
| 262 | } |
| 263 | if (!maId.equals(that.maId)) { |
| 264 | return false; |
| 265 | } |
| 266 | if (primaryVid != null ? !primaryVid.equals(that.primaryVid) : that.primaryVid != null) { |
| 267 | return false; |
| 268 | } |
| 269 | if (ccmLtmPriority != that.ccmLtmPriority) { |
| 270 | return false; |
| 271 | } |
| 272 | if (fngAddress != null ? !fngAddress.equals(that.fngAddress) : that.fngAddress != null) { |
| 273 | return false; |
| 274 | } |
| 275 | if (lowestFaultPriorityDefect != that.lowestFaultPriorityDefect) { |
| 276 | return false; |
| 277 | } |
| 278 | if (defectPresentTime != null ? |
| 279 | !defectPresentTime.equals(that.defectPresentTime) : that.defectPresentTime != null) { |
| 280 | return false; |
| 281 | } |
| 282 | return defectAbsentTime != null ? |
| 283 | defectAbsentTime.equals(that.defectAbsentTime) : that.defectAbsentTime == null; |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public int hashCode() { |
| 288 | int result = mepId.hashCode(); |
| 289 | result = 31 * result + deviceId.hashCode(); |
| 290 | result = 31 * result + port.hashCode(); |
| 291 | result = 31 * result + (direction != null ? direction.hashCode() : 0); |
| 292 | result = 31 * result + mdId.hashCode(); |
| 293 | result = 31 * result + maId.hashCode(); |
| 294 | result = 31 * result + (primaryVid != null ? primaryVid.hashCode() : 0); |
| 295 | result = 31 * result + (administrativeState ? 1 : 0); |
| 296 | result = 31 * result + (cciEnabled ? 1 : 0); |
| 297 | result = 31 * result + (ccmLtmPriority != null ? ccmLtmPriority.hashCode() : 0); |
| 298 | result = 31 * result + (fngAddress != null ? fngAddress.hashCode() : 0); |
| 299 | result = 31 * result + (lowestFaultPriorityDefect != null ? lowestFaultPriorityDefect.hashCode() : 0); |
| 300 | result = 31 * result + (defectPresentTime != null ? defectPresentTime.hashCode() : 0); |
| 301 | result = 31 * result + (defectAbsentTime != null ? defectAbsentTime.hashCode() : 0); |
| 302 | return result; |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public String toString() { |
| 307 | return "DefaultMep{" + |
| 308 | "mepId=" + mepId + |
| 309 | ", deviceId=" + deviceId + |
| 310 | ", port=" + port + |
| 311 | ", direction=" + direction + |
| 312 | ", mdId=" + mdId + |
| 313 | ", maId=" + maId + |
| 314 | ", primaryVid=" + primaryVid + |
| 315 | ", administrativeState=" + administrativeState + |
| 316 | ", cciEnabled=" + cciEnabled + |
| 317 | ", ccmLtmPriority=" + ccmLtmPriority + |
| 318 | ", fngAddress=" + fngAddress + |
| 319 | ", lowestFaultPriorityDefect=" + lowestFaultPriorityDefect + |
| 320 | ", defectPresentTime=" + defectPresentTime + |
| 321 | ", defectAbsentTime=" + defectAbsentTime + |
| 322 | '}'; |
| 323 | } |
| 324 | |
| 325 | public static MepBuilder builder( |
| 326 | MepId mepId, |
| 327 | DeviceId deviceId, |
| 328 | PortNumber port, |
| 329 | MepDirection direction, |
| 330 | MdId mdId, |
| 331 | MaIdShort maId) |
| 332 | throws CfmConfigException { |
| 333 | return new DefaultMepBuilder(mepId, deviceId, port, direction, mdId, maId); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Builder for {@link org.onosproject.incubator.net.l2monitoring.cfm.Mep}. |
| 338 | */ |
| 339 | protected static class DefaultMepBuilder implements Mep.MepBuilder { |
| 340 | protected final MepId mepId; |
| 341 | protected final DeviceId deviceId; |
| 342 | protected final PortNumber port; |
| 343 | protected final MepDirection direction; |
| 344 | protected final MdId mdId; |
| 345 | protected final MaIdShort maId; |
| 346 | protected VlanId primaryVid; |
| 347 | protected boolean administrativeState; |
| 348 | protected boolean cciEnabled; |
| 349 | protected Priority ccmLtmPriority; |
| 350 | protected FngAddress fngAddress; |
| 351 | protected LowestFaultDefect lowestFaultPriorityDefect; |
| 352 | protected Duration defectPresentTime; |
| 353 | protected Duration defectAbsentTime; |
| 354 | |
| 355 | protected DefaultMepBuilder(MepId mepId, DeviceId deviceId, PortNumber port, |
| 356 | MepDirection direction, MdId mdId, MaIdShort maId) |
| 357 | throws CfmConfigException { |
| 358 | if (port.isLogical()) { |
| 359 | throw new CfmConfigException("Port must be physical. Rejecting; " + port.toString()); |
| 360 | } else if (mepId == null) { |
| 361 | throw new CfmConfigException("MepId is null"); |
| 362 | } else if (mdId == null) { |
| 363 | throw new CfmConfigException("MdId is null"); |
| 364 | } else if (maId == null) { |
| 365 | throw new CfmConfigException("MaId is null"); |
| 366 | } |
| 367 | this.mepId = mepId; |
| 368 | this.deviceId = deviceId; |
| 369 | this.port = port; |
| 370 | this.direction = direction; |
| 371 | this.mdId = mdId; |
| 372 | this.maId = maId; |
| 373 | } |
| 374 | |
| 375 | public DefaultMepBuilder(Mep mep) { |
| 376 | this.mepId = mep.mepId(); |
| 377 | this.deviceId = mep.deviceId(); |
| 378 | this.port = mep.port(); |
| 379 | this.direction = mep.direction(); |
| 380 | this.mdId = mep.mdId(); |
| 381 | this.maId = mep.maId(); |
| 382 | this.primaryVid = mep.primaryVid(); |
| 383 | this.administrativeState = mep.administrativeState(); |
| 384 | this.cciEnabled = mep.cciEnabled(); |
| 385 | this.ccmLtmPriority = mep.ccmLtmPriority(); |
| 386 | this.fngAddress = mep.fngAddress(); |
| 387 | this.lowestFaultPriorityDefect = mep.lowestFaultPriorityDefect(); |
| 388 | this.defectPresentTime = mep.defectPresentTime(); |
| 389 | this.defectAbsentTime = mep.defectAbsentTime(); |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public MepBuilder primaryVid(VlanId primaryVid) { |
| 394 | this.primaryVid = primaryVid; |
| 395 | return this; |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public MepBuilder administrativeState(boolean administrativeState) { |
| 400 | this.administrativeState = administrativeState; |
| 401 | return this; |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public MepBuilder cciEnabled(boolean cciEnabled) { |
| 406 | this.cciEnabled = cciEnabled; |
| 407 | return this; |
| 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public MepBuilder ccmLtmPriority(Priority ccmLtmPriority) { |
| 412 | this.ccmLtmPriority = ccmLtmPriority; |
| 413 | return this; |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public MepBuilder fngAddress(FngAddress fngAddress) { |
| 418 | this.fngAddress = fngAddress; |
| 419 | return this; |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public MepBuilder lowestFaultPriorityDefect( |
| 424 | LowestFaultDefect lowestFaultPriorityDefect) { |
| 425 | this.lowestFaultPriorityDefect = lowestFaultPriorityDefect; |
| 426 | return this; |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public MepBuilder defectPresentTime(Duration defectPresentTime) { |
| 431 | this.defectPresentTime = defectPresentTime; |
| 432 | return this; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public MepBuilder defectAbsentTime(Duration defectAbsentTime) { |
| 437 | this.defectAbsentTime = defectAbsentTime; |
| 438 | return this; |
| 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public Mep build() throws CfmConfigException { |
| 443 | return new DefaultMep(this); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | private enum AttributeName { |
| 448 | PRIMARY_VID, |
| 449 | ADMINISTRATIVE_STATE, |
| 450 | CCI_ENABLED, |
| 451 | CCM_LTM_PRIORITY, |
| 452 | FNG_ADDRESS, |
| 453 | LOWEST_FAULT_PRIORITY_DEFECT, |
| 454 | DEFECT_PRESENT_TIME, |
| 455 | DEFECT_ABSENT_TIME; |
| 456 | } |
| 457 | } |