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.netconf.sal.connect.netconf.sal;
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.dom.api.DOMActionService;
16 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
17 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
18 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
19 import org.opendaylight.mdsal.dom.api.DOMNotification;
20 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
21 import org.opendaylight.mdsal.dom.api.DOMRpcService;
22 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
23 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
24 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
25 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
26 import org.opendaylight.yangtools.concepts.ObjectRegistration;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class NetconfDeviceSalProvider implements AutoCloseable {
32 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
34 private final RemoteDeviceId id;
35 private final MountInstance mountInstance;
37 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
39 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
40 this(deviceId, mountService, null);
43 // FIXME: NETCONF-918: remove this method
44 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
45 final DataBroker dataBroker) {
47 mountInstance = new MountInstance(mountService, id);
48 if (dataBroker != null) {
49 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(dataBroker, id);
53 public MountInstance getMountInstance() {
54 checkState(mountInstance != null, "%s: Mount instance was not initialized by sal. Cannot get mount instance",
59 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
60 final NetconfDeviceTopologyAdapter local = topologyDatastoreAdapter;
61 checkState(local != null,
62 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id, this);
68 mountInstance.close();
69 if (topologyDatastoreAdapter != null) {
70 topologyDatastoreAdapter.close();
71 topologyDatastoreAdapter = null;
75 public static class MountInstance implements AutoCloseable {
77 private final DOMMountPointService mountService;
78 private final RemoteDeviceId id;
80 private NetconfDeviceNotificationService notificationService;
81 private ObjectRegistration<DOMMountPoint> topologyRegistration;
83 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
84 this.mountService = requireNonNull(mountService);
85 this.id = requireNonNull(id);
88 public synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
89 final DOMDataBroker broker, final NetconfDataTreeService netconfService, final DOMRpcService rpc,
90 final NetconfDeviceNotificationService newNotificationService, final DOMActionService deviceAction) {
91 requireNonNull(mountService, "Closed");
92 checkState(topologyRegistration == null, "Already initialized");
94 final DOMMountPointService.DOMMountPointBuilder mountBuilder =
95 mountService.createMountPoint(id.getTopologyPath());
96 mountBuilder.addService(DOMSchemaService.class, FixedDOMSchemaService.of(() -> initialCtx));
99 mountBuilder.addService(DOMDataBroker.class, broker);
101 mountBuilder.addService(DOMRpcService.class, rpc);
102 mountBuilder.addService(DOMNotificationService.class, newNotificationService);
103 if (deviceAction != null) {
104 mountBuilder.addService(DOMActionService.class, deviceAction);
106 if (netconfService != null) {
107 mountBuilder.addService(NetconfDataTreeService.class, netconfService);
109 notificationService = newNotificationService;
111 topologyRegistration = mountBuilder.register();
112 LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
115 @SuppressWarnings("checkstyle:IllegalCatch")
116 public synchronized void onTopologyDeviceDisconnected() {
117 if (topologyRegistration == null) {
118 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
123 topologyRegistration.close();
124 } catch (final Exception e) {
125 // Only log and ignore
126 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
128 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
129 topologyRegistration = null;
134 public synchronized void close() {
135 onTopologyDeviceDisconnected();
138 public synchronized void publish(final DOMNotification domNotification) {
139 checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
140 .publishNotification(domNotification);