Merge "Startup arch - remove artifactId prefix from dir names."
[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     }
95
96     static final class MountInstance implements AutoCloseable {
97
98         private DOMMountPointService mountService;
99         private final RemoteDeviceId id;
100         private ObjectRegistration<DOMMountPoint> registration;
101         private NotificationPublishService notificationSerivce;
102
103         private ObjectRegistration<DOMMountPoint> topologyRegistration;
104
105         MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
106             this.mountService = Preconditions.checkNotNull(mountService);
107             this.id = Preconditions.checkNotNull(id);
108         }
109
110         @Deprecated
111         synchronized void onDeviceConnected(final SchemaContext initialCtx,
112                 final DOMDataBroker broker, final RpcProvisionRegistry rpc,
113                 final NotificationPublishService notificationSerivce) {
114
115             Preconditions.checkNotNull(mountService, "Closed");
116             Preconditions.checkState(registration == null, "Already initialized");
117
118             final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getPath());
119             mountBuilder.addInitialSchemaContext(initialCtx);
120
121             mountBuilder.addService(DOMDataBroker.class, broker);
122             mountBuilder.addService(RpcProvisionRegistry.class, rpc);
123             this.notificationSerivce = notificationSerivce;
124             mountBuilder.addService(NotificationPublishService.class, notificationSerivce);
125
126             registration = mountBuilder.register();
127         }
128
129         @Deprecated
130         synchronized void onDeviceDisconnected() {
131             if(registration == null) {
132                 return;
133             }
134
135             try {
136                 registration.close();
137             } catch (final Exception e) {
138                 // Only log and ignore
139                 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getPath(), e);
140             } finally {
141                 registration = null;
142             }
143         }
144
145         synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
146                 final DOMDataBroker broker, final RpcProvisionRegistry rpc,
147                 final NotificationPublishService notificationSerivce) {
148
149             Preconditions.checkNotNull(mountService, "Closed");
150             Preconditions.checkState(topologyRegistration == null, "Already initialized");
151
152             final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getTopologyPath());
153             mountBuilder.addInitialSchemaContext(initialCtx);
154
155             mountBuilder.addService(DOMDataBroker.class, broker);
156             mountBuilder.addService(RpcProvisionRegistry.class, rpc);
157             this.notificationSerivce = notificationSerivce;
158             mountBuilder.addService(NotificationPublishService.class, notificationSerivce);
159
160             topologyRegistration = mountBuilder.register();
161         }
162
163         synchronized void onTopologyDeviceDisconnected() {
164             if(topologyRegistration == null) {
165                 return;
166             }
167
168             try {
169                 topologyRegistration.close();
170             } catch (final Exception e) {
171                 // Only log and ignore
172                 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
173             } finally {
174                 topologyRegistration = null;
175             }
176         }
177
178         @Override
179         synchronized public void close() throws Exception {
180             if(registration != null) {
181                 onDeviceDisconnected();
182                 onTopologyDeviceDisconnected();
183             }
184             mountService = null;
185         }
186
187         public synchronized void publish(final CompositeNode domNotification) {
188             Preconditions.checkNotNull(notificationSerivce, "Device not set up yet, cannot handle notification {}", domNotification);
189             notificationSerivce.publish(domNotification);
190         }
191     }
192
193 }