Add new revision for pcep types model
[bgpcep.git] / pcep / topology / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / SyncOptimization.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.bgpcep.pcep.topology.provider;
10
11 import static java.util.Objects.requireNonNull;
12
13 import org.opendaylight.protocol.pcep.PCEPSession;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.Stateful1;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.Tlvs3;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.lsp.db.version.tlv.LspDbVersion;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Tlvs1;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.Tlvs;
19
20 final class SyncOptimization {
21
22     private final boolean dbVersionMatch;
23     private final boolean isSyncAvoidanceEnabled;
24     private final boolean isDeltaSyncEnabled;
25     private final boolean isDbVersionPresent;
26     private final boolean isTriggeredInitialSyncEnable;
27     private final boolean isTriggeredReSyncEnable;
28
29     SyncOptimization(final PCEPSession session) {
30         requireNonNull(session);
31         final Tlvs remote = session.getRemoteTlvs();
32         final Tlvs local = session.getLocalTlvs();
33         final LspDbVersion localLspDbVersion = getLspDbVersion(local);
34         final LspDbVersion remoteLspDbVersion = getLspDbVersion(remote);
35         this.dbVersionMatch = compareLspDbVersion(localLspDbVersion, remoteLspDbVersion);
36         this.isSyncAvoidanceEnabled = isSyncAvoidance(local) && isSyncAvoidance(remote);
37         this.isDeltaSyncEnabled = isDeltaSync(local) && isDeltaSync(remote);
38         this.isDbVersionPresent = localLspDbVersion != null || remoteLspDbVersion != null;
39         this.isTriggeredInitialSyncEnable = isTriggeredInitialSync(local) && isTriggeredInitialSync(remote)
40                 && (this.isDeltaSyncEnabled || this.isSyncAvoidanceEnabled);
41         this.isTriggeredReSyncEnable = isTriggeredReSync(local) && isTriggeredReSync(remote);
42     }
43
44     boolean doesLspDbMatch() {
45         return this.dbVersionMatch;
46     }
47
48     boolean isSyncAvoidanceEnabled() {
49         return this.isSyncAvoidanceEnabled;
50     }
51
52     boolean isDeltaSyncEnabled() {
53         return this.isDeltaSyncEnabled;
54     }
55
56     boolean isTriggeredInitSyncEnabled() {
57         return this.isTriggeredInitialSyncEnable;
58     }
59
60     boolean isTriggeredReSyncEnabled() {
61         return this.isTriggeredReSyncEnable;
62     }
63
64     boolean isDbVersionPresent() {
65         return this.isDbVersionPresent;
66     }
67
68     private static LspDbVersion getLspDbVersion(final Tlvs openTlvs) {
69         if (openTlvs != null) {
70             final Tlvs3 tlvs3 = openTlvs.augmentation(Tlvs3.class);
71             if (tlvs3 != null && tlvs3.getLspDbVersion() != null
72                     && tlvs3.getLspDbVersion().getLspDbVersionValue() != null) {
73                 return tlvs3.getLspDbVersion();
74             }
75         }
76         return null;
77     }
78
79     private static boolean compareLspDbVersion(final LspDbVersion local, final LspDbVersion remote) {
80         if (local != null && remote != null) {
81             return local.equals(remote);
82         }
83         return false;
84     }
85
86     private static Stateful1 getStateful1(final Tlvs openTlvs) {
87         if (openTlvs != null) {
88             final Tlvs1 tlvs1 = openTlvs.augmentation(Tlvs1.class);
89             if (tlvs1 != null && tlvs1.getStateful() != null) {
90                 return tlvs1.getStateful().augmentation(Stateful1.class);
91             }
92         }
93         return null;
94     }
95
96     private static boolean isSyncAvoidance(final Tlvs openTlvs) {
97         final Stateful1 stateful1 = getStateful1(openTlvs);
98         if (stateful1 != null && stateful1.isIncludeDbVersion() != null) {
99             return stateful1.isIncludeDbVersion();
100         }
101         return false;
102     }
103
104     private static boolean isDeltaSync(final Tlvs openTlvs) {
105         final Stateful1 stateful1 = getStateful1(openTlvs);
106         if (stateful1 != null && stateful1.isDeltaLspSyncCapability() != null) {
107             return stateful1.isDeltaLspSyncCapability();
108         }
109         return false;
110     }
111
112     private static boolean isTriggeredInitialSync(final Tlvs openTlvs) {
113         final Stateful1 stateful1 = getStateful1(openTlvs);
114         if (stateful1 != null && stateful1.isTriggeredInitialSync() != null) {
115             return stateful1.isTriggeredInitialSync();
116         }
117         return false;
118     }
119
120     private static boolean isTriggeredReSync(final Tlvs openTlvs) {
121         final Stateful1 stateful1 = getStateful1(openTlvs);
122         if (stateful1 != null && stateful1.isTriggeredResync() != null) {
123             return stateful1.isTriggeredResync();
124         }
125         return false;
126     }
127 }