Add support of new a NetconfDataTreeService in clustered 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 synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
122                 final DOMDataBroker broker, final NetconfDataTreeService netconfService, final DOMRpcService rpc,
123                 final NetconfDeviceNotificationService newNotificationService, final DOMActionService deviceAction) {
124             requireNonNull(mountService, "Closed");
125             checkState(topologyRegistration == null, "Already initialized");
126
127             final DOMMountPointService.DOMMountPointBuilder mountBuilder =
128                     mountService.createMountPoint(id.getTopologyPath());
129             mountBuilder.addInitialSchemaContext(initialCtx);
130
131             if (broker != null) {
132                 mountBuilder.addService(DOMDataBroker.class, broker);
133             }
134             mountBuilder.addService(DOMRpcService.class, rpc);
135             mountBuilder.addService(DOMNotificationService.class, newNotificationService);
136             if (deviceAction != null) {
137                 mountBuilder.addService(DOMActionService.class, deviceAction);
138             }
139             if (netconfService != null) {
140                 mountBuilder.addService(NetconfDataTreeService.class, netconfService);
141             }
142             this.notificationService = newNotificationService;
143
144             topologyRegistration = mountBuilder.register();
145             LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
146         }
147
148         @SuppressWarnings("checkstyle:IllegalCatch")
149         public synchronized void onTopologyDeviceDisconnected() {
150             if (topologyRegistration == null) {
151                 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
152                 return;
153             }
154
155             try {
156                 topologyRegistration.close();
157             } catch (final Exception e) {
158                 // Only log and ignore
159                 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
160             } finally {
161                 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
162                 topologyRegistration = null;
163             }
164         }
165
166         @Override
167         public synchronized void close() {
168             onTopologyDeviceDisconnected();
169         }
170
171         public synchronized void publish(final DOMNotification domNotification) {
172             checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
173                 .publishNotification(domNotification);
174         }
175     }
176
177 }