2 * Copyright (c) 2016 Brocade Communication Systems and others. All rights reserved.
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
8 package org.opendaylight.netconf.callhome.mount;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import java.io.IOException;
17 import java.net.InetSocketAddress;
18 import java.util.Collection;
19 import java.util.HashSet;
21 import java.util.Optional;
23 import java.util.concurrent.ExecutionException;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.api.DataObjectModification;
26 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
27 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
28 import org.opendaylight.mdsal.binding.api.DataTreeModification;
29 import org.opendaylight.mdsal.binding.api.ReadTransaction;
30 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
31 import org.opendaylight.mdsal.binding.api.WriteTransaction;
32 import org.opendaylight.mdsal.common.api.CommitInfo;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServer;
35 import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServerBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.NetconfCallhomeServer;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.AllowedDevices;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.allowed.devices.Device;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.allowed.devices.Device.DeviceStatus;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.allowed.devices.DeviceBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.allowed.devices.DeviceKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.allowed.devices.device.Transport;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.allowed.devices.device.transport.Ssh;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.allowed.devices.device.transport.SshBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.allowed.devices.device.transport.Tls;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.allowed.devices.device.transport.ssh.SshClientParams;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.allowed.devices.device.transport.ssh.SshClientParamsBuilder;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.common.Uint16;
51 import org.osgi.service.component.annotations.Activate;
52 import org.osgi.service.component.annotations.Component;
53 import org.osgi.service.component.annotations.Deactivate;
54 import org.osgi.service.component.annotations.Reference;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 @Component(service = { })
59 public final class IetfZeroTouchCallHomeServerProvider
60 implements AutoCloseable, DataTreeChangeListener<AllowedDevices> {
61 private static final String APPNAME = "CallHomeServer";
62 static final InstanceIdentifier<AllowedDevices> ALL_DEVICES = InstanceIdentifier.create(NetconfCallhomeServer.class)
63 .child(AllowedDevices.class);
65 private static final Logger LOG = LoggerFactory.getLogger(IetfZeroTouchCallHomeServerProvider.class);
67 private final DataBroker dataBroker;
68 private final CallHomeMountFactory mountDispacher;
69 private final CallHomeAuthProviderImpl authProvider;
70 private final CallhomeStatusReporter statusReporter;
71 private final int port;
73 private NetconfCallHomeServer server;
74 private ListenerRegistration<IetfZeroTouchCallHomeServerProvider> listenerReg = null;
77 public IetfZeroTouchCallHomeServerProvider(@Reference final DataBroker dataBroker,
78 @Reference final CallHomeMountFactory mountDispacher) {
79 // FIXME: make this configurable
80 this(dataBroker, mountDispacher, Uint16.valueOf(4334));
83 public IetfZeroTouchCallHomeServerProvider(final DataBroker dataBroker,
84 final CallHomeMountFactory mountFactory, final Uint16 port) {
85 this.dataBroker = requireNonNull(dataBroker);
86 this.mountDispacher = requireNonNull(mountFactory);
88 LOG.info("Setting port for call home server to {}", port);
89 this.port = port.toJava();
91 // FIXME: these should be separate components
92 authProvider = new CallHomeAuthProviderImpl(dataBroker);
93 statusReporter = new CallhomeStatusReporter(dataBroker);
95 LOG.info("Initializing provider for {}", APPNAME);
97 // Register itself as a listener to changes in Devices subtree
100 } catch (IOException | Configuration.ConfigurationException e) {
101 LOG.error("Unable to successfully initialize", e);
105 listenerReg = dataBroker.registerDataTreeChangeListener(
106 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, ALL_DEVICES), this);
107 LOG.info("Initialization complete for {}", APPNAME);
110 private void initializeServer() throws IOException {
111 LOG.info("Initializing Call Home server instance");
112 NetconfCallHomeServerBuilder builder = new NetconfCallHomeServerBuilder(authProvider, mountDispacher,
115 builder.setBindAddress(new InetSocketAddress(port));
117 builder.setNettyGroup(new NioEventLoopGroup());
118 server = builder.build();
120 mountDispacher.createTopology();
121 LOG.info("Initialization complete for Call Home server instance");
126 public void close() {
127 authProvider.close();
128 statusReporter.close();
130 // FIXME unbind the server
131 if (listenerReg != null) {
134 if (server != null) {
138 LOG.info("Successfully closed provider for {}", APPNAME);
142 public void onDataTreeChanged(final Collection<DataTreeModification<AllowedDevices>> changes) {
143 // In case of any changes to the devices datatree, register the changed values with callhome server
144 // As of now, no way to add a new callhome client key to the CallHomeAuthorization instance since
145 // its created under CallHomeAuthorizationProvider.
146 // Will have to redesign a bit here.
147 // CallHomeAuthorization.
148 final ListenableFuture<Optional<AllowedDevices>> devicesFuture;
149 try (ReadTransaction roConfigTx = dataBroker.newReadOnlyTransaction()) {
150 devicesFuture = roConfigTx.read(LogicalDatastoreType.CONFIGURATION,
151 IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
154 Set<InstanceIdentifier<?>> deletedDevices = new HashSet<>();
155 for (DataTreeModification<AllowedDevices> change : changes) {
156 DataObjectModification<AllowedDevices> rootNode = change.getRootNode();
157 switch (rootNode.getModificationType()) {
159 deletedDevices.add(change.getRootPath().getRootIdentifier());
166 handleDeletedDevices(deletedDevices);
169 for (Device confDevice : getReadDevices(devicesFuture)) {
170 readAndUpdateStatus(confDevice);
172 } catch (ExecutionException | InterruptedException e) {
173 LOG.error("Error trying to read the allowlist devices", e);
177 private void handleDeletedDevices(final Set<InstanceIdentifier<?>> deletedDevices) {
178 if (deletedDevices.isEmpty()) {
182 WriteTransaction opTx = dataBroker.newWriteOnlyTransaction();
184 for (InstanceIdentifier<?> removedIID : deletedDevices) {
185 LOG.info("Deleting the entry for callhome device {}", removedIID);
186 opTx.delete(LogicalDatastoreType.OPERATIONAL, removedIID);
189 opTx.commit().addCallback(new FutureCallback<CommitInfo>() {
191 public void onSuccess(final CommitInfo result) {
192 LOG.debug("Device deletions committed");
196 public void onFailure(final Throwable cause) {
197 LOG.warn("Failed to commit device deletions", cause);
199 }, MoreExecutors.directExecutor());
202 private static Collection<Device> getReadDevices(final ListenableFuture<Optional<AllowedDevices>> devicesFuture)
203 throws InterruptedException, ExecutionException {
204 return devicesFuture.get().map(AllowedDevices::nonnullDevice).orElse(Map.of()).values();
207 private void readAndUpdateStatus(final Device cfgDevice) throws InterruptedException, ExecutionException {
208 InstanceIdentifier<Device> deviceIID = InstanceIdentifier.create(NetconfCallhomeServer.class)
209 .child(AllowedDevices.class).child(Device.class, new DeviceKey(cfgDevice.getUniqueId()));
211 ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
212 ListenableFuture<Optional<Device>> deviceFuture = tx.read(LogicalDatastoreType.OPERATIONAL, deviceIID);
214 final DeviceStatus devStatus = deviceFuture.get()
215 .map(Device::getDeviceStatus)
216 .orElse(DeviceStatus.DISCONNECTED);
218 final Device opDevice = createOperationalDevice(cfgDevice, devStatus);
219 tx.merge(LogicalDatastoreType.OPERATIONAL, deviceIID, opDevice);
220 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
222 public void onSuccess(final CommitInfo result) {
223 LOG.debug("Device {} status update committed", cfgDevice.key());
227 public void onFailure(final Throwable cause) {
228 LOG.warn("Failed to commit device {} status update", cfgDevice.key(), cause);
230 }, MoreExecutors.directExecutor());
233 private static Device createOperationalDevice(final Device cfgDevice, final DeviceStatus devStatus) {
234 final DeviceBuilder deviceBuilder = new DeviceBuilder()
235 .setUniqueId(cfgDevice.getUniqueId())
236 .setDeviceStatus(devStatus);
237 if (cfgDevice.getTransport() instanceof Ssh ssh) {
238 final String hostKey = ssh.getSshClientParams().getHostKey();
239 final SshClientParams params = new SshClientParamsBuilder().setHostKey(hostKey).build();
240 final Transport sshTransport = new SshBuilder().setSshClientParams(params).build();
241 deviceBuilder.setTransport(sshTransport);
242 } else if (cfgDevice.getTransport() instanceof Tls) {
243 deviceBuilder.setTransport(cfgDevice.getTransport());
244 } else if (cfgDevice.getSshHostKey() != null) {
245 deviceBuilder.setSshHostKey(cfgDevice.getSshHostKey());
247 return deviceBuilder.build();