Netconf stack by default locks the data store before issuing
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceDataBroker.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.netconf.sal.connect.netconf.sal;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ClassToInstanceMap;
12 import com.google.common.collect.ImmutableClassToInstanceMap;
13 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
14 import org.opendaylight.mdsal.dom.api.DOMDataBrokerExtension;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
18 import org.opendaylight.mdsal.dom.api.DOMRpcService;
19 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
20 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
21 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
22 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.ReadOnlyTx;
23 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.ReadWriteTx;
24 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.TxChain;
25 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.WriteCandidateRunningTx;
26 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.WriteCandidateTx;
27 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.WriteRunningTx;
28 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
29 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31
32 public final class NetconfDeviceDataBroker implements DOMDataBroker {
33     private final RemoteDeviceId id;
34     private final NetconfBaseOps netconfOps;
35     private final boolean rollbackSupport;
36     private final boolean candidateSupported;
37     private final boolean runningWritable;
38
39     private boolean isLockAllowed = true;
40
41     public NetconfDeviceDataBroker(final RemoteDeviceId id, final SchemaContext schemaContext,
42                                    final DOMRpcService rpc, final NetconfSessionPreferences netconfSessionPreferences) {
43         this.id = id;
44         this.netconfOps = new NetconfBaseOps(rpc, schemaContext);
45         // get specific attributes from netconf preferences and get rid of it
46         // no need to keep the entire preferences object, its quite big with all the capability QNames
47         candidateSupported = netconfSessionPreferences.isCandidateSupported();
48         runningWritable = netconfSessionPreferences.isRunningWritable();
49         rollbackSupport = netconfSessionPreferences.isRollbackSupported();
50         Preconditions.checkArgument(candidateSupported || runningWritable,
51             "Device %s has advertised neither :writable-running nor :candidate capability."
52                     + "At least one of these should be advertised. Failed to establish a session.", id.getName());
53     }
54
55     @Override
56     public DOMDataTreeReadTransaction newReadOnlyTransaction() {
57         return new ReadOnlyTx(netconfOps, id);
58     }
59
60     @Override
61     public DOMDataTreeReadWriteTransaction newReadWriteTransaction() {
62         return new ReadWriteTx(newReadOnlyTransaction(), newWriteOnlyTransaction());
63     }
64
65     @Override
66     public DOMDataTreeWriteTransaction newWriteOnlyTransaction() {
67         if (candidateSupported) {
68             if (runningWritable) {
69                 return new WriteCandidateRunningTx(id, netconfOps, rollbackSupport, isLockAllowed);
70             } else {
71                 return new WriteCandidateTx(id, netconfOps, rollbackSupport, isLockAllowed);
72             }
73         } else {
74             return new WriteRunningTx(id, netconfOps, rollbackSupport, isLockAllowed);
75         }
76     }
77
78     @Override
79     public DOMTransactionChain createTransactionChain(final DOMTransactionChainListener listener) {
80         return new TxChain(this, listener);
81     }
82
83     @Override
84     public ClassToInstanceMap<DOMDataBrokerExtension> getExtensions() {
85         return ImmutableClassToInstanceMap.of();
86     }
87
88     void setLockAllowed(boolean isLockAllowedOrig) {
89         this.isLockAllowed = isLockAllowedOrig;
90     }
91
92 }