From cc0fc052c1204a36dd997d4ce6d67a26ef80b361 Mon Sep 17 00:00:00 2001 From: frank feng Date: Fri, 15 Sep 2023 11:10:27 +0800 Subject: 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 --- .../onap/modeling/yangkit/compiler/PluginInfo.java | 37 +++++++++++++++++++--- .../yangkit/compiler/PluginParameterInfo.java | 12 +++++++ .../modeling/yangkit/compiler/YangCompiler.java | 13 ++++++-- .../yangkit/compiler/app/YangCompilerRunner.java | 2 -- .../plugin/yangtree/YangTreeGenerator.java | 2 +- yang-compiler/src/main/resources/plugins.json | 8 ++--- 6 files changed, 60 insertions(+), 14 deletions(-) (limited to 'yang-compiler/src/main') 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) classLoader.loadClass(className); - } catch (IOException e) { - throw new RuntimeException(e); - } + classLoader = new URLClassLoader(cp); + + pluginClass = (Class) classLoader.loadClass(className); + } else { pluginClass = (Class) Class.forName(className); } @@ -151,6 +161,7 @@ public class PluginInfo { Constructor 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 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> 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 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": [ { -- cgit 1.2.3-korg