BUG-2739 Fixed leftover data when removing NETCONF device from topology
[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 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.concurrent.ExecutorService;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
17 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
20 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
21 import org.opendaylight.controller.sal.core.api.Broker;
22 import org.opendaylight.controller.sal.core.api.Provider;
23 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
24 import org.opendaylight.controller.sal.core.api.notify.NotificationPublishService;
25 import org.opendaylight.yangtools.concepts.ObjectRegistration;
26 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 final class NetconfDeviceSalProvider implements AutoCloseable, Provider, BindingAwareProvider {
32
33     private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
34
35     private final RemoteDeviceId id;
36     private final ExecutorService executor;
37     private volatile NetconfDeviceDatastoreAdapter datastoreAdapter;
38     private MountInstance mountInstance;
39
40     private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
41
42     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final ExecutorService executor) {
43         this.id = deviceId;
44         this.executor = executor;
45     }
46
47     public MountInstance getMountInstance() {
48         Preconditions.checkState(mountInstance != null,
49                 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
50         return mountInstance;
51     }
52
53     public NetconfDeviceDatastoreAdapter getDatastoreAdapter() {
54         Preconditions.checkState(datastoreAdapter != null,
55                 "%s: Sal provider %s was not initialized by sal. Cannot get datastore adapter", id);
56         return datastoreAdapter;
57     }
58
59     public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
60         Preconditions.checkState(topologyDatastoreAdapter != null,
61                 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id);
62         return topologyDatastoreAdapter;
63     }
64
65     @Override
66     public void onSessionInitiated(final Broker.ProviderSession session) {
67         logger.debug("{}: (BI)Session with sal established {}", id, session);
68
69         final DOMMountPointService mountService = session.getService(DOMMountPointService.class);
70         if (mountService != null) {
71             mountInstance = new MountInstance(mountService, id);
72         }
73     }
74
75     @Override
76     public Collection<Provider.ProviderFunctionality> getProviderFunctionality() {
77         return Collections.emptySet();
78     }
79
80     @Override
81     public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
82         logger.debug("{}: Session with sal established {}", id, session);
83
84         final DataBroker dataBroker = session.getSALService(DataBroker.class);
85         datastoreAdapter = new NetconfDeviceDatastoreAdapter(id, dataBroker);
86
87         topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, dataBroker);
88     }
89
90     public void close() throws Exception {
91         mountInstance.close();
92         datastoreAdapter.close();
93         datastoreAdapter = null;
94         topologyDatastoreAdapter.close();
95         topologyDatastoreAdapter = null;
96     }
97
98     static final class MountInstance implements AutoCloseable {
99
100         private DOMMountPointService mountService;
101         private final RemoteDeviceId id;
102         private ObjectRegistration<DOMMountPoint> registration;
103         private NotificationPublishService notificationSerivce;
104
105         private ObjectRegistration<DOMMountPoint> topologyRegistration;
106
107         MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
108             this.mountService = Preconditions.checkNotNull(mountService);
109             this.id = Preconditions.checkNotNull(id);
110         }
111
112         @Deprecated
113         synchronized void onDeviceConnected(final SchemaContext initialCtx,
114                 final DOMDataBroker broker, final RpcProvisionRegistry rpc,
115                 final NotificationPublishService notificationSerivce) {
116
117             Preconditions.checkNotNull(mountService, "Closed");
118             Preconditions.checkState(registration == null, "Already initialized");
119
120             final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getPath());
121             mountBuilder.addInitialSchemaContext(initialCtx);
122
123             mountBuilder.addService(DOMDataBroker.class, broker);
124             mountBuilder.addService(RpcProvisionRegistry.class, rpc);
125             this.notificationSerivce = notificationSerivce;
126             mountBuilder.addService(NotificationPublishService.class, notificationSerivce);
127
128             registration = mountBuilder.register();
129         }
130
131         @Deprecated
132         synchronized void onDeviceDisconnected() {
133             if(registration == null) {
134                 return;
135             }
136
137             try {
138                 registration.close();
139             } catch (final Exception e) {
140                 // Only log and ignore
141                 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getPath(), e);
142             } finally {
143                 registration = null;
144             }
145         }
146
147         synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
148                 final DOMDataBroker broker, final RpcProvisionRegistry rpc,
149                 final NotificationPublishService notificationSerivce) {
150
151             Preconditions.checkNotNull(mountService, "Closed");
152             Preconditions.checkState(topologyRegistration == null, "Already initialized");
153
154             final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getTopologyPath());
155             mountBuilder.addInitialSchemaContext(initialCtx);
156
157             mountBuilder.addService(DOMDataBroker.class, broker);
158             mountBuilder.addService(RpcProvisionRegistry.class, rpc);
159             this.notificationSerivce = notificationSerivce;
160             mountBuilder.addService(NotificationPublishService.class, notificationSerivce);
161
162             topologyRegistration = mountBuilder.register();
163         }
164
165         synchronized void onTopologyDeviceDisconnected() {
166             if(topologyRegistration == null) {
167                 return;
168             }
169
170             try {
171                 topologyRegistration.close();
172             } catch (final Exception e) {
173                 // Only log and ignore
174                 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
175             } finally {
176                 topologyRegistration = null;
177             }
178         }
179
180         @Override
181         synchronized public void close() throws Exception {
182             if(registration != null) {
183                 onDeviceDisconnected();
184                 onTopologyDeviceDisconnected();
185             }
186             mountService = null;
187         }
188
189         public synchronized void publish(final CompositeNode domNotification) {
190             Preconditions.checkNotNull(notificationSerivce, "Device not set up yet, cannot handle notification {}", domNotification);
191             notificationSerivce.publish(domNotification);
192         }
193     }
194
195 }