2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.connect.netconf.sal;
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;
31 final class NetconfDeviceSalProvider implements AutoCloseable, Provider, BindingAwareProvider {
33 private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
35 private final RemoteDeviceId id;
36 private final ExecutorService executor;
37 private volatile NetconfDeviceDatastoreAdapter datastoreAdapter;
38 private MountInstance mountInstance;
40 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
42 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final ExecutorService executor) {
44 this.executor = executor;
47 public MountInstance getMountInstance() {
48 Preconditions.checkState(mountInstance != null,
49 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
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;
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;
66 public void onSessionInitiated(final Broker.ProviderSession session) {
67 logger.debug("{}: (BI)Session with sal established {}", id, session);
69 final DOMMountPointService mountService = session.getService(DOMMountPointService.class);
70 if (mountService != null) {
71 mountInstance = new MountInstance(mountService, id);
76 public Collection<Provider.ProviderFunctionality> getProviderFunctionality() {
77 return Collections.emptySet();
81 public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
82 logger.debug("{}: Session with sal established {}", id, session);
84 final DataBroker dataBroker = session.getSALService(DataBroker.class);
85 datastoreAdapter = new NetconfDeviceDatastoreAdapter(id, dataBroker);
87 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, dataBroker);
90 public void close() throws Exception {
91 mountInstance.close();
92 datastoreAdapter.close();
93 datastoreAdapter = null;
96 static final class MountInstance implements AutoCloseable {
98 private DOMMountPointService mountService;
99 private final RemoteDeviceId id;
100 private ObjectRegistration<DOMMountPoint> registration;
101 private NotificationPublishService notificationSerivce;
103 private ObjectRegistration<DOMMountPoint> topologyRegistration;
105 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
106 this.mountService = Preconditions.checkNotNull(mountService);
107 this.id = Preconditions.checkNotNull(id);
111 synchronized void onDeviceConnected(final SchemaContext initialCtx,
112 final DOMDataBroker broker, final RpcProvisionRegistry rpc,
113 final NotificationPublishService notificationSerivce) {
115 Preconditions.checkNotNull(mountService, "Closed");
116 Preconditions.checkState(registration == null, "Already initialized");
118 final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getPath());
119 mountBuilder.addInitialSchemaContext(initialCtx);
121 mountBuilder.addService(DOMDataBroker.class, broker);
122 mountBuilder.addService(RpcProvisionRegistry.class, rpc);
123 this.notificationSerivce = notificationSerivce;
124 mountBuilder.addService(NotificationPublishService.class, notificationSerivce);
126 registration = mountBuilder.register();
130 synchronized void onDeviceDisconnected() {
131 if(registration == null) {
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);
145 synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
146 final DOMDataBroker broker, final RpcProvisionRegistry rpc,
147 final NotificationPublishService notificationSerivce) {
149 Preconditions.checkNotNull(mountService, "Closed");
150 Preconditions.checkState(topologyRegistration == null, "Already initialized");
152 final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getTopologyPath());
153 mountBuilder.addInitialSchemaContext(initialCtx);
155 mountBuilder.addService(DOMDataBroker.class, broker);
156 mountBuilder.addService(RpcProvisionRegistry.class, rpc);
157 this.notificationSerivce = notificationSerivce;
158 mountBuilder.addService(NotificationPublishService.class, notificationSerivce);
160 topologyRegistration = mountBuilder.register();
163 synchronized void onTopologyDeviceDisconnected() {
164 if(topologyRegistration == null) {
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);
174 topologyRegistration = null;
179 synchronized public void close() throws Exception {
180 if(registration != null) {
181 onDeviceDisconnected();
182 onTopologyDeviceDisconnected();
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);