Move NetconfDeviceTopologyAdapter
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
14 import org.opendaylight.mdsal.dom.api.DOMNotification;
15 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
16 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
17 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
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 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 boolean lockDatastore) {
32         this(id, new NetconfDeviceSalProvider(id, mountPointService), lockDatastore);
33     }
34
35     @VisibleForTesting
36     NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceSalProvider salProvider,
37             final boolean lockDatastore) {
38         this.id = requireNonNull(id);
39         this.salProvider = requireNonNull(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     }
64
65     @Override
66     public synchronized void onDeviceDisconnected() {
67         salProvider.getMountInstance().onTopologyDeviceDisconnected();
68     }
69
70     @Override
71     public synchronized void onDeviceFailed(final Throwable throwable) {
72         salProvider.getMountInstance().onTopologyDeviceDisconnected();
73     }
74
75     @Override
76     public synchronized void close() {
77         closeGracefully(salProvider);
78     }
79
80     @SuppressWarnings("checkstyle:IllegalCatch")
81     private void closeGracefully(final AutoCloseable resource) {
82         if (resource != null) {
83             try {
84                 resource.close();
85             } catch (final Exception e) {
86                 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
87             }
88         }
89     }
90 }