BUG-2208: PCC-MOCK Stateful Sync Opt
[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
9 package org.opendaylight.protocol.pcep.pcc.mock;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.math.BigInteger;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.protocol.pcep.pcc.mock.api.PCCSession;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Stateful1;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs3;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.lsp.db.version.tlv.LspDbVersion;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
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     private BigInteger lspDBVersion = BigInteger.ONE;
31     private Boolean resynchronizing = Boolean.FALSE;
32
33     public PCCSyncOptimization(@Nonnull final PCCSession session) {
34         Preconditions.checkNotNull(session);
35         final Tlvs remote = session.getRemoteTlvs();
36         final Tlvs local = session.localSessionCharacteristics();
37         this.localLspDbVersion = getLspDbVersion(local);
38         this.remoteLspDbVersion = getLspDbVersion(remote);
39         this.dbVersionMatch = compareLspDbVersion(localLspDbVersion, remoteLspDbVersion);
40         this.isSyncAvoidanceEnabled = isSyncAvoidance(local) && isSyncAvoidance(remote);
41         this.isDeltaSyncEnabled = isDeltaSync(local) && isDeltaSync(remote);
42         this.isTriggeredInitialSynEnable = isTriggeredInitialSync(local) && isTriggeredInitialSync(remote) &&
43             (this.isDeltaSyncEnabled || this.isSyncAvoidanceEnabled);
44         this.isTriggeredReSyncEnable = isTriggeredReSync(local) && isTriggeredReSync(remote);
45     }
46
47     public boolean doesLspDbMatch() {
48         return dbVersionMatch;
49     }
50
51     public boolean isSyncAvoidanceEnabled() {
52         return isSyncAvoidanceEnabled;
53     }
54
55     public boolean isDeltaSyncEnabled() {
56         return isDeltaSyncEnabled;
57     }
58
59     public boolean isTriggeredInitSyncEnabled() {
60         return isTriggeredInitialSynEnable;
61     }
62
63     public boolean isTriggeredReSyncEnabled() {
64         return isTriggeredReSyncEnable;
65     }
66
67     public BigInteger getLocalLspDbVersionValue() {
68         if (this.localLspDbVersion == null) {
69             return null;
70         }
71         return this.localLspDbVersion.getLspDbVersionValue();
72     }
73
74     public BigInteger getRemoteLspDbVersionValue() {
75         if (this.remoteLspDbVersion == null) {
76             return BigInteger.ONE;
77         }
78         return this.remoteLspDbVersion.getLspDbVersionValue();
79     }
80
81     public boolean isRemoteLspDbVersionNull() {
82         if (this.remoteLspDbVersion == null) {
83             return true;
84         }
85         return false;
86     }
87
88     private static LspDbVersion getLspDbVersion(final Tlvs openTlvs) {
89         if (openTlvs != null) {
90             final Tlvs3 tlvs3 = openTlvs.getAugmentation(Tlvs3.class);
91             if (tlvs3 != null && tlvs3.getLspDbVersion() != null
92                 && tlvs3.getLspDbVersion().getLspDbVersionValue() != null) {
93                 return tlvs3.getLspDbVersion();
94             }
95         }
96         return null;
97     }
98
99     private static boolean compareLspDbVersion(final LspDbVersion local, final LspDbVersion remote) {
100         if (local != null && remote != null) {
101             return local.equals(remote);
102         }
103         return false;
104     }
105
106     private static Stateful1 getStateful1(final Tlvs openTlvs) {
107         if (openTlvs != null) {
108             final Tlvs1 tlvs1 = openTlvs.getAugmentation(Tlvs1.class);
109             if (tlvs1 != null && tlvs1.getStateful() != null) {
110                 return tlvs1.getStateful().getAugmentation(Stateful1.class);
111             }
112         }
113         return null;
114     }
115
116     private static boolean isSyncAvoidance(final Tlvs openTlvs) {
117         final Stateful1 stateful1 = getStateful1(openTlvs);
118         if (stateful1 != null && stateful1.isIncludeDbVersion() != null) {
119             return stateful1.isIncludeDbVersion();
120         }
121         return false;
122     }
123
124     private static boolean isDeltaSync(final Tlvs openTlvs) {
125         final Stateful1 stateful1 = getStateful1(openTlvs);
126         if (stateful1 != null && stateful1.isDeltaLspSyncCapability() != null) {
127             return stateful1.isDeltaLspSyncCapability();
128         }
129         return false;
130     }
131
132     private static boolean isTriggeredInitialSync(final Tlvs openTlvs) {
133         final Stateful1 stateful1 = getStateful1(openTlvs);
134         if (stateful1 != null && stateful1.isTriggeredInitialSync() != null) {
135             return stateful1.isTriggeredInitialSync();
136         }
137         return false;
138     }
139
140     private static boolean isTriggeredReSync(final Tlvs openTlvs) {
141         final Stateful1 stateful1 = getStateful1(openTlvs);
142         if (stateful1 != null && stateful1.isTriggeredResync() != null) {
143             return stateful1.isTriggeredResync();
144         }
145         return false;
146     }
147
148     public Optional<BigInteger> incrementLspDBVersion() {
149         if (!isSyncAvoidanceEnabled()) {
150             return Optional.absent();
151         } else if (isSyncNeedIt() && getLocalLspDbVersionValue() != null && !this.resynchronizing) {
152             this.lspDBVersion = getLocalLspDbVersionValue();
153             return Optional.of(this.lspDBVersion);
154         } else if (this.resynchronizing) {
155             return Optional.of(this.lspDBVersion);
156         }
157
158         this.lspDBVersion = this.lspDBVersion.add(BigInteger.ONE);
159         return Optional.of(this.lspDBVersion);
160     }
161
162     public boolean isSyncNeedIt() {
163         if (doesLspDbMatch() && !this.resynchronizing) {
164             return false;
165         }
166         return true;
167     }
168
169     public void setResynchronizingState(final Boolean resync) {
170         this.resynchronizing = resync;
171     }
172 }