Bump versions to 0.21.6-SNAPSHOT
[bgpcep.git] / pcep / pcc-mock / src / main / java / org / opendaylight / protocol / pcep / pcc / mock / PCCSyncOptimization.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.pcc.mock;
9
10 import static java.util.Objects.requireNonNull;
11
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;
21
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;
30
31     private Uint64 lspDBVersion = Uint64.ONE;
32     private boolean resynchronizing = false;
33
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);
46     }
47
48     public boolean doesLspDbMatch() {
49         return this.dbVersionMatch;
50     }
51
52     public boolean isSyncAvoidanceEnabled() {
53         return this.isSyncAvoidanceEnabled;
54     }
55
56     public boolean isDeltaSyncEnabled() {
57         return this.isDeltaSyncEnabled;
58     }
59
60     public boolean isTriggeredInitSyncEnabled() {
61         return this.isTriggeredInitialSynEnable;
62     }
63
64     public boolean isTriggeredReSyncEnabled() {
65         return this.isTriggeredReSyncEnable;
66     }
67
68     public Uint64 getLocalLspDbVersionValue() {
69         if (this.localLspDbVersion == null) {
70             return null;
71         }
72         return this.localLspDbVersion.getLspDbVersionValue();
73     }
74
75     public Uint64 getRemoteLspDbVersionValue() {
76         if (this.remoteLspDbVersion == null) {
77             return Uint64.ONE;
78         }
79         return this.remoteLspDbVersion.getLspDbVersionValue();
80     }
81
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();
88             }
89         }
90         return null;
91     }
92
93     private static boolean compareLspDbVersion(final LspDbVersion local, final LspDbVersion remote) {
94         if (local != null && remote != null) {
95             return local.equals(remote);
96         }
97         return false;
98     }
99
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);
105             }
106         }
107         return null;
108     }
109
110     private static boolean isSyncAvoidance(final Tlvs openTlvs) {
111         final Stateful1 stateful1 = getStateful1(openTlvs);
112         return stateful1 != null && Boolean.TRUE.equals(stateful1.getIncludeDbVersion());
113     }
114
115     private static boolean isDeltaSync(final Tlvs openTlvs) {
116         final Stateful1 stateful1 = getStateful1(openTlvs);
117         return stateful1 != null && Boolean.TRUE.equals(stateful1.getDeltaLspSyncCapability());
118     }
119
120     private static boolean isTriggeredInitialSync(final Tlvs openTlvs) {
121         final Stateful1 stateful1 = getStateful1(openTlvs);
122         return stateful1 != null && Boolean.TRUE.equals(stateful1.getTriggeredInitialSync());
123     }
124
125     private static boolean isTriggeredReSync(final Tlvs openTlvs) {
126         final Stateful1 stateful1 = getStateful1(openTlvs);
127         return stateful1 != null && Boolean.TRUE.equals(stateful1.getTriggeredResync());
128     }
129
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);
138         }
139
140         this.lspDBVersion = Uint64.fromLongBits(lspDBVersion.longValue() + 1);
141         return Optional.of(this.lspDBVersion);
142     }
143
144     public boolean isSyncNeedIt() {
145         return !doesLspDbMatch() || this.resynchronizing;
146     }
147
148     void setResynchronizingState(final boolean resync) {
149         this.resynchronizing = resync;
150     }
151 }