Netconf Device Notification
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / NetconfDeviceSimulator.java
index ab45f2d73743d5ce78823da1adeb07a3161b7630..0a92bc109ceee289be25725c20782bd7c35283a7 100644 (file)
@@ -5,15 +5,11 @@
  * 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;
-import com.google.common.base.Optional;
+import static java.util.Objects.requireNonNullElseGet;
+
 import com.google.common.collect.Collections2;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
-import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import io.netty.channel.Channel;
@@ -23,89 +19,88 @@ 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.file.Files;
-import java.nio.file.Path;
+import java.nio.channels.AsynchronousChannelGroup;
+import java.nio.charset.StandardCharsets;
+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.util.ThreadUtils;
-import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
-import org.opendaylight.controller.config.util.capability.BasicCapability;
-import org.opendaylight.controller.config.util.capability.Capability;
-import org.opendaylight.controller.config.util.capability.YangModuleCapability;
+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.monitoring.osgi.NetconfMonitoringActivator;
-import org.opendaylight.netconf.monitoring.osgi.NetconfMonitoringOperationService;
+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;
 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.SimpleDateFormatUtil;
+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.SchemaResolutionException;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
+import org.opendaylight.yangtools.yang.model.api.ModuleLike;
+import org.opendaylight.yangtools.yang.model.api.Submodule;
 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.util.TextToASTTransformer;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class NetconfDeviceSimulator implements Closeable {
-
     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSimulator.class);
 
     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;
 
-    public NetconfDeviceSimulator(Configuration configuration) {
+    public NetconfDeviceSimulator(final Configuration configuration) {
         this.configuration = configuration;
-        this.nettyThreadgroup = new NioEventLoopGroup();
-        this.hashedWheelTimer = new HashedWheelTimer();
-        this.minaTimerExecutor = Executors.newScheduledThreadPool(configuration.getThreadPoolSize(),
+        nettyThreadgroup = new NioEventLoopGroup();
+        hashedWheelTimer = new HashedWheelTimer();
+        minaTimerExecutor = Executors.newScheduledThreadPool(configuration.getThreadPoolSize(),
                 new ThreadFactoryBuilder().setNameFormat("netconf-ssh-server-mina-timers-%d").build());
-        this.nioExecutor = ThreadUtils
-                .newFixedThreadPool("netconf-ssh-server-nio-group", configuration.getThreadPoolSize());
+        nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", configuration.getThreadPoolSize());
     }
 
     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);
@@ -127,8 +122,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);
     }
 
@@ -147,18 +142,22 @@ 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 if (configuration.isNotificationsSupported()) {
+            LOG.info("using SimulatedOperationProvider.");
+            operationProvider = new SimulatedOperationProvider(idProvider, transformedCapabilities,
+                    Optional.ofNullable(configuration.getNotificationFile()),
+                    Optional.empty());
         } else {
             LOG.info("using OperationsProvider.");
             operationProvider = new OperationsProvider(idProvider, transformedCapabilities,
-                configuration.getOperationsCreator() != null ? configuration.getOperationsCreator()
-                    : DefaultOperationsCreator.getDefaultOperationServiceCreator(idProvider.getCurrentSessionId()));
+                requireNonNullElseGet(configuration.getOperationsCreator(),
+                    () -> new DefaultOperationsCreator(idProvider.getCurrentSessionId())));
         }
 
-
-        final NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory monitoringService =
-                new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
+        final NetconfMonitoringOperationServiceFactory monitoringService =
+                new NetconfMonitoringOperationServiceFactory(
                         new NetconfMonitoringOperationService(monitoringService1));
         aggregatedNetconfOperationServiceFactory.onAddNetconfOperationServiceFactory(operationProvider);
         aggregatedNetconfOperationServiceFactory.onAddNetconfOperationServiceFactory(monitoringService);
@@ -175,8 +174,9 @@ public class NetconfDeviceSimulator implements Closeable {
     }
 
     public List<Integer> start() {
+        final var proto = configuration.isSsh() ? "SSH" : "TCP";
         LOG.info("Starting {}, {} simulated devices starting on port {}",
-                configuration.getDeviceCount(), configuration.isSsh() ? "SSH" : "TCP", configuration.getStartingPort());
+                configuration.getDeviceCount(), proto, configuration.getStartingPort());
 
         final SharedSchemaRepository schemaRepo = new SharedSchemaRepository("netconf-simulator");
         final Set<Capability> capabilities = parseSchemasToModuleCapabilities(schemaRepo);
@@ -186,10 +186,17 @@ 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 PEMGeneratorHostKeyProvider keyPairProvider = getPemGeneratorHostKeyProvider();
+        final KeyPairProvider keyPairProvider = new VirtualKeyPairProvider();
+
+        final AsynchronousChannelGroup group;
+        try {
+            group = AsynchronousChannelGroup.withThreadPool(nioExecutor);
+        } catch (final IOException e) {
+            throw new IllegalStateException("Failed to create group", e);
+        }
 
         for (int i = 0; i < configuration.getDeviceCount(); i++) {
             if (currentPort > 65535) {
@@ -206,7 +213,7 @@ public class NetconfDeviceSimulator implements Closeable {
                 server = dispatcher.createLocalServer(tcpLocalAddress);
                 try {
                     final SshProxyServer sshServer = new SshProxyServer(
-                        minaTimerExecutor, nettyThreadgroup, nioExecutor);
+                        minaTimerExecutor, nettyThreadgroup, group);
                     sshServer.bind(getSshConfiguration(bindingAddress, tcpLocalAddress, keyPairProvider));
                     sshWrappers.add(sshServer);
                 } catch (final BindException e) {
@@ -227,7 +234,7 @@ public class NetconfDeviceSimulator implements Closeable {
                 try {
                     server.get();
                 } catch (final InterruptedException e) {
-                    throw new RuntimeException(e);
+                    throw new IllegalStateException("Interrupted while waiting for server", e);
                 } catch (final ExecutionException e) {
                     LOG.warn("Cannot start ssh simulated device on {}, skipping", address, e);
                     continue;
@@ -242,13 +249,13 @@ public class NetconfDeviceSimulator implements Closeable {
                 try {
                     server.get();
                 } catch (final InterruptedException e) {
-                    throw new RuntimeException(e);
+                    throw new IllegalStateException("Interrupted while waiting for server", e);
                 } catch (final ExecutionException e) {
                     LOG.warn("Cannot start tcp simulated device on {}, skipping", address, e);
                     continue;
                 }
 
-                LOG.debug("Simulated TCP device started on {}", address);
+                LOG.debug("Simulated TCP device started on {}", server.channel().localAddress());
             }
 
             devicesChannels.add(server.channel());
@@ -268,32 +275,25 @@ public class NetconfDeviceSimulator implements Closeable {
     }
 
     private SshProxyServerConfiguration getSshConfiguration(final InetSocketAddress bindingAddress,
-            final LocalAddress tcpLocalAddress, final PEMGeneratorHostKeyProvider keyPairProvider) throws IOException {
+            final LocalAddress tcpLocalAddress, final KeyPairProvider keyPairProvider) {
         return new SshProxyServerConfigurationBuilder()
                 .setBindingAddress(bindingAddress)
                 .setLocalAddress(tcpLocalAddress)
-                .setAuthenticator((username, password) -> true)
+                .setAuthenticator(configuration.getAuthProvider())
+                .setPublickeyAuthenticator(configuration.getPublickeyAuthenticator())
                 .setKeyPairProvider(keyPairProvider)
                 .setIdleTimeout(Integer.MAX_VALUE)
                 .createSshProxyServerConfiguration();
     }
 
-    private PEMGeneratorHostKeyProvider getPemGeneratorHostKeyProvider() {
-        try {
-            final Path tempFile = Files.createTempFile("tempKeyNetconfTest", "suffix");
-            return new PEMGeneratorHostKeyProvider(tempFile.toAbsolutePath().toString(), "RSA", 4096);
-        } 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) {
@@ -303,7 +303,9 @@ public class NetconfDeviceSimulator implements Closeable {
             }
 
             @Override
-            public void schemaSourceUnregistered(final PotentialSchemaSource<?> potentialSchemaSource) {}
+            public void schemaSourceUnregistered(final PotentialSchemaSource<?> potentialSchemaSource) {
+
+            }
         });
 
         if (configuration.getSchemasDir() != null) {
@@ -320,21 +322,24 @@ public class NetconfDeviceSimulator implements Closeable {
             LOG.info("Custom module loading skipped.");
         }
 
-        addDefaultSchemas(consumer);
+        configuration.getDefaultYangResources().forEach(r -> {
+            final SourceIdentifier sourceIdentifier = new SourceIdentifier(r.getModuleName(), 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).checkedGet();
-        } catch (final SchemaResolutionException e) {
-            throw new RuntimeException("Cannot parse schema context", e);
+            schemaContext = consumer.createEffectiveModelContextFactory()
+                    .createEffectiveModelContext(loadedSources).get();
+        } catch (final InterruptedException | ExecutionException e) {
+            throw new IllegalStateException(
+                "Cannot parse schema context. Please read stack trace and check YANG files in schema directory.", 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);
@@ -342,71 +347,47 @@ public class NetconfDeviceSimulator implements Closeable {
         return capabilities;
     }
 
-    private void addModuleCapability(final SharedSchemaRepository consumer, final Set<Capability> capabilities,
-                                     final Module module) {
-        final SourceIdentifier moduleSourceIdentifier = SourceIdentifier.create(module.getName(),
-                (SimpleDateFormatUtil.DEFAULT_DATE_REV == module.getRevision() ? Optional.absent() :
-                        Optional.of(module.getQNameModule().getFormattedRevision())));
+    private static void addModuleCapability(final SharedSchemaRepository consumer, final Set<Capability> capabilities,
+                                            final ModuleLike module) {
+        final var sourceId = new SourceIdentifier(module.getName(),
+            module.getRevision().map(Revision::toString).orElse(null));
+
+        final String moduleContent;
         try {
-            final String moduleContent = new String(
-                consumer.getSchemaSource(moduleSourceIdentifier, YangTextSchemaSource.class).checkedGet().read());
-            capabilities.add(new YangModuleCapability(module, moduleContent));
-            //IOException would be thrown in creating SchemaContext already
-        } catch (SchemaSourceException | IOException e) {
-            throw new RuntimeException("Cannot retrieve schema source for module "
-                + moduleSourceIdentifier.toString() + " from schema repository", e);
+            moduleContent = consumer.getSchemaSource(sourceId, YangTextSchemaSource.class).get()
+                .asCharSource(StandardCharsets.UTF_8).read();
+        } catch (ExecutionException | InterruptedException | IOException e) {
+            throw new IllegalStateException(
+                "Cannot retrieve schema source for module " + sourceId + " from schema repository", e);
         }
-    }
-
-    private void addDefaultSchemas(final SharedSchemaRepository consumer) {
-        SourceIdentifier srcId = RevisionSourceIdentifier.create("ietf-netconf-monitoring", "2010-10-04");
-        registerSource(consumer, "/META-INF/yang/ietf-netconf-monitoring.yang", srcId);
-
-        srcId = RevisionSourceIdentifier.create("ietf-netconf-monitoring-extension", "2013-12-10");
-        registerSource(consumer, "/META-INF/yang/ietf-netconf-monitoring-extension.yang", srcId);
 
-        srcId = RevisionSourceIdentifier.create("ietf-yang-types", "2013-07-15");
-        registerSource(consumer, "/META-INF/yang/ietf-yang-types@2013-07-15.yang", srcId);
-
-        srcId = RevisionSourceIdentifier.create("ietf-inet-types", "2013-07-15");
-        registerSource(consumer, "/META-INF/yang/ietf-inet-types@2013-07-15.yang", srcId);
+        capabilities.add(new YangModuleCapability(module, moduleContent));
     }
 
-    private void registerSource(final SharedSchemaRepository consumer, final String resource,
-                                final SourceIdentifier sourceId) {
-        consumer.registerSchemaSource(new SchemaSourceProvider<SchemaSourceRepresentation>() {
-            @Override
-            public CheckedFuture<? extends SchemaSourceRepresentation, SchemaSourceException> getSource(
-                    final SourceIdentifier sourceIdentifier) {
-                return Futures.immediateCheckedFuture(new YangTextSchemaSource(sourceId) {
-                    @Override
-                    protected MoreObjects.ToStringHelper addToStringAttributes(
-                            final MoreObjects.ToStringHelper toStringHelper) {
-                        return toStringHelper;
-                    }
-
-                    @Override
-                    public InputStream openStream() throws IOException {
-                        return getClass().getResourceAsStream(resource);
-                    }
-                });
-            }
-        }, PotentialSchemaSource.create(
-            sourceId, YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
+    private static void registerSource(final SharedSchemaRepository consumer, final String resource,
+            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) {
         try {
             return new InetSocketAddress(Inet4Address.getByName(ip), port);
         } catch (final UnknownHostException e) {
-            throw new RuntimeException(e);
+            throw new IllegalArgumentException("Cannot resolve address " + ip, e);
         }
     }
 
     @Override
     public void close() {
         for (final SshProxyServer sshWrapper : sshWrappers) {
-            sshWrapper.close();
+            try {
+                sshWrapper.close();
+            } catch (final IOException e) {
+                LOG.debug("Wrapper {} failed to close", sshWrapper, e);
+            }
         }
         for (final Channel deviceCh : devicesChannels) {
             deviceCh.close();