Add TLS data to call-home yang-models
[netconf.git] / netconf / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / IetfZeroTouchCallHomeServerProvider.java
index d2dbec65d27e856154f65c7fb36bd83583d1eb40..e2a99a34c34e683c7bcf607814ca67c34d6e79bf 100644 (file)
@@ -17,7 +17,6 @@ import java.net.InetSocketAddress;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
@@ -41,6 +40,11 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.Device;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.DeviceBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.DeviceKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.device.Transport;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.device.transport.Ssh;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.device.transport.SshBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.device.transport.ssh.SshClientParams;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.device.transport.ssh.SshClientParamsBuilder;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
@@ -147,9 +151,11 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
         // its created under CallHomeAuthorizationProvider.
         // Will have to redesign a bit here.
         // CallHomeAuthorization.
-        ReadTransaction roConfigTx = dataBroker.newReadOnlyTransaction();
-        ListenableFuture<Optional<AllowedDevices>> devicesFuture = roConfigTx
-                .read(LogicalDatastoreType.CONFIGURATION, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
+        final ListenableFuture<Optional<AllowedDevices>> devicesFuture;
+        try (ReadTransaction roConfigTx = dataBroker.newReadOnlyTransaction()) {
+            devicesFuture = roConfigTx.read(LogicalDatastoreType.CONFIGURATION,
+                IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
+        }
 
         Set<InstanceIdentifier<?>> deletedDevices = new HashSet<>();
         for (DataTreeModification<AllowedDevices> change : changes) {
@@ -199,9 +205,9 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
         }, MoreExecutors.directExecutor());
     }
 
-    private static List<Device> getReadDevices(final ListenableFuture<Optional<AllowedDevices>> devicesFuture)
+    private static Collection<Device> getReadDevices(final ListenableFuture<Optional<AllowedDevices>> devicesFuture)
             throws InterruptedException, ExecutionException {
-        return devicesFuture.get().map(AllowedDevices::getDevice).orElse(Collections.emptyList());
+        return devicesFuture.get().map(AllowedDevices::nonnullDevice).orElse(Collections.emptyMap()).values();
     }
 
     private void readAndUpdateStatus(final Device cfgDevice) throws InterruptedException, ExecutionException {
@@ -219,9 +225,8 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
             devStatus = new Device1Builder().setDeviceStatus(Device1.DeviceStatus.DISCONNECTED).build();
         }
 
-        tx.merge(LogicalDatastoreType.OPERATIONAL, deviceIID, new DeviceBuilder()
-            .addAugmentation(Device1.class, devStatus).setSshHostKey(cfgDevice.getSshHostKey())
-            .setUniqueId(cfgDevice.getUniqueId()).build());
+        final Device opDevice = createOperationalDevice(cfgDevice, devStatus);
+        tx.merge(LogicalDatastoreType.OPERATIONAL, deviceIID, opDevice);
         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
             @Override
             public void onSuccess(final CommitInfo result) {
@@ -234,4 +239,19 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
             }
         }, MoreExecutors.directExecutor());
     }
+
+    private Device createOperationalDevice(final Device cfgDevice, final Device1 devStatus) {
+        final DeviceBuilder deviceBuilder = new DeviceBuilder()
+            .addAugmentation(devStatus)
+            .setUniqueId(cfgDevice.getUniqueId());
+        if (cfgDevice.getTransport() instanceof Ssh) {
+            final String hostKey = ((Ssh) cfgDevice.getTransport()).getSshClientParams().getHostKey();
+            final SshClientParams params = new SshClientParamsBuilder().setHostKey(hostKey).build();
+            final Transport sshTransport = new SshBuilder().setSshClientParams(params).build();
+            deviceBuilder.setTransport(sshTransport);
+        } else if (cfgDevice.getSshHostKey() != null) {
+            deviceBuilder.setSshHostKey(cfgDevice.getSshHostKey());
+        }
+        return deviceBuilder.build();
+    }
 }