2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.test.tool;
11 import com.google.common.base.Charsets;
12 import com.google.common.base.Function;
13 import com.google.common.base.Objects;
14 import com.google.common.base.Optional;
15 import com.google.common.collect.Collections2;
16 import com.google.common.collect.Lists;
17 import com.google.common.collect.Maps;
18 import com.google.common.collect.Sets;
19 import com.google.common.io.CharStreams;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import com.google.common.util.concurrent.Futures;
22 import io.netty.channel.Channel;
23 import io.netty.channel.ChannelFuture;
24 import io.netty.channel.local.LocalAddress;
25 import io.netty.channel.nio.NioEventLoopGroup;
26 import io.netty.util.HashedWheelTimer;
27 import java.io.Closeable;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.lang.management.ManagementFactory;
33 import java.net.Inet4Address;
34 import java.net.InetSocketAddress;
36 import java.net.UnknownHostException;
37 import java.util.AbstractMap;
38 import java.util.Date;
39 import java.util.HashMap;
40 import java.util.List;
43 import java.util.TreeMap;
44 import java.util.concurrent.ExecutionException;
45 import org.antlr.v4.runtime.ParserRuleContext;
46 import org.antlr.v4.runtime.tree.ParseTreeWalker;
47 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
48 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
49 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
50 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
51 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
52 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
53 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
54 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
55 import org.opendaylight.controller.netconf.mapping.api.Capability;
56 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
57 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
58 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
59 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
60 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOperationService;
61 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
62 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
63 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
64 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
65 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
66 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
67 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
68 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
69 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
70 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
71 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
72 import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
73 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
74 import org.opendaylight.yangtools.yang.parser.impl.YangParserListenerImpl;
75 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
76 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
77 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
81 public class NetconfDeviceSimulator implements Closeable {
83 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSimulator.class);
85 private final NioEventLoopGroup nettyThreadgroup;
86 private final HashedWheelTimer hashedWheelTimer;
87 private final List<Channel> devicesChannels = Lists.newArrayList();
89 public NetconfDeviceSimulator() {
90 this(new NioEventLoopGroup(), new HashedWheelTimer());
93 public NetconfDeviceSimulator(final NioEventLoopGroup eventExecutors, final HashedWheelTimer hashedWheelTimer) {
94 this.nettyThreadgroup = eventExecutors;
95 this.hashedWheelTimer = hashedWheelTimer;
98 private NetconfServerDispatcher createDispatcher(final Map<ModuleBuilder, String> moduleBuilders, final boolean exi, final int generateConfigsTimeout) {
100 final Set<Capability> capabilities = Sets.newHashSet(Collections2.transform(moduleBuilders.keySet(), new Function<ModuleBuilder, Capability>() {
102 public Capability apply(final ModuleBuilder input) {
103 return new ModuleBuilderCapability(input, moduleBuilders.get(input));
107 final SessionIdProvider idProvider = new SessionIdProvider();
109 final SimulatedOperationProvider simulatedOperationProvider = new SimulatedOperationProvider(idProvider, capabilities);
110 final NetconfMonitoringOperationService monitoringService = new NetconfMonitoringOperationService(new NetconfMonitoringServiceImpl(simulatedOperationProvider));
111 simulatedOperationProvider.addService(monitoringService);
113 final DefaultCommitNotificationProducer commitNotifier = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
115 final Set<String> serverCapabilities = exi
116 ? NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES
117 : Sets.newHashSet(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0, XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
119 final NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
120 hashedWheelTimer, simulatedOperationProvider, idProvider, generateConfigsTimeout, commitNotifier, new LoggingMonitoringService(), serverCapabilities);
122 final NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
123 serverNegotiatorFactory);
124 return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
127 private Map<ModuleBuilder, String> toModuleBuilders(final Map<SourceIdentifier, Map.Entry<ASTSchemaSource, YangTextSchemaSource>> sources) {
128 final Map<SourceIdentifier, ParserRuleContext> asts = Maps.transformValues(sources, new Function<Map.Entry<ASTSchemaSource, YangTextSchemaSource>, ParserRuleContext>() {
130 public ParserRuleContext apply(final Map.Entry<ASTSchemaSource, YangTextSchemaSource> input) {
131 return input.getKey().getAST();
134 final Map<String, TreeMap<Date, URI>> namespaceContext = BuilderUtils.createYangNamespaceContext(
135 asts.values(), Optional.<SchemaContext>absent());
137 final ParseTreeWalker walker = new ParseTreeWalker();
138 final Map<ModuleBuilder, String> sourceToBuilder = new HashMap<>();
140 for (final Map.Entry<SourceIdentifier, ParserRuleContext> entry : asts.entrySet()) {
141 final ModuleBuilder moduleBuilder = YangParserListenerImpl.create(namespaceContext, entry.getKey().getName(),
142 walker, entry.getValue()).getModuleBuilder();
144 try(InputStreamReader stream = new InputStreamReader(sources.get(entry.getKey()).getValue().openStream(), Charsets.UTF_8)) {
145 sourceToBuilder.put(moduleBuilder, CharStreams.toString(stream));
146 } catch (final IOException e) {
147 throw new RuntimeException(e);
151 return sourceToBuilder;
155 public List<Integer> start(final Main.Params params) {
156 LOG.info("Starting {}, {} simulated devices starting on port {}", params.deviceCount, params.ssh ? "SSH" : "TCP", params.startingPort);
158 final Map<ModuleBuilder, String> moduleBuilders = parseSchemasToModuleBuilders(params);
160 final NetconfServerDispatcher dispatcher = createDispatcher(moduleBuilders, params.exi, params.generateConfigsTimeout);
162 int currentPort = params.startingPort;
164 final List<Integer> openDevices = Lists.newArrayList();
165 for (int i = 0; i < params.deviceCount; i++) {
166 final InetSocketAddress address = getAddress(currentPort);
168 final ChannelFuture server;
170 final LocalAddress tcpLocalAddress = new LocalAddress(address.toString());
172 server = dispatcher.createLocalServer(tcpLocalAddress);
174 final NetconfSSHServer sshServer = NetconfSSHServer.start(currentPort, tcpLocalAddress, nettyThreadgroup, getPemArray());
175 sshServer.setAuthProvider(new AcceptingAuthProvider());
176 } catch (final Exception e) {
177 LOG.warn("Cannot start simulated device on {}, skipping", address, e);
178 // Close local server and continue
180 if(server.isDone()) {
181 server.channel().close();
190 } catch (final InterruptedException e) {
191 throw new RuntimeException(e);
192 } catch (final ExecutionException e) {
193 LOG.warn("Cannot start ssh simulated device on {}, skipping", address, e);
197 LOG.debug("Simulated SSH device started on {}", address);
200 server = dispatcher.createServer(address);
205 } catch (final InterruptedException e) {
206 throw new RuntimeException(e);
207 } catch (final ExecutionException e) {
208 LOG.warn("Cannot start tcp simulated device on {}, skipping", address, e);
212 LOG.debug("Simulated TCP device started on {}", address);
215 devicesChannels.add(server.channel());
216 openDevices.add(currentPort - 1);
219 if(openDevices.size() == params.deviceCount) {
220 LOG.info("All simulated devices started successfully from port {} to {}", params.startingPort, currentPort - 1);
222 LOG.warn("Not all simulated devices started successfully. Started devices ar on ports {}", openDevices);
228 private char[] getPemArray() {
230 return PEMGenerator.readOrGeneratePK(new File("PK")).toCharArray();
231 } catch (final IOException e) {
232 throw new RuntimeException(e);
236 private Map<ModuleBuilder, String> parseSchemasToModuleBuilders(final Main.Params params) {
237 final SharedSchemaRepository consumer = new SharedSchemaRepository("netconf-simulator");
238 consumer.registerSchemaSourceListener(TextToASTTransformer.create(consumer, consumer));
240 final Set<SourceIdentifier> loadedSources = Sets.newHashSet();
242 consumer.registerSchemaSourceListener(new SchemaSourceListener() {
244 public void schemaSourceEncountered(final SchemaSourceRepresentation schemaSourceRepresentation) {}
247 public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> potentialSchemaSources) {
248 for (final PotentialSchemaSource<?> potentialSchemaSource : potentialSchemaSources) {
249 loadedSources.add(potentialSchemaSource.getSourceIdentifier());
254 public void schemaSourceUnregistered(final PotentialSchemaSource<?> potentialSchemaSource) {}
257 if(params.schemasDir != null) {
258 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(consumer, YangTextSchemaSource.class, params.schemasDir);
259 consumer.registerSchemaSourceListener(cache);
262 addDefaultSchemas(consumer);
264 final Map<SourceIdentifier, Map.Entry<ASTSchemaSource, YangTextSchemaSource>> asts = Maps.newHashMap();
265 for (final SourceIdentifier loadedSource : loadedSources) {
267 final CheckedFuture<ASTSchemaSource, SchemaSourceException> ast = consumer.getSchemaSource(loadedSource, ASTSchemaSource.class);
268 final CheckedFuture<YangTextSchemaSource, SchemaSourceException> text = consumer.getSchemaSource(loadedSource, YangTextSchemaSource.class);
269 asts.put(loadedSource, new AbstractMap.SimpleEntry<>(ast.get(), text.get()));
270 } catch (final InterruptedException e) {
271 throw new RuntimeException(e);
272 } catch (final ExecutionException e) {
273 throw new RuntimeException("Cannot parse schema context", e);
276 return toModuleBuilders(asts);
279 private void addDefaultSchemas(final SharedSchemaRepository consumer) {
280 SourceIdentifier sId = new SourceIdentifier("ietf-netconf-monitoring", "2010-10-04");
281 registerSource(consumer, "/META-INF/yang/ietf-netconf-monitoring.yang", sId);
283 sId = new SourceIdentifier("ietf-yang-types", "2013-07-15");
284 registerSource(consumer, "/META-INF/yang/ietf-yang-types@2013-07-15.yang", sId);
286 sId = new SourceIdentifier("ietf-inet-types", "2010-09-24");
287 registerSource(consumer, "/META-INF/yang/ietf-inet-types.yang", sId);
290 private void registerSource(final SharedSchemaRepository consumer, final String resource, final SourceIdentifier sourceId) {
291 consumer.registerSchemaSource(new SchemaSourceProvider<SchemaSourceRepresentation>() {
293 public CheckedFuture<? extends SchemaSourceRepresentation, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
294 return Futures.immediateCheckedFuture(new YangTextSchemaSource(sourceId) {
296 protected Objects.ToStringHelper addToStringAttributes(final Objects.ToStringHelper toStringHelper) {
297 return toStringHelper;
301 public InputStream openStream() throws IOException {
302 return getClass().getResourceAsStream(resource);
306 }, PotentialSchemaSource.create(sourceId, YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
309 private static InetSocketAddress getAddress(final int port) {
311 // TODO make address configurable
312 return new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port);
313 } catch (final UnknownHostException e) {
314 throw new RuntimeException(e);
319 public void close() {
320 for (final Channel deviceCh : devicesChannels) {
323 nettyThreadgroup.shutdownGracefully();
327 private static class SimulatedOperationProvider implements NetconfOperationProvider {
328 private final SessionIdProvider idProvider;
329 private final Set<NetconfOperationService> netconfOperationServices;
332 public SimulatedOperationProvider(final SessionIdProvider idProvider, final Set<Capability> caps) {
333 this.idProvider = idProvider;
334 final SimulatedOperationService simulatedOperationService = new SimulatedOperationService(caps, idProvider.getCurrentSessionId());
335 this.netconfOperationServices = Sets.<NetconfOperationService>newHashSet(simulatedOperationService);
339 public NetconfOperationServiceSnapshot openSnapshot(final String sessionIdForReporting) {
340 return new SimulatedServiceSnapshot(idProvider, netconfOperationServices);
343 public void addService(final NetconfOperationService monitoringService) {
344 netconfOperationServices.add(monitoringService);
347 private static class SimulatedServiceSnapshot implements NetconfOperationServiceSnapshot {
348 private final SessionIdProvider idProvider;
349 private final Set<NetconfOperationService> netconfOperationServices;
351 public SimulatedServiceSnapshot(final SessionIdProvider idProvider, final Set<NetconfOperationService> netconfOperationServices) {
352 this.idProvider = idProvider;
353 this.netconfOperationServices = netconfOperationServices;
357 public String getNetconfSessionIdForReporting() {
358 return String.valueOf(idProvider.getCurrentSessionId());
362 public Set<NetconfOperationService> getServices() {
363 return netconfOperationServices;
367 public void close() throws Exception {}
370 static class SimulatedOperationService implements NetconfOperationService {
371 private final Set<Capability> capabilities;
372 private final long currentSessionId;
374 public SimulatedOperationService(final Set<Capability> capabilities, final long currentSessionId) {
375 this.capabilities = capabilities;
376 this.currentSessionId = currentSessionId;
380 public Set<Capability> getCapabilities() {
385 public Set<NetconfOperation> getNetconfOperations() {
386 final DataList storage = new DataList();
387 final SimulatedGet sGet = new SimulatedGet(String.valueOf(currentSessionId), storage);
388 final SimulatedEditConfig sEditConfig = new SimulatedEditConfig(String.valueOf(currentSessionId), storage);
389 final SimulatedGetConfig sGetConfig = new SimulatedGetConfig(String.valueOf(currentSessionId), storage);
390 final SimulatedCommit sCommit = new SimulatedCommit(String.valueOf(currentSessionId));
391 return Sets.<NetconfOperation>newHashSet(sGet, sGetConfig, sEditConfig, sCommit);
395 public void close() {
401 private class LoggingMonitoringService implements SessionMonitoringService {
403 public void onSessionUp(final NetconfManagementSession session) {
404 LOG.debug("Session {} established", session);
408 public void onSessionDown(final NetconfManagementSession session) {
409 LOG.debug("Session {} down", session);