2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.bgpcep.pcep.topology.provider;
11 import static java.util.Objects.requireNonNull;
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;
20 final class SyncOptimization {
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;
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);
44 public boolean doesLspDbMatch() {
45 return this.dbVersionMatch;
48 public boolean isSyncAvoidanceEnabled() {
49 return this.isSyncAvoidanceEnabled;
52 public boolean isDeltaSyncEnabled() {
53 return this.isDeltaSyncEnabled;
56 public boolean isTriggeredInitSyncEnabled() {
57 return this.isTriggeredInitialSyncEnable;
59 public boolean isTriggeredReSyncEnabled() {
60 return this.isTriggeredReSyncEnable;
63 public boolean isDbVersionPresent() {
64 return this.isDbVersionPresent;
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();
78 private static boolean compareLspDbVersion(final LspDbVersion local, final LspDbVersion remote) {
79 if (local != null && remote != null) {
80 return local.equals(remote);
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);
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();
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();
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();
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();