Netconf stack by default locks the data store before issuing
[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 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.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.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
40
41     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
42
43     private final RemoteDeviceId id;
44     private final NetconfDeviceSalProvider salProvider;
45     private final List<AutoCloseable> salRegistrations = new ArrayList<>();
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 SchemaContext schemaContext,
72                                                final NetconfSessionPreferences netconfSessionPreferences,
73                                                final DOMRpcService deviceRpc, final DOMActionService deviceAction) {
74
75         final NetconfDeviceDataBroker netconfDeviceDataBroker =
76                 new NetconfDeviceDataBroker(id, schemaContext, deviceRpc, netconfSessionPreferences);
77         registerLockListener(netconfDeviceDataBroker);
78         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
79
80         salProvider.getMountInstance()
81                 .onTopologyDeviceConnected(schemaContext, netconfDeviceDataBroker, deviceRpc, notificationService,
82                         deviceAction);
83         salProvider.getTopologyDatastoreAdapter()
84                 .updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
85     }
86
87     @Override
88     public synchronized void onDeviceDisconnected() {
89         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
90         salProvider.getMountInstance().onTopologyDeviceDisconnected();
91         closeLockChangeListener();
92     }
93
94     @Override
95     public synchronized void onDeviceFailed(final Throwable throwable) {
96         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
97         salProvider.getMountInstance().onTopologyDeviceDisconnected();
98         closeLockChangeListener();
99     }
100
101     @Override
102     public synchronized void close() {
103         for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
104             closeGracefully(reg);
105         }
106         closeGracefully(salProvider);
107         closeLockChangeListener();
108     }
109
110     @SuppressWarnings("checkstyle:IllegalCatch")
111     private void closeGracefully(final AutoCloseable resource) {
112         if (resource != null) {
113             try {
114                 resource.close();
115             } catch (final Exception e) {
116                 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
117             }
118         }
119     }
120
121     private void closeLockChangeListener() {
122         if (listenerRegistration != null) {
123             listenerRegistration.close();
124         }
125     }
126
127     private void registerLockListener(final NetconfDeviceDataBroker netconfDeviceDataBroker) {
128         listenerRegistration = dataBroker.registerDataTreeChangeListener(
129                 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, createTopologyListPath()),
130                 new LockChangeListener(netconfDeviceDataBroker));
131     }
132
133     private InstanceIdentifier<DatastoreLock> createTopologyListPath() {
134         return InstanceIdentifier.create(NetconfNodeFieldsOptional.class)
135                 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
136                 .child(Node.class, new NodeKey(new NodeId(id.getName())))
137                 .child(DatastoreLock.class);
138
139     }
140 }