Eliminate LockChangeListener
[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.dom.api.DOMMountPointService;
13 import org.opendaylight.mdsal.dom.api.DOMNotification;
14 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
15 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
16 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
17 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
18 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
19 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public final class NetconfDeviceSalFacade implements RemoteDeviceHandler, AutoCloseable {
24     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
25
26     private final RemoteDeviceId id;
27     private final NetconfDeviceSalProvider salProvider;
28     private final boolean lockDatastore;
29
30     public NetconfDeviceSalFacade(final RemoteDeviceId id, final DOMMountPointService mountPointService,
31             final DataBroker dataBroker, final boolean lockDatastore) {
32         this(id, new NetconfDeviceSalProvider(id, mountPointService, dataBroker), lockDatastore);
33     }
34
35     @VisibleForTesting
36     NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceSalProvider salProvider,
37             final boolean lockDatastore) {
38         this.id = id;
39         this.salProvider = salProvider;
40         this.lockDatastore = lockDatastore;
41     }
42
43     @Override
44     public synchronized void onNotification(final DOMNotification domNotification) {
45         salProvider.getMountInstance().publish(domNotification);
46     }
47
48     @Override
49     public synchronized void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
50             final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
51         final var mountContext = deviceSchema.mountContext();
52         final var modelContext = mountContext.getEffectiveModelContext();
53
54         final var deviceRpc = services.rpcs();
55
56         final var netconfDataTree = AbstractNetconfDataTreeService.of(id, mountContext, deviceRpc, sessionPreferences,
57             lockDatastore);
58         final var netconfDataBroker = new NetconfDeviceDataBroker(id, mountContext, deviceRpc, sessionPreferences,
59             lockDatastore);
60
61         salProvider.getMountInstance().onTopologyDeviceConnected(modelContext, services, netconfDataBroker,
62             netconfDataTree);
63         salProvider.getTopologyDatastoreAdapter().updateDeviceData(true, deviceSchema.capabilities());
64     }
65
66     @Override
67     public synchronized void onDeviceDisconnected() {
68         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, NetconfDeviceCapabilities.empty());
69         salProvider.getMountInstance().onTopologyDeviceDisconnected();
70     }
71
72     @Override
73     public synchronized void onDeviceFailed(final Throwable throwable) {
74         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
75         salProvider.getMountInstance().onTopologyDeviceDisconnected();
76     }
77
78     @Override
79     public synchronized void close() {
80         closeGracefully(salProvider);
81     }
82
83     @SuppressWarnings("checkstyle:IllegalCatch")
84     private void closeGracefully(final AutoCloseable resource) {
85         if (resource != null) {
86             try {
87                 resource.close();
88             } catch (final Exception e) {
89                 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
90             }
91         }
92     }
93 }