Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 922b231

Browse files
committed
Adding Java binary to invoke YANG compiler from command line.
Change-Id: Iab19e3b91cfb44da67650f857a9045c30c2dda7b
1 parent 289d2ca commit 922b231

File tree

7 files changed

+209
-4
lines changed

7 files changed

+209
-4
lines changed

compiler/base/parser/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
</parent>
2626

2727
<artifactId>onos-yang-compiler-parser</artifactId>
28-
<version>2.5-SNAPSHOT</version>
2928
<packaging>bundle</packaging>
3029

3130
<dependencies>

compiler/base/tool/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
</parent>
2828

2929
<artifactId>onos-yang-compiler-tool</artifactId>
30-
<version>2.5-SNAPSHOT</version>
3130
<packaging>bundle</packaging>
3231

3332
<dependencies>

compiler/plugin/main/pom.xml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!--
2+
~ Copyright 2018-present Open Networking Laboratory
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
<project xmlns="http://maven.apache.org/POM/4.0.0"
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
21+
<parent>
22+
<groupId>org.onosproject</groupId>
23+
<artifactId>onos-yang-compiler-plugin</artifactId>
24+
<version>2.5-SNAPSHOT</version>
25+
</parent>
26+
27+
<artifactId>onos-yang-compiler-main</artifactId>
28+
<packaging>bundle</packaging>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.onosproject</groupId>
33+
<artifactId>onos-yang-compiler-plugin-utils</artifactId>
34+
<version>${project.version}</version>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-shade-plugin</artifactId>
43+
<version>2.4.3</version>
44+
<executions>
45+
<execution>
46+
<phase>package</phase>
47+
<goals>
48+
<goal>shade</goal>
49+
</goals>
50+
</execution>
51+
</executions>
52+
<configuration>
53+
<transformers>
54+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
55+
<mainClass>org.onosproject.yang.compiler.main.YangCompilerMain</mainClass>
56+
</transformer>
57+
</transformers>
58+
</configuration>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
</project>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2018-present Open Networking Laboratory
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.onosproject.yang.compiler.main;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import org.onosproject.yang.compiler.tool.DefaultYangCompilationParam;
21+
import org.onosproject.yang.compiler.tool.YangCompilerManager;
22+
23+
import java.io.BufferedReader;
24+
import java.io.File;
25+
import java.io.IOException;
26+
import java.io.InputStreamReader;
27+
import java.nio.file.Path;
28+
import java.util.List;
29+
30+
import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.parseDepSchemaPath;
31+
32+
/**
33+
* Provides Java binary to invoke the YANG compiler as an executable.
34+
*/
35+
public final class YangCompilerMain {
36+
37+
private static final String USAGE =
38+
"usage: main modelId outputDir [-d dependencyJar1 -d dependencyJar2...] " +
39+
"inputYangFile1 inputYangFile2...";
40+
41+
private static final String DEPENDENCY_FLAG = "-d";
42+
private static final String STDIN_FLAG = "-";
43+
44+
// No use creating an instance
45+
private YangCompilerMain() {
46+
}
47+
48+
private static void usage() {
49+
System.err.println(USAGE);
50+
System.exit(1);
51+
}
52+
53+
/**
54+
* Main executable entry point.
55+
* <p>
56+
* usage: main modelId outputDir [-d dependencyJar1 -d dependencyJar2...] \
57+
* inputYangFile1 inputYangFile2...
58+
*
59+
* @param args see usage above
60+
* @throws IOException if issues arise when writing out generated classes
61+
*/
62+
public static void main(String[] args) throws IOException {
63+
if (args.length < 3) {
64+
usage();
65+
}
66+
67+
ImmutableList.Builder<Path> yangFiles = ImmutableList.builder();
68+
ImmutableList.Builder<Path> depJars = ImmutableList.builder();
69+
70+
// Scan tail arguments and separate dependencies from input sources
71+
for (int i = 2; i < args.length; i++) {
72+
if (DEPENDENCY_FLAG.equals(args[i])) {
73+
i++;
74+
if (i >= args.length) {
75+
usage();
76+
}
77+
depJars.add(new File(args[i]).toPath());
78+
} else if (STDIN_FLAG.equals(args[i])) {
79+
if (i != args.length - 1) {
80+
usage();
81+
}
82+
try (BufferedReader input = new BufferedReader(new InputStreamReader(System.in))) {
83+
String line;
84+
while ((line = input.readLine()) != null) {
85+
yangFiles.add(new File(line).toPath());
86+
}
87+
}
88+
89+
} else {
90+
yangFiles.add(new File(args[i]).toPath());
91+
}
92+
}
93+
94+
runYangCompiler(args[0], args[1], depJars.build(), yangFiles.build());
95+
}
96+
97+
// Runs the YANG compiler on the YANG sources in the specified directory.
98+
private static void runYangCompiler(String modelId, String outputDir,
99+
List<Path> depJars, List<Path> yangFiles)
100+
throws IOException {
101+
File dir = new File(outputDir);
102+
if (!dir.exists() && !dir.mkdirs()) {
103+
throw new IOException("Unable to create output directory");
104+
}
105+
106+
// Prepare the compilation parameter
107+
DefaultYangCompilationParam.Builder param = DefaultYangCompilationParam.builder()
108+
.setCodeGenDir(new File(outputDir, "src").toPath())
109+
.setMetadataGenDir(new File(outputDir, "schema").toPath())
110+
.setModelId(modelId);
111+
112+
for (Path d : depJars) {
113+
File jar = parseDepSchemaPath(d.toString(), outputDir);
114+
if (jar != null) {
115+
param.addDependentSchema(jar.toPath());
116+
}
117+
}
118+
119+
// Enumerate all input YANG source files.
120+
yangFiles.forEach(param::addYangFile);
121+
122+
// Run the YANG compiler and collect the results
123+
new YangCompilerManager().compileYangFiles(param.build());
124+
}
125+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2018-present Open Networking Laboratory
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Provides Java binary to invoke the YANG compiler as an executable.
19+
*/
20+
package org.onosproject.yang.compiler.main;

compiler/plugin/maven/pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
</parent>
2626

2727
<artifactId>onos-yang-compiler-maven-plugin</artifactId>
28-
<version>2.5-SNAPSHOT</version>
2928
<packaging>maven-plugin</packaging>
3029

3130
<dependencies>
@@ -100,7 +99,7 @@
10099
<dependency>
101100
<groupId>org.onosproject</groupId>
102101
<artifactId>onos-yang-compiler-plugin-utils</artifactId>
103-
<version>2.5-SNAPSHOT</version>
102+
<version>${project.version}</version>
104103
</dependency>
105104
</dependencies>
106105

compiler/plugin/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<modules>
3535
<module>maven</module>
3636
<module>buck</module>
37+
<module>main</module>
3738
<module>utils</module>
3839
</modules>
3940

0 commit comments

Comments
 (0)