X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-testtool%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Ftest%2Ftool%2FNetconfDeviceSimulator.java;h=d0939a288f3ee0bb85915675e64f441165af8731;hp=2cd5b19bd12a4c44a222a43acd8004df82debef1;hb=bfae8a4c16f0fabe4e425aa0849166db1b104671;hpb=f71a2c712690ecfd1260543ab58d8e16453f7918 diff --git a/opendaylight/netconf/netconf-testtool/src/main/java/org/opendaylight/controller/netconf/test/tool/NetconfDeviceSimulator.java b/opendaylight/netconf/netconf-testtool/src/main/java/org/opendaylight/controller/netconf/test/tool/NetconfDeviceSimulator.java index 2cd5b19bd1..d0939a288f 100644 --- a/opendaylight/netconf/netconf-testtool/src/main/java/org/opendaylight/controller/netconf/test/tool/NetconfDeviceSimulator.java +++ b/opendaylight/netconf/netconf-testtool/src/main/java/org/opendaylight/controller/netconf/test/tool/NetconfDeviceSimulator.java @@ -10,6 +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.Optional; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; @@ -17,6 +18,7 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.common.io.CharStreams; import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.Futures; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.local.LocalAddress; @@ -25,6 +27,7 @@ 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.Inet4Address; @@ -64,6 +67,7 @@ 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.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.builder.impl.BuilderUtils; import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder; @@ -78,8 +82,6 @@ public class NetconfDeviceSimulator implements Closeable { private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSimulator.class); - public static final int CONNECTION_TIMEOUT_MILLIS = 20000; - private final NioEventLoopGroup nettyThreadgroup; private final HashedWheelTimer hashedWheelTimer; private final List devicesChannels = Lists.newArrayList(); @@ -93,7 +95,7 @@ public class NetconfDeviceSimulator implements Closeable { this.hashedWheelTimer = hashedWheelTimer; } - private NetconfServerDispatcher createDispatcher(final Map moduleBuilders, final boolean exi) { + private NetconfServerDispatcher createDispatcher(final Map moduleBuilders, final boolean exi, final int generateConfigsTimeout) { final Set capabilities = Sets.newHashSet(Collections2.transform(moduleBuilders.keySet(), new Function() { @Override @@ -115,7 +117,7 @@ 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, CONNECTION_TIMEOUT_MILLIS, commitNotifier, new LoggingMonitoringService(), serverCapabilities); + hashedWheelTimer, simulatedOperationProvider, idProvider, generateConfigsTimeout, commitNotifier, new LoggingMonitoringService(), serverCapabilities); final NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer( serverNegotiatorFactory); @@ -123,37 +125,39 @@ public class NetconfDeviceSimulator implements Closeable { } private Map toModuleBuilders(final Map> sources) { - final Map asts = Maps.transformValues(sources, new Function, ParserRuleContext>() { - @Override - public ParserRuleContext apply(final Map.Entry input) { - return input.getKey().getAST(); - } - }); - final Map> namespaceContext = BuilderUtils.createYangNamespaceContext( - asts.values(), Optional.absent()); + final Map asts = Maps.transformValues(sources, new Function, ParserRuleContext>() { + @Override + public ParserRuleContext apply(final Map.Entry input) { + return input.getKey().getAST(); + } + }); + final Map> namespaceContext = BuilderUtils.createYangNamespaceContext( + asts.values(), Optional.absent()); - final ParseTreeWalker walker = new ParseTreeWalker(); - final Map sourceToBuilder = new HashMap<>(); + final ParseTreeWalker walker = new ParseTreeWalker(); + final Map sourceToBuilder = new HashMap<>(); - for (final Map.Entry entry : asts.entrySet()) { - final ModuleBuilder moduleBuilder = YangParserListenerImpl.create(namespaceContext, entry.getKey().getName(), - walker, entry.getValue()).getModuleBuilder(); + for (final Map.Entry entry : asts.entrySet()) { + final ModuleBuilder moduleBuilder = YangParserListenerImpl.create(namespaceContext, entry.getKey().getName(), + walker, entry.getValue()).getModuleBuilder(); - try(InputStreamReader stream = new InputStreamReader(sources.get(entry.getKey()).getValue().openStream(), Charsets.UTF_8)) { - sourceToBuilder.put(moduleBuilder, CharStreams.toString(stream)); - } catch (final IOException e) { - throw new RuntimeException(e); - } + try(InputStreamReader stream = new InputStreamReader(sources.get(entry.getKey()).getValue().openStream(), Charsets.UTF_8)) { + sourceToBuilder.put(moduleBuilder, CharStreams.toString(stream)); + } catch (final IOException e) { + throw new RuntimeException(e); } - - return sourceToBuilder; } + return sourceToBuilder; + } + public List start(final Main.Params params) { + LOG.info("Starting {}, {} simulated devices starting on port {}", params.deviceCount, params.ssh ? "SSH" : "TCP", params.startingPort); + final Map moduleBuilders = parseSchemasToModuleBuilders(params); - final NetconfServerDispatcher dispatcher = createDispatcher(moduleBuilders, params.exi); + final NetconfServerDispatcher dispatcher = createDispatcher(moduleBuilders, params.exi, params.generateConfigsTimeout); int currentPort = params.startingPort; @@ -210,11 +214,10 @@ public class NetconfDeviceSimulator implements Closeable { devicesChannels.add(server.channel()); openDevices.add(currentPort - 1); - } if(openDevices.size() == params.deviceCount) { - LOG.info("All simulated devices started successfully from port {} to {}", params.startingPort, currentPort); + LOG.info("All simulated devices started successfully from port {} to {}", params.startingPort, currentPort - 1); } else { LOG.warn("Not all simulated devices started successfully. Started devices ar on ports {}", openDevices); } @@ -251,8 +254,12 @@ public class NetconfDeviceSimulator implements Closeable { public void schemaSourceUnregistered(final PotentialSchemaSource potentialSchemaSource) {} }); - final FilesystemSchemaSourceCache cache = new FilesystemSchemaSourceCache<>(consumer, YangTextSchemaSource.class, params.schemasDir); - consumer.registerSchemaSourceListener(cache); + if(params.schemasDir != null) { + final FilesystemSchemaSourceCache cache = new FilesystemSchemaSourceCache<>(consumer, YangTextSchemaSource.class, params.schemasDir); + consumer.registerSchemaSourceListener(cache); + } + + addDefaultSchemas(consumer); final Map> asts = Maps.newHashMap(); for (final SourceIdentifier loadedSource : loadedSources) { @@ -269,6 +276,36 @@ public class NetconfDeviceSimulator implements Closeable { return toModuleBuilders(asts); } + private void addDefaultSchemas(final SharedSchemaRepository consumer) { + 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-inet-types", "2010-09-24"); + registerSource(consumer, "/META-INF/yang/ietf-inet-types.yang", sId); + } + + private void registerSource(final SharedSchemaRepository consumer, final String resource, final SourceIdentifier sourceId) { + consumer.registerSchemaSource(new SchemaSourceProvider() { + @Override + public CheckedFuture getSource(final SourceIdentifier sourceIdentifier) { + return Futures.immediateCheckedFuture(new YangTextSchemaSource(sourceId) { + @Override + protected Objects.ToStringHelper addToStringAttributes(final Objects.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 InetSocketAddress getAddress(final int port) { try { // TODO make address configurable @@ -332,11 +369,11 @@ public class NetconfDeviceSimulator implements Closeable { static class SimulatedOperationService implements NetconfOperationService { private final Set capabilities; - private static SimulatedGet sGet; + private final long currentSessionId; public SimulatedOperationService(final Set capabilities, final long currentSessionId) { this.capabilities = capabilities; - sGet = new SimulatedGet(String.valueOf(currentSessionId)); + this.currentSessionId = currentSessionId; } @Override @@ -346,7 +383,12 @@ public class NetconfDeviceSimulator implements Closeable { @Override public Set getNetconfOperations() { - return Sets.newHashSet(sGet); + final DataList storage = new DataList(); + final SimulatedGet sGet = new SimulatedGet(String.valueOf(currentSessionId), storage); + 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.newHashSet(sGet, sGetConfig, sEditConfig, sCommit); } @Override