Create NetconfDataTreeService with base and additional operations for netconf
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceSalProvider.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 static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
13
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.Transaction;
17 import org.opendaylight.mdsal.binding.api.TransactionChain;
18 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
19 import org.opendaylight.mdsal.dom.api.DOMActionService;
20 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
21 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
22 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
23 import org.opendaylight.mdsal.dom.api.DOMNotification;
24 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
25 import org.opendaylight.mdsal.dom.api.DOMRpcService;
26 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
27 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
28 import org.opendaylight.yangtools.concepts.ObjectRegistration;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class NetconfDeviceSalProvider implements AutoCloseable {
34     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
35
36     private final RemoteDeviceId id;
37     private final MountInstance mountInstance;
38     private final DataBroker dataBroker;
39
40     private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
41
42     private TransactionChain txChain;
43
44     private final TransactionChainListener transactionChainListener =  new TransactionChainListener() {
45         @Override
46         public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
47                 final Throwable cause) {
48             LOG.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
49             chain.close();
50             resetTransactionChainForAdapaters();
51             throw new IllegalStateException(id + "  TransactionChain(" + chain + ") not committed correctly", cause);
52         }
53
54         @Override
55         public void onTransactionChainSuccessful(final TransactionChain chain) {
56             LOG.trace("{}: TransactionChain({}) SUCCESSFUL", id, chain);
57         }
58     };
59
60     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
61         this(deviceId, mountService, null);
62     }
63
64     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
65             final DataBroker dataBroker) {
66         this.id = deviceId;
67         mountInstance = new MountInstance(mountService, id);
68         this.dataBroker = dataBroker;
69         if (dataBroker != null) {
70             txChain = requireNonNull(dataBroker).createTransactionChain(transactionChainListener);
71             topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
72         }
73     }
74
75     public MountInstance getMountInstance() {
76         checkState(mountInstance != null, "%s: Mount instance was not initialized by sal. Cannot get mount instance",
77                 id);
78         return mountInstance;
79     }
80
81     public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
82         final NetconfDeviceTopologyAdapter local = topologyDatastoreAdapter;
83         checkState(local != null,
84                 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id, this);
85         return local;
86     }
87
88     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
89             justification = "https://github.com/spotbugs/spotbugs/issues/811")
90     private void resetTransactionChainForAdapaters() {
91         txChain = requireNonNull(dataBroker).createTransactionChain(transactionChainListener);
92         topologyDatastoreAdapter.setTxChain(txChain);
93         LOG.trace("{}: Resetting TransactionChain {}", id, txChain);
94     }
95
96     @Override
97     public void close() {
98         mountInstance.close();
99         if (topologyDatastoreAdapter != null) {
100             topologyDatastoreAdapter.close();
101         }
102         topologyDatastoreAdapter = null;
103         if (txChain != null) {
104             txChain.close();
105         }
106     }
107
108     public static class MountInstance implements AutoCloseable {
109
110         private final DOMMountPointService mountService;
111         private final RemoteDeviceId id;
112
113         private NetconfDeviceNotificationService notificationService;
114         private ObjectRegistration<DOMMountPoint> topologyRegistration;
115
116         MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
117             this.mountService = requireNonNull(mountService);
118             this.id = requireNonNull(id);
119         }
120
121         public void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
122                 final DOMDataBroker broker, final DOMRpcService rpc,
123                 final NetconfDeviceNotificationService newNotificationService) {
124             onTopologyDeviceConnected(initialCtx, broker, null, rpc, newNotificationService, null);
125         }
126
127         public synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
128                 final DOMDataBroker broker, final NetconfDataTreeService netconfService, final DOMRpcService rpc,
129                 final NetconfDeviceNotificationService newNotificationService, final DOMActionService deviceAction) {
130             requireNonNull(mountService, "Closed");
131             checkState(topologyRegistration == null, "Already initialized");
132
133             final DOMMountPointService.DOMMountPointBuilder mountBuilder =
134                     mountService.createMountPoint(id.getTopologyPath());
135             mountBuilder.addInitialSchemaContext(initialCtx);
136
137             if (broker != null) {
138                 mountBuilder.addService(DOMDataBroker.class, broker);
139             }
140             mountBuilder.addService(DOMRpcService.class, rpc);
141             mountBuilder.addService(DOMNotificationService.class, newNotificationService);
142             if (deviceAction != null) {
143                 mountBuilder.addService(DOMActionService.class, deviceAction);
144             }
145             if (netconfService != null) {
146                 mountBuilder.addService(NetconfDataTreeService.class, netconfService);
147             }
148             this.notificationService = newNotificationService;
149
150             topologyRegistration = mountBuilder.register();
151             LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
152         }
153
154         @SuppressWarnings("checkstyle:IllegalCatch")
155         public synchronized void onTopologyDeviceDisconnected() {
156             if (topologyRegistration == null) {
157                 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
158                 return;
159             }
160
161             try {
162                 topologyRegistration.close();
163             } catch (final Exception e) {
164                 // Only log and ignore
165                 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
166             } finally {
167                 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
168                 topologyRegistration = null;
169             }
170         }
171
172         @Override
173         public synchronized void close() {
174             onTopologyDeviceDisconnected();
175         }
176
177         public synchronized void publish(final DOMNotification domNotification) {
178             checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
179                 .publishNotification(domNotification);
180         }
181     }
182
183 }