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.api.RemoteDeviceServices;
26 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
27 import org.opendaylight.yangtools.concepts.ObjectRegistration;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class NetconfDeviceSalProvider implements AutoCloseable {
33 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
35 private final RemoteDeviceId id;
36 private final MountInstance mountInstance;
38 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
40 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
41 this(deviceId, mountService, null);
44 // FIXME: NETCONF-918: remove this method
45 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
46 final DataBroker dataBroker) {
48 mountInstance = new MountInstance(mountService, id);
49 if (dataBroker != null) {
50 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(dataBroker, id);
54 public MountInstance getMountInstance() {
55 checkState(mountInstance != null, "%s: Mount instance was not initialized by sal. Cannot get mount instance",
60 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
61 final NetconfDeviceTopologyAdapter local = topologyDatastoreAdapter;
62 checkState(local != null,
63 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id, this);
69 mountInstance.close();
70 if (topologyDatastoreAdapter != null) {
71 topologyDatastoreAdapter.close();
72 topologyDatastoreAdapter = null;
76 public static class MountInstance implements AutoCloseable {
78 private final DOMMountPointService mountService;
79 private final RemoteDeviceId id;
81 private NetconfDeviceNotificationService notificationService;
82 private ObjectRegistration<DOMMountPoint> topologyRegistration;
84 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
85 this.mountService = requireNonNull(mountService);
86 this.id = requireNonNull(id);
89 public void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
90 final RemoteDeviceServices services, final DOMDataBroker broker,
91 final NetconfDataTreeService dataTreeService) {
92 onTopologyDeviceConnected(initialCtx, services, new NetconfDeviceNotificationService(), broker,
96 public synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
97 final RemoteDeviceServices services, final NetconfDeviceNotificationService newNotificationService,
98 final DOMDataBroker broker, final NetconfDataTreeService dataTreeService) {
99 requireNonNull(mountService, "Closed");
100 checkState(topologyRegistration == null, "Already initialized");
102 final DOMMountPointService.DOMMountPointBuilder mountBuilder =
103 mountService.createMountPoint(id.getTopologyPath());
104 mountBuilder.addService(DOMSchemaService.class, FixedDOMSchemaService.of(() -> initialCtx));
106 if (broker != null) {
107 mountBuilder.addService(DOMDataBroker.class, broker);
109 mountBuilder.addService(DOMRpcService.class, services.rpcs());
110 final var actions = services.actions();
111 if (actions != null) {
112 mountBuilder.addService(DOMActionService.class, actions);
114 if (dataTreeService != null) {
115 mountBuilder.addService(NetconfDataTreeService.class, dataTreeService);
117 mountBuilder.addService(DOMNotificationService.class, newNotificationService);
118 notificationService = newNotificationService;
120 topologyRegistration = mountBuilder.register();
121 LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
124 @SuppressWarnings("checkstyle:IllegalCatch")
125 public synchronized void onTopologyDeviceDisconnected() {
126 if (topologyRegistration == null) {
127 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
132 topologyRegistration.close();
133 } catch (final Exception e) {
134 // Only log and ignore
135 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
137 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
138 topologyRegistration = null;
143 public synchronized void close() {
144 onTopologyDeviceDisconnected();
147 public synchronized void publish(final DOMNotification domNotification) {
148 checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
149 .publishNotification(domNotification);