summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfrank feng <frank.fengchong@huawei.com>2023-09-15 11:10:27 +0800
committerfrank feng <frank.fengchong@huawei.com>2023-09-15 11:10:41 +0800
commitcc0fc052c1204a36dd997d4ce6d67a26ef80b361 (patch)
tree3270c52800c3d8bc3bbbe1207e0229827d533244
parentc591d5e079dee38969ddd6366481bb1b476aaa31 (diff)
modify yang-comparator and yang-compiler for external plugin call, and fix bug for yang-tree-generator plugin
Issue-ID: MODELING-680 Change-Id: I8ccbfdce79344c4734fe10931fa818601701f350 Signed-off-by: frank feng <frank.fengchong@huawei.com>
-rw-r--r--yang-comparator/pom.xml2
-rw-r--r--yang-comparator/src/main/java/org/onap/modeling/yangkit/comparator/app/YangComparatorPlugin.java52
-rw-r--r--yang-comparator/src/main/java/org/onap/modeling/yangkit/comparator/app/YangComparatorPluginParameter.java61
-rw-r--r--yang-comparator/src/main/resources/plugins.json6
-rw-r--r--yang-compiler/README.md8
-rw-r--r--yang-compiler/build.json40
-rw-r--r--yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/PluginInfo.java37
-rw-r--r--yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/PluginParameterInfo.java12
-rw-r--r--yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/YangCompiler.java13
-rw-r--r--yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/app/YangCompilerRunner.java2
-rw-r--r--yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/plugin/yangtree/YangTreeGenerator.java2
-rw-r--r--yang-compiler/src/main/resources/plugins.json8
12 files changed, 166 insertions, 77 deletions
diff --git a/yang-comparator/pom.xml b/yang-comparator/pom.xml
index cac954e..38884f8 100644
--- a/yang-comparator/pom.xml
+++ b/yang-comparator/pom.xml
@@ -60,7 +60,7 @@
<dependency>
<groupId>io.github.yang-central.yangkit</groupId>
<artifactId>yangkit-parser</artifactId>
- <version>1.3.5</version>
+ <version>1.3.6</version>
</dependency>
<dependency>
<groupId>org.onap.modeling.yangkit</groupId>
diff --git a/yang-comparator/src/main/java/org/onap/modeling/yangkit/comparator/app/YangComparatorPlugin.java b/yang-comparator/src/main/java/org/onap/modeling/yangkit/comparator/app/YangComparatorPlugin.java
index a46b97d..45b3941 100644
--- a/yang-comparator/src/main/java/org/onap/modeling/yangkit/comparator/app/YangComparatorPlugin.java
+++ b/yang-comparator/src/main/java/org/onap/modeling/yangkit/comparator/app/YangComparatorPlugin.java
@@ -18,6 +18,7 @@ package org.onap.modeling.yangkit.comparator.app;
import com.google.gson.JsonElement;
+import java.io.File;
import java.io.IOException;
import java.util.List;
@@ -47,44 +48,8 @@ import org.yangcentral.yangkit.utils.xml.XmlWriter;
*/
public class YangComparatorPlugin implements YangCompilerPlugin {
@Override
- public YangCompilerPluginParameter getParameter(String name, JsonElement value)
- throws YangCompilerException {
- if (!name.equals("old-yang") && !name.equals("settings")
- && !name.equals("compare-type") && !name.equals("rule")
- && !name.equals("result")) {
- throw new YangCompilerException("unrecognized parameter:" + name);
- }
- if (name.equals("old-yang") || name.equals("compare-type")) {
- YangCompilerPluginParameter yangCompilerPluginParameter = new YangCompilerPluginParameter() {
- @Override
- public String getName() {
- return name;
- }
-
- @Override
- public Object getValue() throws YangCompilerException {
- if (name.equals("old-yang")) {
- return BuildOption.parseSources(value);
- }
-
- if (name.equals("compare-type")) {
- if (value.equals("stmt")) {
- return CompareType.STMT;
- } else if (value.equals("tree")) {
- return CompareType.TREE;
- } else if (value.equals("compatible-check")) {
- return CompareType.COMPATIBLE_CHECK;
- }
- throw new YangCompilerException("unrecognized value:" + value);
- }
- return null;
- }
-
- };
- return yangCompilerPluginParameter;
- }
- return YangCompilerPlugin.super.getParameter(name, value);
-
+ public YangCompilerPluginParameter getParameter(String name, JsonElement value) {
+ return new YangComparatorPluginParameter(name,value);
}
@Override
@@ -124,6 +89,17 @@ public class YangComparatorPlugin implements YangCompilerPlugin {
if (resultPath == null) {
throw new YangCompilerException("missing mandatory parameter:result");
}
+ File outputFile = new File(resultPath);
+ if (!outputFile.exists()) {
+ if (outputFile.getParentFile() != null && !outputFile.getParentFile().exists()) {
+ outputFile.getParentFile().mkdirs();
+ }
+ try {
+ outputFile.createNewFile();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
YangSchemaContext oldSchemaContext = YangCompilerUtil.buildSchemaContext(sources, settings);
ValidatorResult oldResult = oldSchemaContext.validate();
if (!oldResult.isOk()) {
diff --git a/yang-comparator/src/main/java/org/onap/modeling/yangkit/comparator/app/YangComparatorPluginParameter.java b/yang-comparator/src/main/java/org/onap/modeling/yangkit/comparator/app/YangComparatorPluginParameter.java
new file mode 100644
index 0000000..7f78022
--- /dev/null
+++ b/yang-comparator/src/main/java/org/onap/modeling/yangkit/comparator/app/YangComparatorPluginParameter.java
@@ -0,0 +1,61 @@
+/*
+Copyright 2023 Huawei Technologies
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+ */
+
+package org.onap.modeling.yangkit.comparator.app;
+
+import com.google.gson.JsonElement;
+import org.onap.modeling.yangkit.comparator.CompareType;
+import org.onap.modeling.yangkit.compiler.BuildOption;
+import org.onap.modeling.yangkit.compiler.YangCompilerException;
+import org.onap.modeling.yangkit.compiler.plugin.YangCompilerPluginParameter;
+
+public class YangComparatorPluginParameter implements YangCompilerPluginParameter {
+ private String name;
+ private JsonElement value;
+
+ public YangComparatorPluginParameter(String name, JsonElement value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public Object getValue() throws YangCompilerException {
+ if (!name.equals("old-yang") && !name.equals("settings")
+ && !name.equals("compare-type") && !name.equals("rule")
+ && !name.equals("result")) {
+ throw new YangCompilerException("unrecognized parameter:" + name);
+ }
+ if (name.equals("old-yang")) {
+ return BuildOption.parseSources(value);
+ } else if (name.equals("compare-type")) {
+ if (value.getAsString().equals("stmt")) {
+ return CompareType.STMT;
+ } else if (value.getAsString().equals("tree")) {
+ return CompareType.TREE;
+ } else if (value.getAsString().equals("compatible-check")) {
+ return CompareType.COMPATIBLE_CHECK;
+ }
+ throw new YangCompilerException("unrecognized value:" + value + " for parameter:" + name);
+ } else {
+ return value.getAsString();
+ }
+ }
+}
diff --git a/yang-comparator/src/main/resources/plugins.json b/yang-comparator/src/main/resources/plugins.json
index 4534d4d..39d196d 100644
--- a/yang-comparator/src/main/resources/plugins.json
+++ b/yang-comparator/src/main/resources/plugins.json
@@ -3,13 +3,13 @@
"plugin": [
{
"name": "yang_comparator",
- "class-path": "yang-comparator-1.0-SNAPSHOT.jar",
- "class": "com.huawei.yang.comparator.app.YangComparatorPlugin",
+ "class-path": "yang-comparator/yang-comparator-1.0.0-SNAPSHOT.jar",
+ "class": "org.onap.modeling.yangkit.comparator.app.YangComparatorPlugin",
"description": "a plugin for comparing two yang schema.",
"parameter": [
{
"name": "old-yang",
- "description": "mandatory,the old version yang directory."
+ "description": "mandatory,the old version yang sources."
},
{
"name": "settings",
diff --git a/yang-compiler/README.md b/yang-compiler/README.md
index 264d2f1..e151cfc 100644
--- a/yang-compiler/README.md
+++ b/yang-compiler/README.md
@@ -142,7 +142,7 @@ The plugin system of Yang compiler support built-in plugin and external plugin.
"name": "validator_plugin",
- "class": "org.yangcentral.yangkit.compiler.plugin.validator.YangValidator",
+ "class": "org.onap.modelling.yangkit.compiler.plugin.validator.YangValidator",
"description": "a plugin for validating yang files",
@@ -179,12 +179,12 @@ The plugin system of Yang compiler support built-in plugin and external plugin.
{
"name": "yang_comparator",
"class-path": "yang-comparator/yang-comparator-1.0-SNAPSHOT.jar",
- "class": "com.huawei.yang.comparator.YangComparatorPlugin",
+ "class": "org.onap.modelling.yangkit.comparator.app.YangComparatorPlugin",
"description": "a plugin for comparing two yang schema.",
"parameter": [
{
"name": "old-yang",
- "description": "mandatory,the old version yang directory."
+ "description": "mandatory,the old version yang sources."
},
{
"name": "settings",
@@ -307,7 +307,7 @@ The plugin system of Yang compiler support built-in plugin and external plugin.
```
### &emsp;Commandline
```
-# java -jar yang-compiler-1.0-SNAPSHOT.jar [option=<_build.json_>] [install]
+# java -jar yang-compiler-1.0.0-SNAPSHOT.jar [option=<_build.json_>] [install]
```
#### &emsp;&emsp;Parameters
1. option: optional, specify the build option. It's the path of build.json, if not present, the build.json in current directory will be used.
diff --git a/yang-compiler/build.json b/yang-compiler/build.json
index 43d258e..2625c76 100644
--- a/yang-compiler/build.json
+++ b/yang-compiler/build.json
@@ -1,22 +1,7 @@
{
"yang": {
- "module": [
- {
- "name": "ietf-interfaces",
- "revision": ""
- },
- {
- "name": "huawei-ifm",
- "revision": "2022-08-06"
- },
- {
- "name": "huawei-bgp",
- "revision": ""
- },
- {
- "name": "huawei-network-instance",
- "revision": ""
- }
+ "dir" : [
+ "C:\\Users\\Administrator\\IdeaProjects\\huawei-yang\\network-router\\8.21.0\\atn-980b"
]
},
"plugin": [
@@ -34,6 +19,27 @@
]
},
{
+ "name": "yang_comparator",
+ "parameter": [
+ {
+ "name": "old-yang",
+ "value": {
+ "dir" : [
+ "C:\\Users\\Administrator\\IdeaProjects\\huawei-yang\\network-router\\8.20.0\\atn980b"
+ ]
+ }
+ },
+ {
+ "name": "compare-type",
+ "value": "stmt"
+ },
+ {
+ "name" : "result",
+ "value" : "out/diff_stmt.xml"
+ }
+ ]
+ },
+ {
"name": "yang_statistics",
"parameter" : [
{
diff --git a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/PluginInfo.java b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/PluginInfo.java
index 63b82ef..628f72b 100644
--- a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/PluginInfo.java
+++ b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/PluginInfo.java
@@ -38,6 +38,8 @@ import org.onap.modeling.yangkit.compiler.plugin.YangCompilerPlugin;
@SuppressWarnings("ALL")
public class PluginInfo {
private final String pluginName;
+
+ private URLClassLoader classLoader;
private final YangCompilerPlugin plugin;
private String description;
@@ -63,6 +65,14 @@ public class PluginInfo {
return pluginName;
}
+ public URLClassLoader getClassLoader() {
+ return classLoader;
+ }
+
+ public void setClassLoader(URLClassLoader classLoader) {
+ this.classLoader = classLoader;
+ }
+
/**
* get the plugin.
*
@@ -118,6 +128,7 @@ public class PluginInfo {
public static PluginInfo parse(File pluginFile, JsonElement jsonElement) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
String pluginName = jsonObject.get("name").getAsString();
+ URLClassLoader classLoader = null;
String classPath = null;
JsonElement classPathElement = jsonObject.get("class-path");
if (classPathElement != null) {
@@ -139,11 +150,10 @@ public class PluginInfo {
return null;
}
URL[] cp = {file.toURI().toURL()};
- try (URLClassLoader classLoader = new URLClassLoader(cp)) {
- pluginClass = (Class<? extends YangCompilerPlugin>) classLoader.loadClass(className);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
+ classLoader = new URLClassLoader(cp);
+
+ pluginClass = (Class<? extends YangCompilerPlugin>) classLoader.loadClass(className);
+
} else {
pluginClass = (Class<? extends YangCompilerPlugin>) Class.forName(className);
}
@@ -151,6 +161,7 @@ public class PluginInfo {
Constructor<? extends YangCompilerPlugin> constructor = pluginClass.getConstructor();
YangCompilerPlugin yangCompilerPlugin = constructor.newInstance();
PluginInfo pluginInfo = new PluginInfo(pluginName, yangCompilerPlugin);
+ pluginInfo.setClassLoader(classLoader);
if (jsonObject.get("description") != null) {
String description = jsonObject.get("description").getAsString();
pluginInfo.setDescription(description);
@@ -185,4 +196,20 @@ public class PluginInfo {
throw new RuntimeException(e);
}
}
+
+ @Override
+ public String toString() {
+ return "PluginInfo{"
+ + "pluginName='"
+ + pluginName
+ + '\''
+ + ", plugin="
+ + plugin
+ + ", description='"
+ + description
+ + '\''
+ + ", parameters="
+ + parameters
+ + '}';
+ }
}
diff --git a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/PluginParameterInfo.java b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/PluginParameterInfo.java
index 526b94a..aa34b9d 100644
--- a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/PluginParameterInfo.java
+++ b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/PluginParameterInfo.java
@@ -56,4 +56,16 @@ public class PluginParameterInfo {
public void setDescription(String description) {
this.description = description;
}
+
+ @Override
+ public String toString() {
+ return "PluginParameterInfo{"
+ + "name='"
+ + name
+ + '\''
+ + ", description='"
+ + description
+ + '\''
+ + '}';
+ }
}
diff --git a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/YangCompiler.java b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/YangCompiler.java
index ccbfcbc..d5823fb 100644
--- a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/YangCompiler.java
+++ b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/YangCompiler.java
@@ -17,7 +17,6 @@ limitations under the License.
package org.onap.modeling.yangkit.compiler;
-
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@@ -121,12 +120,13 @@ public class YangCompiler {
/**
* build schema context from build option.
+ *
* @return yang schema context
*/
public YangSchemaContext buildSchemaContext() {
YangSchemaContext schemaContext = null;
try {
- schemaContext = YangCompilerUtil.buildSchemaContext(buildOption.getSources(),getSettings());
+ schemaContext = YangCompilerUtil.buildSchemaContext(buildOption.getSources(), getSettings());
return schemaContext;
} catch (YangCompilerException e) {
@@ -189,6 +189,12 @@ public class YangCompiler {
continue;
}
YangCompilerPlugin plugin = pluginInfo.getPlugin();
+ ClassLoader curClassLoader = Thread.currentThread().getContextClassLoader();
+ ClassLoader pluginClassLoader = curClassLoader;
+ if (pluginInfo.getClassLoader() != null) {
+ pluginClassLoader = pluginInfo.getClassLoader();
+ Thread.currentThread().setContextClassLoader(pluginClassLoader);
+ }
try {
List<YangCompilerPluginParameter> parameters = new ArrayList<>();
if (!pluginBuilder.getParameters().isEmpty()) {
@@ -206,6 +212,9 @@ public class YangCompiler {
} catch (YangCompilerException e) {
logger.error(e.getMessage());
}
+ if (pluginClassLoader != curClassLoader) {
+ Thread.currentThread().setContextClassLoader(curClassLoader);
+ }
}
ValidatorResultBuilder validatorResultBuilder = new ValidatorResultBuilder();
List<ValidatorRecord<?, ?>> records = validatorResult.getRecords();
diff --git a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/app/YangCompilerRunner.java b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/app/YangCompilerRunner.java
index 217ffad..11736bb 100644
--- a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/app/YangCompilerRunner.java
+++ b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/app/YangCompilerRunner.java
@@ -64,12 +64,10 @@ public class YangCompilerRunner {
.toURI());
File pluginsDir = new File(programDir.getParentFile(), "plugins");
if (!pluginsDir.exists()) {
- System.out.println("[WARNING]plugins dir:" + pluginsDir.getAbsolutePath() + " is not exists");
return;
}
File pluginsFile = new File(pluginsDir, "plugins.json");
if (pluginsFile.exists()) {
- System.out.println("[INFO]reading the information of plugins from:" + pluginsFile.getAbsolutePath());
List<PluginInfo> pluginInfos = parsePlugins(pluginsFile, FileUtil.readFile2String(pluginsFile));
for (PluginInfo pluginInfo : pluginInfos) {
yangCompiler.addPluginInfo(pluginInfo);
diff --git a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/plugin/yangtree/YangTreeGenerator.java b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/plugin/yangtree/YangTreeGenerator.java
index b5b5643..6561ab4 100644
--- a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/plugin/yangtree/YangTreeGenerator.java
+++ b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/plugin/yangtree/YangTreeGenerator.java
@@ -396,7 +396,7 @@ public class YangTreeGenerator implements YangCompilerPlugin {
SchemaNodeContainer parent = dataNode.getClosestAncestorNode();
if (parent instanceof YangList) {
YangList list = (YangList) parent;
- if (list.getKey().getKeyNode(dataNode.getIdentifier()) != null) {
+ if (list.getKey() != null && list.getKey().getKeyNode(dataNode.getIdentifier()) != null) {
return true;
}
}
diff --git a/yang-compiler/src/main/resources/plugins.json b/yang-compiler/src/main/resources/plugins.json
index afd32f0..a984d08 100644
--- a/yang-compiler/src/main/resources/plugins.json
+++ b/yang-compiler/src/main/resources/plugins.json
@@ -4,7 +4,7 @@
{
"name": "schema_validator",
"class-path": "",
- "class": "org.yangcentral.yangkit.compiler.plugin.validator.YangValidator",
+ "class": "org.onap.modeling.yangkit.compiler.plugin.validator.YangValidator",
"description": "a plugin for validating yang files",
"parameter": [
{
@@ -16,7 +16,7 @@
{
"name": "yang_statistics",
"class-path": "",
- "class": "org.yangcentral.yangkit.compiler.plugin.stat.YangStatistics",
+ "class": "org.onap.modeling.yangkit.compiler.plugin.stat.YangStatistics",
"description": "a plugin for retrieving yang statistics",
"parameter": [
{
@@ -27,7 +27,7 @@
},
{
"name": "yangtree_generator",
- "class": "org.yangcentral.yangkit.compiler.plugin.yangtree.YangTreeGenerator",
+ "class": "org.onap.modeling.yangkit.compiler.plugin.yangtree.YangTreeGenerator",
"description": "a plugin for generating yang tree",
"parameter": [
{
@@ -46,7 +46,7 @@
},
{
"name": "yangpackage_generator",
- "class": "org.yangcentral.yangkit.compiler.plugin.yangpackage.YangPackageGenerator",
+ "class": "org.onap.modeling.yangkit.compiler.plugin.yangpackage.YangPackageGenerator",
"description": "a plugin for generating yang package",
"parameter": [
{