Integrate createTopologyListPath()
[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.DOMMountPointService;
15 import org.opendaylight.mdsal.dom.api.DOMNotification;
16 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
17 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
18 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
19 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
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.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public final class NetconfDeviceSalFacade implements RemoteDeviceHandler, AutoCloseable {
37     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
38
39     private final RemoteDeviceId id;
40     private final NetconfDeviceSalProvider salProvider;
41     private final DataBroker dataBroker;
42     private final String topologyId;
43
44     private ListenerRegistration<LockChangeListener> listenerRegistration = null;
45
46     public NetconfDeviceSalFacade(final RemoteDeviceId id, final DOMMountPointService mountPointService,
47             final DataBroker dataBroker, final String topologyId) {
48         this(id, new NetconfDeviceSalProvider(id, mountPointService, dataBroker), dataBroker, topologyId);
49     }
50
51     @VisibleForTesting
52     NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceSalProvider salProvider,
53             final DataBroker dataBroker, final String topologyId) {
54         this.id = id;
55         this.salProvider = salProvider;
56         this.dataBroker = dataBroker;
57         this.topologyId = topologyId;
58     }
59
60     @Override
61     public synchronized void onNotification(final DOMNotification domNotification) {
62         salProvider.getMountInstance().publish(domNotification);
63     }
64
65     @Override
66     public synchronized void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
67             final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
68         final var mountContext = deviceSchema.mountContext();
69         final var modelContext = mountContext.getEffectiveModelContext();
70
71         final var deviceRpc = services.rpcs();
72         final var netconfDataTree = AbstractNetconfDataTreeService.of(id, mountContext, deviceRpc, sessionPreferences);
73         final var netconfDataBroker = new NetconfDeviceDataBroker(id, mountContext, deviceRpc, sessionPreferences);
74         registerLockListener(netconfDataBroker, netconfDataTree);
75
76         salProvider.getMountInstance().onTopologyDeviceConnected(modelContext, services, netconfDataBroker,
77             netconfDataTree);
78         salProvider.getTopologyDatastoreAdapter().updateDeviceData(true, deviceSchema.capabilities());
79     }
80
81     @Override
82     public synchronized void onDeviceDisconnected() {
83         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, NetconfDeviceCapabilities.empty());
84         salProvider.getMountInstance().onTopologyDeviceDisconnected();
85         closeLockChangeListener();
86     }
87
88     @Override
89     public synchronized void onDeviceFailed(final Throwable throwable) {
90         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
91         salProvider.getMountInstance().onTopologyDeviceDisconnected();
92         closeLockChangeListener();
93     }
94
95     @Override
96     public synchronized void close() {
97         closeGracefully(salProvider);
98         closeLockChangeListener();
99     }
100
101     @SuppressWarnings("checkstyle:IllegalCatch")
102     private void closeGracefully(final AutoCloseable resource) {
103         if (resource != null) {
104             try {
105                 resource.close();
106             } catch (final Exception e) {
107                 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
108             }
109         }
110     }
111
112     private void closeLockChangeListener() {
113         if (listenerRegistration != null) {
114             listenerRegistration.close();
115         }
116     }
117
118     private void registerLockListener(final NetconfDeviceDataBroker netconfDeviceDataBroker,
119                                       final NetconfDataTreeService netconfDataTreeService) {
120         listenerRegistration = dataBroker.registerDataTreeChangeListener(
121             DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
122                 InstanceIdentifier.create(NetconfNodeFieldsOptional.class)
123                 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
124                 .child(Node.class, new NodeKey(new NodeId(id.getName())))
125                 .child(DatastoreLock.class)),
126             new LockChangeListener(netconfDeviceDataBroker, netconfDataTreeService));
127     }
128 }