Gaurav Agrawal
Committed by Gerrit Code Review

[ONOS-4350] Inter file linking implementation and inter-jar linking framework

Change-Id: I71a26ba3e0b9d17261e78a9313fe7f047195932e
Showing 63 changed files with 1548 additions and 273 deletions
/*
* 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.datamodel;
/**
* Represents ENUM to identify the YANG resolution type.
*/
public enum ResolutionType {
/**
* Identifies that resolution is for typedef.
*/
TYPEDEF_RESOLUTION,
/**
* Identifies that resolution is for grouping.
*/
GROUPING_RESOLUTION
}
......@@ -15,10 +15,14 @@
*/
package org.onosproject.yangutils.datamodel;
import java.util.Set;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findReferredNode;
/*-
* Reference 6020.
*
......@@ -46,7 +50,7 @@ import org.onosproject.yangutils.utils.YangConstructType;
/**
* Represents the belongs-to data type information.
*/
public class YangBelongsTo implements Parsable {
public class YangBelongsTo implements Parsable, LocationInfo {
/**
* Reference RFC 6020.
......@@ -70,6 +74,12 @@ public class YangBelongsTo implements Parsable {
*/
private String prefix;
// Error Line number.
private int lineNumber;
// Error character position.
private int charPosition;
/**
* Create a belongs to object.
*/
......@@ -90,7 +100,6 @@ public class YangBelongsTo implements Parsable {
* Sets the belongs to module name.
*
* @param belongsToModuleName the belongs to module name to set
*
*/
public void setBelongsToModuleName(String belongsToModuleName) {
this.belongsToModuleName = belongsToModuleName;
......@@ -161,4 +170,47 @@ public class YangBelongsTo implements Parsable {
public void validateDataOnExit() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
@Override
public int getLineNumber() {
return lineNumber;
}
@Override
public int getCharPosition() {
return charPosition;
}
@Override
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
@Override
public void setCharPosition(int charPositionInLine) {
this.charPosition = charPositionInLine;
}
/**
* Links the belongs to with a module.
*
* @param yangFileInfoSet YANG file information set
* @throws DataModelException a violation in data model rule
*/
public void linkWithModule(Set<YangFileInfo> yangFileInfoSet)
throws DataModelException {
String belongsToModuleName = getBelongsToModuleName();
YangNode moduleNode = findReferredNode(yangFileInfoSet, belongsToModuleName);
if (moduleNode != null) {
if (moduleNode instanceof YangModule) {
setModuleNode(moduleNode);
return;
}
}
DataModelException exception = new DataModelException("YANG file error : Module " + belongsToModuleName +
"to which sub-module belongs to is not found.");
exception.setLine(getLineNumber());
exception.setCharPosition(getCharPosition());
throw exception;
}
}
......
......@@ -17,10 +17,10 @@
package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.linker.impl.ResolvableStatus;
import com.google.common.base.Strings;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.INTRA_FILE_RESOLVED;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.RESOLVED;
import static org.onosproject.yangutils.datamodel.YangDataTypes.BINARY;
import static org.onosproject.yangutils.datamodel.YangDataTypes.BITS;
import static org.onosproject.yangutils.datamodel.YangDataTypes.BOOLEAN;
import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
......@@ -30,6 +30,8 @@ import static org.onosproject.yangutils.datamodel.YangDataTypes.IDENTITYREF;
import static org.onosproject.yangutils.datamodel.YangDataTypes.LEAFREF;
import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
import static org.onosproject.yangutils.datamodel.YangDataTypes.UNION;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.INTRA_FILE_RESOLVED;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.RESOLVED;
import static org.onosproject.yangutils.utils.RestrictionResolver.isOfRangeRestrictedType;
import static org.onosproject.yangutils.utils.RestrictionResolver.processLengthRestriction;
import static org.onosproject.yangutils.utils.RestrictionResolver.processRangeRestriction;
......@@ -236,7 +238,7 @@ public class YangDerivedInfo<T> implements LocationInfo {
* Check whether the referred typedef is resolved.
*/
if (baseType.getResolvableStatus() != INTRA_FILE_RESOLVED && baseType.getResolvableStatus() != RESOLVED) {
throw new DataModelException("Linker Error: Referred typedef is not resolved.");
throw new DataModelException("Linker Error: Referred typedef is not resolved for type.");
}
/*
......@@ -301,6 +303,28 @@ public class YangDerivedInfo<T> implements LocationInfo {
*/
return RESOLVED;
}
} else if (getEffectiveBuiltInType() == BINARY) {
if (refDerivedInfo.getResolvedExtendedInfo() == null) {
resolveLengthRestriction(null);
/*
* Return the resolution status as resolved, if it's not
* resolve length restriction will throw exception
* in previous function.
*/
return RESOLVED;
} else {
if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangRangeRestriction)) {
throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
"type.");
}
resolveLengthRestriction((YangRangeRestriction) refDerivedInfo.getResolvedExtendedInfo());
/*
* Return the resolution status as resolved, if it's not
* resolve length restriction will throw exception
* in previous function.
*/
return RESOLVED;
}
}
} else {
setEffectiveBuiltInType((baseType.getDataType()));
......@@ -356,6 +380,28 @@ public class YangDerivedInfo<T> implements LocationInfo {
*/
return RESOLVED;
}
} else if (getEffectiveBuiltInType() == BINARY) {
if (baseType.getDataTypeExtendedInfo() == null) {
resolveLengthRestriction(null);
/*
* Return the resolution status as resolved, if it's not
* resolve length restriction will throw exception
* in previous function.
*/
return RESOLVED;
} else {
if (!(baseType.getDataTypeExtendedInfo() instanceof YangRangeRestriction)) {
throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
"type.");
}
resolveLengthRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo());
/*
* Return the resolution status as resolved, if it's not
* resolve length restriction will throw exception
* in previous function.
*/
return RESOLVED;
}
}
}
......
......@@ -15,10 +15,14 @@
*/
package org.onosproject.yangutils.datamodel;
import java.util.Set;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findReferredNode;
/*
* Reference:RFC 6020.
* The "import" statement makes definitions from one module available
......@@ -64,7 +68,7 @@ import org.onosproject.yangutils.utils.YangConstructType;
* Represents the information about the imported modules.
*/
public class YangImport
implements Parsable {
implements Parsable, LocationInfo {
/**
* Name of the module that is being imported.
......@@ -78,7 +82,7 @@ public class YangImport
/**
* Reference:RFC 6020.
*
* <p>
* The import's "revision-date" statement is used to specify the exact
* version of the module to import. The "revision-date" statement MUST match
* the most recent "revision" statement in the imported module. organization
......@@ -87,10 +91,20 @@ public class YangImport
private String revision;
/**
* Reference to node which is imported.
*/
private YangNode importedNode;
// Error Line number.
private int lineNumber;
// Error character position.
private int charPosition;
/**
* Creates a YANG import.
*/
public YangImport() {
}
/**
......@@ -181,4 +195,91 @@ public class YangImport
// TODO auto-generated method stub, to be implemented by parser
}
/**
* Returns imported node.
*
* @return imported node
*/
public YangNode getImportedNode() {
return importedNode;
}
/**
* Sets imported node.
*
* @param importedNode imported node
*/
public void setImportedNode(YangNode importedNode) {
this.importedNode = importedNode;
}
@Override
public int getLineNumber() {
return lineNumber;
}
@Override
public int getCharPosition() {
return charPosition;
}
@Override
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
@Override
public void setCharPosition(int charPositionInLine) {
this.charPosition = charPositionInLine;
}
/**
* Adds reference to an import.
*
* @param yangFileInfoSet YANG file info set
* @throws DataModelException a violation of data model rules
*/
public void addReferenceToImport(Set<YangFileInfo> yangFileInfoSet) throws DataModelException {
String importedModuleName = getModuleName();
String importedModuleRevision = getRevision();
YangNode moduleNode = null;
/*
* Find the imported module node for a given module name
* with a specified revision if revision is not null.
*/
if (importedModuleRevision != null) {
String importedModuleNameWithRevision = importedModuleName + "@" + importedModuleRevision;
moduleNode = findReferredNode(yangFileInfoSet, importedModuleNameWithRevision);
}
/*
* Find the imported module node for a given module name
* without revision if can't find with revision.
*/
if (moduleNode == null) {
moduleNode = findReferredNode(yangFileInfoSet, importedModuleName);
}
if (moduleNode != null) {
if (moduleNode instanceof YangModule) {
if (getRevision() == null || getRevision().isEmpty()) {
setImportedNode(moduleNode);
return;
}
// Match revision if import is with revision.
if (((YangModule) moduleNode).getRevision().getRevDate().equals(importedModuleRevision)) {
setImportedNode(moduleNode);
return;
}
}
}
// Exception if there is no match.
DataModelException exception = new DataModelException("YANG file error : Imported module "
+ importedModuleName + " with revision " + importedModuleRevision + " is not found.");
exception.setLine(getLineNumber());
exception.setCharPosition(getCharPosition());
throw exception;
}
}
......
......@@ -15,10 +15,14 @@
*/
package org.onosproject.yangutils.datamodel;
import java.util.Set;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findReferredNode;
/*
* Reference:RFC 6020.
* The "include" statement is used to make content from a submodule
......@@ -38,7 +42,7 @@ import org.onosproject.yangutils.utils.YangConstructType;
* Represents the information about the included sub-modules.
*/
public class YangInclude
implements Parsable {
implements Parsable, LocationInfo {
/**
* Name of the sub-module that is being included.
......@@ -52,6 +56,17 @@ public class YangInclude
private String revision;
/**
* Reference to node which is included.
*/
private YangNode includedNode;
// Error Line number.
private int lineNumber;
// Error character position.
private int charPosition;
/**
* Creates a YANG include.
*/
public YangInclude() {
......@@ -127,4 +142,98 @@ public class YangInclude
}
public YangNode getIncludedNode() {
return includedNode;
}
public void setIncludedNode(YangNode includedNode) {
this.includedNode = includedNode;
}
@Override
public int getLineNumber() {
return lineNumber;
}
@Override
public int getCharPosition() {
return charPosition;
}
@Override
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
@Override
public void setCharPosition(int charPositionInLine) {
this.charPosition = charPositionInLine;
}
/**
* Adds reference to an include.
*
* @param yangFileInfoSet YANG file info set
* @return YANG sub module node
* @throws DataModelException a violation of data model rules
*/
public YangSubModule addReferenceToInclude(Set<YangFileInfo> yangFileInfoSet) throws DataModelException {
String includedSubModuleName = getSubModuleName();
String includedSubModuleRevision = getRevision();
YangNode subModuleNode = null;
/*
* Find the included sub-module node for a given module name
* with a specified revision if revision is not null.
*/
if (includedSubModuleRevision != null) {
String includedSubModuleNameWithRevision = includedSubModuleName + "@" + includedSubModuleRevision;
subModuleNode = findReferredNode(yangFileInfoSet, includedSubModuleNameWithRevision);
}
/*
* Find the imported sub module node for a given module name
* without revision if can't find with revision.
*/
if (subModuleNode == null) {
subModuleNode = findReferredNode(yangFileInfoSet, includedSubModuleName);
}
if (subModuleNode != null) {
if (subModuleNode instanceof YangSubModule) {
if (getRevision() == null || getRevision().isEmpty()) {
setIncludedNode(subModuleNode);
return (YangSubModule) subModuleNode;
}
// Match revision if inclusion is with revision.
if (((YangSubModule) subModuleNode).getRevision().getRevDate().equals(includedSubModuleRevision)) {
setIncludedNode(subModuleNode);
return (YangSubModule) subModuleNode;
}
}
}
// Exception if there is no match.
DataModelException exception = new DataModelException("YANG file error : Included sub module " +
includedSubModuleName + "with a given revision is not found.");
exception.setLine(getLineNumber());
exception.setCharPosition(getCharPosition());
throw exception;
}
/**
* Reports an error when included sub-module doesn't meet condition that
* "included sub-modules should belong module, as defined by the
* "belongs-to" statement or sub-modules are only allowed to include other
* sub-modules belonging to the same module.
*
* @throws DataModelException a violation in data model rule
*/
public void reportIncludeError() throws DataModelException {
DataModelException exception = new DataModelException("YANG file error : Included sub-module " +
getSubModuleName() + "doesn't belongs to parent module also it doesn't belongs" +
"to sub-module belonging to the same parent module.");
exception.setLine(getLineNumber());
exception.setCharPosition(getCharPosition());
throw exception;
}
}
......
/*
* 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.datamodel;
/*-
* Reference RFC 6020.
*
* Binary can be restricted with "length" statements alone.
*
*/
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
import org.onosproject.yangutils.utils.builtindatatype.YangUint64;
/**
* Represents the restriction for length data type.
*/
public class YangLengthRestriction implements YangDesc, YangReference, YangAppErrorInfo, Parsable {
/*-
* Reference RFC 6020.
* The length Statement
*
* The "length" statement, which is an optional sub-statement to the
* "type" statement, takes as an argument a length expression string.
* It is used to restrict the built-in type "string", or types derived
* from "string".
* A "length" statement restricts the number of unicode characters in
* the string.
* A length range consists of an explicit value, or a lower bound, two
* consecutive dots "..", and an upper bound. Multiple values or ranges
* can be given, separated by "|". Length-restricting values MUST NOT
* be negative. If multiple values or ranges are given, they all MUST
* be disjoint and MUST be in ascending order. If a length restriction
* is applied to an already length-restricted type, the new restriction
* MUST be equal or more limiting, that is, raising the lower bounds,
* reducing the upper bounds, removing explicit length values or ranges,
* or splitting ranges into multiple ranges with intermediate gaps. A
* length value is a non-negative integer, or one of the special values
* "min" or "max". "min" and "max" mean the minimum and maximum length
* accepted for the type being restricted, respectively. An
* implementation is not required to support a length value larger than
* 18446744073709551615.
* The length's sub-statements
*
* +---------------+---------+-------------+-----------------+
* | substatement | section | cardinality | mapped data type|
* +---------------+---------+-------------+-----------------+
* | description | 7.19.3 | 0..1 | string |
* | error-app-tag | 7.5.4.2 | 0..1 | string |
* | error-message | 7.5.4.1 | 0..1 | string |
* | reference | 7.19.4 | 0..1 | string |
* +---------------+---------+-------------+-----------------+
*/
/**
* Length restriction information.
*/
private YangRangeRestriction<YangUint64> lengthRestriction;
/**
* Textual reference.
*/
private String reference;
/**
* Application's error message, to be used for data error.
*/
private String errorMessage;
/**
* Application's error tag, to be filled in data validation error response.
*/
private String errorAppTag;
/**
* Textual description.
*/
private String description;
/**
* Creates a YANG length restriction object.
*/
public YangLengthRestriction() {
}
/**
* Returns the length restriction on the string data.
*
* @return length restriction on the string data
*/
public YangRangeRestriction<YangUint64> getLengthRestriction() {
return lengthRestriction;
}
/**
* Sets the length restriction on the string data.
*
* @param lengthRestriction length restriction on the string data
*/
public void setLengthRestriction(YangRangeRestriction<YangUint64> lengthRestriction) {
this.lengthRestriction = lengthRestriction;
}
/**
* Returns the textual reference of the length restriction.
*
* @return textual reference of the length restriction
*/
@Override
public String getReference() {
return reference;
}
/**
* Sets the textual reference of the length restriction.
*
* @param ref textual reference of the length restriction
*/
@Override
public void setReference(String ref) {
reference = ref;
}
/**
* Returns the description of the length restriction.
*
* @return description of the length restriction
*/
@Override
public String getDescription() {
return description;
}
/**
* Sets the description of the length restriction.
*
* @param desc description of the length restriction
*/
@Override
public void setDescription(String desc) {
description = desc;
}
/**
* Returns application's error message, to be used for data error.
*
* @return Application's error message, to be used for data error
*/
@Override
public String getGetErrorMessage() {
return errorMessage;
}
/**
* Sets Application's error message, to be used for data error.
*
* @param errMsg Application's error message, to be used for data error
*/
@Override
public void setErrorMessage(String errMsg) {
errorMessage = errMsg;
}
/**
* Returns application's error tag, to be used for data error.
*
* @return application's error tag, to be used for data error
*/
@Override
public String getGetErrorAppTag() {
return errorAppTag;
}
/**
* Sets application's error tag, to be used for data error.
*
* @param errTag application's error tag, to be used for data error.
*/
@Override
public void setErrorAppTag(String errTag) {
errorAppTag = errTag;
}
@Override
public YangConstructType getYangConstructType() {
return YangConstructType.PATTERN_DATA;
}
@Override
public void validateDataOnEntry() throws DataModelException {
//TODO: implement the method.
}
@Override
public void validateDataOnExit() throws DataModelException {
//TODO: implement the method.
}
}
......@@ -15,14 +15,20 @@
*/
package org.onosproject.yangutils.datamodel;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
import org.onosproject.yangutils.linker.impl.YangReferenceResolver;
import org.onosproject.yangutils.linker.impl.YangResolutionInfo;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.linkInterFileReferences;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.resolveLinkingForResolutionList;
/*-
......@@ -79,7 +85,7 @@ public class YangModule extends YangNode
/**
* Reference:RFC 6020.
*
* <p>
* The "contact" statement provides contact information for the module. The
* argument is a string that is used to specify contact information for the
* person or persons to whom technical queries concerning this module should
......@@ -90,7 +96,7 @@ public class YangModule extends YangNode
/**
* Reference:RFC 6020.
*
* <p>
* The "description" statement takes as an argument a string that contains a
* human-readable textual description of this definition. The text is
* provided in a language (or languages) chosen by the module developer; for
......@@ -125,7 +131,7 @@ public class YangModule extends YangNode
/**
* Reference:RFC 6020.
*
* <p>
* The "organization" statement defines the party responsible for this
* module. The argument is a string that is used to specify a textual
* description of the organization(s) under whose auspices this module was
......@@ -408,6 +414,14 @@ public class YangModule extends YangNode
resolveLinkingForResolutionList(resolutionList, this);
}
@Override
public void resolveInterFileLinking() throws DataModelException {
// Get the list to be resolved.
List<YangResolutionInfo> resolutionList = getUnresolvedResolutionList();
// Resolve linking for a resolution list.
linkInterFileReferences(resolutionList, this);
}
/**
* Returns the textual reference.
*
......@@ -525,4 +539,43 @@ public class YangModule extends YangNode
public void setResolutionList(List<YangResolutionInfo> resolutionList) {
unresolvedResolutionList = resolutionList;
}
@Override
public void addReferencesToImportList(Set<YangFileInfo> yangFileInfoSet)
throws LinkerException {
Iterator<YangImport> importInfoIterator = getImportList().iterator();
// Run through the imported list to add references.
while (importInfoIterator.hasNext()) {
YangImport yangImport = importInfoIterator.next();
try {
yangImport.addReferenceToImport(yangFileInfoSet);
} catch (DataModelException e) {
throw new LinkerException(e.getMessage());
}
}
}
@Override
public void addReferencesToIncludeList(Set<YangFileInfo> yangFileInfoSet)
throws LinkerException {
Iterator<YangInclude> includeInfoIterator = getIncludeList().iterator();
// Run through the included list to add references.
while (includeInfoIterator.hasNext()) {
YangInclude yangInclude = includeInfoIterator.next();
YangSubModule subModule = null;
try {
subModule = yangInclude.addReferenceToInclude(yangFileInfoSet);
} catch (DataModelException e) {
throw new LinkerException(e.getMessage());
}
// Check if the referred sub-modules parent is self
if (!(subModule.getBelongsTo().getModuleNode() == this)) {
try {
yangInclude.reportIncludeError();
} catch (DataModelException e) {
throw new LinkerException(e.getMessage());
}
}
}
}
}
......
......@@ -15,14 +15,20 @@
*/
package org.onosproject.yangutils.datamodel;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
import org.onosproject.yangutils.linker.impl.YangReferenceResolver;
import org.onosproject.yangutils.linker.impl.YangResolutionInfo;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.linkInterFileReferences;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.resolveLinkingForResolutionList;
/*
......@@ -92,7 +98,7 @@ public class YangSubModule extends YangNode
/**
* Reference RFC 6020.
*
* <p>
* The "contact" statement provides contact information for the module. The
* argument is a string that is used to specify contact information for the
* person or persons to whom technical queries concerning this module should
......@@ -338,6 +344,14 @@ public class YangSubModule extends YangNode
resolveLinkingForResolutionList(resolutionList, this);
}
@Override
public void resolveInterFileLinking() throws DataModelException {
// Get the list to be resolved.
List<YangResolutionInfo> resolutionList = getUnresolvedResolutionList();
// Resolve linking for a resolution list.
linkInterFileReferences(resolutionList, this);
}
/**
* Returns the list of leaves.
*
......@@ -507,4 +521,54 @@ public class YangSubModule extends YangNode
public void setResolutionList(List<YangResolutionInfo> resolutionList) {
this.unresolvedResolutionList = resolutionList;
}
/**
* Links the sub-module with module.
*
* @param yangFileInfoSet YANG file information set
* @throws DataModelException a violation in data model rule
*/
public void linkWithModule(Set<YangFileInfo> yangFileInfoSet)
throws DataModelException {
getBelongsTo().linkWithModule(yangFileInfoSet);
}
@Override
public void addReferencesToIncludeList(Set<YangFileInfo> yangFileInfoSet)
throws LinkerException {
Iterator<YangInclude> includeInfoIterator = getIncludeList().iterator();
// Run through the included list to add references.
while (includeInfoIterator.hasNext()) {
YangInclude yangInclude = includeInfoIterator.next();
YangSubModule subModule = null;
try {
subModule = yangInclude.addReferenceToInclude(yangFileInfoSet);
} catch (DataModelException e) {
throw new LinkerException(e.getMessage());
}
// Check if the referred sub-modules parent is self
if (!(subModule.getBelongsTo().getModuleNode() == getBelongsTo().getModuleNode())) {
try {
yangInclude.reportIncludeError();
} catch (DataModelException e) {
throw new LinkerException(e.getMessage());
}
}
}
}
@Override
public void addReferencesToImportList(Set<YangFileInfo> yangFileInfoSet)
throws LinkerException {
Iterator<YangImport> importInfoIterator = getImportList().iterator();
// Run through the imported list to add references.
while (importInfoIterator.hasNext()) {
YangImport yangImport = importInfoIterator.next();
try {
yangImport.addReferenceToImport(yangFileInfoSet);
} catch (DataModelException e) {
throw new LinkerException(e.getMessage());
}
}
}
}
......
......@@ -17,6 +17,9 @@
package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
import org.onosproject.yangutils.linker.impl.Resolvable;
import org.onosproject.yangutils.linker.impl.ResolvableStatus;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
......@@ -244,21 +247,25 @@ public class YangType<T>
}
@Override
public void resolve() throws DataModelException {
public void resolve() throws LinkerException {
/*
* Check whether the data type is derived.
*/
if (getDataType() != DERIVED) {
throw new DataModelException("Linker Error: Resolve should only be called for derived data types.");
throw new LinkerException("Linker Error: Resolve should only be called for derived data types.");
}
// Check if the derived info is present.
YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo();
if (derivedInfo == null) {
throw new DataModelException("Linker Error: Derived information is missing.");
throw new LinkerException("Linker Error: Derived information is missing.");
}
// Initiate the resolution
setResolvableStatus(derivedInfo.resolve());
try {
setResolvableStatus(derivedInfo.resolve());
} catch (DataModelException e) {
throw new LinkerException(e.getMessage());
}
}
}
......
......@@ -16,6 +16,9 @@
package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
import org.onosproject.yangutils.linker.impl.Resolvable;
import org.onosproject.yangutils.linker.impl.ResolvableStatus;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
......@@ -259,37 +262,49 @@ public class YangUses
@Override
public void resolve()
throws DataModelException {
throws LinkerException {
YangGrouping referredGrouping = getRefGroup();
if (referredGrouping == null) {
throw new DataModelException("YANG uses linker error, cannot resolve uses");
throw new LinkerException("Linker Exception: YANG uses linker error, cannot resolve uses");
}
YangNode usesParentNode = getParentNodeInGenCode(this);
if ((!(usesParentNode instanceof YangLeavesHolder))
|| (!(usesParentNode instanceof CollisionDetector))) {
throw new DataModelException("YANG uses holder construct is wrong");
throw new LinkerException("Linker Exception: YANG uses holder construct is wrong");
}
YangLeavesHolder usesParentLeavesHolder = (YangLeavesHolder) usesParentNode;
if (referredGrouping.getListOfLeaf() != null) {
for (YangLeaf leaf : referredGrouping.getListOfLeaf()) {
((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leaf.getName(),
YangConstructType.LEAF_DATA);
try {
((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leaf.getName(),
YangConstructType.LEAF_DATA);
} catch (DataModelException e) {
throw new LinkerException(e.getMessage());
}
usesParentLeavesHolder.addLeaf(leaf);
}
}
if (referredGrouping.getListOfLeafList() != null) {
for (YangLeafList leafList : referredGrouping.getListOfLeafList()) {
((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leafList.getName(),
YangConstructType.LEAF_LIST_DATA);
try {
((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leafList.getName(),
YangConstructType.LEAF_LIST_DATA);
} catch (DataModelException e) {
throw new LinkerException(e.getMessage());
}
usesParentLeavesHolder.addLeafList(leafList);
}
}
YangNode.cloneSubTree(getRefGroup(), usesParentNode);
try {
YangNode.cloneSubTree(getRefGroup(), usesParentNode);
} catch (DataModelException e) {
throw new LinkerException(e.getMessage());
}
}
@Override
......
......@@ -16,17 +16,15 @@
package org.onosproject.yangutils.datamodel.utils;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.onosproject.yangutils.datamodel.CollisionDetector;
import org.onosproject.yangutils.datamodel.YangImport;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangLeavesHolder;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangReferenceResolver;
import org.onosproject.yangutils.datamodel.YangResolutionInfo;
import org.onosproject.yangutils.linker.impl.YangReferenceResolver;
import org.onosproject.yangutils.linker.impl.YangResolutionInfo;
import org.onosproject.yangutils.datamodel.YangRpc;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
......@@ -48,14 +46,13 @@ public final class DataModelUtils {
* Detects the colliding identifier name in a given YANG node and its child.
*
* @param identifierName name for which collision detection is to be
* checked
* @param dataType type of YANG node asking for detecting collision
* @param node instance of calling node
* checked
* @param dataType type of YANG node asking for detecting collision
* @param node instance of calling node
* @throws DataModelException a violation of data model rules
*/
public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
throws DataModelException {
if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
detectCollidingForUsesGrouping(identifierName, dataType, node);
} else {
......@@ -81,9 +78,9 @@ public final class DataModelUtils {
* Detects colliding of uses and grouping only with uses and grouping respectively.
*
* @param identifierName name for which collision detection is to be
* checked
* @param dataType type of YANG node asking for detecting collision
* @param node node instance of calling node
* checked
* @param dataType type of YANG node asking for detecting collision
* @param node node instance of calling node
* @throws DataModelException a violation of data model rules
*/
public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node)
......@@ -103,9 +100,9 @@ public final class DataModelUtils {
/**
* Detects the colliding identifier name in a given leaf node.
*
* @param listOfLeaf List of leaves to detect collision
* @param listOfLeaf List of leaves to detect collision
* @param identifierName name for which collision detection is to be
* checked
* checked
* @throws DataModelException a violation of data model rules
*/
private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
......@@ -127,7 +124,7 @@ public final class DataModelUtils {
*
* @param listOfLeafList list of leaf-lists to detect collision
* @param identifierName name for which collision detection is to be
* checked
* checked
* @throws DataModelException a violation of data model rules
*/
private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
......@@ -148,7 +145,7 @@ public final class DataModelUtils {
* Add a resolution information.
*
* @param resolutionInfo information about the YANG construct which has to
* be resolved
* be resolved
* @throws DataModelException a violation of data model rules
*/
public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
......@@ -165,64 +162,41 @@ public final class DataModelUtils {
}
YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
if (!isPrefixValid(resolutionInfo.getEntityToResolveInfo().getEntityPrefix(),
resolutionNode)) {
throw new DataModelException("The prefix used is not valid");
}
resolutionNode.addToResolutionList(resolutionInfo);
}
/**
* Evaluates whether the prefix in uses/type is valid.
* Resolve linking for a resolution list.
*
* @param entityPrefix prefix in the current module/sub-module
* @param resolutionNode uses/type node which has the prefix with it
* @return whether prefix is valid or not
* @param resolutionList resolution list for which linking to be done
* @param dataModelRootNode module/sub-module node
* @throws DataModelException a violation of data model rules
*/
private static boolean isPrefixValid(String entityPrefix, YangReferenceResolver resolutionNode) {
if (entityPrefix == null) {
return true;
}
if (resolutionNode.getPrefix().contentEquals(entityPrefix)) {
return true;
}
if (resolutionNode.getImportList() != null) {
for (YangImport importedInfo : resolutionNode.getImportList()) {
if (importedInfo.getPrefixId().contentEquals(entityPrefix)) {
return true;
}
}
}
if (resolutionNode.getIncludeList() != null) {
/**
* TODO: check if the prefix matches with the imported data
public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
YangReferenceResolver dataModelRootNode)
throws DataModelException {
for (YangInclude includedInfo : resolutionNode.getIncludeList()) {
if (includedInfo.contentEquals(prefix)) {
return true;
}
}*/
for (YangResolutionInfo resolutionInfo : resolutionList) {
resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode);
}
return false;
}
/**
* Resolve linking for a resolution list.
* Links type/uses referring to typedef/uses of inter YANG file.
*
* @param resolutionList resolution list for which linking to be done
* @param resolutionList resolution list for which linking to be done
* @param dataModelRootNode module/sub-module node
* @throws DataModelException a violation of data model rules
*/
public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
YangReferenceResolver dataModelRootNode)
public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList,
YangReferenceResolver dataModelRootNode)
throws DataModelException {
/*
* Run through the resolution list, find type/uses referring to
* inter file typedef/grouping, ask for linking.
*/
for (YangResolutionInfo resolutionInfo : resolutionList) {
resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode.getPrefix());
resolutionInfo.linkInterFile(dataModelRootNode);
}
}
......@@ -244,24 +218,23 @@ public final class DataModelUtils {
}
/**
* Returns module's data model node to which sub-module belongs to.
* Returns referred node in a given set.
*
* @param yangFileInfo YANG file information
* @param belongsToModuleName name of the module to which sub-module belongs to
* @return module node to which sub-module belongs to
* @throws DataModelException when belongs to module node is not found
* @param yangFileInfoSet YANG file info set
* @param refNodeName name of the node which is referred
* @return referred node's reference
*/
public static YangNode findBelongsToModuleNode(List<YangFileInfo> yangFileInfo,
String belongsToModuleName) throws DataModelException {
Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
while (yangFileIterator.hasNext()) {
YangFileInfo yangFile = yangFileIterator.next();
YangNode yangNode = yangFile.getRootNode();
if (yangNode.getName().equals(belongsToModuleName)) {
return yangNode;
public static YangNode findReferredNode(Set<YangFileInfo> yangFileInfoSet, String refNodeName) {
/*
* Run through the YANG files to see which YANG file matches the
* referred node name.
*/
for (YangFileInfo yangFileInfo : yangFileInfoSet) {
YangNode yangNode = yangFileInfo.getRootNode();
if (yangNode.getName().equals(refNodeName)) {
return yangFileInfo.getRootNode();
}
}
throw new DataModelException("YANG file error : Module " + belongsToModuleName + " to which sub-module " +
"belongs to is not found.");
return null;
}
}
......
......@@ -16,8 +16,8 @@
package org.onosproject.yangutils.linker;
import java.util.Map;
import org.onosproject.yangutils.datamodel.YangReferenceResolver;
import java.util.Set;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
/**
* Abstraction of entity which provides linking service of YANG files.
......@@ -28,8 +28,7 @@ public interface YangLinker {
* Resolve the import and include dependencies for a given resolution
* information.
*
* @param fileMapEntry map entry for which resolution is to be done
* @param yangFilesMap map of dependent file and resolution information*/
void resolveDependencies(Map.Entry<String, YangReferenceResolver> fileMapEntry, Map<String,
YangReferenceResolver> yangFilesMap);
* @param yangFileInfoSet set of all dependent YANG files
*/
void resolveDependencies(Set<YangFileInfo> yangFileInfoSet);
}
......
/*
* 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.linker.exceptions;
/**
* Represents base class for exceptions in linker operations.
*/
public class LinkerException extends RuntimeException {
private static final long serialVersionUID = 20160211L;
private int lineNumber;
private int charPositionInLine;
private String fileName;
/**
* Creates a new linker exception.
*/
public LinkerException() {
super();
}
/**
* Creates a new linker exception with given message.
*
* @param message the detail of exception in string
*/
public LinkerException(String message) {
super(message);
}
/**
* Creates a new linker exception from given message and cause.
*
* @param message the detail of exception in string
* @param cause underlying cause of the error
*/
public LinkerException(final String message, final Throwable cause) {
super(message, cause);
}
/**
* Creates a new linker exception from cause.
*
* @param cause underlying cause of the error
*/
public LinkerException(final Throwable cause) {
super(cause);
}
/**
* Returns line number of the exception.
*
* @return line number of the exception
*/
public int getLineNumber() {
return this.lineNumber;
}
/**
* Returns YANG file name of the exception.
*
* @return YANG file name of the exception
*/
public String getFileName() {
return this.fileName;
}
/**
* Returns position of the exception.
*
* @return position of the exception
*/
public int getCharPositionInLine() {
return this.charPositionInLine;
}
/**
* Sets line number of YANG file.
*
* @param line line number of YANG file
*/
public void setLine(int line) {
this.lineNumber = line;
}
/**
* Sets position of exception.
*
* @param charPosition position of exception
*/
public void setCharPosition(int charPosition) {
this.charPositionInLine = charPosition;
}
/**
* Sets file name in parser exception.
*
* @param fileName YANG file name
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
/*
* 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.
*/
/**
* Custom linker exceptions.
*/
package org.onosproject.yangutils.linker.exceptions;
......@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.onosproject.yangutils.datamodel;
package org.onosproject.yangutils.linker.impl;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
/**
* Abstraction of YANG resolvable information. Abstracted to obtain the
......@@ -47,8 +47,8 @@ public interface Resolvable {
/**
* Resolves the linking.
*
* @throws DataModelException data model error
* @throws LinkerException linker error
*/
void resolve()
throws DataModelException;
throws LinkerException;
}
......
......@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.onosproject.yangutils.datamodel;
package org.onosproject.yangutils.linker.impl;
/**
* Represents the status of resolvable entity.
......@@ -40,6 +40,12 @@ public enum ResolvableStatus {
/**
* Identifies that resolvable entity is resolved.
*/
RESOLVED
RESOLVED,
/**
* Identifies that resolvable entity is inter file linked (i.e. complete
* linking with external files).
*/
INTER_FILE_LINKED
}
......
......@@ -13,9 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.datamodel;
package org.onosproject.yangutils.linker.impl;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
/**
* Represents information about entity being resolved.
......@@ -61,7 +64,7 @@ public class YangEntityToResolveInfo<T> {
* Sets parent node which contains the entity to be resolved.
*
* @param holderOfEntityToResolve parent node which contains the entity to
* be resolved
* be resolved
*/
public void setHolderOfEntityToResolve(YangNode holderOfEntityToResolve) {
this.holderOfEntityToResolve = holderOfEntityToResolve;
......@@ -71,10 +74,10 @@ public class YangEntityToResolveInfo<T> {
* Retrieves the prefix of the entity.
*
* @return entities prefix
* @throws DataModelException data model error
* @throws LinkerException linker error
*/
public String getEntityPrefix()
throws DataModelException {
throws LinkerException {
if (getEntityToResolve() == null) {
return null;
}
......@@ -86,7 +89,7 @@ public class YangEntityToResolveInfo<T> {
} else if (entityToBeResolved instanceof YangUses) {
prefix = ((YangUses) entityToBeResolved).getPrefix();
} else {
throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses");
throw new LinkerException("Linker Exception: Entity to resolved is other than type/uses");
}
return prefix;
}
......
/*
* 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.linker.impl;
import java.util.Set;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.linker.YangLinker;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
/**
* Representation of entity which provides linking service of YANG files.
*/
public class YangLinkerManager implements YangLinker {
@Override
public void resolveDependencies(Set<YangFileInfo> yangFileInfoSet) {
// Carry out linking of sub module with module.
linkSubModulesToParentModule(yangFileInfoSet);
// Add references to import list.
addRefToYangFilesImportList(yangFileInfoSet);
// Add reference to include list.
addRefToYangFilesIncludeList(yangFileInfoSet);
// TODO check for circular import/include.
// Carry out inter-file linking.
processInterFileLinking(yangFileInfoSet);
}
/**
* Resolves sub-module linking by linking sub module with parent module.
*
* @param yangFileInfoSet set of YANG files info
* @throws LinkerException fails to link sub-module to parent module
*/
public void linkSubModulesToParentModule(Set<YangFileInfo> yangFileInfoSet) throws LinkerException {
for (YangFileInfo yangFileInfo : yangFileInfoSet) {
YangNode yangNode = yangFileInfo.getRootNode();
if (yangNode instanceof YangSubModule) {
try {
((YangSubModule) yangNode).linkWithModule(yangFileInfoSet);
} catch (DataModelException e) {
String errorInfo = "YANG file error: " + yangFileInfo.getYangFileName() + " at line: "
+ e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
+ e.getMessage();
throw new LinkerException(errorInfo);
}
}
}
}
/**
* Adds imported node information to the import list.
*
* @param yangFileInfoSet set of YANG files info
* @throws LinkerException fails to find imported module
*/
public void addRefToYangFilesImportList(Set<YangFileInfo> yangFileInfoSet) throws LinkerException {
for (YangFileInfo yangFileInfo : yangFileInfoSet) {
YangNode yangNode = yangFileInfo.getRootNode();
if (yangNode instanceof YangReferenceResolver) {
((YangReferenceResolver) yangNode).addReferencesToImportList(yangFileInfoSet);
}
}
}
/**
* Adds included node information to the include list.
*
* @param yangFileInfoSet set of YANG files info
* @throws LinkerException fails to find included sub-module
*/
public void addRefToYangFilesIncludeList(Set<YangFileInfo> yangFileInfoSet) throws LinkerException {
for (YangFileInfo yangFileInfo : yangFileInfoSet) {
YangNode yangNode = yangFileInfo.getRootNode();
if (yangNode instanceof YangReferenceResolver) {
((YangReferenceResolver) yangNode).addReferencesToIncludeList(yangFileInfoSet);
}
}
}
/**
* Processes inter file linking for type and uses.
*
* @param yangFileInfoSet set of YANG files info
* @throws LinkerException a violation in linker execution
*/
public void processInterFileLinking(Set<YangFileInfo> yangFileInfoSet) throws LinkerException {
for (YangFileInfo yangFileInfo : yangFileInfoSet) {
try {
((YangReferenceResolver) yangFileInfo.getRootNode()).resolveInterFileLinking();
} catch (DataModelException e) {
String errorInfo = "Error in file: " + yangFileInfo.getYangFileName() + " at line: "
+ e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE + e.getMessage();
throw new LinkerException(errorInfo);
}
}
}
}
......@@ -14,10 +14,15 @@
* limitations under the License.
*/
package org.onosproject.yangutils.datamodel;
package org.onosproject.yangutils.linker.impl;
import java.util.List;
import java.util.Set;
import org.onosproject.yangutils.datamodel.YangImport;
import org.onosproject.yangutils.datamodel.YangInclude;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
/**
* Abstraction of YANG dependency resolution information. Abstracted to obtain the
......@@ -33,7 +38,7 @@ public interface YangReferenceResolver {
List<YangResolutionInfo> getUnresolvedResolutionList();
/**
* Add to the resolution list.
* Adds to the resolution list.
*
* @param resolutionInfo resolution information
*/
......@@ -54,7 +59,7 @@ public interface YangReferenceResolver {
List<YangImport> getImportList();
/**
* Add to the import list.
* Adds to the import list.
*
* @param yangImport import to be added
*/
......@@ -75,14 +80,14 @@ public interface YangReferenceResolver {
List<YangInclude> getIncludeList();
/**
* Add to the include list.
* Adds to the include list.
*
* @param yangInclude include to be added
*/
void addToIncludeList(YangInclude yangInclude);
/**
* Create include list.
* Creates include list.
*
* @param includeList include list
*/
......@@ -96,16 +101,39 @@ public interface YangReferenceResolver {
String getPrefix();
/**
* Set prefix of resolution list root node.
* Sets prefix of resolution list root node.
*
* @param prefix resolution root node prefix
*/
void setPrefix(String prefix);
/**
* Resolve self file linking.
* Resolves self file linking.
*
* @throws DataModelException a violation in data model rule
*/
void resolveSelfFileLinking() throws DataModelException;
/**
* Resolves inter file linking.
*
* @throws DataModelException a violation in data model rule
*/
void resolveInterFileLinking() throws DataModelException;
/**
* Adds references to include.
*
* @param yangFileInfoSet YANG file info set
* @throws LinkerException a violation of linker rules
*/
void addReferencesToIncludeList(Set<YangFileInfo> yangFileInfoSet) throws LinkerException;
/**
* Adds references to import.
*
* @param yangFileInfoSet YANG file info set
* @throws LinkerException a violation of linker rules
*/
void addReferencesToImportList(Set<YangFileInfo> yangFileInfoSet) throws LinkerException;
}
......
......@@ -15,6 +15,6 @@
*/
/**
* Provide inter file and inter jar linking implementation.
* Provide intra/inter file and inter jar linking implementation.
*/
package org.onosproject.yangutils.linker.impl;
\ No newline at end of file
package org.onosproject.yangutils.linker.impl;
......
......@@ -74,20 +74,26 @@ public final class BelongsToListener {
* (belongsto), perform validations and update the data model tree.
*
* @param listener Listener's object
* @param ctx context object of the grammar rule
* @param ctx context object of the grammar rule
*/
public static void processBelongsToEntry(TreeWalkListener listener,
GeneratedYangParser.BelongstoStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.identifier().getText(),
ENTRY);
ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), BELONGS_TO_DATA, ctx);
YangBelongsTo belongstoNode = new YangBelongsTo();
belongstoNode.setBelongsToModuleName(identifier);
// Set the line number and character position in line for the belongs to.
int errorLine = ctx.getStart().getLine();
int errorPosition = ctx.getStart().getCharPositionInLine();
belongstoNode.setLineNumber(errorLine);
belongstoNode.setCharPosition(errorPosition);
// Push belongsto into the stack.
listener.getParsedDataStack().push(belongstoNode);
}
......@@ -97,14 +103,14 @@ public final class BelongsToListener {
* validations and update the data model tree.
*
* @param listener Listener's object
* @param ctx context object of the grammar rule
* @param ctx context object of the grammar rule
*/
public static void processBelongsToExit(TreeWalkListener listener,
GeneratedYangParser.BelongstoStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.identifier().getText(),
EXIT);
EXIT);
Parsable tmpBelongstoNode = listener.getParsedDataStack().peek();
if (tmpBelongstoNode instanceof YangBelongsTo) {
......@@ -112,7 +118,7 @@ public final class BelongsToListener {
// Check for stack to be empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA,
ctx.identifier().getText(), EXIT);
ctx.identifier().getText(), EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getYangConstructType()) {
......@@ -129,7 +135,7 @@ public final class BelongsToListener {
}
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BELONGS_TO_DATA,
ctx.identifier().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
}
......
......@@ -73,7 +73,7 @@ public final class ImportListener {
* (import), perform validations and update the data model tree.
*
* @param listener Listener's object
* @param ctx context object of the grammar rule
* @param ctx context object of the grammar rule
*/
public static void processImportEntry(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
......@@ -85,6 +85,12 @@ public final class ImportListener {
YangImport importNode = new YangImport();
importNode.setModuleName(identifier);
// Set the line number and character position in line for the belongs to.
int errorLine = ctx.getStart().getLine();
int errorPosition = ctx.getStart().getCharPositionInLine();
importNode.setLineNumber(errorLine);
importNode.setCharPosition(errorPosition);
// Push import node to the stack.
listener.getParsedDataStack().push(importNode);
}
......@@ -94,7 +100,7 @@ public final class ImportListener {
* validations and update the data model tree.
*
* @param listener Listener's object
* @param ctx context object of the grammar rule
* @param ctx context object of the grammar rule
*/
public static void processImportExit(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
......@@ -107,7 +113,7 @@ public final class ImportListener {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.identifier().getText(),
EXIT);
EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getYangConstructType()) {
......@@ -128,7 +134,7 @@ public final class ImportListener {
}
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, IMPORT_DATA,
ctx.identifier().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
}
......
......@@ -72,19 +72,25 @@ public final class IncludeListener {
* (include), perform validations and update the data model tree.
*
* @param listener Listener's object
* @param ctx context object of the grammar rule
* @param ctx context object of the grammar rule
*/
public static void processIncludeEntry(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.identifier().getText(),
ENTRY);
ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), INCLUDE_DATA, ctx);
YangInclude includeNode = new YangInclude();
includeNode.setSubModuleName(identifier);
// Set the line number and character position in line for the belongs to.
int errorLine = ctx.getStart().getLine();
int errorPosition = ctx.getStart().getCharPositionInLine();
includeNode.setLineNumber(errorLine);
includeNode.setCharPosition(errorPosition);
listener.getParsedDataStack().push(includeNode);
}
......@@ -93,7 +99,7 @@ public final class IncludeListener {
* validations and update the data model tree.
*
* @param listener Listener's object
* @param ctx context object of the grammar rule
* @param ctx context object of the grammar rule
*/
public static void processIncludeExit(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) {
......@@ -106,7 +112,7 @@ public final class IncludeListener {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.identifier().getText(),
EXIT);
EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getYangConstructType()) {
......@@ -127,7 +133,7 @@ public final class IncludeListener {
}
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, INCLUDE_DATA,
ctx.identifier().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
}
......
......@@ -16,7 +16,6 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangRangeRestriction;
import org.onosproject.yangutils.datamodel.YangStringRestriction;
......@@ -27,7 +26,9 @@ import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.YangDataTypes.BINARY;
import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
......@@ -99,8 +100,8 @@ public final class LengthRestrictionListener {
* Sets the length restriction to type.
*
* @param listener listener's object
* @param type Yang type for which length restriction to be set
* @param ctx context object of the grammar rule
* @param type Yang type for which length restriction to be set
* @param ctx context object of the grammar rule
*/
private static void setLengthRestriction(TreeWalkListener listener, YangType type,
GeneratedYangParser.LengthStatementContext ctx) {
......@@ -115,10 +116,10 @@ public final class LengthRestrictionListener {
return;
}
if (type.getDataType() != YangDataTypes.STRING) {
if (type.getDataType() != STRING && type.getDataType() != BINARY) {
ParserException parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(LENGTH_DATA) + " name " + ctx.length().getText() +
" can be used to restrict the built-in type string or types derived from string.");
" can be used to restrict the built-in type string/binary or types derived from string/binary.");
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
......@@ -127,14 +128,18 @@ public final class LengthRestrictionListener {
YangRangeRestriction lengthRestriction = processLengthRestriction(null, ctx.getStart().getLine(),
ctx.getStart().getCharPositionInLine(), false, ctx.length().getText());
YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
if (type.getDataType() == STRING) {
YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
if (stringRestriction == null) {
stringRestriction = new YangStringRestriction();
type.setDataTypeExtendedInfo(stringRestriction);
}
if (stringRestriction == null) {
stringRestriction = new YangStringRestriction();
type.setDataTypeExtendedInfo(stringRestriction);
stringRestriction.setLengthRestriction(lengthRestriction);
} else {
type.setDataTypeExtendedInfo(lengthRestriction);
}
stringRestriction.setLengthRestriction(lengthRestriction);
listener.getParsedDataStack().push(lengthRestriction);
}
......@@ -143,7 +148,7 @@ public final class LengthRestrictionListener {
* It is called when parser exits from grammar rule (length).
*
* @param listener listener's object
* @param ctx context object of the grammar rule
* @param ctx context object of the grammar rule
*/
public static void processLengthRestrictionExit(TreeWalkListener listener,
GeneratedYangParser.LengthStatementContext ctx) {
......
......@@ -16,7 +16,7 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangReferenceResolver;
import org.onosproject.yangutils.linker.impl.YangReferenceResolver;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangRevision;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
......
......@@ -16,7 +16,7 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangReferenceResolver;
import org.onosproject.yangutils.linker.impl.YangReferenceResolver;
import org.onosproject.yangutils.datamodel.YangRevision;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
......
......@@ -22,26 +22,24 @@ import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
import org.onosproject.yangutils.datamodel.YangResolutionInfo;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUnion;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.linker.impl.YangResolutionInfo;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.UNRESOLVED;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangType;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.UNRESOLVED;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
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.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;
......@@ -81,10 +79,10 @@ public final class TypeListener {
* (type), performs validation and updates the data model tree.
*
* @param listener listener's object
* @param ctx context object of the grammar rule
* @param ctx context object of the grammar rule
*/
public static void processTypeEntry(TreeWalkListener listener,
GeneratedYangParser.TypeStatementContext ctx) {
GeneratedYangParser.TypeStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY);
......@@ -190,6 +188,8 @@ public final class TypeListener {
YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
type.setResolvableStatus(UNRESOLVED);
// Add resolution information to the list
YangResolutionInfo resolutionInfo =
new YangResolutionInfo<YangType>(type, unionNode, errorLine, errorPosition);
......@@ -211,13 +211,15 @@ public final class TypeListener {
YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
type.setResolvableStatus(UNRESOLVED);
// Add resolution information to the list
YangResolutionInfo resolutionInfo =
new YangResolutionInfo<YangType>(type, typeDef, errorLine, errorPosition);
addToResolutionList(resolutionInfo, ctx);
}
break;
//TODO: deviate replacement statement.case TYPEDEF_DATA: //TODO
//TODO: deviate replacement statement.
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
......@@ -233,10 +235,10 @@ public final class TypeListener {
* validations and update the data model tree.
*
* @param listener Listener's object
* @param ctx context object of the grammar rule
* @param ctx context object of the grammar rule
*/
public static void processTypeExit(TreeWalkListener listener,
GeneratedYangParser.TypeStatementContext ctx) {
GeneratedYangParser.TypeStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
......@@ -252,15 +254,15 @@ public final class TypeListener {
* Adds to resolution list.
*
* @param resolutionInfo resolution information
* @param ctx context object of the grammar rule
* @param ctx context object of the grammar rule
*/
private static void addToResolutionList(YangResolutionInfo<YangType> resolutionInfo,
GeneratedYangParser.TypeStatementContext ctx) {
GeneratedYangParser.TypeStatementContext ctx) {
try {
addResolutionInfo(resolutionInfo);
} catch (DataModelException e) {
throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
TYPE_DATA, ctx.string().getText(), EXIT, e.getMessage()));
TYPE_DATA, ctx.string().getText(), ENTRY, e.getMessage()));
}
}
}
......
......@@ -26,7 +26,7 @@ import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
import org.onosproject.yangutils.datamodel.YangNotification;
import org.onosproject.yangutils.datamodel.YangOutput;
import org.onosproject.yangutils.datamodel.YangResolutionInfo;
import org.onosproject.yangutils.linker.impl.YangResolutionInfo;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
......
......@@ -16,6 +16,8 @@
package org.onosproject.yangutils.plugin.manager;
import java.util.Objects;
import org.onosproject.yangutils.linker.impl.ResolvableStatus;
import org.onosproject.yangutils.datamodel.YangNode;
/**
......@@ -29,11 +31,21 @@ public class YangFileInfo {
private String yangFileName;
/**
* YANG file revision.
*/
private String revision;
/**
* Data model node after parsing YANG file.
*/
private YangNode rootNode;
/**
* Resolution status of YANG file.
*/
private ResolvableStatus resolvableStatus;
/**
* Returns data model node for YANG file.
*
* @return data model node for YANG file
......@@ -68,4 +80,58 @@ public class YangFileInfo {
public void setYangFileName(String yangFileName) {
this.yangFileName = yangFileName;
}
/**
* Returns the revision of YANG file.
*
* @return revision of YANG file
*/
public String getRevision() {
return revision;
}
/**
* Sets the revision of YANG file.
*
* @param revision revision of YANG file
*/
public void setRevision(String revision) {
this.revision = revision;
}
/**
* Returns the resolution status of YANG file.
*
* @return resolution status of YANG file
*/
public ResolvableStatus getResolvableStatus() {
return resolvableStatus;
}
/**
* Sets the resolution status of YANG file.
*
* @param resolvableStatus resolution status of YANG file
*/
public void setResolvableStatus(ResolvableStatus resolvableStatus) {
this.resolvableStatus = resolvableStatus;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof YangFileInfo) {
final YangFileInfo other = (YangFileInfo) obj;
return Objects.equals(this.yangFileName, other.yangFileName);
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(this.yangFileName);
}
}
......
......@@ -16,7 +16,6 @@
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangBelongsTo;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangSubModule;
......@@ -105,10 +104,10 @@ public class YangJavaSubModule
* Returns the name space of the module to which the sub module belongs to.
*
* @param belongsToInfo Information of the module to which the sub module
* belongs
* belongs
* @return the name space string of the module.
*/
private String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
public String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
return ((YangModule) belongsToInfo.getModuleNode()).getNameSpace().getUri();
}
......
......@@ -24,14 +24,17 @@ import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUnion;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUnion;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_WRAPPER;
......@@ -109,7 +112,7 @@ public final class AttributesJavaDataType {
* Returns from string method parsed string.
*
* @param targetDataType target data type
* @param yangType YANG type
* @param yangType YANG type
* @return parsed string
*/
public static String getParseFromStringMethod(String targetDataType, YangType<?> yangType) {
......@@ -155,7 +158,7 @@ public final class AttributesJavaDataType {
/**
* Returns java import class.
*
* @param yangType YANG type
* @param yangType YANG type
* @param isListAttr if the attribute need to be a list
* @return java import class
*/
......@@ -260,9 +263,9 @@ public final class AttributesJavaDataType {
/**
* Returns java import package.
*
* @param yangType YANG type
* @param yangType YANG type
* @param isListAttr if the attribute is of list type
* @param classInfo java import class info
* @param classInfo java import class info
* @return java import package
*/
public static String getJavaImportPackage(YangType<?> yangType, boolean isListAttr, String classInfo) {
......@@ -424,6 +427,20 @@ public final class AttributesJavaDataType {
throw new TranslatorException("invalid child node is being processed.");
}
JavaFileInfo parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
if (parentInfo.getPackage() == null) {
if (parent instanceof YangJavaModule) {
YangJavaModule module = (YangJavaModule) parent;
String modulePkg = getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module
.getRevision().getRevDate());
return modulePkg + PERIOD + getCamelCase(module.getName(), null).toLowerCase();
} else if (parent instanceof YangJavaSubModule) {
YangJavaSubModule submodule = (YangJavaSubModule) parent;
String subModulePkg = getRootPackage(submodule.getVersion(),
submodule.getNameSpaceFromModule(submodule.getBelongsTo()),
submodule.getRevision().getRevDate());
return subModulePkg + PERIOD + getCamelCase(submodule.getName(), null).toLowerCase();
}
}
return parentInfo.getPackage() + PERIOD + parentInfo.getJavaName().toLowerCase();
}
}
......
......@@ -21,7 +21,6 @@ import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
/**
* Represents utility for searching the files in a directory.
......@@ -43,8 +42,8 @@ public final class YangFileScanner {
* @param root specified directory
* @return list of java files
* @throws NullPointerException when no files are there.
* @throws IOException when files get deleted while performing the
* operations
* @throws IOException when files get deleted while performing the
* operations
*/
public static List<String> getJavaFiles(String root) throws IOException {
......@@ -52,33 +51,27 @@ public final class YangFileScanner {
}
/**
* Returns the list of YANG file information.
* Returns the list of YANG file.
*
* @param root specified directory
* @return list of YANG file information
* @throws NullPointerException when no files are there
* @throws IOException when files get deleted while performing the
* operations
* @throws IOException when files get deleted while performing the
* operations
*/
public static List<YangFileInfo> getYangFiles(String root) throws IOException {
public static List<String> getYangFiles(String root) throws IOException {
List<String> yangFiles = getFiles(root, YANG_FILE_EXTENTION);
List<YangFileInfo> fileInfo = new LinkedList<>();
for (String yangFile : yangFiles) {
YangFileInfo yangFileInfo = new YangFileInfo();
yangFileInfo.setYangFileName(yangFile);
fileInfo.add(yangFileInfo);
}
return fileInfo;
return yangFiles;
}
/**
* Returns the list of required files.
*
* @param root specified directory
* @param root specified directory
* @param extension file extension
* @return list of required files
* @throws NullPointerException when no file is there
* @throws IOException when files get deleted while performing the operations
* @throws IOException when files get deleted while performing the operations
*/
public static List<String> getFiles(String root, String extension) throws IOException {
......
......@@ -23,18 +23,20 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.Stack;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
import org.slf4j.Logger;
import org.sonatype.plexus.build.incremental.BuildContext;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
......@@ -81,9 +83,9 @@ public final class YangIoUtils {
/**
* Adds package info file for the created directory.
*
* @param path directory path
* @param classInfo class info for the package
* @param pack package of the directory
* @param path directory path
* @param classInfo class info for the package
* @param pack package of the directory
* @param isChildNode is it a child node
* @throws IOException when fails to create package info file
*/
......@@ -167,7 +169,7 @@ public final class YangIoUtils {
/**
* Adds generated source directory to the compilation root.
*
* @param source directory
* @param source directory
* @param project current maven project
* @param context current build context
*/
......@@ -180,7 +182,7 @@ public final class YangIoUtils {
/**
* Removes extra char from the string.
*
* @param valueString string to be trimmed
* @param valueString string to be trimmed
* @param removealStirng extra chars
* @return new string
*/
......@@ -215,8 +217,8 @@ public final class YangIoUtils {
* Returns the directory path of the package in canonical form.
*
* @param baseCodeGenPath base path where the generated files needs to be
* put
* @param pathOfJavaPkg java package of the file being generated
* put
* @param pathOfJavaPkg java package of the file being generated
* @return absolute path of the package in canonical form
*/
public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
......@@ -236,8 +238,8 @@ public final class YangIoUtils {
* Returns the absolute path of the package in canonical form.
*
* @param baseCodeGenPath base path where the generated files needs to be
* put
* @param pathOfJavaPkg java package of the file being generated
* put
* @param pathOfJavaPkg java package of the file being generated
* @return absolute path of the package in canonical form
*/
public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
......@@ -247,12 +249,12 @@ public final class YangIoUtils {
/**
* Copies YANG files to the current project's output directory.
*
* @param yangFileInfo list of YANG files
* @param outputDir project's output directory
* @param project maven project
* @param yangFileInfo set of YANG files
* @param outputDir project's output directory
* @param project maven project
* @throws IOException when fails to copy files to destination resource directory
*/
public static void copyYangFilesToTarget(List<YangFileInfo> yangFileInfo, String outputDir, MavenProject project)
public static void copyYangFilesToTarget(Set<YangFileInfo> yangFileInfo, String outputDir, MavenProject project)
throws IOException {
List<File> files = getListOfFile(yangFileInfo);
......@@ -274,10 +276,10 @@ public final class YangIoUtils {
/**
* Provides a list of files from list of strings.
*
* @param yangFileInfo list of yang file information
* @param yangFileInfo set of yang file information
* @return list of files
*/
private static List<File> getListOfFile(List<YangFileInfo> yangFileInfo) {
private static List<File> getListOfFile(Set<YangFileInfo> yangFileInfo) {
List<File> files = new ArrayList<>();
Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
while (yangFileIterator.hasNext()) {
......@@ -291,7 +293,7 @@ public final class YangIoUtils {
* Merges the temp java files to main java files.
*
* @param appendFile temp file
* @param srcFile main file
* @param srcFile main file
* @throws IOException when fails to append contents
*/
public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException {
......
......@@ -32,12 +32,13 @@ import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.INTRA_FILE_RESOLVED;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.RESOLVED;
import static org.onosproject.yangutils.datamodel.YangDataTypes.BINARY;
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.YangNodeType.MODULE_NODE;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.INTRA_FILE_RESOLVED;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.RESOLVED;
/**
* Test cases for testing "type" intra file linking.
......@@ -516,4 +517,48 @@ public class IntraFileTypeLinkingTest {
YangNode node =
manager.getDataModel("src/test/resources/SelfFileLinkingWithHierarchicalTypeFailureScenario.yang");
}
/**
* Checks self resolution when typedef and leaf using type are siblings for binary type.
*/
@Test
public void processSelfResolutionWhenTypeAndTypedefAtRootLevelForBinary()
throws IOException, ParserException {
YangNode node
= manager.getDataModel("src/test/resources/SelfResolutionWhenTypeAndTypedefAtRootLevelForBinary.yang");
// Check whether the data model tree returned is of type module.
assertThat(node instanceof YangModule, is(true));
// Check whether the node type is set properly to module.
assertThat(node.getNodeType(), is(MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("ospf"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("typedef14"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("type14"));
assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) node.getChild()));
assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
// Check for the effective built-in type.
assertThat(derivedInfo.getEffectiveBuiltInType(), is(BINARY));
// Check for the restriction.
assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
}
}
......
......@@ -21,7 +21,6 @@ import java.util.ListIterator;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.datamodel.ResolvableStatus;
import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangGrouping;
......@@ -32,6 +31,8 @@ import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNodeType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
import org.onosproject.yangutils.linker.impl.ResolvableStatus;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
......@@ -293,9 +294,9 @@ public class IntraFileUsesLinkingTest {
*/
@Test
public void processSelfResolutionGroupingReferencingItselfFailureScenerio()
throws IOException, ParserException {
throws IOException {
thrown.expect(ParserException.class);
thrown.expect(LinkerException.class);
thrown.expectMessage(
"YANG file error: Duplicate input identifier detected, same as leaf \"zip-code\"");
YangNode node = manager
......
......@@ -30,6 +30,7 @@ import org.onosproject.yangutils.datamodel.YangRangeRestriction;
import org.onosproject.yangutils.datamodel.YangStringRestriction;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import org.onosproject.yangutils.utils.builtindatatype.YangInt32;
......@@ -39,11 +40,11 @@ import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.RESOLVED;
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.YangNodeType.MODULE_NODE;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.RESOLVED;
/**
* Test cases for testing restriction resolution.
......@@ -225,9 +226,9 @@ public final class RestrictionResolutionTest {
/**
* Checks length restriction in typedef and in type with not stricter value.
*/
@Test(expected = ParserException.class)
@Test(expected = LinkerException.class)
public void processLengthRestrictionInTypedefAndTypeInValid()
throws IOException, ParserException, DataModelException {
throws IOException, DataModelException {
YangNode node = manager.getDataModel("src/test/resources/LengthRestrictionInTypedefAndTypeInValid.yang");
}
......@@ -429,9 +430,9 @@ public final class RestrictionResolutionTest {
/**
* Checks range restriction for string in referred type.
*/
@Test(expected = ParserException.class)
@Test(expected = LinkerException.class)
public void processRangeRestrictionInStringInRefType()
throws IOException, ParserException, DataModelException {
throws IOException, DataModelException {
YangNode node = manager.getDataModel("src/test/resources/RangeRestrictionInStringInRefType.yang");
}
......@@ -826,9 +827,9 @@ public final class RestrictionResolutionTest {
* Checks multiple pattern and length restriction in referred type and
* typedef invalid scenario.
*/
@Test(expected = ParserException.class)
@Test(expected = LinkerException.class)
public void processMultiplePatternAndLengthRestrictionInValid()
throws IOException, ParserException, DataModelException {
throws IOException, DataModelException {
YangNode node = manager.getDataModel("src/test/resources/MultiplePatternAndLengthRestrictionInValid.yang");
}
}
......
......@@ -144,7 +144,7 @@ public class LengthRestrictionListenerTest {
public void processLengthWithInvalidType() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : length name \"1..100\" can be used to restrict the built-in type" +
" string or types derived from string.");
" string/binary or types derived from string/binary.");
YangNode node = manager.getDataModel("src/test/resources/LengthWithInvalidType.yang");
}
......
......@@ -26,7 +26,6 @@ import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
......@@ -49,11 +48,11 @@ public final class YangFileScannerTest {
/**
* A private constructor is tested.
*
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws InvocationTargetException when an exception occurs by the method or constructor
*/
@Test
......@@ -101,7 +100,7 @@ public final class YangFileScannerTest {
/**
* Method used for creating file inside the specified directory.
*
* @param myDir the path where file has to be created inside
* @param myDir the path where file has to be created inside
* @param fileName the name of the file to be created
*/
private void createFile(File myDir, String fileName) throws IOException {
......@@ -136,7 +135,7 @@ public final class YangFileScannerTest {
String emptyYangDir = baseDir + separator + "scanner1";
File path = createDirectory(emptyYangDir);
List<YangFileInfo> emptyDirContents = getYangFiles(path.toString());
List<String> emptyDirContents = getYangFiles(path.toString());
List<String> expectedContents = new LinkedList<>();
assertThat(true, is(emptyDirContents.equals(expectedContents)));
}
......
......@@ -6,13 +6,13 @@ module Test {
list valid {
key "invalid-interval";
leaf invalid-interval {
type hello;
type Ant:hello;
}
}
}
container isis {
typedef hello {
type String;
type string;
}
}
}
......
......@@ -3,7 +3,7 @@ module Test {
namespace http://huawei.com;
prefix Ant;
typedef Percentage {
type INT;
type Ant:INT;
}
container ospf {
list valid {
......
......@@ -2,9 +2,9 @@ module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
container test{
leaf leaf2{
type String;
container test {
leaf leaf2 {
type string;
}
grouping treat {
grouping create {
......@@ -13,5 +13,5 @@ module Test {
}
}
}
uses treat;
uses Ant:treat;
}
......
module ospf {
namespace "urn:cisco:params:xml:ns:yang:ospf";
// replace with IANA namespace when assigned
prefix ospf;
revision 2020-10-20 {
description
"Initial revision.";
}
typedef type14 {
type binary;
}
leaf typedef14 {
type type14;
}
}
......@@ -3,9 +3,9 @@ module Test {
namespace http://huawei.com;
prefix Ant;
leaf invalid-interval {
type hello;
type Ant:hello;
}
typedef hi {
type String;
type string;
}
}
......
module ietf-inet-types {
yang-version 1;
namespace
"urn:ietf:params:xml:ns:yang:ietf-inet-types";
prefix inet;
typedef uri {
type string;
}
}
module ietf-network-topology {
yang-version 1;
namespace "urn:ietf:params:xml:ns:yang:ietf-network-topology";
prefix nt;
import ietf-inet-types {
prefix inet;
}
import ietf-network {
prefix nd;
}
leaf source-node {
type nd:node-id;
description
"Source node identifier, must be in same topology.";
}
}
module ietf-network {
yang-version 1;
namespace "urn:ietf:params:xml:ns:yang:ietf-network";
prefix nd;
import ietf-inet-types {
prefix inet;
}
typedef node-id {
type inet:uri;
description
"Identifier for a node.";
}
}
module ietf-inet-types {
yang-version 1;
namespace
"urn:ietf:params:xml:ns:yang:ietf-inet-types";
prefix inet;
typedef uri {
type string;
}
}
module ietf-network {
yang-version 1;
namespace "urn:ietf:params:xml:ns:yang:ietf-network";
prefix nd;
import ietf-inet-types {
prefix inet;
}
leaf node-ref {
type node-id;
description
"Used to reference a node.
Nodes are identified relative to the network they are
contained in.";
}
typedef node-id {
type inet:uri;
description
"Identifier for a node.";
}
}
module module1 {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
import module2 {
prefix p;
}
leaf invalid-interval {
type p:hello;
}
typedef hello {
type string;
}
}
module module2 {
yang-version 1;
namespace http://huawei.com;
prefix Ant2;
typedef hello {
type string;
}
}
module module1 {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
include module2;
leaf invalid-interval {
type hello;
}
}
submodule module2 {
yang-version 1;
belongs-to module1 {
prefix "module1";
}
typedef hello {
type string;
}
}
module module1 {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
import module2 {
prefix p;
revision-date 2007-06-09;
}
leaf invalid-interval {
type p:hello;
}
typedef hello {
type string;
}
}
module module2 {
yang-version 1;
namespace http://huawei.com;
prefix Ant2;
revision 2007-06-09 {
description "Initial revision.";
}
typedef hello {
type string;
}
}
module module1 {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
import module2 {
prefix p;
revision-date 2007-06-09;
}
leaf invalid-interval {
type p:hello;
}
typedef hello {
type string;
}
}
module module2 {
yang-version 1;
namespace http://huawei.com;
prefix Ant2;
revision 2007-06-09 {
description "Initial revision.";
}
typedef hello {
type string;
}
}
module module1 {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
import module2 {
prefix p;
}
uses p:hello;
}
module module2 {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
grouping hello {
leaf hello {
type string;
}
}
}
module module1 {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
include module2;
uses hello;
}
submodule module2 {
yang-version 1;
belongs-to module1 {
prefix "module1";
}
grouping hello {
leaf hello {
type string;
}
}
}