diff options
author | frank feng <frank.fengchong@huawei.com> | 2023-09-13 11:40:58 +0800 |
---|---|---|
committer | frank feng <frank.fengchong@huawei.com> | 2023-09-13 14:12:20 +0800 |
commit | c591d5e079dee38969ddd6366481bb1b476aaa31 (patch) | |
tree | e056f675f70814a18fada5ea103ba3dd7c4c8d27 /yang-compiler/src/main | |
parent | 921f0af17f560e45b95668a1b00f895bca48039d (diff) |
modify yang-comparator, such as yang compiler plugin, and some java doc. modify some functions of yang-compiler.
Issue-ID: MODELING-680
Change-Id: I37784c6a3511cfffafe0e19bd4ebdb4ba9d9b958
Signed-off-by: frank feng <frank.fengchong@huawei.com>
Diffstat (limited to 'yang-compiler/src/main')
3 files changed, 82 insertions, 53 deletions
diff --git a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/BuildOption.java b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/BuildOption.java index 328d2a4..0c95e45 100644 --- a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/BuildOption.java +++ b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/BuildOption.java @@ -95,6 +95,65 @@ public class BuildOption { } /** + * parse the sources. + * @param yangElement yang element + * @return list of sources + */ + public static List<Source> parseSources(JsonElement yangElement) { + List<Source> sources = new ArrayList<>(); + JsonObject yang = yangElement.getAsJsonObject(); + JsonElement dirElement = yang.get("dir"); + if (dirElement != null) { + JsonArray dirArray = dirElement.getAsJsonArray(); + List<JsonElement> dirElementList = dirArray.asList(); + List<String> dirs = new ArrayList<>(); + for (JsonElement dirElementItem : dirElementList) { + String yangDir = dirElementItem.getAsString(); + dirs.add(yangDir); + } + DirectorySource directorySource = new DirectorySource(dirs); + sources.add(directorySource); + } + JsonElement filesElement = yang.get("file"); + if (filesElement != null) { + JsonArray fileArray = filesElement.getAsJsonArray(); + List<JsonElement> fileElementList = fileArray.asList(); + List<String> files = new ArrayList<>(); + for (JsonElement fileElementItem : fileElementList) { + String yangFile = fileElementItem.getAsString(); + files.add(yangFile); + } + FileSource fileSource = new FileSource(files); + sources.add(fileSource); + } + JsonElement modulesElement = yang.get("module"); + if (modulesElement != null) { + JsonArray moduleArray = modulesElement.getAsJsonArray(); + List<JsonElement> moduleList = moduleArray.asList(); + List<ModuleInfo> moduleInfos = new ArrayList<>(); + for (JsonElement moduleElement : moduleList) { + JsonObject moduleObject = moduleElement.getAsJsonObject(); + String name = moduleObject.get("name").getAsString(); + String revision = moduleObject.get("revision").getAsString(); + String organization = null; + if (moduleObject.get("organization") != null) { + organization = moduleObject.get("organization").getAsString(); + } + URI schema = null; + if (moduleObject.get("schema") != null) { + schema = URI.create(moduleObject.get("schema").getAsString()); + } + ModuleInfo moduleInfo = new ModuleInfo(name, revision, organization); + moduleInfo.setSchema(schema); + moduleInfos.add(moduleInfo); + } + ModuleSource moduleSource = new ModuleSource(moduleInfos); + sources.add(moduleSource); + } + return sources; + } + + /** * parse the build.json. * * @param jsonElement json element @@ -106,55 +165,7 @@ public class BuildOption { JsonElement yangElement = jsonObject.get("yang"); if (yangElement != null) { - JsonObject yang = yangElement.getAsJsonObject(); - JsonElement dirElement = yang.get("dir"); - if (dirElement != null) { - JsonArray dirArray = dirElement.getAsJsonArray(); - List<JsonElement> dirElementList = dirArray.asList(); - List<String> dirs = new ArrayList<>(); - for (JsonElement dirElementItem : dirElementList) { - String yangDir = dirElementItem.getAsString(); - dirs.add(yangDir); - } - DirectorySource directorySource = new DirectorySource(dirs); - buildOption.addSource(directorySource); - } - JsonElement filesElement = yang.get("file"); - if (filesElement != null) { - JsonArray fileArray = filesElement.getAsJsonArray(); - List<JsonElement> fileElementList = fileArray.asList(); - List<String> files = new ArrayList<>(); - for (JsonElement fileElementItem : fileElementList) { - String yangFile = fileElementItem.getAsString(); - files.add(yangFile); - } - FileSource fileSource = new FileSource(files); - buildOption.addSource(fileSource); - } - JsonElement modulesElement = yang.get("module"); - if (modulesElement != null) { - JsonArray moduleArray = modulesElement.getAsJsonArray(); - List<JsonElement> moduleList = moduleArray.asList(); - List<ModuleInfo> moduleInfos = new ArrayList<>(); - for (JsonElement moduleElement : moduleList) { - JsonObject moduleObject = moduleElement.getAsJsonObject(); - String name = moduleObject.get("name").getAsString(); - String revision = moduleObject.get("revision").getAsString(); - String organization = null; - if (moduleObject.get("organization") != null) { - organization = moduleObject.get("organization").getAsString(); - } - URI schema = null; - if (moduleObject.get("schema") != null) { - schema = URI.create(moduleObject.get("schema").getAsString()); - } - ModuleInfo moduleInfo = new ModuleInfo(name, revision, organization); - moduleInfo.setSchema(schema); - moduleInfos.add(moduleInfo); - } - ModuleSource moduleSource = new ModuleSource(moduleInfos); - buildOption.addSource(moduleSource); - } + buildOption.sources = parseSources(yangElement); } JsonElement settingsElement = jsonObject.get("settings"); if (settingsElement != null) { 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 87a1c5e..ccbfcbc 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 @@ -126,10 +126,7 @@ public class YangCompiler { public YangSchemaContext buildSchemaContext() {
YangSchemaContext schemaContext = null;
try {
- List<Source> sources = buildOption.getSources();
- for (Source source : sources) {
- schemaContext = source.buildSource(settings, schemaContext, true);
- }
+ schemaContext = YangCompilerUtil.buildSchemaContext(buildOption.getSources(),getSettings());
return schemaContext;
} catch (YangCompilerException e) {
diff --git a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/util/YangCompilerUtil.java b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/util/YangCompilerUtil.java index 19a63bd..0f9c99f 100644 --- a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/util/YangCompilerUtil.java +++ b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/util/YangCompilerUtil.java @@ -46,11 +46,14 @@ import javax.net.ssl.X509TrustManager; import org.onap.modeling.yangkit.catalog.ModuleInfo; import org.onap.modeling.yangkit.catalog.YangCatalog; import org.onap.modeling.yangkit.compiler.Settings; +import org.onap.modeling.yangkit.compiler.Source; +import org.onap.modeling.yangkit.compiler.YangCompilerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yangcentral.yangkit.base.YangBuiltinKeyword; +import org.yangcentral.yangkit.model.api.schema.YangSchemaContext; import org.yangcentral.yangkit.model.api.stmt.Module; import org.yangcentral.yangkit.model.api.stmt.SubModule; import org.yangcentral.yangkit.model.api.stmt.YangStatement; @@ -438,4 +441,22 @@ public class YangCompilerUtil { } return dependencies; } + + /** + * build schema context from sources and settings. + * @param sources yang sources + * @param settings settings + * @return yang schema context + * @throws YangCompilerException yang compiler exception + */ + public static YangSchemaContext buildSchemaContext(List<Source> sources, Settings settings) + throws YangCompilerException { + YangSchemaContext schemaContext = null; + + for (Source source : sources) { + schemaContext = source.buildSource(settings, schemaContext, true); + } + return schemaContext; + + } } |