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 com.google.common.annotations.VisibleForTesting;
11 import org.opendaylight.mdsal.binding.api.DataBroker;
12 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
15 import org.opendaylight.mdsal.dom.api.DOMNotification;
16 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
17 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
18 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
19 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
20 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
21 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.NetconfNodeFieldsOptional;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.Topology;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.TopologyKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.NodeKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.node.DatastoreLock;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public final class NetconfDeviceSalFacade implements RemoteDeviceHandler, AutoCloseable {
37 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
39 private final RemoteDeviceId id;
40 private final NetconfDeviceSalProvider salProvider;
41 private final DataBroker dataBroker;
42 private final String topologyId;
44 private ListenerRegistration<LockChangeListener> listenerRegistration = null;
46 public NetconfDeviceSalFacade(final RemoteDeviceId id, final DOMMountPointService mountPointService,
47 final DataBroker dataBroker, final String topologyId) {
48 this(id, new NetconfDeviceSalProvider(id, mountPointService, dataBroker), dataBroker, topologyId);
52 NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceSalProvider salProvider,
53 final DataBroker dataBroker, final String topologyId) {
55 this.salProvider = salProvider;
56 this.dataBroker = dataBroker;
57 this.topologyId = topologyId;
61 public synchronized void onNotification(final DOMNotification domNotification) {
62 salProvider.getMountInstance().publish(domNotification);
66 public synchronized void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
67 final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
68 final var mountContext = deviceSchema.mountContext();
69 final var modelContext = mountContext.getEffectiveModelContext();
71 final var deviceRpc = services.rpcs();
72 final var netconfDataTree = AbstractNetconfDataTreeService.of(id, mountContext, deviceRpc, sessionPreferences);
73 final var netconfDataBroker = new NetconfDeviceDataBroker(id, mountContext, deviceRpc, sessionPreferences);
74 registerLockListener(netconfDataBroker, netconfDataTree);
76 salProvider.getMountInstance().onTopologyDeviceConnected(modelContext, services, netconfDataBroker,
78 salProvider.getTopologyDatastoreAdapter().updateDeviceData(true, deviceSchema.capabilities());
82 public synchronized void onDeviceDisconnected() {
83 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, NetconfDeviceCapabilities.empty());
84 salProvider.getMountInstance().onTopologyDeviceDisconnected();
85 closeLockChangeListener();
89 public synchronized void onDeviceFailed(final Throwable throwable) {
90 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
91 salProvider.getMountInstance().onTopologyDeviceDisconnected();
92 closeLockChangeListener();
96 public synchronized void close() {
97 closeGracefully(salProvider);
98 closeLockChangeListener();
101 @SuppressWarnings("checkstyle:IllegalCatch")
102 private void closeGracefully(final AutoCloseable resource) {
103 if (resource != null) {
106 } catch (final Exception e) {
107 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
112 private void closeLockChangeListener() {
113 if (listenerRegistration != null) {
114 listenerRegistration.close();
118 private void registerLockListener(final NetconfDeviceDataBroker netconfDeviceDataBroker,
119 final NetconfDataTreeService netconfDataTreeService) {
120 listenerRegistration = dataBroker.registerDataTreeChangeListener(
121 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, createTopologyListPath()),
122 new LockChangeListener(netconfDeviceDataBroker, netconfDataTreeService));
125 private InstanceIdentifier<DatastoreLock> createTopologyListPath() {
126 return InstanceIdentifier.create(NetconfNodeFieldsOptional.class)
127 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
128 .child(Node.class, new NodeKey(new NodeId(id.getName())))
129 .child(DatastoreLock.class);