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 com.google.common.collect.Lists;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMActionService;
18 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
19 import org.opendaylight.mdsal.dom.api.DOMNotification;
20 import org.opendaylight.mdsal.dom.api.DOMRpcService;
21 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
22 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
23 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
24 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.NetconfNodeFieldsOptional;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.Topology;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.TopologyKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.NodeKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.node.DatastoreLock;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
44 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
46 private final RemoteDeviceId id;
47 private final NetconfDeviceSalProvider salProvider;
48 private final List<AutoCloseable> salRegistrations = new ArrayList<>();
49 private final DataBroker dataBroker;
50 private final String topologyId;
52 private ListenerRegistration<LockChangeListener> listenerRegistration = null;
54 public NetconfDeviceSalFacade(final RemoteDeviceId id, final DOMMountPointService mountPointService,
55 final DataBroker dataBroker, final String topologyId) {
56 this(id, new NetconfDeviceSalProvider(id, mountPointService, dataBroker), dataBroker, topologyId);
60 NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceSalProvider salProvider,
61 final DataBroker dataBroker, final String topologyId) {
63 this.salProvider = salProvider;
64 this.dataBroker = dataBroker;
65 this.topologyId = topologyId;
69 public synchronized void onNotification(final DOMNotification domNotification) {
70 salProvider.getMountInstance().publish(domNotification);
74 public synchronized void onDeviceConnected(final MountPointContext mountContext,
75 final NetconfSessionPreferences netconfSessionPreferences,
76 final DOMRpcService deviceRpc, final DOMActionService deviceAction) {
77 final SchemaContext schemaContext = mountContext.getSchemaContext();
78 final NetconfDeviceDataBroker netconfDeviceDataBroker =
79 new NetconfDeviceDataBroker(id, mountContext, deviceRpc, netconfSessionPreferences);
80 registerLockListener(netconfDeviceDataBroker);
81 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
83 salProvider.getMountInstance()
84 .onTopologyDeviceConnected(schemaContext, netconfDeviceDataBroker, deviceRpc, notificationService,
86 salProvider.getTopologyDatastoreAdapter()
87 .updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
91 public synchronized void onDeviceReconnected(final NetconfSessionPreferences netconfSessionPreferences,
92 final NetconfNode node) {
93 this.salProvider.getTopologyDatastoreAdapter().updateDeviceData(ConnectionStatus.Connecting,
94 netconfSessionPreferences.getNetconfDeviceCapabilities(), LogicalDatastoreType.CONFIGURATION, node);
98 public synchronized void onDeviceDisconnected() {
99 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
100 salProvider.getMountInstance().onTopologyDeviceDisconnected();
101 closeLockChangeListener();
105 public synchronized void onDeviceFailed(final Throwable throwable) {
106 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
107 salProvider.getMountInstance().onTopologyDeviceDisconnected();
108 closeLockChangeListener();
112 public synchronized void close() {
113 for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
114 closeGracefully(reg);
116 closeGracefully(salProvider);
117 closeLockChangeListener();
120 @SuppressWarnings("checkstyle:IllegalCatch")
121 private void closeGracefully(final AutoCloseable resource) {
122 if (resource != null) {
125 } catch (final Exception e) {
126 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
131 private void closeLockChangeListener() {
132 if (listenerRegistration != null) {
133 listenerRegistration.close();
137 private void registerLockListener(final NetconfDeviceDataBroker netconfDeviceDataBroker) {
138 listenerRegistration = dataBroker.registerDataTreeChangeListener(
139 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, createTopologyListPath()),
140 new LockChangeListener(netconfDeviceDataBroker));
143 private InstanceIdentifier<DatastoreLock> createTopologyListPath() {
144 return InstanceIdentifier.create(NetconfNodeFieldsOptional.class)
145 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
146 .child(Node.class, new NodeKey(new NodeId(id.getName())))
147 .child(DatastoreLock.class);