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;
94 topologyDatastoreAdapter.close();
95 topologyDatastoreAdapter = null;
98 static final class MountInstance implements AutoCloseable {
100 private DOMMountPointService mountService;
101 private final RemoteDeviceId id;
102 private ObjectRegistration<DOMMountPoint> registration;
103 private NotificationPublishService notificationSerivce;
105 private ObjectRegistration<DOMMountPoint> topologyRegistration;
107 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
108 this.mountService = Preconditions.checkNotNull(mountService);
109 this.id = Preconditions.checkNotNull(id);
113 synchronized void onDeviceConnected(final SchemaContext initialCtx,
114 final DOMDataBroker broker, final RpcProvisionRegistry rpc,
115 final NotificationPublishService notificationSerivce) {
117 Preconditions.checkNotNull(mountService, "Closed");
118 Preconditions.checkState(registration == null, "Already initialized");
120 final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getPath());
121 mountBuilder.addInitialSchemaContext(initialCtx);
123 mountBuilder.addService(DOMDataBroker.class, broker);
124 mountBuilder.addService(RpcProvisionRegistry.class, rpc);
125 this.notificationSerivce = notificationSerivce;
126 mountBuilder.addService(NotificationPublishService.class, notificationSerivce);
128 registration = mountBuilder.register();
132 synchronized void onDeviceDisconnected() {
133 if(registration == null) {
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);
147 synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
148 final DOMDataBroker broker, final RpcProvisionRegistry rpc,
149 final NotificationPublishService notificationSerivce) {
151 Preconditions.checkNotNull(mountService, "Closed");
152 Preconditions.checkState(topologyRegistration == null, "Already initialized");
154 final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getTopologyPath());
155 mountBuilder.addInitialSchemaContext(initialCtx);
157 mountBuilder.addService(DOMDataBroker.class, broker);
158 mountBuilder.addService(RpcProvisionRegistry.class, rpc);
159 this.notificationSerivce = notificationSerivce;
160 mountBuilder.addService(NotificationPublishService.class, notificationSerivce);
162 topologyRegistration = mountBuilder.register();
165 synchronized void onTopologyDeviceDisconnected() {
166 if(topologyRegistration == null) {
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);
176 topologyRegistration = null;
181 synchronized public void close() throws Exception {
182 if(registration != null) {
183 onDeviceDisconnected();
184 onTopologyDeviceDisconnected();
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);