2 * Copyright (c) 2016 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
8 package org.opendaylight.yangtools.yang.parser.system.test;
10 import com.google.common.base.Stopwatch;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
18 import org.apache.commons.cli.BasicParser;
19 import org.apache.commons.cli.CommandLine;
20 import org.apache.commons.cli.CommandLineParser;
21 import org.apache.commons.cli.HelpFormatter;
22 import org.apache.commons.cli.Option;
23 import org.apache.commons.cli.Options;
24 import org.apache.commons.cli.ParseException;
25 import org.apache.log4j.BasicConfigurator;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 * Main class of Yang parser system test.
32 * yang-system-test [-f features] [-h help] [-p path] [-v verbose] yangFiles...
33 * -f,--features <arg> features is a string in the form
34 * [feature(,feature)*] and feature is a string in the form
35 * [($namespace?revision=$revision)$local_name].
36 * This option is used to prune the data model by removing
37 * all nodes that are defined with a "if-feature".
38 * -h,--help print help message and exit.
39 * -p,--path <arg> path is a colon (:) separated list of directories
40 * to search for yang modules.
41 * -r, --recursive recursive search of directories specified by -p option
42 * -v,--verbose shows details about the results of test running.
46 private static final Logger LOG = Logger.getLogger(Main.class.getName());
47 private static final int MB = 1024 * 1024;
49 private static Options createOptions() {
50 final Options options = new Options();
52 final Option help = new Option("h", "help", false, "print help message and exit.");
53 help.setRequired(false);
54 options.addOption(help);
56 final Option path = new Option("p", "path", true,
57 "path is a colon (:) separated list of directories to search for yang modules.");
58 path.setRequired(false);
59 options.addOption(path);
61 final Option recursiveSearch = new Option("r", "recursive", false,
62 "recursive search of directories specified by -p option.");
63 recursiveSearch.setRequired(false);
64 options.addOption(recursiveSearch);
66 final Option verbose = new Option("v", "verbose", false, "shows details about the results of test running.");
67 verbose.setRequired(false);
68 options.addOption(verbose);
70 final Option feature = new Option(
74 "features is a string in the form [feature(,feature)*] and feature is a string in the form "
75 + "[($namespace?revision=$revision)$local_name]. This option is used to prune the data model by removing "
76 + "all nodes that are defined with a \"if-feature\".");
77 feature.setRequired(false);
78 options.addOption(feature);
82 public static void main(final String[] args) {
84 BasicConfigurator.configure();
86 final HelpFormatter formatter = new HelpFormatter();
87 final Options options = createOptions();
88 final CommandLine arguments = parseArguments(args, options, formatter);
90 if (arguments.hasOption("help")) {
91 printHelp(options, formatter);
95 if (arguments.hasOption("verbose")) {
96 LOG.setLevel(Level.CONFIG);
98 LOG.setLevel(Level.SEVERE);
101 final List<String> yangLibDirs = initYangDirsPath(arguments);
102 final List<String> yangFiles = Arrays.asList(arguments.getArgs());
103 final HashSet<QName> supportedFeatures = initSupportedFeatures(arguments);
105 runSystemTest(yangLibDirs, yangFiles, supportedFeatures, arguments.hasOption("recursive"));
108 private static void runSystemTest(final List<String> yangLibDirs, final List<String> yangFiles,
109 final HashSet<QName> supportedFeatures, final boolean recursiveSearch) {
110 LOG.log(Level.INFO, "Yang model dirs: {0} ", yangLibDirs);
111 LOG.log(Level.INFO, "Yang model files: {0} ", yangFiles);
112 LOG.log(Level.INFO, "Supported features: {0} ", supportedFeatures);
114 SchemaContext context = null;
116 printMemoryInfo("start");
117 final Stopwatch stopWatch = Stopwatch.createStarted();
120 context = SystemTestUtils.parseYangSources(yangLibDirs, yangFiles, supportedFeatures, recursiveSearch);
121 } catch (final Exception e) {
122 LOG.log(Level.SEVERE, "Failed to create SchemaContext.", e);
127 LOG.log(Level.INFO, "Elapsed time: {0}", stopWatch);
128 printMemoryInfo("end");
129 LOG.log(Level.INFO, "SchemaContext resolved Successfully. {0}", context);
130 Runtime.getRuntime().gc();
131 printMemoryInfo("after gc");
134 private static List<String> initYangDirsPath(final CommandLine arguments) {
135 final List<String> yangDirs = new ArrayList<>();
136 if (arguments.hasOption("path")) {
137 for (final String pathArg : arguments.getOptionValues("path")) {
138 yangDirs.addAll(Arrays.asList(pathArg.split(":")));
144 private static HashSet<QName> initSupportedFeatures(final CommandLine arguments) {
145 HashSet<QName> supportedFeatures = null;
146 if (arguments.hasOption("features")) {
147 supportedFeatures = new HashSet<>();
148 for (final String pathArg : arguments.getOptionValues("features")) {
149 supportedFeatures.addAll(createQNames(pathArg.split(",")));
152 return supportedFeatures;
155 private static Collection<? extends QName> createQNames(final String[] featuresArg) {
156 final HashSet<QName> qnames = new HashSet<>();
157 for (final String featureStr : featuresArg) {
158 qnames.add(QName.create(featureStr));
164 private static CommandLine parseArguments(final String[] args, final Options options, final HelpFormatter formatter) {
165 final CommandLineParser parser = new BasicParser();
167 CommandLine cmd = null;
169 cmd = parser.parse(options, args);
170 } catch (final ParseException e) {
171 LOG.log(Level.SEVERE, "Failed to parse command line options.", e);
172 printHelp(options, formatter);
180 private static void printHelp(final Options options, final HelpFormatter formatter) {
181 formatter.printHelp("yang-system-test [-f features] [-h help] [-p path] [-v verbose] yangFiles...", options);
184 private static void printMemoryInfo(final String info) {
185 LOG.log(Level.INFO, "Memory INFO [{0}]: free {1}MB, used {2}MB, total {3}MB, max {4}MB", new Object[] { info,
186 Runtime.getRuntime().freeMemory() / MB,
187 (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / MB,
188 Runtime.getRuntime().totalMemory() / MB, Runtime.getRuntime().maxMemory() / MB });