Propagate MountPointContext to NetconfRpcStructureTransformer
[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.DOMDataBrokerExtension;
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
17 import org.opendaylight.mdsal.dom.api.DOMRpcService;
18 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
19 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
20 import org.opendaylight.mdsal.dom.spi.PingPongMergingDOMDataBroker;
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.rfc8528.data.api.MountPointContext;
31
32 public final class NetconfDeviceDataBroker implements PingPongMergingDOMDataBroker {
33
34     private final RemoteDeviceId id;
35     private final NetconfBaseOps netconfOps;
36     private final boolean rollbackSupport;
37     private final boolean candidateSupported;
38     private final boolean runningWritable;
39
40     private boolean isLockAllowed = true;
41
42     public NetconfDeviceDataBroker(final RemoteDeviceId id, final MountPointContext mountContext,
43                                    final DOMRpcService rpc, final NetconfSessionPreferences netconfSessionPreferences) {
44         this.id = id;
45         this.netconfOps = new NetconfBaseOps(rpc, mountContext);
46         // get specific attributes from netconf preferences and get rid of it
47         // no need to keep the entire preferences object, its quite big with all the capability QNames
48         candidateSupported = netconfSessionPreferences.isCandidateSupported();
49         runningWritable = netconfSessionPreferences.isRunningWritable();
50         rollbackSupport = netconfSessionPreferences.isRollbackSupported();
51         Preconditions.checkArgument(candidateSupported || runningWritable,
52             "Device %s has advertised neither :writable-running nor :candidate capability."
53                     + "At least one of these should be advertised. Failed to establish a session.", id.getName());
54     }
55
56     @Override
57     public DOMDataTreeReadTransaction newReadOnlyTransaction() {
58         return new ReadOnlyTx(netconfOps, id);
59     }
60
61     @Override
62     public DOMDataTreeReadWriteTransaction newReadWriteTransaction() {
63         return new ReadWriteTx(newReadOnlyTransaction(), newWriteOnlyTransaction());
64     }
65
66     @Override
67     public DOMDataTreeWriteTransaction newWriteOnlyTransaction() {
68         if (candidateSupported) {
69             if (runningWritable) {
70                 return new WriteCandidateRunningTx(id, netconfOps, rollbackSupport, isLockAllowed);
71             } else {
72                 return new WriteCandidateTx(id, netconfOps, rollbackSupport, isLockAllowed);
73             }
74         } else {
75             return new WriteRunningTx(id, netconfOps, rollbackSupport, isLockAllowed);
76         }
77     }
78
79     @Override
80     public DOMTransactionChain createTransactionChain(final DOMTransactionChainListener listener) {
81         return new TxChain(this, listener);
82     }
83
84     @Override
85     public ClassToInstanceMap<DOMDataBrokerExtension> getExtensions() {
86         return ImmutableClassToInstanceMap.of();
87     }
88
89     void setLockAllowed(final boolean isLockAllowedOrig) {
90         this.isLockAllowed = isLockAllowedOrig;
91     }
92
93 }