2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.protocol.pcep.pcc.mock;
11 import static java.util.Objects.requireNonNull;
13 import java.math.BigInteger;
14 import java.util.Optional;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.protocol.pcep.pcc.mock.api.PCCSession;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.Stateful1;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.Tlvs3;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.lsp.db.version.tlv.LspDbVersion;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Tlvs1;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.Tlvs;
23 final class PCCSyncOptimization {
24 private final boolean dbVersionMatch;
25 private final boolean isSyncAvoidanceEnabled;
26 private final boolean isDeltaSyncEnabled;
27 private final boolean isTriggeredInitialSynEnable;
28 private final boolean isTriggeredReSyncEnable;
29 private final LspDbVersion localLspDbVersion;
30 private final LspDbVersion remoteLspDbVersion;
31 private BigInteger lspDBVersion = BigInteger.ONE;
32 private Boolean resynchronizing = Boolean.FALSE;
34 PCCSyncOptimization(@Nonnull final PCCSession session) {
35 requireNonNull(session);
36 final Tlvs remote = session.getRemoteTlvs();
37 final Tlvs local = session.localSessionCharacteristics();
38 this.localLspDbVersion = getLspDbVersion(local);
39 this.remoteLspDbVersion = getLspDbVersion(remote);
40 this.dbVersionMatch = compareLspDbVersion(this.localLspDbVersion, this.remoteLspDbVersion);
41 this.isSyncAvoidanceEnabled = isSyncAvoidance(local) && isSyncAvoidance(remote);
42 this.isDeltaSyncEnabled = isDeltaSync(local) && isDeltaSync(remote);
43 this.isTriggeredInitialSynEnable = isTriggeredInitialSync(local) && isTriggeredInitialSync(remote)
44 && (this.isDeltaSyncEnabled || this.isSyncAvoidanceEnabled);
45 this.isTriggeredReSyncEnable = isTriggeredReSync(local) && isTriggeredReSync(remote);
48 public boolean doesLspDbMatch() {
49 return this.dbVersionMatch;
52 public boolean isSyncAvoidanceEnabled() {
53 return this.isSyncAvoidanceEnabled;
56 public boolean isDeltaSyncEnabled() {
57 return this.isDeltaSyncEnabled;
60 public boolean isTriggeredInitSyncEnabled() {
61 return this.isTriggeredInitialSynEnable;
64 public boolean isTriggeredReSyncEnabled() {
65 return this.isTriggeredReSyncEnable;
68 public BigInteger getLocalLspDbVersionValue() {
69 if (this.localLspDbVersion == null) {
72 return this.localLspDbVersion.getLspDbVersionValue();
75 public BigInteger getRemoteLspDbVersionValue() {
76 if (this.remoteLspDbVersion == null) {
77 return BigInteger.ONE;
79 return this.remoteLspDbVersion.getLspDbVersionValue();
82 private static LspDbVersion getLspDbVersion(final Tlvs openTlvs) {
83 if (openTlvs != null) {
84 final Tlvs3 tlvs3 = openTlvs.augmentation(Tlvs3.class);
85 if (tlvs3 != null && tlvs3.getLspDbVersion() != null
86 && tlvs3.getLspDbVersion().getLspDbVersionValue() != null) {
87 return tlvs3.getLspDbVersion();
93 private static boolean compareLspDbVersion(final LspDbVersion local, final LspDbVersion remote) {
94 if (local != null && remote != null) {
95 return local.equals(remote);
100 private static Stateful1 getStateful1(final Tlvs openTlvs) {
101 if (openTlvs != null) {
102 final Tlvs1 tlvs1 = openTlvs.augmentation(Tlvs1.class);
103 if (tlvs1 != null && tlvs1.getStateful() != null) {
104 return tlvs1.getStateful().augmentation(Stateful1.class);
110 private static boolean isSyncAvoidance(final Tlvs openTlvs) {
111 final Stateful1 stateful1 = getStateful1(openTlvs);
112 if (stateful1 != null && stateful1.isIncludeDbVersion() != null) {
113 return stateful1.isIncludeDbVersion();
118 private static boolean isDeltaSync(final Tlvs openTlvs) {
119 final Stateful1 stateful1 = getStateful1(openTlvs);
120 if (stateful1 != null && stateful1.isDeltaLspSyncCapability() != null) {
121 return stateful1.isDeltaLspSyncCapability();
126 private static boolean isTriggeredInitialSync(final Tlvs openTlvs) {
127 final Stateful1 stateful1 = getStateful1(openTlvs);
128 if (stateful1 != null && stateful1.isTriggeredInitialSync() != null) {
129 return stateful1.isTriggeredInitialSync();
134 private static boolean isTriggeredReSync(final Tlvs openTlvs) {
135 final Stateful1 stateful1 = getStateful1(openTlvs);
136 if (stateful1 != null && stateful1.isTriggeredResync() != null) {
137 return stateful1.isTriggeredResync();
142 public Optional<BigInteger> incrementLspDBVersion() {
143 if (!isSyncAvoidanceEnabled()) {
144 return Optional.empty();
145 } else if (isSyncNeedIt() && getLocalLspDbVersionValue() != null && !this.resynchronizing) {
146 this.lspDBVersion = getLocalLspDbVersionValue();
147 return Optional.of(this.lspDBVersion);
148 } else if (this.resynchronizing) {
149 return Optional.of(this.lspDBVersion);
152 this.lspDBVersion = this.lspDBVersion.add(BigInteger.ONE);
153 return Optional.of(this.lspDBVersion);
156 public boolean isSyncNeedIt() {
157 if (doesLspDbMatch() && !this.resynchronizing) {
163 public void setResynchronizingState(final Boolean resync) {
164 this.resynchronizing = resync;