Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / NetconfDeviceSimulator.java
index dffd79e6c564625407af852e9362546175556131..42b162b2def8895f543eecc6422ab05cbe1e4e57 100644 (file)
@@ -8,12 +8,11 @@
 
 package org.opendaylight.netconf.test.tool;
 
-import com.google.common.base.MoreObjects;
+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.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import io.netty.channel.Channel;
@@ -36,8 +35,9 @@ 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.apache.sshd.common.keyprovider.KeyPairProvider;
+import org.apache.sshd.common.util.security.SecurityUtils;
+import org.apache.sshd.common.util.threads.ThreadUtils;
 import org.opendaylight.controller.config.util.capability.BasicCapability;
 import org.opendaylight.controller.config.util.capability.Capability;
 import org.opendaylight.controller.config.util.capability.YangModuleCapability;
@@ -54,15 +54,14 @@ 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.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.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.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
@@ -72,7 +71,7 @@ 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.TextToASTTransformer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -91,7 +90,7 @@ public class NetconfDeviceSimulator implements Closeable {
 
     private boolean sendFakeSchema = false;
 
-    public NetconfDeviceSimulator(Configuration configuration) {
+    public NetconfDeviceSimulator(final Configuration configuration) {
         this.configuration = configuration;
         this.nettyThreadgroup = new NioEventLoopGroup();
         this.hashedWheelTimer = new HashedWheelTimer();
@@ -150,7 +149,9 @@ public class NetconfDeviceSimulator implements Closeable {
                     Optional.fromNullable(configuration.getInitialConfigXMLFile()));
         } else {
             LOG.info("using OperationsProvider.");
-            operationProvider = new OperationsProvider(idProvider, transformedCapabilities);
+            operationProvider = new OperationsProvider(idProvider, transformedCapabilities,
+                configuration.getOperationsCreator() != null ? configuration.getOperationsCreator()
+                    : DefaultOperationsCreator.getDefaultOperationServiceCreator(idProvider.getCurrentSessionId()));
         }
 
 
@@ -186,7 +187,7 @@ public class NetconfDeviceSimulator implements Closeable {
         final List<Integer> openDevices = Lists.newArrayList();
 
         // Generate key to temp folder
-        final PEMGeneratorHostKeyProvider keyPairProvider = getPemGeneratorHostKeyProvider();
+        final KeyPairProvider keyPairProvider = getPemGeneratorHostKeyProvider();
 
         for (int i = 0; i < configuration.getDeviceCount(); i++) {
             if (currentPort > 65535) {
@@ -265,20 +266,21 @@ 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) throws IOException {
         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() {
+    private static KeyPairProvider getPemGeneratorHostKeyProvider() {
         try {
             final Path tempFile = Files.createTempFile("tempKeyNetconfTest", "suffix");
-            return new PEMGeneratorHostKeyProvider(tempFile.toAbsolutePath().toString(), "RSA", 4096);
+            return SecurityUtils.createGeneratorHostKeyProvider(tempFile.toAbsolutePath());
         } catch (final IOException e) {
             LOG.error("Unable to generate PEM key", e);
             throw new RuntimeException(e);
@@ -317,14 +319,17 @@ public class NetconfDeviceSimulator implements Closeable {
             LOG.info("Custom module loading skipped.");
         }
 
-        addDefaultSchemas(consumer);
+        configuration.getDefaultYangResources().forEach(r -> {
+            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).checkedGet();
-        } catch (final SchemaResolutionException e) {
+            this.schemaContext = consumer.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT)
+                .createSchemaContext(loadedSources).get();
+        } catch (final InterruptedException | ExecutionException e) {
             throw new RuntimeException("Cannot parse schema context", e);
         }
 
@@ -339,57 +344,35 @@ public class NetconfDeviceSimulator implements Closeable {
         return capabilities;
     }
 
-    private void addModuleCapability(final SharedSchemaRepository consumer, final Set<Capability> capabilities,
+    private static 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())));
+        final SourceIdentifier moduleSourceIdentifier = RevisionSourceIdentifier.create(module.getName(),
+            module.getRevision());
         try {
             final String moduleContent = new String(
-                consumer.getSchemaSource(moduleSourceIdentifier, YangTextSchemaSource.class).checkedGet().read());
+                consumer.getSchemaSource(moduleSourceIdentifier, YangTextSchemaSource.class).get().read());
             capabilities.add(new YangModuleCapability(module, moduleContent));
             //IOException would be thrown in creating SchemaContext already
-        } catch (SchemaSourceException | IOException e) {
+        } catch (ExecutionException | InterruptedException | IOException e) {
             throw new RuntimeException("Cannot retrieve schema source for module "
                 + moduleSourceIdentifier.toString() + " 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);
-    }
-
-    private void registerSource(final SharedSchemaRepository consumer, final String resource,
+    private static void registerSource(final SharedSchemaRepository consumer, final String resource,
                                 final SourceIdentifier sourceId) {
-        consumer.registerSchemaSource(new SchemaSourceProvider<SchemaSourceRepresentation>() {
+        consumer.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(new YangTextSchemaSource(sourceId) {
             @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;
-                    }
+            protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+                return toStringHelper;
+            }
 
-                    @Override
-                    public InputStream openStream() throws IOException {
-                        return getClass().getResourceAsStream(resource);
-                    }
-                });
+            @Override
+            public InputStream openStream() throws IOException {
+                return getClass().getResourceAsStream(resource);
             }
-        }, PotentialSchemaSource.create(
-            sourceId, YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
+        }), PotentialSchemaSource.create(sourceId, YangTextSchemaSource.class,
+            PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
     }
 
     private static InetSocketAddress getAddress(final String ip, final int port) {
@@ -403,7 +386,11 @@ public class NetconfDeviceSimulator implements Closeable {
     @Override
     public void close() {
         for (final SshProxyServer sshWrapper : sshWrappers) {
-            sshWrapper.close();
+            try {
+                sshWrapper.close();
+            } catch (IOException e) {
+                LOG.debug("Wrapper {} failed to close", sshWrapper, e);
+            }
         }
         for (final Channel deviceCh : devicesChannels) {
             deviceCh.close();