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.dom.api.NetconfDataTreeService;
22 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
23 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
24 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
25 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.NetconfNodeFieldsOptional;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.Topology;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.TopologyKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.NodeKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.node.DatastoreLock;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
36 import org.opendaylight.yangtools.concepts.ListenerRegistration;
37 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public final class NetconfDeviceSalFacade implements RemoteDeviceHandler, AutoCloseable {
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 EffectiveModelContext schemaContext = mountContext.getEffectiveModelContext();
78 final NetconfDeviceDataBroker netconfDeviceDataBroker =
79 new NetconfDeviceDataBroker(id, mountContext, deviceRpc, netconfSessionPreferences);
80 final NetconfDataTreeService netconfService =
81 AbstractNetconfDataTreeService.of(id, mountContext, deviceRpc, netconfSessionPreferences);
82 registerLockListener(netconfDeviceDataBroker, netconfService);
83 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
85 salProvider.getMountInstance()
86 .onTopologyDeviceConnected(schemaContext, netconfDeviceDataBroker, netconfService,
87 deviceRpc, notificationService, deviceAction);
88 salProvider.getTopologyDatastoreAdapter()
89 .updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
93 public synchronized void onDeviceReconnected(final NetconfSessionPreferences netconfSessionPreferences,
94 final NetconfNode node) {
95 salProvider.getTopologyDatastoreAdapter().updateDeviceData(ConnectionStatus.Connecting,
96 netconfSessionPreferences.getNetconfDeviceCapabilities(), LogicalDatastoreType.CONFIGURATION, node);
100 public synchronized void onDeviceDisconnected() {
101 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
102 salProvider.getMountInstance().onTopologyDeviceDisconnected();
103 closeLockChangeListener();
107 public synchronized void onDeviceFailed(final Throwable throwable) {
108 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
109 salProvider.getMountInstance().onTopologyDeviceDisconnected();
110 closeLockChangeListener();
114 public synchronized void close() {
115 for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
116 closeGracefully(reg);
118 closeGracefully(salProvider);
119 closeLockChangeListener();
122 @SuppressWarnings("checkstyle:IllegalCatch")
123 private void closeGracefully(final AutoCloseable resource) {
124 if (resource != null) {
127 } catch (final Exception e) {
128 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
133 private void closeLockChangeListener() {
134 if (listenerRegistration != null) {
135 listenerRegistration.close();
139 private void registerLockListener(final NetconfDeviceDataBroker netconfDeviceDataBroker,
140 final NetconfDataTreeService netconfDataTreeService) {
141 listenerRegistration = dataBroker.registerDataTreeChangeListener(
142 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, createTopologyListPath()),
143 new LockChangeListener(netconfDeviceDataBroker, netconfDataTreeService));
146 private InstanceIdentifier<DatastoreLock> createTopologyListPath() {
147 return InstanceIdentifier.create(NetconfNodeFieldsOptional.class)
148 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
149 .child(Node.class, new NodeKey(new NodeId(id.getName())))
150 .child(DatastoreLock.class);