Use Java 11 HttpClient
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / NetconfDeviceSimulator.java
index 893582f4332b31672e20cee503187aed0f167559..3cb2e19222e530ca1976f9d00bbc1b5552e191ff 100644 (file)
@@ -5,14 +5,9 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.test.tool;
 
-import com.google.common.base.MoreObjects.ToStringHelper;
-import com.google.common.base.Optional;
 import com.google.common.collect.Collections2;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -23,32 +18,32 @@ import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.util.HashedWheelTimer;
 import java.io.Closeable;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.BindException;
 import java.net.Inet4Address;
 import java.net.InetSocketAddress;
 import java.net.UnknownHostException;
 import java.nio.channels.AsynchronousChannelGroup;
-import java.nio.file.Files;
-import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
-import org.apache.sshd.common.keyprovider.KeyPairProvider;
-import org.apache.sshd.common.util.security.SecurityUtils;
-import org.apache.sshd.common.util.threads.ThreadUtils;
 import org.opendaylight.netconf.api.capability.BasicCapability;
 import org.opendaylight.netconf.api.capability.Capability;
 import org.opendaylight.netconf.api.capability.YangModuleCapability;
 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
 import org.opendaylight.netconf.impl.NetconfServerDispatcherImpl;
 import org.opendaylight.netconf.impl.NetconfServerSessionNegotiatorFactory;
+import org.opendaylight.netconf.impl.ServerChannelInitializer;
 import org.opendaylight.netconf.impl.SessionIdProvider;
 import org.opendaylight.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.netconf.shaded.sshd.common.keyprovider.KeyPairProvider;
+import org.opendaylight.netconf.shaded.sshd.common.util.threads.ThreadUtils;
 import org.opendaylight.netconf.ssh.SshProxyServer;
 import org.opendaylight.netconf.ssh.SshProxyServerConfiguration;
 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
@@ -61,19 +56,20 @@ import org.opendaylight.netconf.test.tool.operations.OperationsProvider;
 import org.opendaylight.netconf.test.tool.rpchandler.SettableOperationRpcProvider;
 import org.opendaylight.netconf.test.tool.schemacache.SchemaSourceCache;
 import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.ModuleLike;
+import org.opendaylight.yangtools.yang.model.api.Submodule;
 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.model.repo.fs.FilesystemSchemaSourceCache;
 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
-import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
-import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -84,12 +80,12 @@ public class NetconfDeviceSimulator implements Closeable {
 
     private final NioEventLoopGroup nettyThreadgroup;
     private final HashedWheelTimer hashedWheelTimer;
-    private final List<Channel> devicesChannels = Lists.newArrayList();
-    private final List<SshProxyServer> sshWrappers = Lists.newArrayList();
+    private final List<Channel> devicesChannels = new ArrayList<>();
+    private final List<SshProxyServer> sshWrappers = new ArrayList<>();
     private final ScheduledExecutorService minaTimerExecutor;
     private final ExecutorService nioExecutor;
     private final Configuration configuration;
-    private SchemaContext schemaContext;
+    private EffectiveModelContext schemaContext;
 
     private boolean sendFakeSchema = false;
 
@@ -106,7 +102,7 @@ public class NetconfDeviceSimulator implements Closeable {
     private NetconfServerDispatcherImpl createDispatcher(final Set<Capability> capabilities,
             final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
 
-        final Set<Capability> transformedCapabilities = Sets.newHashSet(Collections2.transform(capabilities, input -> {
+        final Set<Capability> transformedCapabilities = new HashSet<>(Collections2.transform(capabilities, input -> {
             if (sendFakeSchema) {
                 sendFakeSchema = false;
                 return new FakeCapability((YangModuleCapability) input);
@@ -128,8 +124,8 @@ public class NetconfDeviceSimulator implements Closeable {
                 configuration.getGenerateConfigsTimeout(),
                 monitoringService1, serverCapabilities);
 
-        final NetconfServerDispatcherImpl.ServerChannelInitializer serverChannelInitializer =
-            new NetconfServerDispatcherImpl.ServerChannelInitializer(serverNegotiatorFactory);
+        final ServerChannelInitializer serverChannelInitializer =
+            new ServerChannelInitializer(serverNegotiatorFactory);
         return new NetconfServerDispatcherImpl(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
     }
 
@@ -148,8 +144,8 @@ public class NetconfDeviceSimulator implements Closeable {
         } else if (configuration.isXmlConfigurationProvided()) {
             LOG.info("using SimulatedOperationProvider.");
             operationProvider = new SimulatedOperationProvider(idProvider, transformedCapabilities,
-                    Optional.fromNullable(configuration.getNotificationFile()),
-                    Optional.fromNullable(configuration.getInitialConfigXMLFile()));
+                    Optional.ofNullable(configuration.getNotificationFile()),
+                    Optional.ofNullable(configuration.getInitialConfigXMLFile()));
         } else {
             LOG.info("using OperationsProvider.");
             operationProvider = new OperationsProvider(idProvider, transformedCapabilities,
@@ -187,10 +183,10 @@ public class NetconfDeviceSimulator implements Closeable {
 
         int currentPort = configuration.getStartingPort();
 
-        final List<Integer> openDevices = Lists.newArrayList();
+        final List<Integer> openDevices = new ArrayList<>();
 
         // Generate key to temp folder
-        final KeyPairProvider keyPairProvider = getPemGeneratorHostKeyProvider();
+        final KeyPairProvider keyPairProvider = new VirtualKeyPairProvider();
 
         final AsynchronousChannelGroup group;
         try {
@@ -256,7 +252,7 @@ public class NetconfDeviceSimulator implements Closeable {
                     continue;
                 }
 
-                LOG.debug("Simulated TCP device started on {}", address);
+                LOG.debug("Simulated TCP device started on {}", server.channel().localAddress());
             }
 
             devicesChannels.add(server.channel());
@@ -287,22 +283,14 @@ public class NetconfDeviceSimulator implements Closeable {
                 .createSshProxyServerConfiguration();
     }
 
-    private static KeyPairProvider getPemGeneratorHostKeyProvider() {
-        try {
-            final Path tempFile = Files.createTempFile("tempKeyNetconfTest", "suffix");
-            return SecurityUtils.createGeneratorHostKeyProvider(tempFile.toAbsolutePath());
-        } catch (final IOException e) {
-            LOG.error("Unable to generate PEM key", e);
-            throw new RuntimeException(e);
-        }
-    }
-
     private Set<Capability> parseSchemasToModuleCapabilities(final SharedSchemaRepository consumer) {
-        final Set<SourceIdentifier> loadedSources = Sets.newHashSet();
-        consumer.registerSchemaSourceListener(TextToASTTransformer.create(consumer, consumer));
+        final Set<SourceIdentifier> loadedSources = new HashSet<>();
+        consumer.registerSchemaSourceListener(TextToIRTransformer.create(consumer, consumer));
         consumer.registerSchemaSourceListener(new SchemaSourceListener() {
             @Override
-            public void schemaSourceEncountered(final SchemaSourceRepresentation schemaSourceRepresentation) {}
+            public void schemaSourceEncountered(final SchemaSourceRepresentation schemaSourceRepresentation) {
+
+            }
 
             @Override
             public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> potentialSchemaSources) {
@@ -312,7 +300,9 @@ public class NetconfDeviceSimulator implements Closeable {
             }
 
             @Override
-            public void schemaSourceUnregistered(final PotentialSchemaSource<?> potentialSchemaSource) {}
+            public void schemaSourceUnregistered(final PotentialSchemaSource<?> potentialSchemaSource) {
+
+            }
         });
 
         if (configuration.getSchemasDir() != null) {
@@ -337,16 +327,16 @@ public class NetconfDeviceSimulator implements Closeable {
 
         try {
             //necessary for creating mdsal data stores and operations
-            this.schemaContext = consumer.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT)
-                .createSchemaContext(loadedSources).get();
+            this.schemaContext = consumer.createEffectiveModelContextFactory()
+                    .createEffectiveModelContext(loadedSources).get();
         } catch (final InterruptedException | ExecutionException e) {
             throw new RuntimeException("Cannot parse schema context", e);
         }
 
-        final Set<Capability> capabilities = Sets.newHashSet();
+        final Set<Capability> capabilities = new HashSet<>();
 
         for (final Module module : schemaContext.getModules()) {
-            for (final Module subModule : module.getSubmodules()) {
+            for (final Submodule subModule : module.getSubmodules()) {
                 addModuleCapability(consumer, capabilities, subModule);
             }
             addModuleCapability(consumer, capabilities, module);
@@ -355,7 +345,7 @@ public class NetconfDeviceSimulator implements Closeable {
     }
 
     private static void addModuleCapability(final SharedSchemaRepository consumer, final Set<Capability> capabilities,
-                                     final Module module) {
+                                            final ModuleLike module) {
         final SourceIdentifier moduleSourceIdentifier = RevisionSourceIdentifier.create(module.getName(),
             module.getRevision());
         try {
@@ -370,19 +360,11 @@ public class NetconfDeviceSimulator implements Closeable {
     }
 
     private static void registerSource(final SharedSchemaRepository consumer, final String resource,
-                                final SourceIdentifier sourceId) {
-        consumer.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(new YangTextSchemaSource(sourceId) {
-            @Override
-            protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
-                return toStringHelper;
-            }
-
-            @Override
-            public InputStream openStream() {
-                return getClass().getResourceAsStream(resource);
-            }
-        }), PotentialSchemaSource.create(sourceId, YangTextSchemaSource.class,
-            PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
+            final SourceIdentifier sourceId) {
+        consumer.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(
+            YangTextSchemaSource.forResource(NetconfDeviceSimulator.class, resource)),
+            PotentialSchemaSource.create(sourceId, YangTextSchemaSource.class,
+                PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
     }
 
     private static InetSocketAddress getAddress(final String ip, final int port) {