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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
22 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.controller.sal.core.api.Broker;
24 import org.opendaylight.controller.sal.core.api.Provider;
25 import org.opendaylight.yangtools.concepts.ObjectRegistration;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 final class NetconfDeviceSalProvider implements AutoCloseable, Provider, BindingAwareProvider {
32 private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
34 private final RemoteDeviceId id;
35 private volatile NetconfDeviceDatastoreAdapter datastoreAdapter;
36 private MountInstance mountInstance;
38 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
40 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId) {
44 public MountInstance getMountInstance() {
45 Preconditions.checkState(mountInstance != null,
46 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
50 public NetconfDeviceDatastoreAdapter getDatastoreAdapter() {
51 Preconditions.checkState(datastoreAdapter != null,
52 "%s: Sal provider %s was not initialized by sal. Cannot get datastore adapter", id);
53 return datastoreAdapter;
56 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
57 Preconditions.checkState(topologyDatastoreAdapter != null,
58 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id);
59 return topologyDatastoreAdapter;
63 public void onSessionInitiated(final Broker.ProviderSession session) {
64 logger.debug("{}: (BI)Session with sal established {}", id, session);
66 final DOMMountPointService mountService = session.getService(DOMMountPointService.class);
67 if (mountService != null) {
68 mountInstance = new MountInstance(mountService, id);
73 public Collection<Provider.ProviderFunctionality> getProviderFunctionality() {
74 return Collections.emptySet();
78 public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
79 logger.debug("{}: Session with sal established {}", id, session);
81 final DataBroker dataBroker = session.getSALService(DataBroker.class);
82 datastoreAdapter = new NetconfDeviceDatastoreAdapter(id, dataBroker);
84 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, dataBroker);
87 public void close() throws Exception {
88 mountInstance.close();
89 datastoreAdapter.close();
90 datastoreAdapter = null;
91 topologyDatastoreAdapter.close();
92 topologyDatastoreAdapter = null;
95 static final class MountInstance implements AutoCloseable {
97 private DOMMountPointService mountService;
98 private final RemoteDeviceId id;
99 private ObjectRegistration<DOMMountPoint> registration;
100 private NetconfDeviceNotificationService notificationService;
102 private ObjectRegistration<DOMMountPoint> topologyRegistration;
104 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
105 this.mountService = Preconditions.checkNotNull(mountService);
106 this.id = Preconditions.checkNotNull(id);
110 synchronized void onDeviceConnected(final SchemaContext initialCtx,
111 final DOMDataBroker broker, final DOMRpcService rpc,
112 final NetconfDeviceNotificationService notificationService) {
114 Preconditions.checkNotNull(mountService, "Closed");
115 Preconditions.checkState(registration == null, "Already initialized");
117 final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getPath());
118 mountBuilder.addInitialSchemaContext(initialCtx);
120 mountBuilder.addService(DOMDataBroker.class, broker);
121 mountBuilder.addService(DOMRpcService.class, rpc);
122 mountBuilder.addService(DOMNotificationService.class, notificationService);
123 this.notificationService = notificationService;
125 registration = mountBuilder.register();
126 logger.debug("{}: Mountpoint exposed into MD-SAL {}", id, registration);
130 synchronized void onDeviceDisconnected() {
131 if(registration == null) {
132 logger.trace("{}: Not removing mountpoint from MD-SAL, mountpoint was not registered yet", id);
137 registration.close();
138 } catch (final Exception e) {
139 // Only log and ignore
140 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getPath(), e);
142 logger.debug("{}: Mountpoint removed from MD-SAL {}", id, registration);
147 synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
148 final DOMDataBroker broker, final DOMRpcService rpc,
149 final NetconfDeviceNotificationService notificationService) {
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(DOMRpcService.class, rpc);
159 mountBuilder.addService(DOMNotificationService.class, notificationService);
161 topologyRegistration = mountBuilder.register();
162 logger.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, registration);
166 synchronized void onTopologyDeviceDisconnected() {
167 if(topologyRegistration == null) {
168 logger.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
173 topologyRegistration.close();
174 } catch (final Exception e) {
175 // Only log and ignore
176 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
178 logger.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, registration);
179 topologyRegistration = null;
184 synchronized public void close() throws Exception {
185 onDeviceDisconnected();
186 onTopologyDeviceDisconnected();
190 public synchronized void publish(final DOMNotification domNotification) {
191 Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}", domNotification);
192 notificationService.publishNotification(domNotification);