Merge "Added the Buffer-Id to packet-in."
[controller.git] / opendaylight / netconf / netconf-testtool / src / main / java / org / opendaylight / controller / netconf / test / tool / Main.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.test.tool;
10
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static com.google.common.base.Preconditions.checkNotNull;
13 import static com.google.common.base.Preconditions.checkState;
14
15 import com.google.common.io.Files;
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.util.List;
21
22 import net.sourceforge.argparse4j.ArgumentParsers;
23 import net.sourceforge.argparse4j.annotation.Arg;
24 import net.sourceforge.argparse4j.inf.ArgumentParser;
25 import net.sourceforge.argparse4j.inf.ArgumentParserException;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.base.Charsets;
31 import com.google.common.io.CharStreams;
32
33 public final class Main {
34
35     // TODO add logback config
36
37     // TODO make exi configurable
38
39     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
40
41     static class Params {
42
43         @Arg(dest = "schemas-dir")
44         public File schemasDir;
45
46         @Arg(dest = "devices-count")
47         public int deviceCount;
48
49         @Arg(dest = "starting-port")
50         public int startingPort;
51
52         @Arg(dest = "generate-configs-dir")
53         public File generateConfigsDir;
54
55         @Arg(dest = "generate-configs-batch-size")
56         public int generateConfigBatchSize;
57
58         @Arg(dest = "ssh")
59         public boolean ssh;
60
61         static ArgumentParser getParser() {
62             final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testool");
63             parser.addArgument("--devices-count")
64                     .type(Integer.class)
65                     .setDefault(1)
66                     .type(Integer.class)
67                     .help("Number of simulated netconf devices to spin")
68                     .dest("devices-count");
69
70             parser.addArgument("--schemas-dir")
71                     .type(File.class)
72                     .required(true)
73                     .help("Directory containing yang schemas to describe simulated devices")
74                     .dest("schemas-dir");
75
76             parser.addArgument("--starting-port")
77                     .type(Integer.class)
78                     .setDefault(17830)
79                     .help("First port for simulated device. Each other device will have previous+1 port number")
80                     .dest("starting-port");
81
82             parser.addArgument("--generate-configs-batch-size")
83                     .type(Integer.class)
84                     .setDefault(100)
85                     .help("Number of connector configs per generated file")
86                     .dest("generate-configs-batch-size");
87
88             parser.addArgument("--generate-configs-dir")
89                     .type(File.class)
90                     .help("Directory where initial config files for ODL distribution should be generated")
91                     .dest("generate-configs-dir");
92
93             parser.addArgument("--ssh")
94                     .type(Boolean.class)
95                     .setDefault(true)
96                     .help("Whether to use ssh for transport or just pure tcp")
97                     .dest("ssh");
98
99             return parser;
100         }
101
102         void validate() {
103             checkArgument(deviceCount > 0, "Device count has to be > 0");
104             checkArgument(startingPort > 1024, "Starting port has to be > 1024");
105
106             checkArgument(schemasDir.exists(), "Schemas dir has to exist");
107             checkArgument(schemasDir.isDirectory(), "Schemas dir has to be a directory");
108             checkArgument(schemasDir.canRead(), "Schemas dir has to be readable");
109         }
110     }
111
112     public static void main(final String[] args) {
113         final Params params = parseArgs(args, Params.getParser());
114         params.validate();
115
116         final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator();
117         try {
118             final List<Integer> openDevices = netconfDeviceSimulator.start(params);
119             if(params.generateConfigsDir != null) {
120                 new ConfigGenerator(params.generateConfigsDir, openDevices).generate(params.ssh, params.generateConfigBatchSize);
121             }
122         } catch (final Exception e) {
123             LOG.error("Unhandled exception", e);
124             netconfDeviceSimulator.close();
125             System.exit(1);
126         }
127
128         // Block main thread
129         synchronized (netconfDeviceSimulator) {
130             try {
131                 netconfDeviceSimulator.wait();
132             } catch (final InterruptedException e) {
133                 throw new RuntimeException(e);
134             }
135         }
136     }
137
138
139     private static Params parseArgs(final String[] args, final ArgumentParser parser) {
140         final Params opt = new Params();
141         try {
142             parser.parseArgs(args, opt);
143             return opt;
144         } catch (final ArgumentParserException e) {
145             parser.handleError(e);
146         }
147
148         System.exit(1);
149         return null;
150     }
151
152     private static class ConfigGenerator {
153         public static final String NETCONF_CONNECTOR_XML = "/initial/99-netconf-connector.xml";
154         public static final String NETCONF_CONNECTOR_NAME = "controller-config";
155         public static final String NETCONF_CONNECTOR_PORT = "1830";
156         public static final String NETCONF_USE_SSH = "false";
157         public static final String SIM_DEVICE_SUFFIX = "-sim-device";
158
159         private final File directory;
160         private final List<Integer> openDevices;
161
162         public ConfigGenerator(final File directory, final List<Integer> openDevices) {
163             this.directory = directory;
164             this.openDevices = openDevices;
165         }
166
167         public void generate(final boolean useSsh, final int batchSize) {
168             if(directory.exists() == false) {
169                 checkState(directory.mkdirs(), "Unable to create folder %s" + directory);
170             }
171
172             try(InputStream stream = Main.class.getResourceAsStream(NETCONF_CONNECTOR_XML)) {
173                 checkNotNull(stream, "Cannot load %s", NETCONF_CONNECTOR_XML);
174                 String configBlueprint = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
175
176                 // TODO make address configurable
177                 checkState(configBlueprint.contains(NETCONF_CONNECTOR_NAME));
178                 checkState(configBlueprint.contains(NETCONF_CONNECTOR_PORT));
179                 checkState(configBlueprint.contains(NETCONF_USE_SSH));
180                 configBlueprint = configBlueprint.replace(NETCONF_CONNECTOR_NAME, "%s");
181                 configBlueprint = configBlueprint.replace(NETCONF_CONNECTOR_PORT, "%s");
182                 configBlueprint = configBlueprint.replace(NETCONF_USE_SSH, "%s");
183
184                 final String before = configBlueprint.substring(0, configBlueprint.indexOf("<module>"));
185                 final String middleBlueprint = configBlueprint.substring(configBlueprint.indexOf("<module>"), configBlueprint.indexOf("</module>") + "</module>".length());
186                 final String after = configBlueprint.substring(configBlueprint.indexOf("</module>") + "</module>".length());
187
188                 int connectorCount = 0;
189                 Integer batchStart = null;
190                 StringBuilder b = new StringBuilder();
191                 b.append(before);
192
193                 for (final Integer openDevice : openDevices) {
194                     if(batchStart == null) {
195                         batchStart = openDevice;
196                     }
197
198                     final String name = String.valueOf(openDevice) + SIM_DEVICE_SUFFIX;
199                     final String configContent = String.format(middleBlueprint, name, String.valueOf(openDevice), String.valueOf(!useSsh));
200                     b.append(configContent);
201                     connectorCount++;
202                     if(connectorCount == batchSize) {
203                         b.append(after);
204                         Files.write(b.toString(), new File(directory, String.format("simulated-devices_%d-%d.xml", batchStart, openDevice)), Charsets.UTF_8);
205                         connectorCount = 0;
206                         b = new StringBuilder();
207                         b.append(before);
208                         batchStart = null;
209                     }
210                 }
211
212                 // Write remaining
213                 if(connectorCount != 0) {
214                     b.append(after);
215                     Files.write(b.toString(), new File(directory, String.format("simulated-devices_%d-%d.xml", batchStart, openDevices.get(openDevices.size() - 1))), Charsets.UTF_8);
216                 }
217
218                 LOG.info("Config files generated in {}", directory);
219             } catch (final IOException e) {
220                 throw new RuntimeException("Unable to generate config files", e);
221             }
222         }
223     }
224 }