BUG-139: State Synchronization Avoidance Procedure
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / PCEPStatefulPeerProposal.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.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.PathComputationClient1;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Stateful1;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs3;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs3Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.lsp.db.version.tlv.LspDbVersion;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClient;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class PCEPStatefulPeerProposal {
36
37     private static final Logger LOG = LoggerFactory.getLogger(PCEPStatefulPeerProposal.class);
38
39     private final DataBroker dataBroker;
40     private final InstanceIdentifier<Topology> topologyId;
41
42     private PCEPStatefulPeerProposal(final DataBroker dataBroker, final InstanceIdentifier<Topology> topologyId) {
43         this.dataBroker = Preconditions.checkNotNull(dataBroker);
44         this.topologyId = Preconditions.checkNotNull(topologyId);
45     }
46
47     public static PCEPStatefulPeerProposal createStatefulPeerProposal(final DataBroker dataBroker,
48             final InstanceIdentifier<Topology> topologyId) {
49         return new PCEPStatefulPeerProposal(dataBroker, topologyId);
50     }
51
52     void setPeerProposal(final NodeId nodeId, final TlvsBuilder openTlvsBuilder) {
53         if (isSynOptimizationEnabled(openTlvsBuilder)) {
54             final ListenableFuture<Optional<LspDbVersion>> future = this.dataBroker.newReadOnlyTransaction().read(
55                     LogicalDatastoreType.OPERATIONAL,
56                     topologyId.child(Node.class, new NodeKey(nodeId)).augmentation(Node1.class)
57                             .child(PathComputationClient.class).augmentation(PathComputationClient1.class)
58                             .child(LspDbVersion.class));
59             Futures.addCallback(future, new FutureCallback<Optional<LspDbVersion>>() {
60                 @Override
61                 public void onSuccess(final Optional<LspDbVersion> result) {
62                     if (result.isPresent()) {
63                         openTlvsBuilder.addAugmentation(Tlvs3.class, new Tlvs3Builder().setLspDbVersion(result.get())
64                                 .build());
65                     }
66                 }
67
68                 @Override
69                 public void onFailure(final Throwable t) {
70                     LOG.warn("Failed to read toplogy {}.", InstanceIdentifier.keyOf(topologyId), t);
71                 }
72             });
73         }
74     }
75
76     private static boolean isSynOptimizationEnabled(final TlvsBuilder openTlvsBuilder) {
77         final Tlvs1 statefulTlv = openTlvsBuilder.getAugmentation(Tlvs1.class);
78         if (statefulTlv != null && statefulTlv.getStateful() != null) {
79             return statefulTlv.getStateful().getAugmentation(Stateful1.class) != null;
80         }
81         return false;
82     }
83
84 }