Bharat saraswal
Committed by Gerrit Code Review

[ONOS-4286],[ONOS-3911] YANG typedef and YANG augment

                        translator implementation.

Change-Id: I3e21d1cb52bcb90b935b672eee42b836c21f448b
Showing 29 changed files with 622 additions and 82 deletions
......@@ -17,11 +17,13 @@
package org.onosproject.yangutils.parser.impl.listeners;
import java.util.List;
import org.onosproject.yangutils.datamodel.YangAugment;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
......@@ -35,21 +37,22 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLoc
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidAbsoluteSchemaNodeId;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidAbsoluteSchemaNodeId;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.utils.YangConstructType.AUGMENT_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.DATA_DEF_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.WHEN_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
......@@ -73,11 +76,13 @@ import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
*/
/**
* Implements listener based call back function corresponding to the "augment"
* Represents listener based call back function corresponding to the "augment"
* rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
*/
public final class AugmentListener {
private static final String AUGMENTED = "Augmented";
/**
* Creates a new augment listener.
*/
......@@ -110,11 +115,12 @@ public final class AugmentListener {
detectCollidingChildUtil(listener, line, charPositionInLine, "", AUGMENT_DATA);
Parsable curData = listener.getParsedDataStack().peek();
if (curData instanceof YangModule || curData instanceof YangSubModule) {
if (curData instanceof YangModule || curData instanceof YangSubModule || curData instanceof YangUses) {
YangNode curNode = (YangNode) curData;
YangAugment yangAugment = getYangAugmentNode(JAVA_GENERATION);
yangAugment.setTargetNode(targetNodes);
yangAugment.setName(getValidNameForAugment(targetNodes));
try {
curNode.addChild(yangAugment);
} catch (DataModelException e) {
......@@ -163,4 +169,30 @@ public final class AugmentListener {
validateMutuallyExclusiveChilds(ctx.dataDefStatement(), DATA_DEF_DATA, ctx.caseStatement(),
CASE_DATA, AUGMENT_DATA, ctx.augment().getText());
}
/**
* Returns a name identifier for augment.
*
* @param targetNode list of target nodes
* @return name identifier
*/
private static String getValidNameForAugment(List<YangNodeIdentifier> targetNodes) {
String name = "";
YangNodeIdentifier nodeId = targetNodes.get(targetNodes.size() - 1);
if (nodeId.getPrefix() != null) {
name = AUGMENTED + getCaptialCase(nodeId.getPrefix()) + getCaptialCase(nodeId.getName());
} else {
//TODO: name = name + ((HasAugmentation)getParentNode()).getAugmentPrefix(nodeId);
}
return name;
}
/**
* Validates for the child nodes of augment node.
*/
private static void validateForChildNodes() {
//TODO: implement with linker.
return;
}
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.onosproject.yangutils.translator.tojava;
/**
* Abstraction of an entity which represents augmented info.
*/
public interface AugmentedInfo {
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.onosproject.yangutils.translator.tojava;
import java.util.List;
/**
* Abstraction of an entity which represents augmentation of a YANG node.
*/
public interface HasAugmentation {
/**
* Adds augment info to the augment info list.
*
* @param augmentInfo augment info of node
*/
void addAugmentation(AugmentedInfo augmentInfo);
/**
* Removes augment info from the node.
*/
void removeAugmentation();
/**
* Returns list of augment info.
*
* @return list of augment info
*/
List<AugmentedInfo> getAugmentedInfoList();
}
......@@ -19,7 +19,6 @@ package org.onosproject.yangutils.translator.tojava;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getIsQualifiedAccessOrAddToImportList;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
......@@ -163,7 +162,7 @@ public final class JavaAttributeInfo {
}
/**
* Returns the import info for the attribute type. It will be null, of the type
* Returns the import info for the attribute type. It will be null, if the type
* is basic built-in java type.
*
* @return import info
......@@ -255,7 +254,6 @@ public final class JavaAttributeInfo {
*/
JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode,
attributeType, attributeName, isListAttribute);
AttributesJavaDataType.addImportInfo(importInfo);
return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
}
......
......@@ -16,8 +16,6 @@
package org.onosproject.yangutils.translator.tojava;
import org.onosproject.yangutils.translator.exception.TranslatorException;
/**
* Represents cached java file handle, which supports the addition of member attributes and
* methods.
......@@ -36,7 +34,7 @@ public class JavaFileInfo {
private String javaName;
/**
* java Package of the mapped java class.
* Java Package of the mapped java class.
*/
private String pkg;
......@@ -96,10 +94,6 @@ public class JavaFileInfo {
* @return the java package
*/
public String getPackage() {
if (pkg == null) {
throw new TranslatorException("Referencing package of a generated java file which is not set");
}
return pkg;
}
......
......@@ -24,10 +24,15 @@ import static java.util.Collections.sort;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST;
import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION_CLASS_IMPORT_CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION_CLASS_IMPORT_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
......@@ -88,7 +93,7 @@ public class JavaImportData {
}
/**
* Assign the set containing the imported class/interface info.
* Assigns the set containing the imported class/interface info.
*
* @param importSet the set containing the imported class/interface info
*/
......@@ -97,7 +102,7 @@ public class JavaImportData {
}
/**
* Add an imported class/interface info if it is not already part of the
* Adds an imported class/interface info if it is not already part of the
* collection.
*
* If already part of the collection, check if the packages are same, if so
......@@ -177,10 +182,36 @@ public class JavaImportData {
/**
* Returns import for list attribute.
*
* @return import for for list attribute
* @return import for list attribute
*/
private static String getImportForList() {
public static String getImportForList() {
return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
}
/**
* Returns import for array list attribute.
*
* @return import for array list attribute
*/
public static String getImportForArrayList() {
return IMPORT + COLLECTION_IMPORTS + PERIOD + ARRAY_LIST + SEMI_COLAN + NEW_LINE;
}
/**
* Returns import string for HasAugmentation class.
*
* @return import string for HasAugmentation class
*/
public static String getHasAugmentationImport() {
return IMPORT + HAS_AUGMENTATION_CLASS_IMPORT_PKG + PERIOD + HAS_AUGMENTATION_CLASS_IMPORT_CLASS;
}
/**
* Returns import string for AugmentedInfo class.
*
* @return import string for AugmentedInfo class
*/
public static String getAugmentedInfoImport() {
return IMPORT + AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD + AUGMENTED_INFO_CLASS_IMPORT_CLASS;
}
}
......
......@@ -87,11 +87,11 @@ public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo>
* generation for import or for qualified access.
*
* @param curNode current data model node for which the java file is being
* generated.
* generated
* @param attrType type of attribute being added, it will be null, when the
* child class is added as an attribute
* @param attributeName name of the attribute being added, it will used in
* import info for child class.
* import info for child class
* @param isListAttr is the added attribute going to be used as a list
* @return return the import info for this attribute
*/
......@@ -140,9 +140,9 @@ public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo>
* generation for import or for qualified access.
*
* @param curNode current data model node for which the java file is being
* generated.
* generated
* @param attributeName name of the attribute being added, it will used in
* import info for child class.
* import info for child class
* @param isListAttr is the added attribute going to be used as a list
* @return return the import info for this attribute
*/
......@@ -241,8 +241,7 @@ public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo>
throw new TranslatorException("missing java file info for the data model node");
}
return ((HasJavaFileInfo) curNode).getJavaFileInfo().getPackage()
.contentEquals(importInfo.getPkgInfo()
+ "." + importInfo.getClassInfo());
.contentEquals(importInfo.getPkgInfo());
}
@Override
......@@ -265,7 +264,7 @@ public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo>
}
/**
* checks if the import info matches.
* Checks if the import info matches.
*
* @param importInfo matched import
* @return if equal or not
......
......@@ -16,6 +16,7 @@
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangAugment;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
......@@ -137,11 +138,12 @@ public class YangJavaAugment extends YangAugment implements JavaCodeGeneratorInf
}
/**
* Creates a java file using the YANG grouping info.
* Create a java file using the YANG augment info.
*
* @throws IOException when failed to do IO operations
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -136,7 +136,7 @@ public class YangJavaCase extends YangCase implements JavaCodeGeneratorInfo, Jav
}
/**
* Creates a java file using the YANG grouping info.
* Creates a java file using the YANG case info.
*/
@Override
public void generateCodeExit() {
......
......@@ -125,7 +125,7 @@ public class YangJavaChoice extends YangChoice implements JavaCodeGeneratorInfo,
/**
* Prepare the information for java code generation corresponding to YANG
* case info.
* choice info.
*
* @param yangPlugin YANG plugin config
* @throws IOException IO operation fail
......@@ -137,7 +137,7 @@ public class YangJavaChoice extends YangChoice implements JavaCodeGeneratorInfo,
}
/**
* Creates a java file using the YANG grouping info.
* Creates a java file using the YANG choice info.
*/
@Override
public void generateCodeExit() {
......
......@@ -125,7 +125,7 @@ public class YangJavaGrouping extends YangGrouping implements JavaCodeGeneratorI
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
* grouping info.
*
* @param yangPlugin YANG plugin config
* @throws IOException IO operation fail
......
......@@ -126,7 +126,7 @@ public class YangJavaInput extends YangInput implements JavaCodeGeneratorInfo, J
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
* input info.
*
* @param yangPlugin YANG plugin config
* @throws IOException IO operation fail
......@@ -137,7 +137,7 @@ public class YangJavaInput extends YangInput implements JavaCodeGeneratorInfo, J
}
/**
* Creates a java file using the YANG grouping info.
* Creates a java file using the YANG input info.
*
* @throws IOException IO operation fail
*/
......
......@@ -125,7 +125,7 @@ public class YangJavaList extends YangList implements JavaCodeGeneratorInfo, Jav
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
* list info.
*
* @param yangPlugin YANG plugin config
* @throws IOException IO operation fail
......@@ -136,7 +136,7 @@ public class YangJavaList extends YangList implements JavaCodeGeneratorInfo, Jav
}
/**
* Creates a java file using the YANG grouping info.
* Creates a java file using the YANG list info.
*
* @throws IOException IO operation fail
*/
......
......@@ -165,7 +165,7 @@ public class YangJavaNotification extends YangNotification
}
/**
* Create a java file using the YANG notification info.
* Creates a java file using the YANG notification info.
*/
@Override
public void generateCodeExit() {
......
......@@ -126,7 +126,7 @@ public class YangJavaOutput extends YangOutput implements JavaCodeGeneratorInfo,
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
* output info.
*
* @param yangPlugin YANG plugin config
* @throws IOException IO operation fail
......@@ -137,7 +137,7 @@ public class YangJavaOutput extends YangOutput implements JavaCodeGeneratorInfo,
}
/**
* Creates a java file using the YANG grouping info.
* Creates a java file using the YANG output info.
*
* @throws IOException IO operation fail
*/
......
......@@ -34,7 +34,7 @@ public class YangJavaRpc extends YangRpc implements JavaCodeGenerator {
/**
* Prepares the information for java code generation corresponding to YANG
* rpc info.
* RPC info.
*
* @param yangPlugin YANG plugin config
* @throws IOException IO operation fail
......@@ -45,7 +45,7 @@ public class YangJavaRpc extends YangRpc implements JavaCodeGenerator {
}
/**
* Creates a java file using the YANG rpc info.
* Creates a java file using the YANG RPC info.
*
* @throws IOException IO operation fail
*/
......
......@@ -139,7 +139,7 @@ public class YangJavaSubModule extends YangSubModule implements JavaCodeGenerato
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
* submodule info.
*
* @param yangPlugin YANG plugin config
* @throws IOException IO operation fail
......@@ -152,7 +152,7 @@ public class YangJavaSubModule extends YangSubModule implements JavaCodeGenerato
}
/**
* Creates a java file using the YANG grouping info.
* Creates a java file using the YANG submodule info.
*/
@Override
public void generateCodeExit() {
......
......@@ -135,7 +135,7 @@ public class YangJavaTypeDef extends YangTypeDef
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
* typedef info.
*
* @param yangPlugin YANG plugin config
* @throws IOException IO operations fails
......@@ -162,7 +162,7 @@ public class YangJavaTypeDef extends YangTypeDef
}
/**
* Create a java file using the YANG grouping info.
* Create a java file using the YANG typedef info.
*
* @throws IOException IO operations fails
*/
......
......@@ -103,7 +103,7 @@ public class YangJavaUses extends YangUses implements JavaCodeGenerator, HasJava
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
* uses info.
*
* @param yangPlugin YANG plugin config
*/
......@@ -120,7 +120,7 @@ public class YangJavaUses extends YangUses implements JavaCodeGenerator, HasJava
}
/**
* Create a java file using the YANG grouping info.
* Create a java file using the YANG uses info.
*/
@Override
public void generateCodeExit() {
......
......@@ -21,8 +21,12 @@ import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getExtendsList;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.isExtendsList;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
import static org.onosproject.yangutils.utils.UtilConstants.EXTEND;
import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
......@@ -32,6 +36,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
/**
* Represents generator for class definition of generated files.
......@@ -84,8 +89,16 @@ public final class ClassDefinitionGenerator {
* @return definition
*/
private static String getInterfaceDefinition(String yangName) {
if (!isExtendsList()) {
return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
String def = PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + EXTEND + SPACE;
for (String extend : getExtendsList()) {
def = def + extend + COMMA;
}
def = trimAtLast(def, COMMA);
return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
/**
......@@ -96,7 +109,6 @@ public final class ClassDefinitionGenerator {
* @return definition
*/
private static String getBuilderInterfaceDefinition(String yangName) {
return INTERFACE + SPACE + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE;
}
......@@ -107,7 +119,6 @@ public final class ClassDefinitionGenerator {
* @return definition
*/
private static String getBuilderClassDefinition(String yangName) {
return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
+ yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
......@@ -119,7 +130,6 @@ public final class ClassDefinitionGenerator {
* @return definition
*/
private static String getImplClassDefinition(String yangName) {
return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + IMPL + SPACE + IMPLEMENTS + SPACE + yangName
+ SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
......@@ -131,7 +141,6 @@ public final class ClassDefinitionGenerator {
* @return definition
*/
private static String getTypeDefClassDefinition(String yangName) {
return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
}
......
......@@ -20,12 +20,20 @@ import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import static org.onosproject.yangutils.translator.tojava.utils.ClassDefinitionGenerator.generateClassDefinition;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST;
import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
import static org.onosproject.yangutils.utils.UtilConstants.LIST;
import static org.onosproject.yangutils.utils.UtilConstants.NEW;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
......@@ -127,6 +135,18 @@ public final class JavaCodeSnippetGen {
}
/**
* Returns attribute of augmented info for generated impl file.
*
* @return attribute of augmented info for generated impl file
*/
public static String getAugmentedInfoAttribute() {
return FOUR_SPACE_INDENTATION + PRIVATE + SPACE + getListAttribute(AUGMENTED_INFO) + SPACE
+ getSmallCase(AUGMENTED_INFO) + LIST + SPACE + EQUAL + SPACE + NEW + SPACE + ARRAY_LIST
+ DIAMOND_OPEN_BRACKET + DIAMOND_CLOSE_BRACKET + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN
+ NEW_LINE;
}
/**
* Returns based on the file type and the YANG name of the file, generate the class
* / interface definition close.
*
......
......@@ -40,16 +40,21 @@ import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getAugmentedInfoAttribute;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getDataFromTempFileHandle;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.initiateJavaFileGeneration;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddAugmentInfoMethodImpl;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAugmentInfoListImpl;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodOpen;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodOpen;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRemoveAugmentationImpl;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodOpen;
import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isHasAugmentationExtended;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
......@@ -67,12 +72,58 @@ import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.partString;
public final class JavaFileGenerator {
/**
* Creates an instance of java file generator.
* Flag to check whether generated interface file need to extends any class.
*/
private static boolean isExtendsList = false;
/**
* List of classes to be extended by generated interface file.
*/
private static List<String> extendsList = new ArrayList<>();
/**
* Creates an instance of java file generator.
*/
private JavaFileGenerator() {
}
/**
* Returns true if extends list is not empty.
*
* @return true or false
*/
public static boolean isExtendsList() {
return isExtendsList;
}
/**
* Sets the value of is extends list.
*
* @param isExtends true or false
*/
public static void setIsExtendsList(boolean isExtends) {
isExtendsList = isExtends;
}
/**
* Returns list of extended classes.
*
* @return list of extended classes
*/
public static List<String> getExtendsList() {
return extendsList;
}
/**
* Sets the list of extended classes.
*
* @param extendList list of extended classes
*/
public static void setExtendsList(List<String> extendList) {
extendsList = extendList;
}
/**
* Returns generated interface file for current node.
*
* @param file file
......@@ -91,6 +142,7 @@ public final class JavaFileGenerator {
String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
initiateJavaFileGeneration(file, className, INTERFACE_MASK, imports, path);
if (isAttrPresent) {
/**
* Add getter methods to interface file.
......@@ -258,6 +310,12 @@ public final class JavaFileGenerator {
+ " while impl class file generation");
}
/**
* Add attribute for augmented info's list.
*/
if (isHasAugmentationExtended(getExtendsList())) {
insertDataIntoJavaFile(file, getAugmentedInfoAttribute());
}
insertDataIntoJavaFile(file, NEW_LINE);
try {
/**
......@@ -298,6 +356,16 @@ public final class JavaFileGenerator {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while impl class file generation");
}
/**
* Add method for augment info's list.
*/
if (isHasAugmentationExtended(getExtendsList())) {
methods.add(getAddAugmentInfoMethodImpl());
methods.add(getAugmentInfoListImpl());
methods.add(getRemoveAugmentationImpl());
}
/**
* Add methods in impl class.
*/
......@@ -310,7 +378,7 @@ public final class JavaFileGenerator {
}
/**
* Generate class file for type def.
* Generates class file for type def.
*
* @param file generated file
* @param curNode current YANG node
......@@ -364,11 +432,6 @@ public final class JavaFileGenerator {
methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode));
/**
* Setter method.
*/
methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles().addTypeDefsSetter());
/**
* Hash code method.
*/
methods.add(getHashCodeMethodClose(getHashCodeMethodOpen() + partString(
......@@ -388,7 +451,7 @@ public final class JavaFileGenerator {
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while tyoe def class file generation");
+ " while type def class file generation");
}
for (String method : methods) {
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.onosproject.yangutils.translator.tojava.utils;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
import static org.onosproject.yangutils.translator.tojava.JavaImportData.getAugmentedInfoImport;
import static org.onosproject.yangutils.translator.tojava.JavaImportData.getHasAugmentationImport;
import static org.onosproject.yangutils.translator.tojava.JavaImportData.getImportForArrayList;
import static org.onosproject.yangutils.translator.tojava.JavaImportData.getImportForList;
import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
/**
* Represents utilities for temporary java code fragments.
*/
public final class TempJavaCodeFragmentFilesUtils {
/**
* Creates a private instance of temporary java code fragment utils.
*/
private TempJavaCodeFragmentFilesUtils() {
}
/**
* Adds imports for ToString and HashCodeMethod.
*
* @param curNode current YANG node
* @param imports import list
* @return import list
*/
public static List<String> addImportsToStringAndHasCodeMethods(YangNode curNode, List<String> imports) {
if (curNode instanceof HasJavaImportData) {
imports.add(((HasJavaImportData) curNode).getJavaImportData().getImportForHashAndEquals());
imports.add(((HasJavaImportData) curNode).getJavaImportData().getImportForToString());
}
return imports;
}
/**
* Adds import for HasAugmentation class.
*
* @param curNode current YANG node
* @param imports list of imports
* @param operation add or delete import
* @return import for HasAugmentation class
*/
public static List<String> addHasAugmentationImport(YangNode curNode, List<String> imports, boolean operation) {
if (curNode instanceof HasJavaImportData) {
String thisImport = getHasAugmentationImport();
performOperationOnImports(imports, thisImport, operation);
}
return imports;
}
/**
* Adds import for AugmentedInfo class.
*
* @param curNode current YANG node
* @param imports list of imports
* @param operation add or delete import
* @return import for AugmentedInfo class
*/
public static List<String> addAugmentedInfoImport(YangNode curNode, List<String> imports, boolean operation) {
if (curNode instanceof HasJavaImportData) {
String thisImport = getAugmentedInfoImport();
performOperationOnImports(imports, thisImport, operation);
}
return imports;
}
/**
* Adds import for array list.
*
* @param curNode current YANG node
* @param imports list of imports
* @param operation add or delete import
* @return import for HasAugmentation class
*/
public static List<String> addArrayListImport(YangNode curNode, List<String> imports, boolean operation) {
if (curNode instanceof HasJavaImportData) {
String arrayListImport = getImportForArrayList();
String listImport = getImportForList();
performOperationOnImports(imports, arrayListImport, operation);
if (!imports.contains(listImport)) {
/**
* List can be there because of attribute also , so no need to remove it and operation will
* always be add(true).
*/
performOperationOnImports(imports, listImport, true);
}
}
return imports;
}
/**
* Performs given operations on import list.
*
* @param imports list of imports
* @param curImport current import
* @param operation add or remove
* @return import list
*/
private static List<String> performOperationOnImports(List<String> imports, String curImport, boolean operation) {
if (operation) {
imports.add(curImport);
} else {
imports.remove(curImport);
}
java.util.Collections.sort(imports);
return imports;
}
/**
* Prepares java file generator for extends list.
*
* @param extendsList list of classes need to be extended
*/
public static void prepareJavaFileGeneratorForExtendsList(List<String> extendsList) {
if (!extendsList.isEmpty() && !extendsList.equals(null)) {
JavaFileGenerator.setExtendsList(extendsList);
JavaFileGenerator.setIsExtendsList(true);
} else {
JavaFileGenerator.getExtendsList().clear();
JavaFileGenerator.setIsExtendsList(false);
}
}
/**
* Returns true if HasAugmentation class needs to be extended.
*
* @param extendsList list of classes need to be extended
* @return true or false
*/
public static boolean isHasAugmentationExtended(List<String> extendsList) {
if (extendsList != null && extendsList.contains(HAS_AUGMENTATION)) {
return true;
}
return false;
}
/**
* Returns true if AugmentedInfo class needs to be extended.
*
* @param extendsList list of classes need to be extended
* @return true or false
*/
public static boolean isAugmentedInfoExtended(List<String> extendsList) {
if (extendsList != null && extendsList.contains(AUGMENTED_INFO)) {
return true;
}
return false;
}
/**
* Closes the file handle for temporary file.
*
* @param file file to be closed
* @param toBeDeleted flag to indicate if file needs to be deleted
* @throws IOException when failed to close the file handle
*/
public static void closeFile(File file, boolean toBeDeleted) throws IOException {
if (file != null) {
updateFileHandle(file, null, true);
if (toBeDeleted) {
file.delete();
}
}
}
}
......@@ -17,8 +17,17 @@
package org.onosproject.yangutils.translator.tojava.utils;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangAugment;
import org.onosproject.yangutils.datamodel.YangCase;
import org.onosproject.yangutils.datamodel.YangChoice;
import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.YangInput;
import org.onosproject.yangutils.datamodel.YangLeavesHolder;
import org.onosproject.yangutils.datamodel.YangList;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNotification;
import org.onosproject.yangutils.datamodel.YangOutput;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo;
......@@ -26,6 +35,8 @@ import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSy
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
/**
......@@ -143,6 +154,21 @@ public final class YangJavaModelUtils {
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
.addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
/**
* For augmentation of nodes.
*/
if (javaCodeGeneratorInfo instanceof YangContainer
|| javaCodeGeneratorInfo instanceof YangCase
|| javaCodeGeneratorInfo instanceof YangChoice
|| javaCodeGeneratorInfo instanceof YangInput
|| javaCodeGeneratorInfo instanceof YangList
|| javaCodeGeneratorInfo instanceof YangNotification
|| javaCodeGeneratorInfo instanceof YangOutput) {
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(HAS_AUGMENTATION);
} else if (javaCodeGeneratorInfo instanceof YangAugment) {
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(AUGMENTED_INFO);
}
}
/**
......
......@@ -441,58 +441,69 @@ public final class UtilConstants {
* String built in java type.
*/
public static final String STRING_DATA_TYPE = "String";
/**
* java.lang.* packages.
* Java.lang.* packages.
*/
public static final String JAVA_LANG = "java.lang";
/**
* boolean built in java type.
* Java.math.* packages.
*/
public static final String JAVA_MATH = "java.math";
/**
* Boolean built in java type.
*/
public static final String BOOLEAN_DATA_TYPE = "boolean";
/**
* byte java built in type.
* BigInteger built in java type.
*/
public static final String BIG_INTEGER = "BigInteger";
/**
* Byte java built in type.
*/
public static final String BYTE = "byte";
/**
* short java built in type.
* Short java built in type.
*/
public static final String SHORT = "short";
/**
* int java built in type.
* Int java built in type.
*/
public static final String INT = "int";
/**
* long java built in type.
* Long java built in type.
*/
public static final String LONG = "long";
/**
* float java built in type.
* Float java built in type.
*/
public static final String FLOAT = "float";
/**
* double java built in type.
* Double java built in type.
*/
public static final String DOUBLE = "double";
/**
* boolean built in java wrapper type.
* Boolean built in java wrapper type.
*/
public static final String BOOLEAN_WRAPPER = "Boolean";
/**
* byte java built in wrapper type.
* Byte java built in wrapper type.
*/
public static final String BYTE_WRAPPER = "Byte";
/**
* short java built in wrapper type.
* Short java built in wrapper type.
*/
public static final String SHORT_WRAPPER = "Short";
......@@ -502,17 +513,17 @@ public final class UtilConstants {
public static final String INTEGER_WRAPPER = "Integer";
/**
* long java built in wrapper type.
* Long java built in wrapper type.
*/
public static final String LONG_WRAPPER = "Long";
/**
* float java built in wrapper type.
* Float java built in wrapper type.
*/
public static final String FLOAT_WRAPPER = "Float";
/**
* double java built in wrapper type.
* Double java built in wrapper type.
*/
public static final String DOUBLE_WRAPPER = "Double";
......@@ -687,6 +698,41 @@ public final class UtilConstants {
public static final String JAVA_UTIL_OBJECTS_IMPORT_CLASS = "Objects;\n";
/**
* Static attribute for HasAugmentation class import package.
*/
public static final String HAS_AUGMENTATION_CLASS_IMPORT_PKG = "org.onosproject.yangutils.translator.tojava";
/**
* Static attribute for HasAugmentation class import class.
*/
public static final String HAS_AUGMENTATION_CLASS_IMPORT_CLASS = "HasAugmentation;\n";
/**
* Static attribute for AugmentedInfo class import package.
*/
public static final String AUGMENTED_INFO_CLASS_IMPORT_PKG = "org.onosproject.yangutils.translator.tojava";
/**
* Static attribute for AugmentedInfo class import class.
*/
public static final String AUGMENTED_INFO_CLASS_IMPORT_CLASS = "AugmentedInfo;\n";
/**
* Static attribute for augmentation class.
*/
public static final String AUGMENTATION = "Augmentation";
/**
* Static attribute for HasAugmentation class.
*/
public static final String HAS_AUGMENTATION = "HasAugmentation";
/**
* Static attribute for AugmentedInfo class.
*/
public static final String AUGMENTED_INFO = "AugmentedInfo";
/**
* Static attribute for abstract collection.
*/
public static final String ABSTRACT_COLLECTION = "AbstractCollection";
......
......@@ -21,12 +21,19 @@ import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef;
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.datamodel.YangDataTypes.BOOLEAN;
import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
import static org.onosproject.yangutils.datamodel.YangDataTypes.INT32;
import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
import static org.onosproject.yangutils.datamodel.YangDataTypes.UINT8;
......@@ -44,11 +51,13 @@ public class AttributesJavaDataTypeTest {
private static final YangDataTypes TYPE2 = INT32;
private static final YangDataTypes TYPE3 = BOOLEAN;
private static final YangDataTypes TYPE4 = UINT8;
private static final YangDataTypes TYPE_DEF = DERIVED;
private static final String CLASS_INFO1 = "String";
private static final String CLASS_INFO2 = "int";
private static final String CLASS_INFO3 = "boolean";
private static final String CLASS_INFO4 = "short";
private static final String CLASS_INFO5 = "Integer";
private static final String TYPE_DEF_PKG = "target.test";
private static String test = "";
/**
......@@ -128,14 +137,65 @@ public class AttributesJavaDataTypeTest {
}
/**
* Unit test case for typedef.
*
* @throws DataModelException when fails to do data model operations
*/
@Test
public void testForTypeDef() throws DataModelException {
test = getJavaImportPackage(getStubExtendedInfo(getStubYangType(TYPE_DEF)), false, TYPE_DEF_PKG);
assertThat(true, is(test.equals(TYPE_DEF_PKG)));
}
/**
* Returns stub YANG type for test.
*
* @param dataTypes YANG data types
* @return YANG type
*/
private YangType<?> getStubYangType(YangDataTypes dataTypes) {
YangType<?> type = new YangType();
YangType<?> type = new YangType<>();
type.setDataType(dataTypes);
return type;
}
/**
* Returns YANG type with extended info.
*
* @param type YANG type
* @return YANG type with extended info
* @throws DataModelException when fails to do data model operations
*/
@SuppressWarnings("unchecked")
private YangType<?> getStubExtendedInfo(YangType<?> type) throws DataModelException {
YangJavaTypeDef typedef = new YangJavaTypeDef();
getStubParent().addChild(typedef);
YangDerivedInfo<?> derInfo = new YangDerivedInfo<>();
derInfo.setReferredTypeDef(typedef);
((YangType<YangDerivedInfo<?>>) type).setDataTypeExtendedInfo(derInfo);
return type;
}
/**
* Returns java file info.
*
* @return java file info
*/
private JavaFileInfo addStubJavaFileInfo() {
JavaFileInfo fileInfo = new JavaFileInfo();
fileInfo.setJavaName("test");
fileInfo.setPackage("target");
return fileInfo;
}
/**
* Adds stub parent module for typedef.
*
* @return stub parent module
*/
private YangNode getStubParent() {
YangJavaModule parent = new YangJavaModule();
parent.setJavaFileInfo(addStubJavaFileInfo());
return parent;
}
}
......