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