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.DOMActionService;
15 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
16 import org.opendaylight.mdsal.dom.api.DOMNotification;
17 import org.opendaylight.mdsal.dom.api.DOMRpcService;
18 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
19 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
20 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
21 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
22 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
23 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.NetconfNodeFieldsOptional;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.Topology;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.TopologyKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.NodeKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.node.DatastoreLock;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public final class NetconfDeviceSalFacade implements RemoteDeviceHandler, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
42 private final RemoteDeviceId id;
43 private final NetconfDeviceSalProvider salProvider;
44 private final DataBroker dataBroker;
45 private final String topologyId;
47 private ListenerRegistration<LockChangeListener> listenerRegistration = null;
49 public NetconfDeviceSalFacade(final RemoteDeviceId id, final DOMMountPointService mountPointService,
50 final DataBroker dataBroker, final String topologyId) {
51 this(id, new NetconfDeviceSalProvider(id, mountPointService, dataBroker), dataBroker, topologyId);
55 NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceSalProvider salProvider,
56 final DataBroker dataBroker, final String topologyId) {
58 this.salProvider = salProvider;
59 this.dataBroker = dataBroker;
60 this.topologyId = topologyId;
64 public synchronized void onNotification(final DOMNotification domNotification) {
65 salProvider.getMountInstance().publish(domNotification);
69 public synchronized void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
70 final NetconfSessionPreferences sessionPreferences, final DOMRpcService deviceRpc,
71 final DOMActionService deviceAction) {
72 final MountPointContext mountContext = deviceSchema.mountContext();
73 final EffectiveModelContext schemaContext = mountContext.getEffectiveModelContext();
75 final NetconfDeviceDataBroker netconfDeviceDataBroker =
76 new NetconfDeviceDataBroker(id, mountContext, deviceRpc, sessionPreferences);
77 final NetconfDataTreeService netconfService =
78 AbstractNetconfDataTreeService.of(id, mountContext, deviceRpc, sessionPreferences);
79 registerLockListener(netconfDeviceDataBroker, netconfService);
80 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
82 salProvider.getMountInstance().onTopologyDeviceConnected(schemaContext, netconfDeviceDataBroker, netconfService,
83 deviceRpc, notificationService, deviceAction);
84 salProvider.getTopologyDatastoreAdapter().updateDeviceData(true, deviceSchema.capabilities());
88 public synchronized void onDeviceDisconnected() {
89 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, NetconfDeviceCapabilities.empty());
90 salProvider.getMountInstance().onTopologyDeviceDisconnected();
91 closeLockChangeListener();
95 public synchronized void onDeviceFailed(final Throwable throwable) {
96 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
97 salProvider.getMountInstance().onTopologyDeviceDisconnected();
98 closeLockChangeListener();
102 public synchronized void close() {
103 closeGracefully(salProvider);
104 closeLockChangeListener();
107 @SuppressWarnings("checkstyle:IllegalCatch")
108 private void closeGracefully(final AutoCloseable resource) {
109 if (resource != null) {
112 } catch (final Exception e) {
113 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
118 private void closeLockChangeListener() {
119 if (listenerRegistration != null) {
120 listenerRegistration.close();
124 private void registerLockListener(final NetconfDeviceDataBroker netconfDeviceDataBroker,
125 final NetconfDataTreeService netconfDataTreeService) {
126 listenerRegistration = dataBroker.registerDataTreeChangeListener(
127 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, createTopologyListPath()),
128 new LockChangeListener(netconfDeviceDataBroker, netconfDataTreeService));
131 private InstanceIdentifier<DatastoreLock> createTopologyListPath() {
132 return InstanceIdentifier.create(NetconfNodeFieldsOptional.class)
133 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
134 .child(Node.class, new NodeKey(new NodeId(id.getName())))
135 .child(DatastoreLock.class);