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