Expose proper revision of yang-types model in testtool
[controller.git] / opendaylight / netconf / netconf-testtool / src / main / java / org / opendaylight / controller / netconf / test / tool / NetconfDeviceSimulator.java
index e8ba769da547f552dc3fe47aaa9a6e3f35ad0d54..72a9da6d089e734793ed0a2889c0893e69970d82 100644 (file)
@@ -10,7 +10,7 @@ package org.opendaylight.controller.netconf.test.tool;
 
 import com.google.common.base.Charsets;
 import com.google.common.base.Function;
-import com.google.common.base.Objects;
+import com.google.common.base.MoreObjects;
 import com.google.common.base.Optional;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.Lists;
@@ -26,10 +26,12 @@ import io.netty.channel.local.LocalAddress;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.util.HashedWheelTimer;
 import java.io.Closeable;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.lang.management.ManagementFactory;
+import java.net.BindException;
 import java.net.Inet4Address;
 import java.net.InetSocketAddress;
 import java.net.URI;
@@ -53,21 +55,32 @@ import org.apache.sshd.common.util.ThreadUtils;
 import org.apache.sshd.server.PasswordAuthenticator;
 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
 import org.apache.sshd.server.session.ServerSession;
-import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
+import org.opendaylight.controller.netconf.api.Capability;
+import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener;
+import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
-import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
+import org.opendaylight.controller.netconf.impl.NetconfServerDispatcherImpl;
 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
+import org.opendaylight.controller.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
-import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
-import org.opendaylight.controller.netconf.mapping.api.Capability;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
-import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
-import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
+import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringActivator;
 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOperationService;
 import org.opendaylight.controller.netconf.ssh.SshProxyServer;
+import org.opendaylight.controller.netconf.ssh.SshProxyServerConfiguration;
+import org.opendaylight.controller.netconf.ssh.SshProxyServerConfigurationBuilder;
+import org.opendaylight.controller.netconf.test.tool.rpc.DataList;
+import org.opendaylight.controller.netconf.test.tool.rpc.SimulatedCommit;
+import org.opendaylight.controller.netconf.test.tool.rpc.SimulatedCreateSubscription;
+import org.opendaylight.controller.netconf.test.tool.rpc.SimulatedEditConfig;
+import org.opendaylight.controller.netconf.test.tool.rpc.SimulatedGet;
+import org.opendaylight.controller.netconf.test.tool.rpc.SimulatedGetConfig;
+import org.opendaylight.controller.netconf.test.tool.rpc.SimulatedLock;
+import org.opendaylight.controller.netconf.test.tool.rpc.SimulatedUnLock;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
@@ -97,6 +110,8 @@ public class NetconfDeviceSimulator implements Closeable {
     private final ScheduledExecutorService minaTimerExecutor;
     private final ExecutorService nioExecutor;
 
+    private boolean sendFakeSchema = false;
+
     public NetconfDeviceSimulator() {
         // TODO make pool size configurable
         this(new NioEventLoopGroup(), new HashedWheelTimer(),
@@ -111,20 +126,31 @@ public class NetconfDeviceSimulator implements Closeable {
         this.nioExecutor = nioExecutor;
     }
 
-    private NetconfServerDispatcher createDispatcher(final Map<ModuleBuilder, String> moduleBuilders, final boolean exi, final int generateConfigsTimeout) {
+    private NetconfServerDispatcherImpl createDispatcher(final Map<ModuleBuilder, String> moduleBuilders, final boolean exi, final int generateConfigsTimeout, final Optional<File> notificationsFile) {
 
         final Set<Capability> capabilities = Sets.newHashSet(Collections2.transform(moduleBuilders.keySet(), new Function<ModuleBuilder, Capability>() {
             @Override
             public Capability apply(final ModuleBuilder input) {
-                return new ModuleBuilderCapability(input, moduleBuilders.get(input));
+                if (sendFakeSchema) {
+                    sendFakeSchema = false;
+                    return new FakeModuleBuilderCapability(input, moduleBuilders.get(input));
+                } else {
+                    return new ModuleBuilderCapability(input, moduleBuilders.get(input));
+                }
             }
         }));
 
         final SessionIdProvider idProvider = new SessionIdProvider();
 
-        final SimulatedOperationProvider simulatedOperationProvider = new SimulatedOperationProvider(idProvider, capabilities);
-        final NetconfMonitoringOperationService monitoringService = new NetconfMonitoringOperationService(new NetconfMonitoringServiceImpl(simulatedOperationProvider));
-        simulatedOperationProvider.addService(monitoringService);
+
+        final AggregatedNetconfOperationServiceFactory aggregatedNetconfOperationServiceFactory = new AggregatedNetconfOperationServiceFactory();
+        final SimulatedOperationProvider simulatedOperationProvider = new SimulatedOperationProvider(idProvider, capabilities, notificationsFile);
+
+        final NetconfMonitoringService monitoringService1 = new NetconfMonitoringServiceImpl(aggregatedNetconfOperationServiceFactory);
+        final NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory monitoringService =
+                new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(new NetconfMonitoringOperationService(monitoringService1));
+        aggregatedNetconfOperationServiceFactory.onAddNetconfOperationServiceFactory(simulatedOperationProvider);
+        aggregatedNetconfOperationServiceFactory.onAddNetconfOperationServiceFactory(monitoringService);
 
         final DefaultCommitNotificationProducer commitNotifier = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
 
@@ -133,11 +159,11 @@ public class NetconfDeviceSimulator implements Closeable {
                 : Sets.newHashSet(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0, XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
 
         final NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
-                hashedWheelTimer, simulatedOperationProvider, idProvider, generateConfigsTimeout, commitNotifier, new LoggingMonitoringService(), serverCapabilities);
+                hashedWheelTimer, aggregatedNetconfOperationServiceFactory, idProvider, generateConfigsTimeout, commitNotifier, monitoringService1, serverCapabilities);
 
-        final NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
+        final NetconfServerDispatcherImpl.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcherImpl.ServerChannelInitializer(
                 serverNegotiatorFactory);
-        return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
+        return new NetconfServerDispatcherImpl(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
     }
 
     private Map<ModuleBuilder, String> toModuleBuilders(final Map<SourceIdentifier, Map.Entry<ASTSchemaSource, YangTextSchemaSource>> sources) {
@@ -173,7 +199,7 @@ public class NetconfDeviceSimulator implements Closeable {
 
         final Map<ModuleBuilder, String> moduleBuilders = parseSchemasToModuleBuilders(params);
 
-        final NetconfServerDispatcher dispatcher = createDispatcher(moduleBuilders, params.exi, params.generateConfigsTimeout);
+        final NetconfServerDispatcherImpl dispatcher = createDispatcher(moduleBuilders, params.exi, params.generateConfigsTimeout, Optional.fromNullable(params.notificationFile));
 
         int currentPort = params.startingPort;
 
@@ -183,6 +209,10 @@ public class NetconfDeviceSimulator implements Closeable {
         final PEMGeneratorHostKeyProvider keyPairProvider = getPemGeneratorHostKeyProvider();
 
         for (int i = 0; i < params.deviceCount; i++) {
+            if (currentPort > 65535) {
+                LOG.warn("Port cannot be greater than 65535, stopping further attempts.");
+                break;
+            }
             final InetSocketAddress address = getAddress(currentPort);
 
             final ChannelFuture server;
@@ -193,24 +223,19 @@ public class NetconfDeviceSimulator implements Closeable {
                 server = dispatcher.createLocalServer(tcpLocalAddress);
                 try {
                     final SshProxyServer sshServer = new SshProxyServer(minaTimerExecutor, nettyThreadgroup, nioExecutor);
-                    sshServer.bind(bindingAddress, tcpLocalAddress,
-                            new PasswordAuthenticator() {
-                                @Override
-                                public boolean authenticate(final String username, final String password, final ServerSession session) {
-                                    // All connections are accepted
-                                    return true;
-                                }
-                            }, keyPairProvider);
-
+                    sshServer.bind(getSshConfiguration(bindingAddress, tcpLocalAddress, keyPairProvider));
                     sshWrappers.add(sshServer);
-                } catch (final Exception e) {
-                    LOG.warn("Cannot start simulated device on {}, skipping", address, e);
+                } catch (final BindException e) {
+                    LOG.warn("Cannot start simulated device on {}, port already in use. Skipping.", address);
                     // Close local server and continue
                     server.cancel(true);
                     if(server.isDone()) {
                         server.channel().close();
                     }
                     continue;
+                } catch (final IOException e) {
+                    LOG.warn("Cannot start simulated device on {} due to IOException.", address, e);
+                    break;
                 } finally {
                     currentPort++;
                 }
@@ -248,6 +273,8 @@ public class NetconfDeviceSimulator implements Closeable {
 
         if(openDevices.size() == params.deviceCount) {
             LOG.info("All simulated devices started successfully from port {} to {}", params.startingPort, currentPort - 1);
+        } else if (openDevices.size() == 0) {
+            LOG.warn("No simulated devices started.");
         } else {
             LOG.warn("Not all simulated devices started successfully. Started devices ar on ports {}", openDevices);
         }
@@ -255,6 +282,21 @@ public class NetconfDeviceSimulator implements Closeable {
         return openDevices;
     }
 
+    private SshProxyServerConfiguration getSshConfiguration(final InetSocketAddress bindingAddress, final LocalAddress tcpLocalAddress, final PEMGeneratorHostKeyProvider keyPairProvider) throws IOException {
+        return new SshProxyServerConfigurationBuilder()
+                .setBindingAddress(bindingAddress)
+                .setLocalAddress(tcpLocalAddress)
+                .setAuthenticator(new PasswordAuthenticator() {
+                    @Override
+                    public boolean authenticate(final String username, final String password, final ServerSession session) {
+                        return true;
+                    }
+                })
+                .setKeyPairProvider(keyPairProvider)
+                .setIdleTimeout(Integer.MAX_VALUE)
+                .createSshProxyServerConfiguration();
+    }
+
     private PEMGeneratorHostKeyProvider getPemGeneratorHostKeyProvider() {
         try {
             final Path tempFile = Files.createTempFile("tempKeyNetconfTest", "suffix");
@@ -295,15 +337,15 @@ public class NetconfDeviceSimulator implements Closeable {
 
         final Map<SourceIdentifier, Map.Entry<ASTSchemaSource, YangTextSchemaSource>> asts = Maps.newHashMap();
         for (final SourceIdentifier loadedSource : loadedSources) {
-                try {
-                    final CheckedFuture<ASTSchemaSource, SchemaSourceException> ast = consumer.getSchemaSource(loadedSource, ASTSchemaSource.class);
-                    final CheckedFuture<YangTextSchemaSource, SchemaSourceException> text = consumer.getSchemaSource(loadedSource, YangTextSchemaSource.class);
-                    asts.put(loadedSource, new AbstractMap.SimpleEntry<>(ast.get(), text.get()));
-                } catch (final InterruptedException e) {
-                    throw new RuntimeException(e);
-                } catch (final ExecutionException e) {
-                    throw new RuntimeException("Cannot parse schema context", e);
-                }
+            try {
+                final CheckedFuture<ASTSchemaSource, SchemaSourceException> ast = consumer.getSchemaSource(loadedSource, ASTSchemaSource.class);
+                final CheckedFuture<YangTextSchemaSource, SchemaSourceException> text = consumer.getSchemaSource(loadedSource, YangTextSchemaSource.class);
+                asts.put(loadedSource, new AbstractMap.SimpleEntry<>(ast.get(), text.get()));
+            } catch (final InterruptedException e) {
+                throw new RuntimeException(e);
+            } catch (final ExecutionException e) {
+                throw new RuntimeException("Cannot parse schema context", e);
+            }
         }
         return toModuleBuilders(asts);
     }
@@ -312,8 +354,11 @@ public class NetconfDeviceSimulator implements Closeable {
         SourceIdentifier sId = new SourceIdentifier("ietf-netconf-monitoring", "2010-10-04");
         registerSource(consumer, "/META-INF/yang/ietf-netconf-monitoring.yang", sId);
 
-        sId = new SourceIdentifier("ietf-yang-types", "2013-07-15");
-        registerSource(consumer, "/META-INF/yang/ietf-yang-types@2013-07-15.yang", sId);
+        sId = new SourceIdentifier("ietf-netconf-monitoring-extension", "2013-12-10");
+        registerSource(consumer, "/META-INF/yang/ietf-netconf-monitoring-extension.yang", sId);
+
+        sId = new SourceIdentifier("ietf-yang-types", "2010-09-24");
+        registerSource(consumer, "/META-INF/yang/ietf-yang-types.yang", sId);
 
         sId = new SourceIdentifier("ietf-inet-types", "2010-09-24");
         registerSource(consumer, "/META-INF/yang/ietf-inet-types.yang", sId);
@@ -325,7 +370,7 @@ public class NetconfDeviceSimulator implements Closeable {
             public CheckedFuture<? extends SchemaSourceRepresentation, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
                 return Futures.immediateCheckedFuture(new YangTextSchemaSource(sourceId) {
                     @Override
-                    protected Objects.ToStringHelper addToStringAttributes(final Objects.ToStringHelper toStringHelper) {
+                    protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
                         return toStringHelper;
                     }
 
@@ -361,61 +406,42 @@ public class NetconfDeviceSimulator implements Closeable {
         // close Everything
     }
 
-    private static class SimulatedOperationProvider implements NetconfOperationProvider {
-        private final SessionIdProvider idProvider;
-        private final Set<NetconfOperationService> netconfOperationServices;
+    private static class SimulatedOperationProvider implements NetconfOperationServiceFactory {
+        private final Set<Capability> caps;
+        private final SimulatedOperationService simulatedOperationService;
 
 
-        public SimulatedOperationProvider(final SessionIdProvider idProvider, final Set<Capability> caps) {
-            this.idProvider = idProvider;
-            final SimulatedOperationService simulatedOperationService = new SimulatedOperationService(caps, idProvider.getCurrentSessionId());
-            this.netconfOperationServices = Sets.<NetconfOperationService>newHashSet(simulatedOperationService);
+        public SimulatedOperationProvider(final SessionIdProvider idProvider, final Set<Capability> caps, final Optional<File> notificationsFile) {
+            this.caps = caps;
+            simulatedOperationService = new SimulatedOperationService(idProvider.getCurrentSessionId(), notificationsFile);
         }
 
         @Override
-        public NetconfOperationServiceSnapshot openSnapshot(final String sessionIdForReporting) {
-            return new SimulatedServiceSnapshot(idProvider, netconfOperationServices);
+        public Set<Capability> getCapabilities() {
+            return caps;
         }
 
-        public void addService(final NetconfOperationService monitoringService) {
-            netconfOperationServices.add(monitoringService);
+        @Override
+        public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+            listener.onCapabilitiesAdded(caps);
+            return new AutoCloseable() {
+                @Override
+                public void close() throws Exception {}
+            };
         }
 
-        private static class SimulatedServiceSnapshot implements NetconfOperationServiceSnapshot {
-            private final SessionIdProvider idProvider;
-            private final Set<NetconfOperationService> netconfOperationServices;
-
-            public SimulatedServiceSnapshot(final SessionIdProvider idProvider, final Set<NetconfOperationService> netconfOperationServices) {
-                this.idProvider = idProvider;
-                this.netconfOperationServices = netconfOperationServices;
-            }
-
-            @Override
-            public String getNetconfSessionIdForReporting() {
-                return String.valueOf(idProvider.getCurrentSessionId());
-            }
-
-            @Override
-            public Set<NetconfOperationService> getServices() {
-                return netconfOperationServices;
-            }
-
-            @Override
-            public void close() throws Exception {}
+        @Override
+        public NetconfOperationService createService(final String netconfSessionIdForReporting) {
+            return simulatedOperationService;
         }
 
         static class SimulatedOperationService implements NetconfOperationService {
-            private final Set<Capability> capabilities;
             private final long currentSessionId;
+            private final Optional<File> notificationsFile;
 
-            public SimulatedOperationService(final Set<Capability> capabilities, final long currentSessionId) {
-                this.capabilities = capabilities;
+            public SimulatedOperationService(final long currentSessionId, final Optional<File> notificationsFile) {
                 this.currentSessionId = currentSessionId;
-            }
-
-            @Override
-            public Set<Capability> getCapabilities() {
-                return capabilities;
+                this.notificationsFile = notificationsFile;
             }
 
             @Override
@@ -425,7 +451,10 @@ public class NetconfDeviceSimulator implements Closeable {
                 final SimulatedEditConfig sEditConfig = new SimulatedEditConfig(String.valueOf(currentSessionId), storage);
                 final SimulatedGetConfig sGetConfig = new SimulatedGetConfig(String.valueOf(currentSessionId), storage);
                 final SimulatedCommit sCommit = new SimulatedCommit(String.valueOf(currentSessionId));
-                return Sets.<NetconfOperation>newHashSet(sGet,  sGetConfig, sEditConfig, sCommit);
+                final SimulatedLock sLock = new SimulatedLock(String.valueOf(currentSessionId));
+                final SimulatedUnLock sUnlock = new SimulatedUnLock(String.valueOf(currentSessionId));
+                final SimulatedCreateSubscription sCreateSubs = new SimulatedCreateSubscription(String.valueOf(currentSessionId), notificationsFile);
+                return Sets.<NetconfOperation>newHashSet(sGet,  sGetConfig, sEditConfig, sCommit, sLock, sUnlock, sCreateSubs);
             }
 
             @Override
@@ -435,16 +464,4 @@ public class NetconfDeviceSimulator implements Closeable {
         }
     }
 
-    private class LoggingMonitoringService implements SessionMonitoringService {
-        @Override
-        public void onSessionUp(final NetconfManagementSession session) {
-            LOG.debug("Session {} established", session);
-        }
-
-        @Override
-        public void onSessionDown(final NetconfManagementSession session) {
-            LOG.debug("Session {} down", session);
-        }
-    }
-
 }