bd22cf43705675417fd63bb4d2d67b696f9b7fb9
[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.rev150714.Stateful1;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs3;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.lsp.db.version.tlv.LspDbVersion;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.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     public SyncOptimization(final PCEPSession session) {
30         requireNonNull(session);
31         final Tlvs remote = session.getRemoteTlvs();
32         final Tlvs local = session.localSessionCharacteristics();
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     public boolean doesLspDbMatch() {
45         return this.dbVersionMatch;
46     }
47
48     public boolean isSyncAvoidanceEnabled() {
49         return this.isSyncAvoidanceEnabled;
50     }
51
52     public boolean isDeltaSyncEnabled() {
53         return this.isDeltaSyncEnabled;
54     }
55
56     public boolean isTriggeredInitSyncEnabled() {
57         return this.isTriggeredInitialSyncEnable;
58     }
59     public boolean isTriggeredReSyncEnabled() {
60         return this.isTriggeredReSyncEnable;
61     }
62
63     public boolean isDbVersionPresent() {
64         return this.isDbVersionPresent;
65     }
66
67     private static LspDbVersion getLspDbVersion(final Tlvs openTlvs) {
68         if (openTlvs != null) {
69             final Tlvs3 tlvs3 = openTlvs.getAugmentation(Tlvs3.class);
70             if (tlvs3 != null && tlvs3.getLspDbVersion() != null
71                     && tlvs3.getLspDbVersion().getLspDbVersionValue() != null) {
72                 return tlvs3.getLspDbVersion();
73             }
74         }
75         return null;
76     }
77
78     private static boolean compareLspDbVersion(final LspDbVersion local, final LspDbVersion remote) {
79         if (local != null && remote != null) {
80             return local.equals(remote);
81         }
82         return false;
83     }
84
85     private static Stateful1 getStateful1(final Tlvs openTlvs) {
86         if (openTlvs != null) {
87             final Tlvs1 tlvs1 = openTlvs.getAugmentation(Tlvs1.class);
88             if (tlvs1 != null && tlvs1.getStateful() != null) {
89                 return tlvs1.getStateful().getAugmentation(Stateful1.class);
90             }
91         }
92         return null;
93     }
94
95     private static boolean isSyncAvoidance(final Tlvs openTlvs) {
96         final Stateful1 stateful1 = getStateful1(openTlvs);
97         if (stateful1 != null && stateful1.isIncludeDbVersion() != null) {
98             return stateful1.isIncludeDbVersion();
99         }
100         return false;
101     }
102
103     private static boolean isDeltaSync(final Tlvs openTlvs) {
104         final Stateful1 stateful1 = getStateful1(openTlvs);
105         if (stateful1 != null && stateful1.isDeltaLspSyncCapability() != null) {
106             return stateful1.isDeltaLspSyncCapability();
107         }
108         return false;
109     }
110
111     private static boolean isTriggeredInitialSync(final Tlvs openTlvs) {
112         final Stateful1 stateful1 = getStateful1(openTlvs);
113         if (stateful1 != null && stateful1.isTriggeredInitialSync() != null) {
114             return stateful1.isTriggeredInitialSync();
115         }
116         return false;
117     }
118
119     private static boolean isTriggeredReSync(final Tlvs openTlvs) {
120         final Stateful1 stateful1 = getStateful1(openTlvs);
121         if (stateful1 != null && stateful1.isTriggeredResync() != null) {
122             return stateful1.isTriggeredResync();
123         }
124         return false;
125     }
126 }