Prevent NPE for Credentials
[netconf.git] / apps / netconf-topology / src / main / java / org / opendaylight / netconf / topology / spi / NetconfTopologyDeviceSalFacade.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.topology.spi;
9
10 import java.util.concurrent.ExecutionException;
11 import org.opendaylight.mdsal.binding.api.DataBroker;
12 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
13 import org.opendaylight.netconf.client.mdsal.NetconfDeviceCapabilities;
14 import org.opendaylight.netconf.client.mdsal.NetconfDeviceSchema;
15 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
16 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
17 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices;
18 import org.opendaylight.netconf.client.mdsal.spi.NetconfDeviceSalFacade;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.credentials.Credentials;
20
21 /**
22  * {@link NetconfDeviceSalFacade} specialization for netconf topology.
23  */
24 public class NetconfTopologyDeviceSalFacade extends NetconfDeviceSalFacade {
25     private final NetconfDeviceTopologyAdapter datastoreAdapter;
26
27     /**
28      * NetconfTopologyDeviceSalFacade is a specialization of NetconfDeviceSalFacade
29      * for the netconf topology. It handles the lifecycle and data updates for
30      * NETCONF devices within the topology.
31      *
32      * @param id                the unique identifier for the remote device
33      * @param credentials       the credentials used to authenticate the remote device
34      * @param mountPointService the mount point service for managing mount points
35      * @param lockDatastore     a flag indicating whether the datastore should be locked
36      * @param dataBroker        the data broker for accessing and modifying data in the data store
37      */
38     public NetconfTopologyDeviceSalFacade(final RemoteDeviceId id, final Credentials credentials,
39             final DOMMountPointService mountPointService, final boolean lockDatastore, final DataBroker dataBroker) {
40         super(id, mountPointService, NetconfNodeUtils.defaultTopologyMountPath(id), lockDatastore);
41         datastoreAdapter = new NetconfDeviceTopologyAdapter(dataBroker, NetconfNodeUtils.DEFAULT_TOPOLOGY_IID, id,
42             credentials);
43     }
44
45     @Override
46     public synchronized void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
47             final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
48         super.onDeviceConnected(deviceSchema, sessionPreferences, services);
49         datastoreAdapter.updateDeviceData(true, deviceSchema.capabilities(), sessionPreferences.sessionId());
50     }
51
52     @Override
53     public synchronized void onDeviceDisconnected() {
54         datastoreAdapter.updateDeviceData(false, NetconfDeviceCapabilities.empty(), null);
55         super.onDeviceDisconnected();
56     }
57
58     @Override
59     public synchronized void onDeviceFailed(final Throwable throwable) {
60         datastoreAdapter.setDeviceAsFailed(throwable);
61         super.onDeviceFailed(throwable);
62     }
63
64     @Override
65     public synchronized void close() {
66         final var future = datastoreAdapter.shutdown();
67         super.close();
68
69         try {
70             future.get();
71         } catch (InterruptedException e) {
72             Thread.currentThread().interrupt();
73             throw new IllegalStateException("Interrupted while waiting for datastore adapter shutdown", e);
74         } catch (ExecutionException e) {
75             throw new IllegalStateException("Datastore adapter shutdown failed", e);
76         }
77     }
78 }