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.dom.api.DOMActionService;
15 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
16 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
17 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
18 import org.opendaylight.mdsal.dom.api.DOMNotification;
19 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
20 import org.opendaylight.mdsal.dom.api.DOMRpcService;
21 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
22 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
23 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
24 import org.opendaylight.netconf.sal.connect.api.NetconfRpcService;
25 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
26 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Actions;
27 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
28 import org.opendaylight.netconf.sal.connect.api.SchemalessRpcService;
29 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
30 import org.opendaylight.yangtools.concepts.ObjectRegistration;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 // FIXME: remove this class and promote MountInstance to a top-level construct
36 // Non-final for mocking
37 public class NetconfDeviceSalProvider implements AutoCloseable {
38 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
40 private final RemoteDeviceId id;
41 private final MountInstance mountInstance;
43 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
44 id = requireNonNull(deviceId);
45 mountInstance = new MountInstance(mountService, id);
48 public MountInstance getMountInstance() {
49 checkState(mountInstance != null, "%s: Mount instance was not initialized by sal. Cannot get mount instance",
56 mountInstance.close();
59 public static class MountInstance implements AutoCloseable {
61 private final DOMMountPointService mountService;
62 private final RemoteDeviceId id;
64 private NetconfDeviceNotificationService notificationService;
65 private ObjectRegistration<DOMMountPoint> topologyRegistration;
67 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
68 this.mountService = requireNonNull(mountService);
69 this.id = requireNonNull(id);
72 public void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
73 final RemoteDeviceServices services, final DOMDataBroker broker,
74 final NetconfDataTreeService dataTreeService) {
75 onTopologyDeviceConnected(initialCtx, services, new NetconfDeviceNotificationService(), broker,
79 public synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
80 final RemoteDeviceServices services, final NetconfDeviceNotificationService newNotificationService,
81 final DOMDataBroker broker, final NetconfDataTreeService dataTreeService) {
82 requireNonNull(mountService, "Closed");
83 checkState(topologyRegistration == null, "Already initialized");
85 final var mountBuilder = mountService.createMountPoint(id.getTopologyPath());
86 mountBuilder.addService(DOMSchemaService.class, FixedDOMSchemaService.of(() -> initialCtx));
88 final var rpcs = services.rpcs();
89 mountBuilder.addService(NetconfRpcService.class, rpcs);
90 if (rpcs instanceof Rpcs.Normalized normalized) {
91 mountBuilder.addService(DOMRpcService.class, normalized);
92 } else if (rpcs instanceof Rpcs.Schemaless schemaless) {
93 mountBuilder.addService(SchemalessRpcService.class, schemaless);
95 if (services.actions() instanceof Actions.Normalized normalized) {
96 mountBuilder.addService(DOMActionService.class, normalized);
100 mountBuilder.addService(DOMDataBroker.class, broker);
102 if (dataTreeService != null) {
103 mountBuilder.addService(NetconfDataTreeService.class, dataTreeService);
105 mountBuilder.addService(DOMNotificationService.class, newNotificationService);
106 notificationService = newNotificationService;
108 topologyRegistration = mountBuilder.register();
109 LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
112 @SuppressWarnings("checkstyle:IllegalCatch")
113 public synchronized void onTopologyDeviceDisconnected() {
114 if (topologyRegistration == null) {
115 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
120 topologyRegistration.close();
121 } catch (final Exception e) {
122 // Only log and ignore
123 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
125 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
126 topologyRegistration = null;
131 public synchronized void close() {
132 onTopologyDeviceDisconnected();
135 public synchronized void publish(final DOMNotification domNotification) {
136 checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
137 .publishNotification(domNotification);