a75775e0292a2511110e9414380ed8e8b07e5b82
[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.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.rfc8528.data.api.MountPointContext;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public final class NetconfDeviceSalFacade implements RemoteDeviceHandler, AutoCloseable {
39     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
40
41     private final RemoteDeviceId id;
42     private final NetconfDeviceSalProvider salProvider;
43     private final DataBroker dataBroker;
44     private final String topologyId;
45
46     private ListenerRegistration<LockChangeListener> listenerRegistration = null;
47
48     public NetconfDeviceSalFacade(final RemoteDeviceId id, final DOMMountPointService mountPointService,
49             final DataBroker dataBroker, final String topologyId) {
50         this(id, new NetconfDeviceSalProvider(id, mountPointService, dataBroker), dataBroker, topologyId);
51     }
52
53     @VisibleForTesting
54     NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceSalProvider salProvider,
55             final DataBroker dataBroker, final String topologyId) {
56         this.id = id;
57         this.salProvider = salProvider;
58         this.dataBroker = dataBroker;
59         this.topologyId = topologyId;
60     }
61
62     @Override
63     public synchronized void onNotification(final DOMNotification domNotification) {
64         salProvider.getMountInstance().publish(domNotification);
65     }
66
67     @Override
68     public synchronized void onDeviceConnected(final MountPointContext mountContext,
69                                                final NetconfSessionPreferences netconfSessionPreferences,
70                                                final DOMRpcService deviceRpc, final DOMActionService deviceAction) {
71         final EffectiveModelContext schemaContext = mountContext.getEffectiveModelContext();
72         final NetconfDeviceDataBroker netconfDeviceDataBroker =
73                 new NetconfDeviceDataBroker(id, mountContext, deviceRpc, netconfSessionPreferences);
74         final NetconfDataTreeService netconfService =
75                 AbstractNetconfDataTreeService.of(id, mountContext, deviceRpc, netconfSessionPreferences);
76         registerLockListener(netconfDeviceDataBroker, netconfService);
77         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
78
79         salProvider.getMountInstance()
80                 .onTopologyDeviceConnected(schemaContext, netconfDeviceDataBroker, netconfService,
81                         deviceRpc, notificationService, deviceAction);
82         salProvider.getTopologyDatastoreAdapter()
83                 .updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
84     }
85
86     @Override
87     public synchronized void onDeviceDisconnected() {
88         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
89         salProvider.getMountInstance().onTopologyDeviceDisconnected();
90         closeLockChangeListener();
91     }
92
93     @Override
94     public synchronized void onDeviceFailed(final Throwable throwable) {
95         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
96         salProvider.getMountInstance().onTopologyDeviceDisconnected();
97         closeLockChangeListener();
98     }
99
100     @Override
101     public synchronized void close() {
102         closeGracefully(salProvider);
103         closeLockChangeListener();
104     }
105
106     @SuppressWarnings("checkstyle:IllegalCatch")
107     private void closeGracefully(final AutoCloseable resource) {
108         if (resource != null) {
109             try {
110                 resource.close();
111             } catch (final Exception e) {
112                 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
113             }
114         }
115     }
116
117     private void closeLockChangeListener() {
118         if (listenerRegistration != null) {
119             listenerRegistration.close();
120         }
121     }
122
123     private void registerLockListener(final NetconfDeviceDataBroker netconfDeviceDataBroker,
124                                       final NetconfDataTreeService netconfDataTreeService) {
125         listenerRegistration = dataBroker.registerDataTreeChangeListener(
126                 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, createTopologyListPath()),
127                 new LockChangeListener(netconfDeviceDataBroker, netconfDataTreeService));
128     }
129
130     private InstanceIdentifier<DatastoreLock> createTopologyListPath() {
131         return InstanceIdentifier.create(NetconfNodeFieldsOptional.class)
132                 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
133                 .child(Node.class, new NodeKey(new NodeId(id.getName())))
134                 .child(DatastoreLock.class);
135
136     }
137 }