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