Bump upstreams
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / spi / 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.client.mdsal.spi;
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.client.mdsal.NetconfDeviceSchema;
16 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
17 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceHandler;
18 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
19 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21
22 public class NetconfDeviceSalFacade implements RemoteDeviceHandler, AutoCloseable {
23     private final RemoteDeviceId id;
24     private final NetconfDeviceMount mount;
25     private final boolean lockDatastore;
26
27     public NetconfDeviceSalFacade(final RemoteDeviceId id, final DOMMountPointService mountPointService,
28             final YangInstanceIdentifier mountPath, final boolean lockDatastore) {
29         this(id, new NetconfDeviceMount(id, mountPointService, mountPath), lockDatastore);
30     }
31
32     @VisibleForTesting
33     NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceMount mount, final boolean lockDatastore) {
34         this.id = requireNonNull(id);
35         this.mount = requireNonNull(mount);
36         this.lockDatastore = lockDatastore;
37     }
38
39     @Override
40     public synchronized void onNotification(final DOMNotification domNotification) {
41         mount.publish(domNotification);
42     }
43
44     @Override
45     public synchronized void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
46             final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
47         final var mountContext = deviceSchema.mountContext();
48         final var modelContext = mountContext.modelContext();
49
50         final var deviceRpc = services.rpcs();
51
52         final var netconfDataTree = AbstractNetconfDataTreeService.of(id, mountContext, deviceRpc, sessionPreferences,
53             lockDatastore);
54         final var netconfDataBroker = new NetconfDeviceDataBroker(id, mountContext, deviceRpc, sessionPreferences,
55             lockDatastore);
56
57         mount.onDeviceConnected(modelContext, services, netconfDataBroker, netconfDataTree);
58     }
59
60     @Override
61     public synchronized void onDeviceDisconnected() {
62         mount.onDeviceDisconnected();
63     }
64
65     @Override
66     public synchronized void onDeviceFailed(final Throwable throwable) {
67         mount.onDeviceDisconnected();
68     }
69
70     @Override
71     public synchronized void close() {
72         mount.close();
73     }
74 }