Move netconf.api.monitoring
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / NetconfDeviceSimulator.java
index a4a541b1a6bb4e15ddcd273ed64e440cc05c3ba4..a5e774265d3924869b2342ce82848729abe0aa71 100644 (file)
@@ -7,11 +7,11 @@
  */
 package org.opendaylight.netconf.test.tool;
 
-import com.google.common.base.MoreObjects.ToStringHelper;
+import static java.util.Objects.requireNonNullElseGet;
+
 import com.google.common.collect.Collections2;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.local.LocalAddress;
@@ -19,12 +19,12 @@ 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.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -37,13 +37,14 @@ import java.util.concurrent.ScheduledExecutorService;
 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.server.NetconfServerDispatcherImpl;
+import org.opendaylight.netconf.server.NetconfServerSessionNegotiatorFactory;
+import org.opendaylight.netconf.server.ServerChannelInitializer;
+import org.opendaylight.netconf.server.api.SessionIdProvider;
+import org.opendaylight.netconf.server.api.monitoring.NetconfMonitoringService;
+import org.opendaylight.netconf.server.api.operations.NetconfOperationServiceFactory;
+import org.opendaylight.netconf.server.impl.DefaultSessionIdProvider;
+import org.opendaylight.netconf.server.osgi.AggregatedNetconfOperationServiceFactory;
 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;
@@ -62,22 +63,19 @@ import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
 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.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.TextToIRTransformer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@SuppressFBWarnings("DM_DEFAULT_ENCODING")
 public class NetconfDeviceSimulator implements Closeable {
-
     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSimulator.class);
 
     private final NioEventLoopGroup nettyThreadgroup;
@@ -93,12 +91,11 @@ public class NetconfDeviceSimulator implements Closeable {
 
     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,
@@ -114,7 +111,7 @@ public class NetconfDeviceSimulator implements Closeable {
         }));
         transformedCapabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
         final NetconfMonitoringService monitoringService1 = new DummyMonitoringService(transformedCapabilities);
-        final SessionIdProvider idProvider = new SessionIdProvider();
+        final SessionIdProvider idProvider = new DefaultSessionIdProvider();
 
         final NetconfOperationServiceFactory aggregatedNetconfOperationServiceFactory = createOperationServiceFactory(
             sourceProvider, transformedCapabilities, monitoringService1, idProvider);
@@ -148,14 +145,18 @@ public class NetconfDeviceSimulator implements Closeable {
             operationProvider = new SimulatedOperationProvider(idProvider, transformedCapabilities,
                     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 NetconfMonitoringOperationServiceFactory monitoringService =
                 new NetconfMonitoringOperationServiceFactory(
                         new NetconfMonitoringOperationService(monitoringService1));
@@ -174,8 +175,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);
@@ -233,7 +235,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;
@@ -248,7 +250,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 tcp simulated device on {}, skipping", address, e);
                     continue;
@@ -322,17 +324,17 @@ public class NetconfDeviceSimulator implements Closeable {
         }
 
         configuration.getDefaultYangResources().forEach(r -> {
-            final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create(r.getModuleName(),
-                Revision.ofNullable(r.getRevision()));
+            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.createEffectiveModelContextFactory()
+            schemaContext = consumer.createEffectiveModelContextFactory()
                     .createEffectiveModelContext(loadedSources).get();
         } catch (final InterruptedException | ExecutionException e) {
-            throw new RuntimeException("Cannot parse schema context", e);
+            throw new IllegalStateException(
+                "Cannot parse schema context. Please read stack trace and check YANG files in schema directory.", e);
         }
 
         final Set<Capability> capabilities = new HashSet<>();
@@ -348,40 +350,34 @@ public class NetconfDeviceSimulator implements Closeable {
 
     private static void addModuleCapability(final SharedSchemaRepository consumer, final Set<Capability> capabilities,
                                             final ModuleLike module) {
-        final SourceIdentifier moduleSourceIdentifier = RevisionSourceIdentifier.create(module.getName(),
-            module.getRevision());
+        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).get().read());
-            capabilities.add(new YangModuleCapability(module, moduleContent));
-            //IOException would be thrown in creating SchemaContext already
-        } catch (final ExecutionException | InterruptedException | 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);
         }
+
+        capabilities.add(new YangModuleCapability(module, moduleContent));
     }
 
     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) {
         try {
             return new InetSocketAddress(Inet4Address.getByName(ip), port);
         } catch (final UnknownHostException e) {
-            throw new RuntimeException(e);
+            throw new IllegalArgumentException("Cannot resolve address " + ip, e);
         }
     }
 
@@ -401,5 +397,4 @@ public class NetconfDeviceSimulator implements Closeable {
         minaTimerExecutor.shutdownNow();
         nioExecutor.shutdownNow();
     }
-
 }