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
8 package org.opendaylight.protocol.pcep.pcc.mock;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.protocol.pcep.pcc.mock.api.PCCSession;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.Stateful1;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.Tlvs3;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.lsp.db.version.tlv.LspDbVersion;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.Tlvs1;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.Tlvs;
20 import org.opendaylight.yangtools.yang.common.Uint64;
22 final class PCCSyncOptimization {
23 private final boolean dbVersionMatch;
24 private final boolean isSyncAvoidanceEnabled;
25 private final boolean isDeltaSyncEnabled;
26 private final boolean isTriggeredInitialSynEnable;
27 private final boolean isTriggeredReSyncEnable;
28 private final LspDbVersion localLspDbVersion;
29 private final LspDbVersion remoteLspDbVersion;
31 private Uint64 lspDBVersion = Uint64.ONE;
32 private boolean resynchronizing = false;
34 PCCSyncOptimization(final @NonNull 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 Uint64 getLocalLspDbVersionValue() {
69 if (this.localLspDbVersion == null) {
72 return this.localLspDbVersion.getLspDbVersionValue();
75 public Uint64 getRemoteLspDbVersionValue() {
76 if (this.remoteLspDbVersion == null) {
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 return stateful1 != null && Boolean.TRUE.equals(stateful1.getIncludeDbVersion());
115 private static boolean isDeltaSync(final Tlvs openTlvs) {
116 final Stateful1 stateful1 = getStateful1(openTlvs);
117 return stateful1 != null && Boolean.TRUE.equals(stateful1.getDeltaLspSyncCapability());
120 private static boolean isTriggeredInitialSync(final Tlvs openTlvs) {
121 final Stateful1 stateful1 = getStateful1(openTlvs);
122 return stateful1 != null && Boolean.TRUE.equals(stateful1.getTriggeredInitialSync());
125 private static boolean isTriggeredReSync(final Tlvs openTlvs) {
126 final Stateful1 stateful1 = getStateful1(openTlvs);
127 return stateful1 != null && Boolean.TRUE.equals(stateful1.getTriggeredResync());
130 public Optional<Uint64> incrementLspDBVersion() {
131 if (!isSyncAvoidanceEnabled()) {
132 return Optional.empty();
133 } else if (isSyncNeedIt() && getLocalLspDbVersionValue() != null && !this.resynchronizing) {
134 this.lspDBVersion = getLocalLspDbVersionValue();
135 return Optional.of(this.lspDBVersion);
136 } else if (this.resynchronizing) {
137 return Optional.of(this.lspDBVersion);
140 this.lspDBVersion = Uint64.fromLongBits(lspDBVersion.longValue() + 1);
141 return Optional.of(this.lspDBVersion);
144 public boolean isSyncNeedIt() {
145 return !doesLspDbMatch() || this.resynchronizing;
148 void setResynchronizingState(final boolean resync) {
149 this.resynchronizing = resync;