Bump upstreams
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / spi / 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.client.mdsal.spi;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import java.util.List;
13 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
16 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
17 import org.opendaylight.mdsal.dom.spi.PingPongMergingDOMDataBroker;
18 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
19 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
20 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
21 import org.opendaylight.netconf.client.mdsal.impl.NetconfBaseOps;
22 import org.opendaylight.netconf.dom.api.tx.NetconfDOMDataBrokerFieldsExtension;
23 import org.opendaylight.netconf.dom.api.tx.NetconfDOMFieldsReadTransaction;
24 import org.opendaylight.netconf.dom.api.tx.NetconfDOMFieldsReadWriteTransaction;
25 import org.opendaylight.netconf.dom.api.tx.NetconfDOMFieldsTransactionChain;
26 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
27
28 public final class NetconfDeviceDataBroker implements PingPongMergingDOMDataBroker {
29     private final NetconfDOMDataBrokerFieldsExtension fieldsExtension = new NetconfDOMDataBrokerFieldsExtensionImpl();
30     private final RemoteDeviceId id;
31     private final NetconfBaseOps netconfOps;
32     private final boolean rollbackSupport;
33     private final boolean candidateSupported;
34     private final boolean runningWritable;
35     private final boolean lockDatastore;
36
37     public NetconfDeviceDataBroker(final RemoteDeviceId id, final MountPointContext mountContext, final Rpcs rpcs,
38             final NetconfSessionPreferences netconfSessionPreferences, final boolean lockDatastore) {
39         this.id = id;
40         netconfOps = new NetconfBaseOps(rpcs, mountContext);
41         // get specific attributes from netconf preferences and get rid of it
42         // no need to keep the entire preferences object, its quite big with all the capability QNames
43         candidateSupported = netconfSessionPreferences.isCandidateSupported();
44         runningWritable = netconfSessionPreferences.isRunningWritable();
45         rollbackSupport = netconfSessionPreferences.isRollbackSupported();
46         checkArgument(candidateSupported || runningWritable,
47             "Device %s has advertised neither :writable-running nor :candidate capability. At least one of these "
48                 + "should be advertised. Failed to establish a session.", id.name());
49         this.lockDatastore = lockDatastore;
50     }
51
52     @Override
53     public DOMDataTreeReadTransaction newReadOnlyTransaction() {
54         return new ReadOnlyTx(netconfOps, id);
55     }
56
57     @Override
58     public DOMDataTreeReadWriteTransaction newReadWriteTransaction() {
59         return new ReadWriteTx<>(newReadOnlyTransaction(), newWriteOnlyTransaction());
60     }
61
62     @Override
63     public DOMDataTreeWriteTransaction newWriteOnlyTransaction() {
64         final AbstractWriteTx ret;
65         if (candidateSupported) {
66             ret = runningWritable ? new WriteCandidateRunningTx(id, netconfOps, rollbackSupport, lockDatastore)
67                 : new WriteCandidateTx(id, netconfOps, rollbackSupport, lockDatastore);
68         } else {
69             ret = new WriteRunningTx(id, netconfOps, rollbackSupport, lockDatastore);
70         }
71         ret.init();
72         return ret;
73     }
74
75     @Override
76     public DOMTransactionChain createTransactionChain() {
77         return new TxChain(this);
78     }
79
80     @Override
81     public List<Extension> supportedExtensions() {
82         return List.of(fieldsExtension);
83     }
84
85     private final class NetconfDOMDataBrokerFieldsExtensionImpl implements NetconfDOMDataBrokerFieldsExtension {
86         @Override
87         public NetconfDOMFieldsReadTransaction newReadOnlyTransaction() {
88             return new FieldsAwareReadOnlyTx(netconfOps, id);
89         }
90
91         @Override
92         public NetconfDOMFieldsReadWriteTransaction newReadWriteTransaction() {
93             return new FieldsAwareReadWriteTx(newReadOnlyTransaction(), newWriteOnlyTransaction());
94         }
95
96         @Override
97         public NetconfDOMFieldsTransactionChain createTransactionChain() {
98             return new FieldsAwareTxChain(NetconfDeviceDataBroker.this, this);
99         }
100     }
101 }