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 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final ExecutorService executor) {
42 this.executor = executor;
45 public MountInstance getMountInstance() {
46 Preconditions.checkState(mountInstance != null,
47 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
51 public NetconfDeviceDatastoreAdapter getDatastoreAdapter() {
52 Preconditions.checkState(datastoreAdapter != null,
53 "%s: Sal provider %s was not initialized by sal. Cannot get datastore adapter", id);
54 return datastoreAdapter;
58 public void onSessionInitiated(final Broker.ProviderSession session) {
59 logger.debug("{}: (BI)Session with sal established {}", id, session);
61 final DOMMountPointService mountService = session.getService(DOMMountPointService.class);
62 if (mountService != null) {
63 mountInstance = new MountInstance(mountService, id);
68 public Collection<Provider.ProviderFunctionality> getProviderFunctionality() {
69 return Collections.emptySet();
73 public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
74 logger.debug("{}: Session with sal established {}", id, session);
76 final DataBroker dataBroker = session.getSALService(DataBroker.class);
77 datastoreAdapter = new NetconfDeviceDatastoreAdapter(id, dataBroker);
80 public void close() throws Exception {
81 mountInstance.close();
82 datastoreAdapter.close();
83 datastoreAdapter = null;
86 static final class MountInstance implements AutoCloseable {
88 private DOMMountPointService mountService;
89 private final RemoteDeviceId id;
90 private ObjectRegistration<DOMMountPoint> registration;
91 private NotificationPublishService notificationSerivce;
93 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
94 this.mountService = Preconditions.checkNotNull(mountService);
95 this.id = Preconditions.checkNotNull(id);
98 synchronized void onDeviceConnected(final SchemaContext initialCtx,
99 final DOMDataBroker broker, final RpcProvisionRegistry rpc,
100 final NotificationPublishService notificationSerivce) {
102 Preconditions.checkNotNull(mountService, "Closed");
103 Preconditions.checkState(registration == null, "Already initialized");
105 final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getPath());
106 mountBuilder.addInitialSchemaContext(initialCtx);
108 mountBuilder.addService(DOMDataBroker.class, broker);
109 mountBuilder.addService(RpcProvisionRegistry.class, rpc);
110 this.notificationSerivce = notificationSerivce;
111 mountBuilder.addService(NotificationPublishService.class, notificationSerivce);
113 registration = mountBuilder.register();
116 synchronized void onDeviceDisconnected() {
117 if(registration == null) {
122 registration.close();
123 } catch (final Exception e) {
124 // Only log and ignore
125 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getPath(), e);
132 synchronized public void close() throws Exception {
133 if(registration != null) {
134 onDeviceDisconnected();
139 public synchronized void publish(final CompositeNode domNotification) {
140 Preconditions.checkNotNull(notificationSerivce, "Device not set up yet, cannot handle notification {}", domNotification);
141 notificationSerivce.publish(domNotification);