Update Statefuful & Initiated yang models
[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     private Uint64 lspDBVersion = Uint64.ONE;
31     private Boolean resynchronizing = Boolean.FALSE;
32
33     PCCSyncOptimization(final @NonNull PCCSession session) {
34         requireNonNull(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(this.localLspDbVersion, this.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 this.dbVersionMatch;
49     }
50
51     public boolean isSyncAvoidanceEnabled() {
52         return this.isSyncAvoidanceEnabled;
53     }
54
55     public boolean isDeltaSyncEnabled() {
56         return this.isDeltaSyncEnabled;
57     }
58
59     public boolean isTriggeredInitSyncEnabled() {
60         return this.isTriggeredInitialSynEnable;
61     }
62
63     public boolean isTriggeredReSyncEnabled() {
64         return this.isTriggeredReSyncEnable;
65     }
66
67     public Uint64 getLocalLspDbVersionValue() {
68         if (this.localLspDbVersion == null) {
69             return null;
70         }
71         return this.localLspDbVersion.getLspDbVersionValue();
72     }
73
74     public Uint64 getRemoteLspDbVersionValue() {
75         if (this.remoteLspDbVersion == null) {
76             return Uint64.ONE;
77         }
78         return this.remoteLspDbVersion.getLspDbVersionValue();
79     }
80
81     private static LspDbVersion getLspDbVersion(final Tlvs openTlvs) {
82         if (openTlvs != null) {
83             final Tlvs3 tlvs3 = openTlvs.augmentation(Tlvs3.class);
84             if (tlvs3 != null && tlvs3.getLspDbVersion() != null
85                 && tlvs3.getLspDbVersion().getLspDbVersionValue() != null) {
86                 return tlvs3.getLspDbVersion();
87             }
88         }
89         return null;
90     }
91
92     private static boolean compareLspDbVersion(final LspDbVersion local, final LspDbVersion remote) {
93         if (local != null && remote != null) {
94             return local.equals(remote);
95         }
96         return false;
97     }
98
99     private static Stateful1 getStateful1(final Tlvs openTlvs) {
100         if (openTlvs != null) {
101             final Tlvs1 tlvs1 = openTlvs.augmentation(Tlvs1.class);
102             if (tlvs1 != null && tlvs1.getStateful() != null) {
103                 return tlvs1.getStateful().augmentation(Stateful1.class);
104             }
105         }
106         return null;
107     }
108
109     private static boolean isSyncAvoidance(final Tlvs openTlvs) {
110         final Stateful1 stateful1 = getStateful1(openTlvs);
111         if (stateful1 != null && stateful1.isIncludeDbVersion() != null) {
112             return stateful1.isIncludeDbVersion();
113         }
114         return false;
115     }
116
117     private static boolean isDeltaSync(final Tlvs openTlvs) {
118         final Stateful1 stateful1 = getStateful1(openTlvs);
119         if (stateful1 != null && stateful1.isDeltaLspSyncCapability() != null) {
120             return stateful1.isDeltaLspSyncCapability();
121         }
122         return false;
123     }
124
125     private static boolean isTriggeredInitialSync(final Tlvs openTlvs) {
126         final Stateful1 stateful1 = getStateful1(openTlvs);
127         if (stateful1 != null && stateful1.isTriggeredInitialSync() != null) {
128             return stateful1.isTriggeredInitialSync();
129         }
130         return false;
131     }
132
133     private static boolean isTriggeredReSync(final Tlvs openTlvs) {
134         final Stateful1 stateful1 = getStateful1(openTlvs);
135         if (stateful1 != null && stateful1.isTriggeredResync() != null) {
136             return stateful1.isTriggeredResync();
137         }
138         return false;
139     }
140
141     public Optional<Uint64> incrementLspDBVersion() {
142         if (!isSyncAvoidanceEnabled()) {
143             return Optional.empty();
144         } else if (isSyncNeedIt() && getLocalLspDbVersionValue() != null && !this.resynchronizing) {
145             this.lspDBVersion = getLocalLspDbVersionValue();
146             return Optional.of(this.lspDBVersion);
147         } else if (this.resynchronizing) {
148             return Optional.of(this.lspDBVersion);
149         }
150
151         this.lspDBVersion = Uint64.fromLongBits(lspDBVersion.longValue() + 1);
152         return Optional.of(this.lspDBVersion);
153     }
154
155     public boolean isSyncNeedIt() {
156         return !doesLspDbMatch() || this.resynchronizing;
157     }
158
159     public void setResynchronizingState(final Boolean resync) {
160         this.resynchronizing = resync;
161     }
162 }