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.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
43 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
45 private final RemoteDeviceId id;
46 private final NetconfDeviceSalProvider salProvider;
47 private final List<AutoCloseable> salRegistrations = new ArrayList<>();
48 private final DataBroker dataBroker;
49 private final String topologyId;
51 private ListenerRegistration<LockChangeListener> listenerRegistration = null;
53 public NetconfDeviceSalFacade(final RemoteDeviceId id, final DOMMountPointService mountPointService,
54 final DataBroker dataBroker, final String topologyId) {
55 this(id, new NetconfDeviceSalProvider(id, mountPointService, dataBroker), dataBroker, topologyId);
59 NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceSalProvider salProvider,
60 final DataBroker dataBroker, final String topologyId) {
62 this.salProvider = salProvider;
63 this.dataBroker = dataBroker;
64 this.topologyId = topologyId;
68 public synchronized void onNotification(final DOMNotification domNotification) {
69 salProvider.getMountInstance().publish(domNotification);
73 public synchronized void onDeviceConnected(final SchemaContext schemaContext,
74 final NetconfSessionPreferences netconfSessionPreferences,
75 final DOMRpcService deviceRpc, final DOMActionService deviceAction) {
77 final NetconfDeviceDataBroker netconfDeviceDataBroker =
78 new NetconfDeviceDataBroker(id, schemaContext, deviceRpc, netconfSessionPreferences);
79 registerLockListener(netconfDeviceDataBroker);
80 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
82 salProvider.getMountInstance()
83 .onTopologyDeviceConnected(schemaContext, netconfDeviceDataBroker, deviceRpc, notificationService,
85 salProvider.getTopologyDatastoreAdapter()
86 .updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
90 public synchronized void onDeviceReconnected(final NetconfSessionPreferences netconfSessionPreferences,
91 final NetconfNode node) {
92 this.salProvider.getTopologyDatastoreAdapter().updateDeviceData(ConnectionStatus.Connecting,
93 netconfSessionPreferences.getNetconfDeviceCapabilities(), LogicalDatastoreType.CONFIGURATION, node);
97 public synchronized void onDeviceDisconnected() {
98 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
99 salProvider.getMountInstance().onTopologyDeviceDisconnected();
100 closeLockChangeListener();
104 public synchronized void onDeviceFailed(final Throwable throwable) {
105 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
106 salProvider.getMountInstance().onTopologyDeviceDisconnected();
107 closeLockChangeListener();
111 public synchronized void close() {
112 for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
113 closeGracefully(reg);
115 closeGracefully(salProvider);
116 closeLockChangeListener();
119 @SuppressWarnings("checkstyle:IllegalCatch")
120 private void closeGracefully(final AutoCloseable resource) {
121 if (resource != null) {
124 } catch (final Exception e) {
125 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
130 private void closeLockChangeListener() {
131 if (listenerRegistration != null) {
132 listenerRegistration.close();
136 private void registerLockListener(final NetconfDeviceDataBroker netconfDeviceDataBroker) {
137 listenerRegistration = dataBroker.registerDataTreeChangeListener(
138 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, createTopologyListPath()),
139 new LockChangeListener(netconfDeviceDataBroker));
142 private InstanceIdentifier<DatastoreLock> createTopologyListPath() {
143 return InstanceIdentifier.create(NetconfNodeFieldsOptional.class)
144 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
145 .child(Node.class, new NodeKey(new NodeId(id.getName())))
146 .child(DatastoreLock.class);