Merge "Initial sketch of YANG runtime API."
diff --git a/datamodel/pom.xml b/datamodel/pom.xml
index 0d1aafe..cfcd714 100644
--- a/datamodel/pom.xml
+++ b/datamodel/pom.xml
@@ -21,11 +21,11 @@
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-utils</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
</parent>
<artifactId>onos-yang-datamodel</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java
index f8ebda1..5fc301d 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java
@@ -125,7 +125,7 @@
/**
* YANG Node in which the leaf is contained.
*/
- private transient YangLeavesHolder containedIn;
+ private YangLeavesHolder containedIn;
/**
* List of must statement constraints.
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java
index b138fc1..f2ff214 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java
@@ -153,6 +153,7 @@
* node type.
*
* @return true if empty data-type is present, false otherwise
+ * @throws DataModelException when fails to do data model operations
*/
boolean isEmptyDataType() throws DataModelException;
}
diff --git a/generator/pom.xml b/generator/pom.xml
index 36f6def..49c65b8 100644
--- a/generator/pom.xml
+++ b/generator/pom.xml
@@ -21,11 +21,11 @@
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-utils</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
</parent>
<artifactId>onos-yang-utils-generator</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
diff --git a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java
index 724c9cf..bde310d 100644
--- a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java
+++ b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java
@@ -39,40 +39,12 @@
public final class JavaCodeGeneratorUtil {
/**
- * Current YANG node.
- */
- private static YangNode curNode;
-
- /**
- * Root node.
- */
- private static YangNode rootNode;
-
- /**
* Creates a java code generator utility object.
*/
private JavaCodeGeneratorUtil() {
}
/**
- * Returns current YANG node.
- *
- * @return current YANG node
- */
- public static YangNode getCurNode() {
- return curNode;
- }
-
- /**
- * Sets current YANG node.
- *
- * @param node current YANG node
- */
- public static void setCurNode(YangNode node) {
- curNode = node;
- }
-
- /**
* Generates Java code files corresponding to the YANG schema.
*
* @param rootNode root node of the data model tree
@@ -84,7 +56,6 @@
throws TranslatorException, IOException {
YangNode codeGenNode = rootNode;
- setRootNode(rootNode);
TraversalType curTraversal = ROOT;
while (codeGenNode != null) {
@@ -94,9 +65,8 @@
codeGenNode.getName() + " in " + codeGenNode.getLineNumber() + " at "
+ codeGenNode.getCharPosition() + " in " + codeGenNode.getFileName());
}
- setCurNode(codeGenNode);
try {
- generateCodeEntry(codeGenNode, yangPlugin);
+ generateCodeEntry(codeGenNode, yangPlugin, rootNode);
codeGenNode.setNameSpaceAndAddToParentSchemaMap();
} catch (InvalidNodeForTranslatorException e) {
if (codeGenNode.getNextSibling() != null) {
@@ -109,7 +79,7 @@
continue;
} catch (Exception e) {
e.printStackTrace();
- close(codeGenNode, yangPlugin);
+ close(codeGenNode, yangPlugin, rootNode);
throw new TranslatorException(e.getMessage());
}
@@ -119,20 +89,20 @@
codeGenNode = codeGenNode.getChild();
} else if (codeGenNode.getNextSibling() != null) {
try {
- generateCodeExit(codeGenNode, yangPlugin);
+ generateCodeExit(codeGenNode, yangPlugin, rootNode);
} catch (Exception e) {
e.printStackTrace();
- close(codeGenNode, yangPlugin);
+ close(codeGenNode, yangPlugin, rootNode);
throw new TranslatorException(e.getMessage());
}
curTraversal = SIBILING;
codeGenNode = codeGenNode.getNextSibling();
} else {
try {
- generateCodeExit(codeGenNode, yangPlugin);
+ generateCodeExit(codeGenNode, yangPlugin, rootNode);
} catch (Exception e) {
e.printStackTrace();
- close(codeGenNode, yangPlugin);
+ close(codeGenNode, yangPlugin, rootNode);
throw new TranslatorException(e.getMessage());
}
curTraversal = PARENT;
@@ -146,16 +116,19 @@
*
* @param codeGenNode current data model node for which the code needs to be generated
* @param yangPlugin YANG plugin config
+ * @param rootNode YANG root node
* @throws TranslatorException when fails to generate java code file the current node
* @throws IOException when fails to do IO operations
*/
- private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin)
+ private static void generateCodeEntry(YangNode codeGenNode,
+ YangPluginConfig yangPlugin,
+ YangNode rootNode)
throws TranslatorException, IOException {
if (codeGenNode instanceof JavaCodeGenerator) {
((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin);
} else {
- close(codeGenNode, yangPlugin);
+ close(codeGenNode, yangPlugin, rootNode);
throw new TranslatorException(
"Generated data model node cannot be translated to target language code for " +
codeGenNode.getName() + " in " + codeGenNode.getLineNumber()
@@ -168,16 +141,19 @@
*
* @param codeGenNode current data model node for which the code needs to be generated
* @param pluginConfig plugin configurations
+ * @param rootNode YANG root node
* @throws TranslatorException when fails to generate java code file the current node
* @throws IOException when fails to do IO operations
*/
- private static void generateCodeExit(YangNode codeGenNode, YangPluginConfig pluginConfig)
+ private static void generateCodeExit(YangNode codeGenNode,
+ YangPluginConfig pluginConfig,
+ YangNode rootNode)
throws TranslatorException, IOException {
if (codeGenNode instanceof JavaCodeGenerator) {
((JavaCodeGenerator) codeGenNode).generateCodeExit();
} else {
- close(codeGenNode, pluginConfig);
+ close(codeGenNode, pluginConfig, rootNode);
throw new TranslatorException(
"Generated data model node cannot be translated to target language code for " +
codeGenNode.getName() + " in " + codeGenNode.getLineNumber()
@@ -187,11 +163,12 @@
/**
* Free other YANG nodes of data-model tree when error occurs while file generation of current node.
+ *
+ * @param freedNode current data model node
*/
- private static void freeRestResources() {
+ private static void freeRestResources(YangNode freedNode) {
- YangNode freedNode = getCurNode();
- if (getCurNode() != null) {
+ if (freedNode != null) {
YangNode tempNode = freedNode;
TraversalType curTraversal = ROOT;
@@ -246,18 +223,16 @@
throws IOException {
if (rootNode != null) {
- //Free other resources where translator has failed.
- freeRestResources();
// Start removing all open files.
YangNode tempNode = rootNode;
- setCurNode(tempNode.getChild());
+ YangNode curNode = tempNode.getChild();
TraversalType curTraversal = ROOT;
while (tempNode != null) {
if (curTraversal != PARENT) {
- close(tempNode, yangPluginConfig);
+ close(tempNode, yangPluginConfig, rootNode);
}
if (curTraversal != PARENT && tempNode.getChild() != null) {
curTraversal = CHILD;
@@ -271,7 +246,7 @@
}
}
- freeRestResources();
+ freeRestResources(curNode);
}
}
@@ -280,19 +255,21 @@
*
* @param node current YANG node
* @param yangPlugin plugin configurations
+ * @param rootNode YANG root node
* @throws IOException when fails to do IO operations
*/
- private static void close(YangNode node, YangPluginConfig yangPlugin)
+ private static void close(YangNode node, YangPluginConfig yangPlugin,
+ YangNode rootNode)
throws IOException {
if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node)
.getTempJavaCodeFragmentFiles() != null) {
((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
}
- if (getRootNode() != null) {
- JavaFileInfoTranslator javaFileInfo = ((JavaFileInfoContainer) getRootNode()).getJavaFileInfo();
+ if (rootNode != null) {
+ JavaFileInfoTranslator javaFileInfo = ((JavaFileInfoContainer) rootNode).getJavaFileInfo();
if (javaFileInfo.getPackage() != null) {
searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() +
- javaFileInfo.getPackageFilePath());
+ javaFileInfo.getPackageFilePath());
} else {
searchAndDeleteTempDir(yangPlugin.getCodeGenDir());
}
@@ -301,24 +278,6 @@
}
/**
- * Returns root node.
- *
- * @return root node
- */
- private static YangNode getRootNode() {
- return rootNode;
- }
-
- /**
- * Sets root node.
- *
- * @param rootNode root node
- */
- private static void setRootNode(YangNode rootNode) {
- JavaCodeGeneratorUtil.rootNode = rootNode;
- }
-
- /**
* Searches child node in data model tree.
*
* @param parentNode parent node
diff --git a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java
index 6bcbb1b..39e75c1 100644
--- a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java
+++ b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java
@@ -320,7 +320,7 @@
+ " in " + type.getFileName());
}
- if (!(((YangDerivedInfo<?>) var).getReferredTypeDef() != null)) {
+ if ((((YangDerivedInfo<?>) var).getReferredTypeDef() == null)) {
throw new TranslatorException("derived info is not an instance of typedef. " +
type.getDataTypeName() + " in " +
type.getLineNumber() + " at " +
diff --git a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
index fe6a40b..221f414 100644
--- a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
+++ b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
@@ -562,7 +562,7 @@
List<String> importsList)
throws IOException {
- insertDataIntoJavaFile(file, CopyrightHeader.getCopyrightHeader());
+ insertDataIntoJavaFile(file, CopyrightHeader.parseCopyrightHeader());
insertDataIntoJavaFile(file, pkg);
/*
diff --git a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
index edf0216..e179903 100644
--- a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
+++ b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
@@ -1191,7 +1191,7 @@
* @return interface of add augmentation
*/
static String getAddAugmentInfoMethodInterface(String className) {
- StringBuilder builder = new StringBuilder(generateForAddAugmentation());
+ StringBuilder builder = new StringBuilder(generateForAddAugmentation(className));
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put(VALUE, OBJECT_STRING);
map.put(CLASS + OBJECT_STRING, CLASS_STRING);
diff --git a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/SubtreeFilteringMethodsGenerator.java b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/SubtreeFilteringMethodsGenerator.java
index 3e60817..d4904ae 100644
--- a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/SubtreeFilteringMethodsGenerator.java
+++ b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/SubtreeFilteringMethodsGenerator.java
@@ -1204,21 +1204,38 @@
return " Class<?>[] classArray = " + call + "" +
".getClass()" +
".getInterfaces();\n" +
- " Class<?> caseClass = classArray[0];\n" +
+ " Class<?> caseIntf = classArray[0];\n" +
" try {\n" +
- " Object obj1 = caseClass.newInstance();\n" +
+ " ClassLoader classLoader = this" +
+ ".getClass().getClassLoader();\n" +
+ " String className = \"Default\" + " +
+ "caseIntf.getSimpleName();\n" +
+ " className = caseIntf.getPackage()" +
+ ".getName() + \".\" + className;\n" +
+ " Class<?> caseClass = classLoader" +
+ ".loadClass(className);\n" +
+ " java.lang.reflect.Constructor<?> " +
+ "constructor = caseClass.getDeclaredConstructor();\n" +
+ " constructor.setAccessible(true);\n" +
+ " Object obj1 = constructor" +
+ ".newInstance();\n" +
" java.lang.reflect.Method method =" +
- " caseClass.getMethod(\"builder\", caseClass);\n" +
- " Object obj = method.invoke(obj1," +
- " (Object) null);\n" +
- " method = caseClass.getMethod(\"build\", caseClass);\n" +
- " Object obj2 = method.invoke(obj, " +
- "(Object) null);\n" +
- " method = caseClass.getMethod(\"processSubtreeFiltering\", caseClass);\n" +
+ " caseClass.getMethod(\"builder\");\n" +
+ " Object obj = method.invoke(obj1);\n" +
+ " Class<?> builderClass = obj.getClass();\n" +
+ " method = builderClass.getMethod(\"build\");\n" +
+ " Object obj2 = method.invoke(obj);\n" +
+ " Class<?> input = this.getClass()" +
+ ".getMethod(" + "\"" + name + "\").getReturnType();\n" +
+ " method = caseClass.getMethod" +
+ "(\"processSubtreeFiltering\", input,\n" +
+ " boolean.class);\n" +
" result = (" + returnType + ") method.invoke" +
"(obj2, " + call + ", true);\n" +
" } catch (NoSuchMethodException | InstantiationException |\n" +
- " IllegalAccessException | InvocationTargetException e) {\n" +
+ " IllegalAccessException | " +
+ "InvocationTargetException |\n" +
+ " ClassNotFoundException e) {\n" +
" e.printStackTrace();\n" +
" }\n";
}
diff --git a/generator/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java b/generator/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
index ecc70b6..cd359cd 100644
--- a/generator/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
+++ b/generator/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
@@ -228,6 +228,21 @@
public static final String PERIOD = ".";
/**
+ * Static attribute for lib.
+ */
+ public static final String LIB = "lib__";
+
+ /**
+ * Static attribute for lib path.
+ */
+ public static final String LIB_PATH = "//lib:";
+
+ /**
+ * Static attribute for output.
+ */
+ public static final String OUT = "__output";
+
+ /**
* Static attribute for period.
*/
public static final String ENTRY = "Entry";
diff --git a/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java b/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java
index 5833de2..8b55768 100644
--- a/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java
+++ b/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java
@@ -37,9 +37,6 @@
private static final String COPYRIGHTS_FIRST_LINE = "/*\n * Copyright " + Calendar.getInstance().get(Calendar.YEAR)
+ "-present Open Networking Laboratory\n";
private static final String TEMP_FILE = "temp.txt";
- private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader();
-
- private static String copyrightHeader;
/**
* Creates an instance of copyright header.
@@ -48,40 +45,18 @@
}
/**
- * Returns copyright file header.
- *
- * @return copyright file header
- * @throws IOException when fails to parse copyright header
- */
- public static String getCopyrightHeader() throws IOException {
-
- if (copyrightHeader == null) {
- parseCopyrightHeader();
- }
- return copyrightHeader;
- }
-
- /**
- * Sets the copyright header.
- *
- * @param header copyright header
- */
- private static void setCopyrightHeader(String header) {
-
- copyrightHeader = header;
- }
-
- /**
* parses Copyright to the temporary file.
*
+ * @return string of file.
* @throws IOException when fails to get the copyright header
*/
- private static void parseCopyrightHeader() throws IOException {
+ public static String parseCopyrightHeader() throws IOException {
File temp = new File(TEMP_FILE);
try {
- InputStream stream = classLoader.getResourceAsStream(COPYRIGHT_HEADER_FILE);
+ InputStream stream = CopyrightHeader.class.getClassLoader()
+ .getResourceAsStream(COPYRIGHT_HEADER_FILE);
OutputStream out = new FileOutputStream(temp);
int index;
@@ -91,8 +66,7 @@
}
out.close();
stream.close();
- getStringFileContent(temp);
- setCopyrightHeader(getStringFileContent(temp));
+ return getStringFileContent(temp);
} catch (IOException e) {
throw new IOException("failed to parse the Copyright header");
} finally {
diff --git a/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java b/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
index 5c499db..8c8e794 100644
--- a/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
+++ b/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
@@ -585,16 +585,17 @@
/**
* Generates javaDocs for add augmentation method.
*
+ * @param name builder class name
* @return javaDocs
*/
- public static String generateForAddAugmentation() {
+ public static String generateForAddAugmentation(String name) {
return getJavaDocStartLine(YANG_AUGMENTED_INFO_LOWER_CASE,
JAVA_DOC_SETTERS_COMMON) +
getJavaDocEmptyAsteriskLine() +
getJavaDocParamLine(YANG_AUGMENTED_INFO_LOWER_CASE, VALUE) +
getJavaDocParamLine(YANG_AUGMENTED_INFO_LOWER_CASE, CLASS +
OBJECT_STRING) +
- getJavaDocEndLine();
+ getJavaDocReturnLine(name) + getJavaDocEndLine();
}
/**
@@ -667,7 +668,9 @@
break;
}
}
- javadoc.append(getJavaDocReturnLine(BUILDER_OBJECT + attribute))
+ javadoc.append(getJavaDocParamLine(
+ attribute, ADD_STRING + TO_CAPS))
+ .append(getJavaDocReturnLine(BUILDER_OBJECT + attribute))
.append(getJavaDocEndLine());
return javadoc.toString();
}
diff --git a/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java b/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
index a03ebb4..15051cb 100644
--- a/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
+++ b/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
@@ -68,7 +68,7 @@
import static org.onosproject.yangutils.utils.UtilConstants.UNDER_SCORE;
import static org.onosproject.yangutils.utils.UtilConstants.UNUSED;
import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX;
-import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.getCopyrightHeader;
+import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.parseCopyrightHeader;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
@@ -134,7 +134,7 @@
FileWriter fileWriter = new FileWriter(packageInfo);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
- bufferedWriter.write(getCopyrightHeader());
+ bufferedWriter.write(parseCopyrightHeader());
//TODO: get the compiler annotations and pass the info
bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode,
null));
@@ -688,7 +688,8 @@
* @param conflictResolver object of YANG to java naming conflict util
* @return camel case rule checked string
*/
- private static String applyCamelCaseRule(String[] stringArray, YangToJavaNamingConflictUtil conflictResolver) {
+ private static String applyCamelCaseRule(String[] stringArray,
+ YangToJavaNamingConflictUtil conflictResolver) {
String ruleChecker = stringArray[0].toLowerCase();
int i;
diff --git a/parser/pom.xml b/parser/pom.xml
index 8520981..993eb6a 100644
--- a/parser/pom.xml
+++ b/parser/pom.xml
@@ -21,11 +21,11 @@
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-utils</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
</parent>
<artifactId>onos-yang-utils-parser</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
diff --git a/parser/src/main/java/org/onosproject/yangutils/parser/impl/parserutils/ListenerUtil.java b/parser/src/main/java/org/onosproject/yangutils/parser/impl/parserutils/ListenerUtil.java
index 493ec09..17ec860 100644
--- a/parser/src/main/java/org/onosproject/yangutils/parser/impl/parserutils/ListenerUtil.java
+++ b/parser/src/main/java/org/onosproject/yangutils/parser/impl/parserutils/ListenerUtil.java
@@ -91,10 +91,6 @@
private static final String REGEX_EQUAL = "[=]";
private static final String REGEX_OPEN_BRACE = "[(]";
- private static YangConstructType pathType;
- private static PathStatementContext pathCtx;
- private static YangLeafRef yangLeafRef;
-
// No instantiation.
private ListenerUtil() {
}
@@ -124,7 +120,9 @@
* @param ctx yang construct's context to get the line number and character position
* @return concatenated string after removing double quotes
*/
- public static String getValidIdentifier(String identifier, YangConstructType yangConstruct, ParserRuleContext ctx) {
+ public static String getValidIdentifier(String identifier,
+ YangConstructType yangConstruct,
+ ParserRuleContext ctx) {
String identifierString = removeQuotesAndHandleConcat(identifier);
ParserException parserException;
@@ -713,33 +711,33 @@
YangLeafRef leafRef) {
String concatPath = removeQuotesAndHandleConcat(path);
- pathType = type;
- pathCtx = ctx;
- yangLeafRef = leafRef;
if (!concatPath.startsWith(SLASH_FOR_STRING) &&
!concatPath.startsWith(ANCESTOR)) {
- throw getPathException();
+ throw getPathException(ctx, leafRef);
}
leafRef.setPath(concatPath);
if (concatPath.startsWith(SLASH_FOR_STRING)) {
List<YangAtomicPath> atomicList = new LinkedList<>();
- valAbsPath(concatPath, atomicList);
+ valAbsPath(concatPath, atomicList, ctx, leafRef, type);
leafRef.setPathType(ABSOLUTE_PATH);
valPrefix(atomicList, leafRef);
leafRef.setAtomicPath(atomicList);
return;
}
leafRef.setPathType(RELATIVE_PATH);
- valRelPath(concatPath, leafRef);
+ valRelPath(concatPath, leafRef, ctx, type);
}
/**
* Validates relative path, parses the string and stores it in the leaf-ref.
*
- * @param path leaf-ref path
- * @param leafRef YANG leaf-ref
+ * @param path leaf-ref path
+ * @param leafRef YANG leaf-ref data model information
+ * @param pathType yang construct for creating error message
*/
- private static void valRelPath(String path, YangLeafRef leafRef) {
+ private static void valRelPath(String path, YangLeafRef leafRef,
+ PathStatementContext pathCtx,
+ YangConstructType pathType) {
YangRelativePath relPath = new YangRelativePath();
int count = 0;
@@ -748,12 +746,13 @@
count = count + 1;
}
if (path.isEmpty()) {
- throw getPathException();
+ throw getPathException(pathCtx, leafRef);
}
List<YangAtomicPath> atomicList = new ArrayList<>();
relPath.setAncestorNodeCount(count);
- valAbsPath(SLASH_FOR_STRING + path, atomicList);
+ valAbsPath(SLASH_FOR_STRING + path, atomicList, pathCtx, leafRef,
+ pathType);
valPrefix(atomicList, leafRef);
relPath.setAtomicPathList(atomicList);
leafRef.setRelativePath(relPath);
@@ -762,23 +761,30 @@
/**
* Validates absolute path, parses the string and stores it in leaf-ref.
*
- * @param path leaf-ref path
- * @param atomics atomic content list
+ * @param path leaf-ref path
+ * @param atomics atomic content list
+ * @param pathCtx path statement context
+ * @param yangLeafRef YANG leaf ref
+ * @param pathType yang construct for creating error message
*/
- private static void valAbsPath(String path, List<YangAtomicPath> atomics) {
+ private static void valAbsPath(String path, List<YangAtomicPath> atomics,
+ PathStatementContext pathCtx,
+ YangLeafRef yangLeafRef,
+ YangConstructType pathType) {
String comPath = path;
while (comPath != null) {
comPath = comPath.substring(1);
if (comPath.isEmpty()) {
- throw getPathException();
+ throw getPathException(pathCtx, yangLeafRef);
}
int nodeId = comPath.indexOf(CHAR_OF_SLASH);
int predicate = comPath.indexOf(CHAR_OF_OPEN_SQUARE_BRACKET);
if (predicate < nodeId && predicate != -1) {
- comPath = getPathWithPredicate(comPath, atomics);
+ comPath = getPathWithPredicate(comPath, atomics,
+ pathCtx, yangLeafRef, pathType);
} else {
- comPath = getPath(comPath, atomics);
+ comPath = getPath(comPath, atomics, pathType, pathCtx);
}
}
}
@@ -787,38 +793,53 @@
* Returns the remaining path after parsing and the predicates of an atomic
* content.
*
- * @param path leaf-ref path
- * @param atomics atomic content list
+ * @param path leaf-ref path
+ * @param atomics atomic content list
+ * @param pathCtx yang construct's context to get the line number and
+ * character position
+ * @param yangLeafRef YANG leaf-ref data model information
+ * @param pathType yang construct for creating error message
* @return parsed path after removing one atomic content.
*/
private static String getPathWithPredicate(String path,
- List<YangAtomicPath> atomics) {
+ List<YangAtomicPath> atomics,
+ PathStatementContext pathCtx,
+ YangLeafRef yangLeafRef,
+ YangConstructType pathType) {
String[] node = new String[2];
int bracket = path.indexOf(CHAR_OF_OPEN_SQUARE_BRACKET);
node[0] = path.substring(0, bracket);
node[1] = path.substring(bracket);
- return getParsedPath(node[0], node[1], atomics);
+ return getParsedPath(node[0], node[1], atomics, pathCtx, yangLeafRef,
+ pathType);
}
/**
* Returns the path after taking all the path predicates of an atomic
* content.
*
- * @param nodeId atomic content nodeId
- * @param path leaf-ref path
- * @param atomics atomic content list
+ * @param nodeId atomic content nodeId
+ * @param path leaf-ref path
+ * @param atomics atomic content list
+ * @param pathCtx yang construct's context to get the line number
+ * and character position
+ * @param yangLeafRef YANG leaf-ref data model information
+ * @param pathType yang construct for creating error message
* @return parsed path after removing one atomic content.
*/
public static String getParsedPath(String nodeId, String path,
- List<YangAtomicPath> atomics) {
+ List<YangAtomicPath> atomics,
+ PathStatementContext pathCtx,
+ YangLeafRef yangLeafRef,
+ YangConstructType pathType) {
String comPath = path;
List<String> predicateList = new ArrayList<>();
while (comPath.startsWith(OPEN_SQUARE_BRACKET)) {
String matchedVal = getMatchedPredicate(comPath);
if (matchedVal == null || matchedVal.isEmpty()) {
- throw getPathException();
+ throw getPathException(pathCtx, yangLeafRef);
}
predicateList.add(matchedVal);
comPath = comPath.substring(matchedVal.length());
@@ -828,7 +849,9 @@
YangNodeIdentifier validId =
getValidNodeIdentifier(nodeId, pathType, pathCtx);
- List<YangPathPredicate> predicates = valPathPredicates(predicateList);
+ List<YangPathPredicate> predicates = valPathPredicates(predicateList,
+ pathType,
+ pathCtx, yangLeafRef);
atomicPath.setNodeIdentifier(validId);
atomicPath.setPathPredicatesList(predicates);
atomics.add(atomicPath);
@@ -839,15 +862,22 @@
* Validates the path predicates of an atomic content after parsing the
* predicates and storing it in the leaf-ref.
*
- * @param predicates list of predicates
+ * @param predicates list of predicates
+ * @param pathType yang construct for creating error message
+ * @param pathCtx yang construct's context to get the line number
+ * and character position
+ * @param yangLeafRef YANG leaf-ref data model information
* @return list of path predicates of an atomic content
*/
- private static List<YangPathPredicate> valPathPredicates(List<String> predicates) {
+ private static List<YangPathPredicate> valPathPredicates(List<String> predicates,
+ YangConstructType pathType,
+ PathStatementContext pathCtx,
+ YangLeafRef yangLeafRef) {
List<YangPathPredicate> result = new ArrayList<>();
for (String p : predicates) {
p = p.substring(1, p.length() - 1);
- result.add(valPathEqualityExp(p.trim()));
+ result.add(valPathEqualityExp(p.trim(), pathType, pathCtx, yangLeafRef));
}
return result;
}
@@ -856,15 +886,23 @@
* Validates the path equality expression of a path predicate and after
* parsing the string assigns it to the YANG path predicate.
*
- * @param predicate path predicate
+ * @param predicate path predicate
+ * @param pathType yang construct for creating error message
+ * @param pathCtx yang construct's context to get the line number
+ * and character position
+ * @param yangLeafRef YANG leaf-ref data model information
* @return YANG path predicate
*/
- private static YangPathPredicate valPathEqualityExp(String predicate) {
+ private static YangPathPredicate valPathEqualityExp(String predicate,
+ YangConstructType pathType,
+ PathStatementContext pathCtx,
+ YangLeafRef yangLeafRef) {
String[] exp = predicate.split(REGEX_EQUAL);
YangNodeIdentifier nodeId =
getValidNodeIdentifier(exp[0].trim(), pathType, pathCtx);
- YangRelativePath relPath = valPathKeyExp(exp[1].trim());
+ YangRelativePath relPath = valPathKeyExp(exp[1].trim(), pathType,
+ pathCtx, yangLeafRef);
YangPathPredicate pathPredicate = new YangPathPredicate();
pathPredicate.setNodeId(nodeId);
@@ -877,10 +915,17 @@
* Validates the path key expression of the path-predicate and stores it
* in the relative path of the leaf-ref.
*
- * @param relPath relative path
+ * @param relPath relative path
+ * @param pathType yang construct for creating error message
+ * @param pathCtx yang construct's context to get the line number
+ * and character position
+ * @param yangLeafRef YANG leaf-ref data model information
* @return YANG relative path
*/
- private static YangRelativePath valPathKeyExp(String relPath) {
+ private static YangRelativePath valPathKeyExp(String relPath,
+ YangConstructType pathType,
+ PathStatementContext pathCtx,
+ YangLeafRef yangLeafRef) {
String[] relative = relPath.split(SLASH_FOR_STRING);
int count = 0;
@@ -895,7 +940,9 @@
YangRelativePath relativePath = new YangRelativePath();
relativePath.setAncestorNodeCount(count);
- relativePath.setAtomicPathList(valRelPathKeyExp(atomicContent));
+ relativePath.setAtomicPathList(valRelPathKeyExp(atomicContent,
+ pathType,
+ pathCtx, yangLeafRef));
return relativePath;
}
@@ -903,16 +950,23 @@
* Validates relative path key expression in the right relative path of
* the path predicate, by taking every atomic content in it.
*
- * @param content atomic content list
+ * @param content atomic content list
+ * @param pathType yang construct for creating error message
+ * @param pathCtx yang construct's context to get the line number
+ * and character position
+ * @param yangLeafRef YANG leaf-ref data model information
* @return YANG atomic content list
*/
- private static List<YangAtomicPath> valRelPathKeyExp(List<String> content) {
+ private static List<YangAtomicPath> valRelPathKeyExp(List<String> content,
+ YangConstructType pathType,
+ PathStatementContext pathCtx,
+ YangLeafRef yangLeafRef) {
String current = content.get(0);
String[] curStr = (current.trim()).split(REGEX_OPEN_BRACE);
if (!(curStr[0].trim().equals(CURRENT)) ||
!(curStr[1].trim().equals(CLOSE_PARENTHESIS))) {
- throw getPathException();
+ throw getPathException(pathCtx, yangLeafRef);
}
content.remove(0);
@@ -933,9 +987,14 @@
*
* @param path leaf-ref path
* @param atomicList atomic content list
+ * @param pathType yang construct for creating error message
+ * @param pathCtx yang construct's context to get the line number
+ * and character position
* @return remaining path after parsing one atomic content
*/
- public static String getPath(String path, List<YangAtomicPath> atomicList) {
+ public static String getPath(String path, List<YangAtomicPath> atomicList,
+ YangConstructType pathType,
+ PathStatementContext pathCtx) {
String comPath = path;
String nodeId;
@@ -958,9 +1017,13 @@
/**
* Returns the path syntax parser exception.
*
+ * @param pathCtx yang construct's context to get the line number
+ * and character position
+ * @param yangLeafRef YANG leaf-ref data model information
* @return parser exception
*/
- private static ParserException getPathException() {
+ private static ParserException getPathException(PathStatementContext pathCtx,
+ YangLeafRef yangLeafRef) {
ParserException exception = new ParserException(
"YANG file error : Path " + yangLeafRef.getPath() +
" does not follow valid path syntax");
diff --git a/plugin/buck/pom.xml b/plugin/buck/pom.xml
index fd1cca9..783e897 100644
--- a/plugin/buck/pom.xml
+++ b/plugin/buck/pom.xml
@@ -21,7 +21,7 @@
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-plugins</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
</parent>
<artifactId>onos-yang-buck-plugin</artifactId>
@@ -34,6 +34,11 @@
<version>0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-yang-tool</artifactId>
+ <version>1.12-SNAPSHOT</version>
+ </dependency>
</dependencies>
<build>
diff --git a/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangGenerator.java b/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangGenerator.java
index 583ecaf..b14a54a 100644
--- a/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangGenerator.java
+++ b/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangGenerator.java
@@ -16,41 +16,82 @@
package org.onosproject.yangutils.plugin.buck;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.tool.CallablePlugin;
+import org.onosproject.yangutils.tool.YangToolManager;
+import org.onosproject.yangutils.utils.io.YangPluginConfig;
+
import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
import java.util.List;
-import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
-import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil;
-import org.onosproject.yangutils.utils.io.YangPluginConfig;
-import org.onosproject.yangutils.datamodel.YangNode;
+import static java.util.stream.Collectors.toList;
+import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.parseJarFile;
+import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
+import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
/**
* Generates Java sources from a Yang model.
*/
-public class YangGenerator {
+public class YangGenerator implements CallablePlugin {
private final List<File> models;
+ private final List<String> depJar;
private String outputDirectory;
+ private final String DEFAULT_JAR_RES_PATH = SLASH + YANG_RESOURCES + SLASH;
- public YangGenerator(List<File> models, String outputDirectory) {
+ YangGenerator(List<File> models, String outputDirectory, List<String> depJar) {
this.models = models;
- this.outputDirectory = outputDirectory + "/";
+ this.depJar = depJar;
+ this.outputDirectory = outputDirectory + SLASH;
}
public void execute() throws YangParsingException {
- for (File model : models) {
+ List<String> files = getListOfFile();
+ synchronized (files) {
try {
YangPluginConfig config = new YangPluginConfig();
config.setCodeGenDir(outputDirectory);
-
- YangNode yangNode = new YangUtilsParserManager()
- .getDataModel(model.toString());
-
- JavaCodeGeneratorUtil.generateJavaCode(yangNode, config);
+ config.resourceGenDir(outputDirectory + DEFAULT_JAR_RES_PATH);
+ //for inter-jar linking.
+ List<YangNode> dependentSchema = new ArrayList<>();
+ for (String jar : depJar) {
+ dependentSchema.addAll(parseJarFile(jar, outputDirectory));
+ }
+ //intra jar file linking.
+ YangToolManager manager = new YangToolManager();
+ manager.compileYangFiles(manager.createYangFileInfoSet(files),
+ dependentSchema, config, this);
} catch (Exception e) {
throw new YangParsingException(e);
}
}
}
+ private List<String> getListOfFile() {
+ List<String> files = new ArrayList<>();
+ if (models != null) {
+ synchronized (models) {
+ files.addAll(models.stream().map(File::toString)
+ .collect(toList()));
+ }
+ }
+ return files;
+ }
+
+ @Override
+ public void addGeneratedCodeToBundle() {
+ //TODO: add functionality.
+ }
+
+ @Override
+ public void addCompiledSchemaToBundle() throws IOException {
+ //TODO: add functionality.
+ }
+
+ @Override
+ public void addYangFilesToBundle() throws IOException {
+ //TODO: add functionality.
+ }
}
diff --git a/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangLibrary.java b/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangLibrary.java
index 8db806b..5d0550c 100644
--- a/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangLibrary.java
+++ b/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangLibrary.java
@@ -16,13 +16,6 @@
package org.onosproject.yangutils.plugin.buck;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.List;
-import java.util.stream.Collectors;
-
-import javax.annotation.Nullable;
-
import com.facebook.buck.jvm.java.JarDirectoryStep;
import com.facebook.buck.model.BuildTargets;
import com.facebook.buck.rules.AbstractBuildRule;
@@ -39,6 +32,16 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;
+import javax.annotation.Nullable;
+import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
+import static org.onosproject.yangutils.utils.UtilConstants.YANG;
+
/**
* Buck rule to define a library built form a Yang model.
*/
@@ -46,7 +49,8 @@
@AddToRuleKey
private final ImmutableSortedSet<SourcePath> srcs;
-
+ private final BuildRuleParams params;
+
private final Path genSrcsDirectory;
private final Path outputDirectory;
private final Path output;
@@ -57,7 +61,7 @@
ImmutableSortedSet<SourcePath> srcs) {
super(params, resolver);
this.srcs = srcs;
-
+ this.params = params;
genSrcsDirectory = BuildTargets.getGenPath(getProjectFilesystem(),
params.getBuildTarget(),
"%s__yang-gen");
@@ -91,7 +95,8 @@
.map(s -> getResolver().getRelativePath(s))
.collect(Collectors.toList());
- steps.add(new YangStep(getProjectFilesystem(), sourcePaths, genSrcsDirectory));
+ steps.add(new YangStep(getProjectFilesystem(), sourcePaths, genSrcsDirectory,
+ params.getDeps()));
steps.add(new JarDirectoryStep(
getProjectFilesystem(),
@@ -108,4 +113,17 @@
public Path getPathToOutput() {
return output;
}
+
+
+ /**
+ * Returns generated sources directory.
+ *
+ * @return generated sources directory
+ */
+ public Path getGenSrcsDirectory() {
+ File dir = new File(genSrcsDirectory.toString() + SLASH + YANG);
+ dir.mkdirs();
+ return genSrcsDirectory;
+ }
+
}
diff --git a/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangLibraryDescription.java b/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangLibraryDescription.java
index 29d4225..83b1370 100644
--- a/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangLibraryDescription.java
+++ b/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangLibraryDescription.java
@@ -16,10 +16,6 @@
package org.onosproject.yangutils.plugin.buck;
-import java.nio.file.Path;
-import java.util.regex.Pattern;
-import java.util.stream.Collectors;
-
import com.facebook.buck.cli.BuckConfig;
import com.facebook.buck.jvm.java.CalculateAbi;
import com.facebook.buck.jvm.java.DefaultJavaLibrary;
@@ -43,6 +39,7 @@
import com.facebook.buck.rules.BuildRuleType;
import com.facebook.buck.rules.BuildTargetSourcePath;
import com.facebook.buck.rules.Description;
+import com.facebook.buck.rules.PathSourcePath;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.SourcePathResolver;
import com.facebook.buck.rules.SourcePaths;
@@ -53,6 +50,14 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
+import static org.onosproject.yangutils.utils.UtilConstants.YANG;
+
/**
* Description of a Buck Yang Library.
*/
@@ -94,7 +99,6 @@
BuildRuleParams yangParams = params.copyWithBuildTarget(
BuildTargets.createFlavoredBuildTarget(
unflavoredBuildTarget, SOURCES));
-
BuildRule yangLib = resolver.addToIndex(new YangLibrary(yangParams, pathResolver, args.srcs));
if (params.getBuildTarget().getFlavors().contains(SOURCES)) {
@@ -126,13 +130,21 @@
BuildTarget abiJarTarget = params.getBuildTarget().withAppendedFlavors(CalculateAbi.FLAVOR);
+ //Add yang meta data resources to generated jar file resources.
+
+ Path rscRoot = ((YangLibrary) yangLib).getGenSrcsDirectory();
+ Path resPath = Paths.get(rscRoot + SLASH + YANG);
+
+ SourcePath path = new PathSourcePath(params.getProjectFilesystem(),
+ resPath);
+
DefaultJavaLibrary library =
resolver.addToIndex(
new DefaultJavaLibrary(
javaParams,
pathResolver,
ImmutableSet.of(SourcePaths.getToBuildTargetSourcePath().apply(yangLib)),
- /* resources */ ImmutableSet.<SourcePath>of(),
+ /* resources */ImmutableSet.of(path),
javacOptions.getGeneratedSourceFolderName(),
/* proguardConfig */ Optional.<SourcePath>absent(),
/* postprocessClassesCommands */ ImmutableList.<String>of(),
@@ -142,7 +154,7 @@
javacOptions.trackClassUsage(),
/* additionalClasspathEntries */ ImmutableSet.<Path>of(),
new JavacToJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY),
- /* resourcesRoot */ Optional.<Path>absent(),
+ /* resourcesRoot */ Optional.<Path>of(rscRoot),
/* manifestFile */ Optional.absent(),
/* mavenCoords */ Optional.<String>absent(),
/* tests */ ImmutableSortedSet.<BuildTarget>of(),
@@ -169,4 +181,5 @@
//TODO other params here
}
+
}
diff --git a/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangStep.java b/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangStep.java
index ae84cc3..d795d8c 100644
--- a/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangStep.java
+++ b/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangStep.java
@@ -15,16 +15,27 @@
*/
package org.onosproject.yangutils.plugin.buck;
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Path;
-import java.util.List;
-import java.util.stream.Collectors;
-
import com.facebook.buck.io.ProjectFilesystem;
+import com.facebook.buck.model.UnflavoredBuildTarget;
+import com.facebook.buck.rules.BuildRule;
import com.facebook.buck.step.AbstractExecutionStep;
import com.facebook.buck.step.ExecutionContext;
import com.facebook.buck.step.StepExecutionResult;
+import com.google.common.collect.ImmutableSortedSet;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.onosproject.yangutils.utils.UtilConstants.JAR;
+import static org.onosproject.yangutils.utils.UtilConstants.LIB;
+import static org.onosproject.yangutils.utils.UtilConstants.LIB_PATH;
+import static org.onosproject.yangutils.utils.UtilConstants.OUT;
+import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
+import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
/**
* Buck build step to trigger Yang Java source file generation.
@@ -32,17 +43,18 @@
public class YangStep extends AbstractExecutionStep {
private static final String DESCRIPTION = "yang-compile";
-
+ private final ImmutableSortedSet<BuildRule> deps;
private final ProjectFilesystem filesystem;
private final List<Path> srcs;
private final Path output;
YangStep(ProjectFilesystem filesystem,
List<Path> srcs,
- Path genSourcesDirectory) {
+ Path genSourcesDirectory, ImmutableSortedSet<BuildRule> deps) {
super(DESCRIPTION);
this.filesystem = filesystem;
this.srcs = srcs;
+ this.deps = deps;
this.output = genSourcesDirectory;
}
@@ -52,13 +64,42 @@
List<File> sourceFiles = srcs.stream().map(Path::toFile)
.collect(Collectors.toList());
-
try {
- new YangGenerator(sourceFiles, output.toString()).execute();
+ new YangGenerator(sourceFiles, output.toString(), getJarPaths())
+ .execute();
return StepExecutionResult.SUCCESS;
- } catch(YangParsingException e) {
+ } catch (YangParsingException e) {
executionContext.getConsole().printErrorText(e.getMessage());
return StepExecutionResult.ERROR;
}
}
+
+ private List<String> getJarPaths() {
+ StringBuilder builder;
+ List<String> depJarPaths = new ArrayList<>();
+ String[] array;
+ UnflavoredBuildTarget uBt;
+ if (deps != null) {
+ for (BuildRule rule : deps) {
+ if (!rule.getBuildTarget().getFullyQualifiedName()
+ .contains(LIB_PATH)) {
+ builder = new StringBuilder();
+ //build absolute path for jar file
+ builder.append(filesystem.getRootPath().toString()).append(SLASH)
+ .append(filesystem.getBuckPaths().getGenDir())
+ .append(SLASH);
+ uBt = rule.getBuildTarget().getUnflavoredBuildTarget();
+ array = uBt.getBaseName().split(SLASH);
+ for (int i = 2; i < array.length; i++) {
+ builder.append(array[i]).append(SLASH);
+ }
+ builder.append(LIB).append(uBt.getShortName())
+ .append(OUT).append(SLASH)
+ .append(uBt.getShortName()).append(PERIOD + JAR);
+ depJarPaths.add(builder.toString());
+ }
+ }
+ }
+ return depJarPaths;
+ }
}
diff --git a/plugin/maven/pom.xml b/plugin/maven/pom.xml
index 9ad8b88..db4920b 100644
--- a/plugin/maven/pom.xml
+++ b/plugin/maven/pom.xml
@@ -21,11 +21,11 @@
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-plugins</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
</parent>
<artifactId>onos-yang-maven-plugin</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<dependencies>
@@ -92,12 +92,12 @@
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-tool</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-utils-parser</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
</dependency>
</dependencies>
diff --git a/plugin/maven/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java b/plugin/maven/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
index 1e38d88..33e8f64 100644
--- a/plugin/maven/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
+++ b/plugin/maven/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
@@ -40,7 +40,6 @@
import org.onosproject.yangutils.tool.exception.YangToolException;
import org.onosproject.yangutils.utils.io.YangPluginConfig;
import org.onosproject.yangutils.utils.io.YangToJavaNamingConflictUtil;
-import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
import org.sonatype.plexus.build.incremental.BuildContext;
import java.io.IOException;
@@ -69,6 +68,7 @@
import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
import static org.onosproject.yangutils.utils.UtilConstants.VERSION_ERROR;
import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
+import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getYangFiles;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
@@ -222,19 +222,17 @@
* Obtain the YANG files at a path mentioned in plugin and creates
* YANG file information set.
*/
- createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
YangToolManager toolManager = new YangToolManager();
+
+ yangFileInfoSet = toolManager.createYangFileInfoSet(
+ getYangFiles(searchDir));
List<YangNode> interJarResolvedNodes =
resolveInterJarDependencies(project, localRepository,
remoteRepository, outputDir);
- System.out.println("going to call tool manager");
toolManager.compileYangFiles(yangFileInfoSet,
interJarResolvedNodes, yangPlugin,
this);
- System.out.println("called tool manager");
-
-
} catch (YangToolException e) {
String fileName = EMPTY_STRING;
if (e.getCurYangFile() != null) {
diff --git a/plugin/maven/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java b/plugin/maven/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java
index 40ac92c..d7da436 100644
--- a/plugin/maven/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java
+++ b/plugin/maven/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java
@@ -33,7 +33,7 @@
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
-import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.getCopyrightHeader;
+import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.parseCopyrightHeader;
/**
* Unit Tests for the CopyrightHeader contents.
@@ -89,7 +89,7 @@
out.write(c);
}
- String licenseHeader = getCopyrightHeader();
+ String licenseHeader = parseCopyrightHeader();
File test = new File("target/TestCopyrightHeader.txt");
FileWriter writer = new FileWriter(test);
diff --git a/plugin/pom.xml b/plugin/pom.xml
index d3526bd..d9f7d3f 100644
--- a/plugin/pom.xml
+++ b/plugin/pom.xml
@@ -21,11 +21,11 @@
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-utils</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
</parent>
<artifactId>onos-yang-plugins</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
diff --git a/plugin/tool/pom.xml b/plugin/tool/pom.xml
index b0209a2..dda6608 100644
--- a/plugin/tool/pom.xml
+++ b/plugin/tool/pom.xml
@@ -23,11 +23,11 @@
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-plugins</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
</parent>
<artifactId>onos-yang-tool</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
diff --git a/plugin/tool/src/main/java/org/onosproject/yangutils/tool/CallablePlugin.java b/plugin/tool/src/main/java/org/onosproject/yangutils/tool/CallablePlugin.java
index 3f44863..21dd05b 100644
--- a/plugin/tool/src/main/java/org/onosproject/yangutils/tool/CallablePlugin.java
+++ b/plugin/tool/src/main/java/org/onosproject/yangutils/tool/CallablePlugin.java
@@ -23,6 +23,7 @@
* will be used in by tool to be plugin agnostic.
*/
public interface CallablePlugin {
+
/**
* Adds generated source directory to the compilation root.
*/
@@ -32,6 +33,7 @@
* serialize the compiled schema and place it in the appropriate location
* so that it will be part of the generated OSGi bundle.
*
+ * @throws IOException when fails to do IO operations
*/
void addCompiledSchemaToBundle()
throws IOException;
@@ -41,6 +43,7 @@
* Add the YANG files in the bundle, to support YANG display in protocols
* like RESTCONF.
*
+ * @throws IOException when fails to do IO operations
*/
void addYangFilesToBundle() throws IOException;
}
diff --git a/plugin/tool/src/main/java/org/onosproject/yangutils/tool/YangToolManager.java b/plugin/tool/src/main/java/org/onosproject/yangutils/tool/YangToolManager.java
index 0853775..4e5d44e 100644
--- a/plugin/tool/src/main/java/org/onosproject/yangutils/tool/YangToolManager.java
+++ b/plugin/tool/src/main/java/org/onosproject/yangutils/tool/YangToolManager.java
@@ -94,99 +94,121 @@
}
/**
+ * Creates a YANG file info set.
+ *
+ * @param yangFileList YANG files list
+ * @return yang file info set
+ */
+ public Set<YangFileInfo> createYangFileInfoSet(List<String> yangFileList) {
+ if (yangFileInfoSet == null) {
+ yangFileInfoSet = new HashSet<>();
+ }
+ for (String yangFile : yangFileList) {
+ YangFileInfo yangFileInfo = new YangFileInfo();
+ yangFileInfo.setYangFileName(yangFile);
+ yangFileInfoSet.add(yangFileInfo);
+ }
+ return yangFileInfoSet;
+ }
+
+ /**
* Compile te YANG files and generate the corresponding Java files.
* Update the generated bundle with the schema metadata.
*
- * @param yangFiles Application YANG files
- * @param config tool configuration
- * @param plugin invoking plugin
+ * @param yangFiles Application YANG files
+ * @param dependentSchema inter jar linked schema nodes
+ * @param config tool configuration
+ * @param plugin invoking plugin
+ * @throws IOException when fails to do IO operations
*/
public void compileYangFiles(Set<YangFileInfo> yangFiles,
List<YangNode> dependentSchema,
YangPluginConfig config,
CallablePlugin plugin) throws IOException {
- try {
+ synchronized (yangFiles) {
+ try {
- if (config == null || yangFiles == null) {
- throw new YangToolException(E_MISSING_INPUT);
- }
- yangFileInfoSet = yangFiles;
-
- if (config.getCodeGenDir() == null) {
- throw new YangToolException(E_CODE_GEN_PATH);
- }
-
- // Check if there are any file to translate, if not return.
- if (yangFileInfoSet == null || yangFileInfoSet.isEmpty()) {
- // No files to translate
- return;
- }
-
- createDirectories(config.resourceGenDir());
-
- // Resolve inter jar dependency.
- addSchemaToFileSet(dependentSchema);
-
-
- // Carry out the parsing for all the YANG files.
- parseYangFileInfoSet();
-
- // Resolve dependencies using linker.
- resolveDependenciesUsingLinker();
-
- // Perform translation to JAVA.
- translateToJava(config);
-
- // Serialize data model.
- Set<YangNode> compiledSchemas = new HashSet<>();
- for (YangFileInfo fileInfo : yangFileInfoSet) {
- compiledSchemas.add(fileInfo.getRootNode());
- }
-
- String serFileName = config.resourceGenDir() + YANG_META_DATA + SERIALIZED_FILE_EXTENSION;
- FileOutputStream fileOutputStream = new FileOutputStream(serFileName);
- ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
- objectOutputStream.writeObject(compiledSchemas);
- objectOutputStream.close();
- fileOutputStream.close();
-
-
- //add YANG files to JAR
- List<File> files = getListOfFile(yangFileInfoSet);
- String path = config.resourceGenDir();
- File targetDir = new File(path);
- targetDir.mkdirs();
-
- for (File file : files) {
- Files.copy(file.toPath(),
- new File(path + file.getName()).toPath(),
- StandardCopyOption.REPLACE_EXISTING);
- }
-
- if (plugin != null) {
- plugin.addCompiledSchemaToBundle();
- plugin.addGeneratedCodeToBundle();
- plugin.addYangFilesToBundle();
- }
-
- } catch (IOException | ParserException e) {
- YangToolException exception =
- new YangToolException(e.getMessage(), e);
- exception.setCurYangFile(curYangFileInfo);
-
- if (curYangFileInfo != null &&
- curYangFileInfo.getRootNode() != null) {
- try {
- translatorErrorHandler(curYangFileInfo.getRootNode(),
- config);
- } catch (IOException ex) {
- e.printStackTrace();
- throw ex;
+ if (config == null) {
+ throw new YangToolException(E_MISSING_INPUT);
}
- }
+ yangFileInfoSet = yangFiles;
- throw exception;
+ if (config.getCodeGenDir() == null) {
+ throw new YangToolException(E_CODE_GEN_PATH);
+ }
+
+ // Check if there are any file to translate, if not return.
+ if (yangFileInfoSet == null || yangFileInfoSet.isEmpty()) {
+ // No files to translate
+ return;
+ }
+
+ createDirectories(config.resourceGenDir());
+
+ // Resolve inter jar dependency.
+ addSchemaToFileSet(dependentSchema);
+
+
+ // Carry out the parsing for all the YANG files.
+ parseYangFileInfoSet();
+
+ // Resolve dependencies using linker.
+ resolveDependenciesUsingLinker();
+
+ // Perform translation to JAVA.
+ translateToJava(config);
+
+ // Serialize data model.
+ Set<YangNode> compiledSchemas = new HashSet<>();
+ for (YangFileInfo fileInfo : yangFileInfoSet) {
+ compiledSchemas.add(fileInfo.getRootNode());
+ }
+
+ String serFileName = config.resourceGenDir() + YANG_META_DATA + SERIALIZED_FILE_EXTENSION;
+ FileOutputStream fileOutputStream = new FileOutputStream(serFileName);
+ ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
+ objectOutputStream.writeObject(compiledSchemas);
+ objectOutputStream.close();
+ fileOutputStream.close();
+
+
+ //add YANG files to JAR
+ List<File> files = getListOfFile(yangFileInfoSet);
+ String path = config.resourceGenDir();
+ File targetDir = new File(path);
+ targetDir.mkdirs();
+
+ for (File file : files) {
+ Files.copy(file.toPath(),
+ new File(path + file.getName()).toPath(),
+ StandardCopyOption.REPLACE_EXISTING);
+ }
+
+ if (plugin != null) {
+ plugin.addCompiledSchemaToBundle();
+ plugin.addGeneratedCodeToBundle();
+ plugin.addYangFilesToBundle();
+ }
+
+ } catch (IOException | ParserException e) {
+ YangToolException exception =
+ new YangToolException(e.getMessage(), e);
+ exception.setCurYangFile(curYangFileInfo);
+
+ if (curYangFileInfo != null &&
+ curYangFileInfo.getRootNode() != null) {
+ try {
+ translatorErrorHandler(curYangFileInfo.getRootNode(),
+ config);
+ } catch (IOException ex) {
+ e.printStackTrace();
+ throw ex;
+ }
+ }
+
+ throw exception;
+ }
}
}
diff --git a/pom.xml b/pom.xml
index b571eea..4134f92 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,7 +26,7 @@
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-utils</artifactId>
- <version>1.10-SNAPSHOT</version>
+ <version>1.12-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>