2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
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;
39 public final class NetconfDeviceDataBroker implements PingPongMergingDOMDataBroker {
41 private final NetconfDOMDataBrokerFieldsExtension fieldsExtension = new NetconfDOMDataBrokerFieldsExtensionImpl();
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;
49 private boolean isLockAllowed = true;
51 public NetconfDeviceDataBroker(final RemoteDeviceId id, final MountPointContext mountContext, final Rpcs rpcs,
52 final NetconfSessionPreferences netconfSessionPreferences) {
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());
66 public DOMDataTreeReadTransaction newReadOnlyTransaction() {
67 return new ReadOnlyTx(netconfOps, id);
71 public DOMDataTreeReadWriteTransaction newReadWriteTransaction() {
72 return new ReadWriteTx<>(newReadOnlyTransaction(), newWriteOnlyTransaction());
76 public DOMDataTreeWriteTransaction newWriteOnlyTransaction() {
77 if (candidateSupported) {
78 if (runningWritable) {
79 return new WriteCandidateRunningTx(id, netconfOps, rollbackSupport, isLockAllowed);
81 return new WriteCandidateTx(id, netconfOps, rollbackSupport, isLockAllowed);
84 return new WriteRunningTx(id, netconfOps, rollbackSupport, isLockAllowed);
89 public DOMTransactionChain createTransactionChain(final DOMTransactionChainListener listener) {
90 return new TxChain(this, listener);
94 public ClassToInstanceMap<DOMDataBrokerExtension> getExtensions() {
95 return ImmutableClassToInstanceMap.of(NetconfDOMDataBrokerFieldsExtension.class, fieldsExtension);
98 void setLockAllowed(final boolean isLockAllowedOrig) {
99 isLockAllowed = isLockAllowedOrig;
102 private final class NetconfDOMDataBrokerFieldsExtensionImpl implements NetconfDOMDataBrokerFieldsExtension {
104 public NetconfDOMFieldsReadTransaction newReadOnlyTransaction() {
105 return new FieldsAwareReadOnlyTx(netconfOps, id);
109 public NetconfDOMFieldsReadWriteTransaction newReadWriteTransaction() {
110 return new FieldsAwareReadWriteTx(newReadOnlyTransaction(), newWriteOnlyTransaction());
114 public NetconfDOMFieldsTransactionChain createTransactionChain(final DOMTransactionChainListener listener) {
115 return new FieldsAwareTxChain(NetconfDeviceDataBroker.this, listener, this);