Merge "Call CreateSubscription close method on session end"
[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
9 package org.opendaylight.netconf.sal.connect.netconf.sal;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Collections;
13 import java.util.Map;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
23 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
24 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
25 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.ReadOnlyTx;
26 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.ReadWriteTx;
27 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.WriteCandidateRunningTx;
28 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.WriteCandidateTx;
29 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.WriteRunningTx;
30 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
31 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35
36 public final class NetconfDeviceDataBroker implements DOMDataBroker {
37     private final RemoteDeviceId id;
38     private final NetconfBaseOps netconfOps;
39
40     private final boolean rollbackSupport;
41     private boolean candidateSupported;
42     private boolean runningWritable;
43
44     public NetconfDeviceDataBroker(final RemoteDeviceId id, final SchemaContext schemaContext, final DOMRpcService rpc, final NetconfSessionPreferences netconfSessionPreferences) {
45         this.id = id;
46         this.netconfOps = new NetconfBaseOps(rpc, schemaContext);
47         // get specific attributes from netconf preferences and get rid of it
48         // no need to keep the entire preferences object, its quite big with all the capability QNames
49         candidateSupported = netconfSessionPreferences.isCandidateSupported();
50         runningWritable = netconfSessionPreferences.isRunningWritable();
51         rollbackSupport = netconfSessionPreferences.isRollbackSupported();
52         Preconditions.checkArgument(candidateSupported || runningWritable,
53             "Device %s has advertised neither :writable-running nor :candidate capability. At least one of these should be advertised. Failed to establish a session.", id.getName());
54     }
55
56     @Override
57     public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
58         return new ReadOnlyTx(netconfOps, id);
59     }
60
61     @Override
62     public DOMDataReadWriteTransaction newReadWriteTransaction() {
63         return new ReadWriteTx(newReadOnlyTransaction(), newWriteOnlyTransaction());
64     }
65
66     @Override
67     public DOMDataWriteTransaction newWriteOnlyTransaction() {
68         if(candidateSupported) {
69             if(runningWritable) {
70                 return new WriteCandidateRunningTx(id, netconfOps, rollbackSupport);
71             } else {
72                 return new WriteCandidateTx(id, netconfOps, rollbackSupport);
73             }
74         } else {
75             return new WriteRunningTx(id, netconfOps, rollbackSupport);
76         }
77     }
78
79     @Override
80     public ListenerRegistration<DOMDataChangeListener> registerDataChangeListener(final LogicalDatastoreType store, final YangInstanceIdentifier path, final DOMDataChangeListener listener, final DataChangeScope triggeringScope) {
81         throw new UnsupportedOperationException(id + ": Data change listeners not supported for netconf mount point");
82     }
83
84     @Override
85     public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
86         throw new UnsupportedOperationException(id + ": Transaction chains not supported for netconf mount point");
87     }
88
89     @Override
90     public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
91         return Collections.emptyMap();
92     }
93
94 }