Update MRI projects for Aluminium
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / NetconfDeviceSimulator.java
index 5409e242d17c1cd6b168112b48989588dd237219..dba9cb0da18354a5afc55912ca3dfa3369f59f90 100644 (file)
@@ -5,14 +5,10 @@
  * 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;
@@ -29,16 +25,16 @@ 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;
@@ -46,25 +42,25 @@ 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.monitoring.osgi.NetconfMonitoringActivator;
-import org.opendaylight.netconf.monitoring.osgi.NetconfMonitoringOperationService;
 import org.opendaylight.netconf.ssh.SshProxyServer;
 import org.opendaylight.netconf.ssh.SshProxyServerConfiguration;
 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
 import org.opendaylight.netconf.test.tool.config.Configuration;
 import org.opendaylight.netconf.test.tool.customrpc.SettableOperationProvider;
+import org.opendaylight.netconf.test.tool.monitoring.NetconfMonitoringOperationService;
+import org.opendaylight.netconf.test.tool.monitoring.NetconfMonitoringOperationServiceFactory;
 import org.opendaylight.netconf.test.tool.operations.DefaultOperationsCreator;
 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.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;
@@ -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,
@@ -158,8 +154,8 @@ public class NetconfDeviceSimulator implements Closeable {
         }
 
 
-        final NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory monitoringService =
-                new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
+        final NetconfMonitoringOperationServiceFactory monitoringService =
+                new NetconfMonitoringOperationServiceFactory(
                         new NetconfMonitoringOperationService(monitoringService1));
         aggregatedNetconfOperationServiceFactory.onAddNetconfOperationServiceFactory(operationProvider);
         aggregatedNetconfOperationServiceFactory.onAddNetconfOperationServiceFactory(monitoringService);
@@ -187,15 +183,15 @@ 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 {
             group = AsynchronousChannelGroup.withThreadPool(nioExecutor);
-        } catch (IOException e) {
+        } catch (final IOException e) {
             throw new IllegalStateException("Failed to create group", e);
         }
 
@@ -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();
+        final Set<SourceIdentifier> loadedSources = new HashSet<>();
         consumer.registerSchemaSourceListener(TextToASTTransformer.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) {
@@ -330,20 +320,20 @@ public class NetconfDeviceSimulator implements Closeable {
         }
 
         configuration.getDefaultYangResources().forEach(r -> {
-            SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create(r.getModuleName(),
+            final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create(r.getModuleName(),
                 Revision.ofNullable(r.getRevision()));
             registerSource(consumer, r.getResourcePath(), sourceIdentifier);
         });
 
         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()) {
@@ -363,7 +353,7 @@ public class NetconfDeviceSimulator implements Closeable {
                 consumer.getSchemaSource(moduleSourceIdentifier, YangTextSchemaSource.class).get().read());
             capabilities.add(new YangModuleCapability(module, moduleContent));
             //IOException would be thrown in creating SchemaContext already
-        } catch (ExecutionException | InterruptedException | IOException e) {
+        } catch (final ExecutionException | InterruptedException | IOException e) {
             throw new RuntimeException("Cannot retrieve schema source for module "
                 + moduleSourceIdentifier.toString() + " from schema repository", e);
         }
@@ -398,7 +388,7 @@ public class NetconfDeviceSimulator implements Closeable {
         for (final SshProxyServer sshWrapper : sshWrappers) {
             try {
                 sshWrapper.close();
-            } catch (IOException e) {
+            } catch (final IOException e) {
                 LOG.debug("Wrapper {} failed to close", sshWrapper, e);
             }
         }