Add RemoteDeviceServices
[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         // FIXME: instanceof DOMRpcService, as it might be others
73         final var netconfDataBroker = new NetconfDeviceDataBroker(id, mountContext, deviceRpc, sessionPreferences);
74         final var netconfDataTree = AbstractNetconfDataTreeService.of(id, mountContext, deviceRpc, sessionPreferences);
75         registerLockListener(netconfDataBroker, netconfDataTree);
76
77         salProvider.getMountInstance().onTopologyDeviceConnected(modelContext, services, netconfDataBroker,
78             netconfDataTree);
79         salProvider.getTopologyDatastoreAdapter().updateDeviceData(true, deviceSchema.capabilities());
80     }
81
82     @Override
83     public synchronized void onDeviceDisconnected() {
84         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, NetconfDeviceCapabilities.empty());
85         salProvider.getMountInstance().onTopologyDeviceDisconnected();
86         closeLockChangeListener();
87     }
88
89     @Override
90     public synchronized void onDeviceFailed(final Throwable throwable) {
91         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
92         salProvider.getMountInstance().onTopologyDeviceDisconnected();
93         closeLockChangeListener();
94     }
95
96     @Override
97     public synchronized void close() {
98         closeGracefully(salProvider);
99         closeLockChangeListener();
100     }
101
102     @SuppressWarnings("checkstyle:IllegalCatch")
103     private void closeGracefully(final AutoCloseable resource) {
104         if (resource != null) {
105             try {
106                 resource.close();
107             } catch (final Exception e) {
108                 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
109             }
110         }
111     }
112
113     private void closeLockChangeListener() {
114         if (listenerRegistration != null) {
115             listenerRegistration.close();
116         }
117     }
118
119     private void registerLockListener(final NetconfDeviceDataBroker netconfDeviceDataBroker,
120                                       final NetconfDataTreeService netconfDataTreeService) {
121         listenerRegistration = dataBroker.registerDataTreeChangeListener(
122                 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, createTopologyListPath()),
123                 new LockChangeListener(netconfDeviceDataBroker, netconfDataTreeService));
124     }
125
126     private InstanceIdentifier<DatastoreLock> createTopologyListPath() {
127         return InstanceIdentifier.create(NetconfNodeFieldsOptional.class)
128                 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
129                 .child(Node.class, new NodeKey(new NodeId(id.getName())))
130                 .child(DatastoreLock.class);
131
132     }
133 }