Bug 4114 - netconf connector replace causes transaction chain failure
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.sal.connect.netconf.sal;
9
10 import com.google.common.base.Preconditions;
11
12 import java.util.Collection;
13 import java.util.Collections;
14
15 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
23 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
24 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
28 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
29 import org.opendaylight.controller.sal.core.api.Broker;
30 import org.opendaylight.controller.sal.core.api.Provider;
31 import org.opendaylight.yangtools.concepts.ObjectRegistration;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 final class NetconfDeviceSalProvider implements AutoCloseable, Provider, BindingAwareProvider {
37
38     private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
39
40     private final RemoteDeviceId id;
41     private volatile NetconfDeviceDatastoreAdapter datastoreAdapter;
42     private MountInstance mountInstance;
43
44     private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
45
46     private DataBroker dataBroker;
47     private BindingTransactionChain txChain;
48
49     private final TransactionChainListener transactionChainListener =  new TransactionChainListener() {
50         @Override
51         public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
52             logger.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
53             chain.close();
54             resetTransactionChainForAdapaters();
55             throw new IllegalStateException(id + "  TransactionChain(" + chain + ") not committed correctly", cause);
56         }
57
58         @Override
59         public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
60             logger.trace("{}: TransactionChain({}) {} SUCCESSFUL", id, chain);
61         }
62     };
63
64     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId) {
65         this.id = deviceId;
66     }
67
68     public MountInstance getMountInstance() {
69         Preconditions.checkState(mountInstance != null,
70                 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
71         return mountInstance;
72     }
73
74     public NetconfDeviceDatastoreAdapter getDatastoreAdapter() {
75         Preconditions.checkState(datastoreAdapter != null,
76                 "%s: Sal provider %s was not initialized by sal. Cannot get datastore adapter", id);
77         return datastoreAdapter;
78     }
79
80     public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
81         Preconditions.checkState(topologyDatastoreAdapter != null,
82                 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id);
83         return topologyDatastoreAdapter;
84     }
85
86     @Override
87     public void onSessionInitiated(final Broker.ProviderSession session) {
88         logger.debug("{}: (BI)Session with sal established {}", id, session);
89
90         final DOMMountPointService mountService = session.getService(DOMMountPointService.class);
91         if (mountService != null) {
92             mountInstance = new MountInstance(mountService, id);
93         }
94     }
95
96     @Override
97     public Collection<Provider.ProviderFunctionality> getProviderFunctionality() {
98         return Collections.emptySet();
99     }
100
101     @Override
102     public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
103         logger.debug("{}: Session with sal established {}", id, session);
104
105         this.dataBroker = session.getSALService(DataBroker.class);
106         txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
107
108         datastoreAdapter = new NetconfDeviceDatastoreAdapter(id, txChain);
109         topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
110     }
111
112     private void resetTransactionChainForAdapaters() {
113         txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
114
115         datastoreAdapter.setTxChain(txChain);
116         topologyDatastoreAdapter.setTxChain(txChain);
117
118         logger.trace("{}: Resetting TransactionChain {}", id, txChain);
119
120     }
121
122     public void close() throws Exception {
123         mountInstance.close();
124         datastoreAdapter.close();
125         datastoreAdapter = null;
126         topologyDatastoreAdapter.close();
127         topologyDatastoreAdapter = null;
128         txChain.close();
129     }
130
131     static final class MountInstance implements AutoCloseable {
132
133         private DOMMountPointService mountService;
134         private final RemoteDeviceId id;
135         private ObjectRegistration<DOMMountPoint> registration;
136         private NetconfDeviceNotificationService notificationService;
137
138         private ObjectRegistration<DOMMountPoint> topologyRegistration;
139
140         MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
141             this.mountService = Preconditions.checkNotNull(mountService);
142             this.id = Preconditions.checkNotNull(id);
143         }
144
145         @Deprecated
146         synchronized void onDeviceConnected(final SchemaContext initialCtx,
147                 final DOMDataBroker broker, final DOMRpcService rpc,
148                 final NetconfDeviceNotificationService notificationService) {
149
150             Preconditions.checkNotNull(mountService, "Closed");
151             Preconditions.checkState(registration == null, "Already initialized");
152
153             final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getPath());
154             mountBuilder.addInitialSchemaContext(initialCtx);
155
156             mountBuilder.addService(DOMDataBroker.class, broker);
157             mountBuilder.addService(DOMRpcService.class, rpc);
158             mountBuilder.addService(DOMNotificationService.class, notificationService);
159             this.notificationService = notificationService;
160
161             registration = mountBuilder.register();
162             logger.debug("{}: Mountpoint exposed into MD-SAL {}", id, registration);
163         }
164
165         @Deprecated
166         synchronized void onDeviceDisconnected() {
167             if(registration == null) {
168                 logger.trace("{}: Not removing mountpoint from MD-SAL, mountpoint was not registered yet", id);
169                 return;
170             }
171
172             try {
173                 registration.close();
174             } catch (final Exception e) {
175                 // Only log and ignore
176                 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getPath(), e);
177             } finally {
178                 logger.debug("{}: Mountpoint removed from MD-SAL {}", id, registration);
179                 registration = null;
180             }
181         }
182
183         synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
184                                                     final DOMDataBroker broker, final DOMRpcService rpc,
185                                                     final NetconfDeviceNotificationService notificationService) {
186
187             Preconditions.checkNotNull(mountService, "Closed");
188             Preconditions.checkState(topologyRegistration == null, "Already initialized");
189
190             final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getTopologyPath());
191             mountBuilder.addInitialSchemaContext(initialCtx);
192
193             mountBuilder.addService(DOMDataBroker.class, broker);
194             mountBuilder.addService(DOMRpcService.class, rpc);
195             mountBuilder.addService(DOMNotificationService.class, notificationService);
196
197             topologyRegistration = mountBuilder.register();
198             logger.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, registration);
199
200         }
201
202         synchronized void onTopologyDeviceDisconnected() {
203             if(topologyRegistration == null) {
204                 logger.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
205                 return;
206             }
207
208             try {
209                 topologyRegistration.close();
210             } catch (final Exception e) {
211                 // Only log and ignore
212                 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
213             } finally {
214                 logger.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, registration);
215                 topologyRegistration = null;
216             }
217         }
218
219         @Override
220         synchronized public void close() throws Exception {
221             onDeviceDisconnected();
222             onTopologyDeviceDisconnected();
223             mountService = null;
224         }
225
226         public synchronized void publish(final DOMNotification domNotification) {
227             Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}", domNotification);
228             notificationService.publishNotification(domNotification);
229         }
230     }
231
232 }