Remove NetconfDeviceFacade.salRegistrations()
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceSalFacade.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
9
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.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.opendaylight.netconf.node.topology.rev150114.NetconfNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
41
42     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
43
44     private final RemoteDeviceId id;
45     private final NetconfDeviceSalProvider salProvider;
46     private final DataBroker dataBroker;
47     private final String topologyId;
48
49     private ListenerRegistration<LockChangeListener> listenerRegistration = null;
50
51     public NetconfDeviceSalFacade(final RemoteDeviceId id, final DOMMountPointService mountPointService,
52             final DataBroker dataBroker, final String topologyId) {
53         this(id, new NetconfDeviceSalProvider(id, mountPointService, dataBroker), dataBroker, topologyId);
54     }
55
56     @VisibleForTesting
57     NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceSalProvider salProvider,
58             final DataBroker dataBroker, final String topologyId) {
59         this.id = id;
60         this.salProvider = salProvider;
61         this.dataBroker = dataBroker;
62         this.topologyId = topologyId;
63     }
64
65     @Override
66     public synchronized void onNotification(final DOMNotification domNotification) {
67         salProvider.getMountInstance().publish(domNotification);
68     }
69
70     @Override
71     public synchronized void onDeviceConnected(final MountPointContext mountContext,
72                                                final NetconfSessionPreferences netconfSessionPreferences,
73                                                final DOMRpcService deviceRpc, final DOMActionService deviceAction) {
74         final EffectiveModelContext schemaContext = mountContext.getEffectiveModelContext();
75         final NetconfDeviceDataBroker netconfDeviceDataBroker =
76                 new NetconfDeviceDataBroker(id, mountContext, deviceRpc, netconfSessionPreferences);
77         final NetconfDataTreeService netconfService =
78                 AbstractNetconfDataTreeService.of(id, mountContext, deviceRpc, netconfSessionPreferences);
79         registerLockListener(netconfDeviceDataBroker, netconfService);
80         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
81
82         salProvider.getMountInstance()
83                 .onTopologyDeviceConnected(schemaContext, netconfDeviceDataBroker, netconfService,
84                         deviceRpc, notificationService, deviceAction);
85         salProvider.getTopologyDatastoreAdapter()
86                 .updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
87     }
88
89     @Override
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);
94     }
95
96     @Override
97     public synchronized void onDeviceDisconnected() {
98         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
99         salProvider.getMountInstance().onTopologyDeviceDisconnected();
100         closeLockChangeListener();
101     }
102
103     @Override
104     public synchronized void onDeviceFailed(final Throwable throwable) {
105         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
106         salProvider.getMountInstance().onTopologyDeviceDisconnected();
107         closeLockChangeListener();
108     }
109
110     @Override
111     public synchronized void close() {
112         closeGracefully(salProvider);
113         closeLockChangeListener();
114     }
115
116     @SuppressWarnings("checkstyle:IllegalCatch")
117     private void closeGracefully(final AutoCloseable resource) {
118         if (resource != null) {
119             try {
120                 resource.close();
121             } catch (final Exception e) {
122                 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
123             }
124         }
125     }
126
127     private void closeLockChangeListener() {
128         if (listenerRegistration != null) {
129             listenerRegistration.close();
130         }
131     }
132
133     private void registerLockListener(final NetconfDeviceDataBroker netconfDeviceDataBroker,
134                                       final NetconfDataTreeService netconfDataTreeService) {
135         listenerRegistration = dataBroker.registerDataTreeChangeListener(
136                 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, createTopologyListPath()),
137                 new LockChangeListener(netconfDeviceDataBroker, netconfDataTreeService));
138     }
139
140     private InstanceIdentifier<DatastoreLock> createTopologyListPath() {
141         return InstanceIdentifier.create(NetconfNodeFieldsOptional.class)
142                 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
143                 .child(Node.class, new NodeKey(new NodeId(id.getName())))
144                 .child(DatastoreLock.class);
145
146     }
147 }