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 --- yang-comparator/pom.xml | 2 +- .../comparator/app/YangComparatorPlugin.java | 52 +++++------------- .../app/YangComparatorPluginParameter.java | 61 ++++++++++++++++++++++ yang-comparator/src/main/resources/plugins.json | 6 +-- 4 files changed, 79 insertions(+), 42 deletions(-) create mode 100644 yang-comparator/src/main/java/org/onap/modeling/yangkit/comparator/app/YangComparatorPluginParameter.java (limited to 'yang-comparator') 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 @@ io.github.yang-central.yangkit yangkit-parser - 1.3.5 + 1.3.6 org.onap.modeling.yangkit 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", -- cgit 1.2.3-korg