Create RemoteDeviceServices.{Actions,Rpcs}
[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.DOMTransactionChain;
18 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
19 import org.opendaylight.mdsal.dom.spi.PingPongMergingDOMDataBroker;
20 import org.opendaylight.netconf.dom.api.tx.NetconfDOMDataBrokerFieldsExtension;
21 import org.opendaylight.netconf.dom.api.tx.NetconfDOMFieldsReadTransaction;
22 import org.opendaylight.netconf.dom.api.tx.NetconfDOMFieldsReadWriteTransaction;
23 import org.opendaylight.netconf.dom.api.tx.NetconfDOMFieldsTransactionChain;
24 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
25 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
26 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.FieldsAwareReadOnlyTx;
27 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.FieldsAwareReadWriteTx;
28 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.FieldsAwareTxChain;
29 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.ReadOnlyTx;
30 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.ReadWriteTx;
31 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.TxChain;
32 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.WriteCandidateRunningTx;
33 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.WriteCandidateTx;
34 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.WriteRunningTx;
35 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
36 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
37 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
38
39 public final class NetconfDeviceDataBroker implements PingPongMergingDOMDataBroker {
40
41     private final NetconfDOMDataBrokerFieldsExtension fieldsExtension = new NetconfDOMDataBrokerFieldsExtensionImpl();
42
43     private final RemoteDeviceId id;
44     private final NetconfBaseOps netconfOps;
45     private final boolean rollbackSupport;
46     private final boolean candidateSupported;
47     private final boolean runningWritable;
48
49     private boolean isLockAllowed = true;
50
51     public NetconfDeviceDataBroker(final RemoteDeviceId id, final MountPointContext mountContext, final Rpcs rpcs,
52             final NetconfSessionPreferences netconfSessionPreferences) {
53         this.id = id;
54         netconfOps = new NetconfBaseOps(rpcs, mountContext);
55         // get specific attributes from netconf preferences and get rid of it
56         // no need to keep the entire preferences object, its quite big with all the capability QNames
57         candidateSupported = netconfSessionPreferences.isCandidateSupported();
58         runningWritable = netconfSessionPreferences.isRunningWritable();
59         rollbackSupport = netconfSessionPreferences.isRollbackSupported();
60         Preconditions.checkArgument(candidateSupported || runningWritable,
61             "Device %s has advertised neither :writable-running nor :candidate capability."
62                     + "At least one of these should be advertised. Failed to establish a session.", id.getName());
63     }
64
65     @Override
66     public DOMDataTreeReadTransaction newReadOnlyTransaction() {
67         return new ReadOnlyTx(netconfOps, id);
68     }
69
70     @Override
71     public DOMDataTreeReadWriteTransaction newReadWriteTransaction() {
72         return new ReadWriteTx<>(newReadOnlyTransaction(), newWriteOnlyTransaction());
73     }
74
75     @Override
76     public DOMDataTreeWriteTransaction newWriteOnlyTransaction() {
77         if (candidateSupported) {
78             if (runningWritable) {
79                 return new WriteCandidateRunningTx(id, netconfOps, rollbackSupport, isLockAllowed);
80             } else {
81                 return new WriteCandidateTx(id, netconfOps, rollbackSupport, isLockAllowed);
82             }
83         } else {
84             return new WriteRunningTx(id, netconfOps, rollbackSupport, isLockAllowed);
85         }
86     }
87
88     @Override
89     public DOMTransactionChain createTransactionChain(final DOMTransactionChainListener listener) {
90         return new TxChain(this, listener);
91     }
92
93     @Override
94     public ClassToInstanceMap<DOMDataBrokerExtension> getExtensions() {
95         return ImmutableClassToInstanceMap.of(NetconfDOMDataBrokerFieldsExtension.class, fieldsExtension);
96     }
97
98     void setLockAllowed(final boolean isLockAllowedOrig) {
99         isLockAllowed = isLockAllowedOrig;
100     }
101
102     private final class NetconfDOMDataBrokerFieldsExtensionImpl implements NetconfDOMDataBrokerFieldsExtension {
103         @Override
104         public NetconfDOMFieldsReadTransaction newReadOnlyTransaction() {
105             return new FieldsAwareReadOnlyTx(netconfOps, id);
106         }
107
108         @Override
109         public NetconfDOMFieldsReadWriteTransaction newReadWriteTransaction() {
110             return new FieldsAwareReadWriteTx(newReadOnlyTransaction(), newWriteOnlyTransaction());
111         }
112
113         @Override
114         public NetconfDOMFieldsTransactionChain createTransactionChain(final DOMTransactionChainListener listener) {
115             return new FieldsAwareTxChain(NetconfDeviceDataBroker.this, listener, this);
116         }
117     }
118 }