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.NetconfRpcService;
26 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
27 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Actions;
28 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
29 import org.opendaylight.netconf.sal.connect.api.SchemalessRpcService;
30 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
31 import org.opendaylight.yangtools.concepts.ObjectRegistration;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class NetconfDeviceSalProvider implements AutoCloseable {
37 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
39 private final RemoteDeviceId id;
40 private final MountInstance mountInstance;
42 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
44 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
45 this(deviceId, mountService, null);
48 // FIXME: NETCONF-918: remove this method
49 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
50 final DataBroker dataBroker) {
52 mountInstance = new MountInstance(mountService, id);
53 if (dataBroker != null) {
54 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(dataBroker, id);
58 public MountInstance getMountInstance() {
59 checkState(mountInstance != null, "%s: Mount instance was not initialized by sal. Cannot get mount instance",
64 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
65 final NetconfDeviceTopologyAdapter local = topologyDatastoreAdapter;
66 checkState(local != null,
67 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id, this);
73 mountInstance.close();
74 if (topologyDatastoreAdapter != null) {
75 topologyDatastoreAdapter.close();
76 topologyDatastoreAdapter = null;
80 public static class MountInstance implements AutoCloseable {
82 private final DOMMountPointService mountService;
83 private final RemoteDeviceId id;
85 private NetconfDeviceNotificationService notificationService;
86 private ObjectRegistration<DOMMountPoint> topologyRegistration;
88 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
89 this.mountService = requireNonNull(mountService);
90 this.id = requireNonNull(id);
93 public void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
94 final RemoteDeviceServices services, final DOMDataBroker broker,
95 final NetconfDataTreeService dataTreeService) {
96 onTopologyDeviceConnected(initialCtx, services, new NetconfDeviceNotificationService(), broker,
100 public synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
101 final RemoteDeviceServices services, final NetconfDeviceNotificationService newNotificationService,
102 final DOMDataBroker broker, final NetconfDataTreeService dataTreeService) {
103 requireNonNull(mountService, "Closed");
104 checkState(topologyRegistration == null, "Already initialized");
106 final var mountBuilder = mountService.createMountPoint(id.getTopologyPath());
107 mountBuilder.addService(DOMSchemaService.class, FixedDOMSchemaService.of(() -> initialCtx));
109 final var rpcs = services.rpcs();
110 mountBuilder.addService(NetconfRpcService.class, rpcs);
111 if (rpcs instanceof Rpcs.Normalized normalized) {
112 mountBuilder.addService(DOMRpcService.class, normalized);
113 } else if (rpcs instanceof Rpcs.Schemaless schemaless) {
114 mountBuilder.addService(SchemalessRpcService.class, schemaless);
116 if (services.actions() instanceof Actions.Normalized normalized) {
117 mountBuilder.addService(DOMActionService.class, normalized);
120 if (broker != null) {
121 mountBuilder.addService(DOMDataBroker.class, broker);
123 if (dataTreeService != null) {
124 mountBuilder.addService(NetconfDataTreeService.class, dataTreeService);
126 mountBuilder.addService(DOMNotificationService.class, newNotificationService);
127 notificationService = newNotificationService;
129 topologyRegistration = mountBuilder.register();
130 LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
133 @SuppressWarnings("checkstyle:IllegalCatch")
134 public synchronized void onTopologyDeviceDisconnected() {
135 if (topologyRegistration == null) {
136 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
141 topologyRegistration.close();
142 } catch (final Exception e) {
143 // Only log and ignore
144 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
146 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
147 topologyRegistration = null;
152 public synchronized void close() {
153 onTopologyDeviceDisconnected();
156 public synchronized void publish(final DOMNotification domNotification) {
157 checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
158 .publishNotification(domNotification);