Add new revision for pcep types model
[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 static java.util.Objects.requireNonNull;
12
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;
22
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;
33
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);
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 BigInteger getLocalLspDbVersionValue() {
69         if (this.localLspDbVersion == null) {
70             return null;
71         }
72         return this.localLspDbVersion.getLspDbVersionValue();
73     }
74
75     public BigInteger getRemoteLspDbVersionValue() {
76         if (this.remoteLspDbVersion == null) {
77             return BigInteger.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         if (stateful1 != null && stateful1.isIncludeDbVersion() != null) {
113             return stateful1.isIncludeDbVersion();
114         }
115         return false;
116     }
117
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();
122         }
123         return false;
124     }
125
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();
130         }
131         return false;
132     }
133
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();
138         }
139         return false;
140     }
141
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);
150         }
151
152         this.lspDBVersion = this.lspDBVersion.add(BigInteger.ONE);
153         return Optional.of(this.lspDBVersion);
154     }
155
156     public boolean isSyncNeedIt() {
157         if (doesLspDbMatch() && !this.resynchronizing) {
158             return false;
159         }
160         return true;
161     }
162
163     public void setResynchronizingState(final Boolean resync) {
164         this.resynchronizing = resync;
165     }
166 }