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 2555 additions and 322 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
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()) {
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()) {
try {
((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leafList.getName(),
YangConstructType.LEAF_LIST_DATA);
} catch (DataModelException e) {
throw new LinkerException(e.getMessage());
}
usesParentLeavesHolder.addLeafList(leafList);
}
}
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;
......@@ -55,7 +53,6 @@ public final class DataModelUtils {
*/
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 {
......@@ -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 dataModelRootNode module/sub-module node
* @throws DataModelException a violation of data model rules
*/
public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
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 findReferredNode(Set<YangFileInfo> yangFileInfoSet, String refNodeName) {
/*
* Run through the YANG files to see which YANG file matches the
* referred node name.
*/
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;
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.
......@@ -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;
}
......
......@@ -14,15 +14,26 @@
* limitations under the License.
*/
package org.onosproject.yangutils.datamodel;
package org.onosproject.yangutils.linker.impl;
import java.util.Stack;
import org.onosproject.yangutils.datamodel.LocationInfo;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangGrouping;
import org.onosproject.yangutils.datamodel.YangImport;
import org.onosproject.yangutils.datamodel.YangInclude;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.INTRA_FILE_RESOLVED;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.LINKED;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.RESOLVED;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.UNRESOLVED;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.INTER_FILE_LINKED;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.INTRA_FILE_RESOLVED;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.LINKED;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.RESOLVED;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.UNRESOLVED;
/**
* Represents resolution object which will be resolved by linker.
......@@ -36,21 +47,28 @@ public class YangResolutionInfo<T> implements LocationInfo {
*/
private YangEntityToResolveInfo<T> entityToResolveInfo;
// Error Line number.
/**
* Error line number.
*/
private int lineNumber;
// Error character position.
/**
* Error character position in number.
*/
private int charPosition;
/*
/**
* Current module/sub-module reference, will be used in inter-file/
* inter-jar scenario to get the import/include list.
*/
private YangReferenceResolver curReferenceResolver;
/**
* Stack for type/uses is maintained for hierarchical references, this is
* used during resolution.
*/
private Stack<YangEntityToResolveInfo<T>> partialResolvedStack;
// Module/Sub-module prefix.
private String resolutionInfoRootNodePrefix;
/**
* It is private to ensure the overloaded method be invoked to create an
* object.
......@@ -69,25 +87,25 @@ public class YangResolutionInfo<T> implements LocationInfo {
* @param charPositionInLine error character position in line
*/
public YangResolutionInfo(T dataNode, YangNode holderNode, int lineNumber, int charPositionInLine) {
setEntityToResolveInfo(new YangEntityToResolveInfo<T>());
setEntityToResolveInfo(new YangEntityToResolveInfo<>());
getEntityToResolveInfo().setEntityToResolve(dataNode);
getEntityToResolveInfo().setHolderOfEntityToResolve(holderNode);
this.setLineNumber(lineNumber);
this.setCharPosition(charPositionInLine);
setPartialResolvedStack(new Stack<YangEntityToResolveInfo<T>>());
setPartialResolvedStack(new Stack<>());
}
/**
* Resolve linking with all the ancestors node for a resolution info.
* Resolves linking with all the ancestors node for a resolution info.
*
* @param resolutionInfoNodePrefix module/sub-module prefix
* @param dataModelRootNode module/sub-module node
* @throws DataModelException DataModelException a violation of data model
* rules
*/
public void resolveLinkingForResolutionInfo(String resolutionInfoNodePrefix)
public void resolveLinkingForResolutionInfo(YangReferenceResolver dataModelRootNode)
throws DataModelException {
this.resolutionInfoRootNodePrefix = resolutionInfoNodePrefix;
setCurReferenceResolver(dataModelRootNode);
// Current node to resolve, it can be a YANG type or YANG uses.
T entityToResolve = getEntityToResolveInfo().getEntityToResolve();
......@@ -142,7 +160,7 @@ public class YangResolutionInfo<T> implements LocationInfo {
/*
* If the top of the stack is already linked then
* resolve the references and pop the entity and
* continue with remaining stack elements to resolve
* continue with remaining stack elements to resolve.
*/
resolveTopOfStack();
getPartialResolvedStack().pop();
......@@ -151,8 +169,7 @@ public class YangResolutionInfo<T> implements LocationInfo {
case INTRA_FILE_RESOLVED: {
/*
* TODO: this needs to be deleted, when inter-file
* resolution is implmeneted
* Pop the top of the stack.
*/
getPartialResolvedStack().pop();
break;
......@@ -187,7 +204,7 @@ public class YangResolutionInfo<T> implements LocationInfo {
}
/**
* Resolve the current entity in the stack.
* Resolves the current entity in the stack.
*/
private void resolveTopOfStack()
throws DataModelException {
......@@ -207,12 +224,12 @@ public class YangResolutionInfo<T> implements LocationInfo {
private void linkTopOfStackReferenceUpdateStack()
throws DataModelException {
if (!isSelfFileReference()) {
/*
* TODO: use mojo utilities to load the referred module/sub-module
* and get the reference to the corresponding referred entity
* Check if self file reference is there, this will not check for the
* scenario when prefix is not present and type/uses is present in
* sub-module from include list.
*/
if (!isCandidateForSelfFileReference()) {
((Resolvable) getCurrentEntityToResolveFromStack()).setResolvableStatus(INTRA_FILE_RESOLVED);
return;
}
......@@ -240,34 +257,29 @@ public class YangResolutionInfo<T> implements LocationInfo {
potentialAncestorWithReferredNode = potentialAncestorWithReferredNode.getParent();
}
/*
* In case prefix is not present it's a candidate for inter-file
* resolution via include list.
*/
if (getRefPrefix() == null) {
((Resolvable) getCurrentEntityToResolveFromStack()).setResolvableStatus(INTRA_FILE_RESOLVED);
}
}
/**
* Check if the reference in self file or in external file.
* Checks if the reference in self file or in external file.
*
* @return true if self file reference, false otherwise
* @throws DataModelException a violation of data model rules
*/
private boolean isSelfFileReference()
throws DataModelException {
String prefix;
if (getCurrentEntityToResolveFromStack() instanceof YangType) {
prefix = ((YangType<?>) getCurrentEntityToResolveFromStack()).getPrefix();
} else if (getCurrentEntityToResolveFromStack() instanceof YangUses) {
prefix = ((YangUses) getCurrentEntityToResolveFromStack()).getPrefix();
} else {
throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses");
}
if (prefix == null) {
return true;
}
return prefix.contentEquals(resolutionInfoRootNodePrefix);
private boolean isCandidateForSelfFileReference() throws DataModelException {
String prefix = getRefPrefix();
return prefix == null || prefix.contentEquals(getCurReferenceResolver().getPrefix());
}
/**
* Check for the referred node defined in a ancestor scope.
* Checks for the referred node defined in a ancestor scope.
*
* @param potentialReferredNode potential referred node
* @return status of resolution and updating the partial resolved stack with
......@@ -282,7 +294,7 @@ public class YangResolutionInfo<T> implements LocationInfo {
if (isReferredNode(potentialReferredNode)) {
// Adds reference link of entity to the node under resolution.
addReferredEntityLink(potentialReferredNode);
addReferredEntityLink(potentialReferredNode, LINKED);
/**
* resolve the reference and update the partial resolution stack
......@@ -303,7 +315,7 @@ public class YangResolutionInfo<T> implements LocationInfo {
}
/**
* Check if the potential referred node is the actual referred node.
* Checks if the potential referred node is the actual referred node.
*
* @param potentialReferredNode typedef/grouping node
* @return true if node is of resolve type otherwise false
......@@ -334,7 +346,7 @@ public class YangResolutionInfo<T> implements LocationInfo {
}
/**
* Check if node name is same as name in resolution info, i.e. name of
* Checks if node name is same as name in resolution info, i.e. name of
* typedef/grouping is same as name of type/uses.
*
* @param node typedef/grouping node
......@@ -366,9 +378,10 @@ public class YangResolutionInfo<T> implements LocationInfo {
* Adds reference of grouping/typedef in uses/type.
*
* @param referredNode grouping/typedef node being referred
* @param linkedStatus linked status if success.
* @throws DataModelException a violation of data model rules
*/
private void addReferredEntityLink(YangNode referredNode)
private void addReferredEntityLink(YangNode referredNode, ResolvableStatus linkedStatus)
throws DataModelException {
if (getCurrentEntityToResolveFromStack() instanceof YangType) {
YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>)
......@@ -382,7 +395,7 @@ public class YangResolutionInfo<T> implements LocationInfo {
}
// Sets the resolution status in inside the type/uses.
((Resolvable) getCurrentEntityToResolveFromStack()).setResolvableStatus(LINKED);
((Resolvable) getCurrentEntityToResolveFromStack()).setResolvableStatus(linkedStatus);
}
/**
......@@ -402,7 +415,7 @@ public class YangResolutionInfo<T> implements LocationInfo {
if (((YangTypeDef) referredNode).getTypeDefBaseType().getDataType()
== YangDataTypes.DERIVED) {
YangEntityToResolveInfo<YangType<?>> unResolvedEntityInfo = new YangEntityToResolveInfo<YangType<?>>();
YangEntityToResolveInfo<YangType<?>> unResolvedEntityInfo = new YangEntityToResolveInfo<>();
unResolvedEntityInfo.setEntityToResolve(((YangTypeDef) referredNode)
.getTypeDefBaseType());
unResolvedEntityInfo.setHolderOfEntityToResolve(referredNode);
......@@ -421,7 +434,7 @@ public class YangResolutionInfo<T> implements LocationInfo {
}
/**
* Return if there is any unresolved uses in grouping.
* Returns if there is any unresolved uses in grouping.
*
* @param node grouping/typedef node
*/
......@@ -433,19 +446,12 @@ public class YangResolutionInfo<T> implements LocationInfo {
YangNode curNode = node.getChild();
while (curNode != null) {
if (curNode instanceof YangUses) {
ResolvableStatus curResolveStatus = ((Resolvable) curNode).getResolvableStatus();
if (curResolveStatus == UNRESOLVED) {
/**
* The current uses is not resolved, add it to partial
* resolved stack
*/
YangEntityToResolveInfo<YangUses> unResolvedEntityInfo = new YangEntityToResolveInfo<YangUses>();
YangEntityToResolveInfo<YangUses> unResolvedEntityInfo = new YangEntityToResolveInfo<>();
unResolvedEntityInfo.setEntityToResolve((YangUses) curNode);
unResolvedEntityInfo.setHolderOfEntityToResolve(node);
addInPartialResolvedStack((YangEntityToResolveInfo<T>) unResolvedEntityInfo);
}
}
curNode = curNode.getNextSibling();
}
}
......@@ -503,7 +509,7 @@ public class YangResolutionInfo<T> implements LocationInfo {
* @param entityToResolveInfo information about the entity that needs to be
* resolved
*/
public void setEntityToResolveInfo(YangEntityToResolveInfo<T> entityToResolveInfo) {
private void setEntityToResolveInfo(YangEntityToResolveInfo<T> entityToResolveInfo) {
this.entityToResolveInfo = entityToResolveInfo;
}
......@@ -526,4 +532,324 @@ public class YangResolutionInfo<T> implements LocationInfo {
public void setCharPosition(int charPositionInLine) {
this.charPosition = charPositionInLine;
}
/**
* Returns current module/sub-module reference, will be used in inter-file/
* inter-jar scenario to get the import/include list.
*
* @return current module/sub-module reference
*/
private YangReferenceResolver getCurReferenceResolver() {
return curReferenceResolver;
}
/**
* Sets current module/sub-module reference, will be used in inter-file/
* inter-jar scenario to get the import/include list.
*
* @param curReferenceResolver current module/sub-module reference
*/
private void setCurReferenceResolver(YangReferenceResolver curReferenceResolver) {
this.curReferenceResolver = curReferenceResolver;
}
/**
* Performs inter file linking of uses/type referring to typedef/grouping
* of other YANG file.
*
* @param dataModelRootNode module/sub-module node
* @throws DataModelException a violation in data model rule
*/
public void linkInterFile(YangReferenceResolver dataModelRootNode)
throws DataModelException {
setCurReferenceResolver(dataModelRootNode);
// Current node to resolve, it can be a YANG type or YANG uses.
T entityToResolve = getEntityToResolveInfo().getEntityToResolve();
// Check if linking is already done
if (entityToResolve instanceof Resolvable) {
Resolvable resolvable = (Resolvable) entityToResolve;
if (resolvable.getResolvableStatus() == RESOLVED) {
return;
}
} else {
throw new DataModelException("Data Model Exception: Entity to resolved is not Resolvable");
}
// Push the initial entity to resolve in stack.
addInPartialResolvedStack(getEntityToResolveInfo());
// Inter file linking and resolution.
linkInterFileAndResolve();
}
/**
* Returns the referenced prefix of entity under resolution.
*
* @return referenced prefix of entity under resolution
* @throws DataModelException a violation in data model rule
*/
private String getRefPrefix() throws DataModelException {
String refPrefix;
if (getCurrentEntityToResolveFromStack() instanceof YangType) {
refPrefix = ((YangType<?>) getCurrentEntityToResolveFromStack()).getPrefix();
} else if (getCurrentEntityToResolveFromStack() instanceof YangUses) {
refPrefix = ((YangUses) getCurrentEntityToResolveFromStack()).getPrefix();
} else {
throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses");
}
return refPrefix;
}
/**
* Performs inter file linking and resolution.
*
* @throws DataModelException a violation in data model rule
*/
private void linkInterFileAndResolve()
throws DataModelException {
while (getPartialResolvedStack().size() != 0) {
// Current node to resolve, it can be a YANG type or YANG uses.
T entityToResolve = getCurrentEntityToResolveFromStack();
// Check if linking is already done
if (entityToResolve instanceof Resolvable) {
Resolvable resolvable = (Resolvable) entityToResolve;
switch (resolvable.getResolvableStatus()) {
case RESOLVED: {
/*
* If the entity is already resolved in the stack, then
* pop it and continue with the remaining stack elements
* to resolve
*/
getPartialResolvedStack().pop();
break;
}
case INTER_FILE_LINKED: {
/*
* If the top of the stack is already linked then
* resolve the references and pop the entity and
* continue with remaining stack elements to resolve
*/
resolveTopOfStack();
getPartialResolvedStack().pop();
break;
}
case INTRA_FILE_RESOLVED: {
/*
* If the top of the stack is intra file resolved
* then check if top of stack is linked, if not
* link it using import/include list and push the
* linked referred entity to the stack, otherwise
* only push it to the stack.
*/
linkInterFileTopOfStackRefUpdateStack();
break;
}
default: {
throw new DataModelException("Data Model Exception: Unsupported, linker state");
}
}
} else {
throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses");
}
}
}
/**
* Links the top of the stack if it's inter-file and update stack.
*
* @throws DataModelException data model error
*/
private void linkInterFileTopOfStackRefUpdateStack() throws DataModelException {
/*
* Obtain the referred node of top of stack entity under resolution
*/
T referredNode = getRefNode();
/*
* Check for null for scenario when it's not linked and inter-file
* linking is required.
*/
if (referredNode == null) {
/*
* Check if prefix is null or not, to identify whether to search
* in import list or include list.
*/
if (getRefPrefix() != null && !(getRefPrefix().contentEquals(getCurReferenceResolver().getPrefix()))) {
if (resolveWithImport()) {
return;
}
} else {
if (resolveWithInclude()) {
return;
}
}
// Exception when referred typedef/grouping is not found.
DataModelException dataModelException = new DataModelException("YANG file error: Referred " +
"typedef/grouping for a given type/uses can't be found.");
dataModelException.setLine(getLineNumber());
dataModelException.setCharPosition(getCharPosition());
throw dataModelException;
} else {
/*
* If referred node is already linked, then just change the status
* and push to the stack.
*/
((Resolvable) getCurrentEntityToResolveFromStack()).setResolvableStatus(INTER_FILE_LINKED);
addUnresolvedRecursiveReferenceToStack((YangNode) referredNode);
}
}
/**
* Finds and resolves with include list.
*
* @return true if resolved, false otherwise
* @throws DataModelException a violation in data model rule
*/
private boolean resolveWithInclude() throws DataModelException {
/*
* Run through all the nodes in include list and search for referred
* typedef/grouping at the root level.
*/
for (YangInclude yangInclude : getCurReferenceResolver().getIncludeList()) {
YangNode linkedNode = null;
if (getCurrentEntityToResolveFromStack() instanceof YangType) {
linkedNode = findRefTypedef(yangInclude.getIncludedNode());
} else if (getCurrentEntityToResolveFromStack() instanceof YangUses) {
linkedNode = findRefGrouping(yangInclude.getIncludedNode());
}
if (linkedNode != null) {
// Add the link to external entity.
addReferredEntityLink(linkedNode, INTER_FILE_LINKED);
/*
* Update the current reference resolver to external
* module/sub-module containing the referred typedef/grouping.
*/
setCurReferenceResolver((YangReferenceResolver) yangInclude.getIncludedNode());
// Add the type/uses of referred typedef/grouping to the stack.
addUnresolvedRecursiveReferenceToStack(linkedNode);
return true;
}
}
// If referred node can't be found return false.
return false;
}
/**
* Finds and resolves with import list.
*
* @return true if resolved, false otherwise
* @throws DataModelException a violation in data model rule
*/
private boolean resolveWithImport() throws DataModelException {
/*
* Run through import list to find the referred typedef/grouping.
*/
for (YangImport yangImport : getCurReferenceResolver().getImportList()) {
/*
* Match the prefix attached to entity under resolution with the
* imported/included module/sub-module's prefix. If found, search
* for the referred typedef/grouping at the root level.
*/
if (yangImport.getPrefixId().contentEquals(getRefPrefix())) {
YangNode linkedNode = null;
if (getCurrentEntityToResolveFromStack() instanceof YangType) {
linkedNode = findRefTypedef(yangImport.getImportedNode());
} else if (getCurrentEntityToResolveFromStack() instanceof YangUses) {
linkedNode = findRefGrouping(yangImport.getImportedNode());
}
if (linkedNode != null) {
// Add the link to external entity.
addReferredEntityLink(linkedNode, INTER_FILE_LINKED);
/*
* Update the current reference resolver to external
* module/sub-module containing the referred typedef/grouping.
*/
setCurReferenceResolver((YangReferenceResolver) yangImport.getImportedNode());
// Add the type/uses of referred typedef/grouping to the stack.
addUnresolvedRecursiveReferenceToStack(linkedNode);
return true;
}
/*
* If referred node can't be found at root level break for loop,
* and return false.
*/
break;
}
}
// If referred node can't be found return false.
return false;
}
/**
* Returns referred typedef/grouping node.
*
* @return referred typedef/grouping node
* @throws DataModelException a violation in data model rule
*/
private T getRefNode() throws DataModelException {
if (getCurrentEntityToResolveFromStack() instanceof YangType) {
YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>)
((YangType<?>) getCurrentEntityToResolveFromStack()).getDataTypeExtendedInfo();
return (T) derivedInfo.getReferredTypeDef();
} else if (getCurrentEntityToResolveFromStack() instanceof YangUses) {
return (T) ((YangUses) getCurrentEntityToResolveFromStack()).getRefGroup();
} else {
throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses");
}
}
/**
* Finds the referred grouping node at the root level of imported/included node.
*
* @param refNode module/sub-module node
* @return referred grouping
*/
private YangNode findRefGrouping(YangNode refNode) {
YangNode tmpNode = refNode.getChild();
while (tmpNode != null) {
if (tmpNode instanceof YangGrouping) {
if (tmpNode.getName()
.equals(((YangUses) getCurrentEntityToResolveFromStack()).getName())) {
return tmpNode;
}
}
tmpNode = tmpNode.getNextSibling();
}
return null;
}
/**
* Finds the referred typedef node at the root level of imported/included node.
*
* @param refNode module/sub-module node
* @return referred typedef
*/
private YangNode findRefTypedef(YangNode refNode) {
YangNode tmpNode = refNode.getChild();
while (tmpNode != null) {
if (tmpNode instanceof YangTypeDef) {
if (tmpNode.getName()
.equals(((YangType) getCurrentEntityToResolveFromStack()).getDataTypeName())) {
return tmpNode;
}
}
tmpNode = tmpNode.getNextSibling();
}
return null;
}
}
......
......@@ -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;
......
......@@ -88,6 +88,12 @@ public final class BelongsToListener {
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);
}
......
......@@ -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);
}
......
......@@ -85,6 +85,12 @@ public final class IncludeListener {
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);
}
......
......@@ -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;
......@@ -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());
if (type.getDataType() == STRING) {
YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
if (stringRestriction == null) {
stringRestriction = new YangStringRestriction();
type.setDataTypeExtendedInfo(stringRestriction);
}
stringRestriction.setLengthRestriction(lengthRestriction);
} else {
type.setDataTypeExtendedInfo(lengthRestriction);
}
listener.getParsedDataStack().push(lengthRestriction);
}
......
......@@ -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;
......@@ -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,
......@@ -260,7 +262,7 @@ public final class TypeListener {
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);
}
}
......
......@@ -17,9 +17,10 @@
package org.onosproject.yangutils.plugin.manager;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
......@@ -28,8 +29,9 @@ import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
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.linker.impl.YangLinkerManager;
import org.onosproject.yangutils.parser.YangUtilsParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
......@@ -48,10 +50,9 @@ import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findBelongsToModuleNode;
/**
* Represents ONOS YANG utility maven plugin.
......@@ -63,6 +64,14 @@ import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findBelon
requiresProject = true)
public class YangUtilManager extends AbstractMojo {
private YangNode rootNode;
// YANG file information set.
private Set<YangFileInfo> yangFileInfoSet = new HashSet<>();
private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
private YangLinker yangLinker = new YangLinkerManager();
private static final String DEFAULT_PKG = SLASH + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG);
/**
* Source directory for YANG files.
*/
......@@ -117,12 +126,6 @@ public class YangUtilManager extends AbstractMojo {
@Component
private BuildContext context;
private static final String DEFAULT_PKG = SLASH
+ getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG);
private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
private YangNode rootNode;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
......@@ -136,47 +139,40 @@ public class YangUtilManager extends AbstractMojo {
String searchDir = getDirectory(baseDir, yangFilesDir);
String codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
// Creates conflict resolver and set values to it.
YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil();
conflictResolver.setReplacementForPeriod(replacementForPeriod);
conflictResolver.setReplacementForHyphen(replacementForHyphen);
conflictResolver.setReplacementForUnderscore(replacementForUnderscore);
List<YangFileInfo> yangFileInfo = YangFileScanner.getYangFiles(searchDir);
if (yangFileInfo == null || yangFileInfo.isEmpty()) {
// no files to translate
return;
}
YangPluginConfig yangPlugin = new YangPluginConfig();
yangPlugin.setCodeGenDir(codeGenDir);
yangPlugin.setConflictResolver(conflictResolver);
Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
while (yangFileIterator.hasNext()) {
YangFileInfo yangFile = yangFileIterator.next();
try {
YangNode yangNode = yangUtilsParser.getDataModel(yangFile.getYangFileName());
yangFile.setRootNode(yangNode);
setRootNode(yangNode);
} catch (ParserException e) {
String logInfo = "Error in file: " + e.getFileName();
if (e.getLineNumber() != 0) {
logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: "
+ e.getCharPositionInLine();
/*
* Obtain the YANG files at a path mentioned in plugin and creates
* YANG file information set.
*/
createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
}
if (e.getMessage() != null) {
logInfo = logInfo + NEW_LINE + e.getMessage();
}
getLog().info(logInfo);
throw e;
}
// Check if there are any file to translate, if not return.
if (getYangFileInfoSet() == null || getYangFileInfoSet().isEmpty()) {
// No files to translate
return;
}
resolveLinkingForSubModule(yangFileInfo);
//Carry out the parsing for all the YANG files.
parseYangFileInfoSet();
translateToJava(yangFileInfo, yangPlugin);
// Resolve dependencies using linker.
resolveDependenciesUsingLinker();
// Perform translation to JAVA.
translateToJava(getYangFileInfoSet(), yangPlugin);
addToSource(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG, project, context);
copyYangFilesToTarget(yangFileInfo, getDirectory(baseDir, outputDirectory), project);
copyYangFilesToTarget(getYangFileInfoSet(), getDirectory(baseDir, outputDirectory), project);
} catch (Exception e) {
String fileName = "";
if (e instanceof TranslatorException) {
......@@ -196,22 +192,45 @@ public class YangUtilManager extends AbstractMojo {
}
/**
* Set current project.
* Links all the provided with the YANG file info set.
*
* @param curProject maven project
* @throws MojoExecutionException a violation in mojo excecution
*/
public void setCurrentProject(MavenProject curProject) {
project = curProject;
public void resolveDependenciesUsingLinker() throws MojoExecutionException {
for (YangFileInfo yangFileInfo : getYangFileInfoSet()) {
try {
yangLinker.resolveDependencies(getYangFileInfoSet());
} catch (LinkerException e) {
throw new MojoExecutionException(e.getMessage());
}
}
}
/**
* Returns current project.
* Parses all the provided YANG files and generates YANG data model tree.
*
* @return current project
* @throws IOException a violation in IO
*/
public MavenProject getCurrentProject() {
return project;
public void parseYangFileInfoSet() throws IOException {
for (YangFileInfo yangFileInfo : getYangFileInfoSet()) {
try {
YangNode yangNode = yangUtilsParser.getDataModel(yangFileInfo.getYangFileName());
yangFileInfo.setRootNode(yangNode);
setRootNode(yangNode);
} catch (ParserException e) {
String logInfo = "Error in file: " + e.getFileName();
if (e.getLineNumber() != 0) {
logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: "
+ e.getCharPositionInLine();
}
if (e.getMessage() != null) {
logInfo = logInfo + NEW_LINE + e.getMessage();
}
getLog().info(logInfo);
throw e;
}
}
}
/**
......@@ -219,7 +238,7 @@ public class YangUtilManager extends AbstractMojo {
*
* @return current root YANG node of data-model tree
*/
public YangNode getRootNode() {
private YangNode getRootNode() {
return rootNode;
}
......@@ -228,44 +247,55 @@ public class YangUtilManager extends AbstractMojo {
*
* @param rootNode current root YANG node of data-model tree
*/
public void setRootNode(YangNode rootNode) {
private void setRootNode(YangNode rootNode) {
this.rootNode = rootNode;
}
/**
* Translates to java code corresponding to the YANG schema.
*
* @param yangFileInfo YANG file information
* @param yangFileInfoSet YANG file information
* @param yangPlugin YANG plugin config
* @throws IOException when fails to generate java code file the current
* node
*/
public static void translateToJava(List<YangFileInfo> yangFileInfo, YangPluginConfig yangPlugin)
public void translateToJava(Set<YangFileInfo> yangFileInfoSet, YangPluginConfig yangPlugin)
throws IOException {
Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
Iterator<YangFileInfo> yangFileIterator = yangFileInfoSet.iterator();
while (yangFileIterator.hasNext()) {
YangFileInfo yangFile = yangFileIterator.next();
generateJavaCode(yangFile.getRootNode(), yangPlugin, yangFile.getYangFileName());
YangFileInfo yangFileInfo = yangFileIterator.next();
generateJavaCode(yangFileInfo.getRootNode(), yangPlugin, yangFileInfo.getYangFileName());
}
}
/**
* Resolves sub-module linking.
* Creates a YANG file info set.
*
* @param yangFileInfo YANG file information
* @throws DataModelException when belongs-to module node is not found
* @param yangFileList YANG files list
*/
public static void resolveLinkingForSubModule(List<YangFileInfo> yangFileInfo) throws DataModelException {
Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
while (yangFileIterator.hasNext()) {
YangFileInfo yangFile = yangFileIterator.next();
YangNode yangNode = yangFile.getRootNode();
if (yangNode instanceof YangSubModule) {
String belongsToModuleName = ((YangSubModule) yangNode).getBelongsTo()
.getBelongsToModuleName();
YangNode moduleNode = findBelongsToModuleNode(yangFileInfo, belongsToModuleName);
((YangSubModule) yangNode).getBelongsTo().setModuleNode(moduleNode);
public void createYangFileInfoSet(List<String> yangFileList) {
for (String yangFile : yangFileList) {
YangFileInfo yangFileInfo = new YangFileInfo();
yangFileInfo.setYangFileName(yangFile);
getYangFileInfoSet().add(yangFileInfo);
}
}
/**
* Returns the YANG file info set.
*
* @return the YANG file info set
*/
public Set<YangFileInfo> getYangFileInfoSet() {
return yangFileInfoSet;
}
/**
* Sets the YANG file info set.
*
* @param yangFileInfoSet the YANG file info set
*/
public void setYangFileInfoSet(Set<YangFileInfo> yangFileInfoSet) {
this.yangFileInfoSet = yangFileInfoSet;
}
}
......
......@@ -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;
......@@ -108,7 +107,7 @@ public class YangJavaSubModule
* 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;
......@@ -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.
......@@ -52,7 +51,7 @@ 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
......@@ -60,15 +59,9 @@ public final class YangFileScanner {
* @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;
}
/**
......
......@@ -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;
......@@ -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 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()) {
......
/*
* 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;
import java.io.IOException;
import java.util.Iterator;
import java.util.ListIterator;
import org.apache.maven.plugin.MojoExecutionException;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangGrouping;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangModule;
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.impl.ResolvableStatus;
import org.onosproject.yangutils.linker.impl.YangLinkerManager;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import org.onosproject.yangutils.plugin.manager.YangFileInfo;
import org.onosproject.yangutils.plugin.manager.YangUtilManager;
import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
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.YangDataTypes.DERIVED;
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 inter file linking.
*/
public class InterFileLinkingTest {
private final YangUtilsParserManager manager = new YangUtilsParserManager();
private final YangUtilManager utilManager = new YangUtilManager();
private final YangLinkerManager yangLinkerManager = new YangLinkerManager();
/**
* Checks inter file type linking.
*/
@Test
public void processInterFileTypeLinking()
throws IOException, ParserException, MojoExecutionException {
String searchDir = "src/test/resources/interfiletype";
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
utilManager.parseYangFileInfoSet();
YangNode refNode = null;
YangNode selfNode = null;
// Add references to import list.
yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
// Carry out inter-file linking.
yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
YangFileInfo yangFileInfo = yangFileInfoIterator.next();
if (yangFileInfo.getRootNode().getName().equals("module1")) {
selfNode = yangFileInfo.getRootNode();
refNode = yangFileInfoIterator.next().getRootNode();
} else {
refNode = yangFileInfo.getRootNode();
selfNode = yangFileInfoIterator.next().getRootNode();
}
// Check whether the data model tree returned is of type module.
assertThat(selfNode instanceof YangModule, is(true));
// Check whether the node type is set properly to module.
assertThat(selfNode.getNodeType(), is(MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) selfNode;
assertThat(yangNode.getName(), is("module1"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("invalid-interval"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) refNode.getChild()));
assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
// Check for the effective built-in type.
assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
// Check for the restriction.
assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
}
/**
* Checks inter file uses linking.
*/
@Test
public void processInterFileUsesLinking()
throws IOException, ParserException, MojoExecutionException {
String searchDir = "src/test/resources/interfileuses";
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
utilManager.parseYangFileInfoSet();
YangNode refNode = null;
YangNode selfNode = null;
// Add references to import list.
yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
// Carry out inter-file linking.
yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
YangFileInfo yangFileInfo = yangFileInfoIterator.next();
if (yangFileInfo.getRootNode().getName().equals("module1")) {
selfNode = yangFileInfo.getRootNode();
refNode = yangFileInfoIterator.next().getRootNode();
} else {
refNode = yangFileInfo.getRootNode();
selfNode = yangFileInfoIterator.next().getRootNode();
}
// Check whether the data model tree returned is of type module.
assertThat((selfNode instanceof YangModule), is(true));
// Check whether the node type is set properly to module.
assertThat(selfNode.getNodeType(), is(YangNodeType.MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) selfNode;
assertThat(yangNode.getName(), is("module1"));
ListIterator<YangLeaf> leafIterator;
YangLeaf leafInfo;
// Check whether grouping is the sibling of module's child.
assertThat((refNode.getChild() instanceof YangGrouping), is(true));
YangGrouping grouping = (YangGrouping) refNode.getChild();
leafIterator = grouping.getListOfLeaf().listIterator();
leafInfo = leafIterator.next();
// Check whether the information in the leaf is correct under grouping.
assertThat(leafInfo.getName(), is("hello"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
// Check whether uses is module's child.
assertThat((yangNode.getChild() instanceof YangUses), is(true));
YangUses uses = (YangUses) yangNode.getChild();
// Check whether uses get resolved.
assertThat(uses.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
leafIterator = yangNode.getListOfLeaf().listIterator();
leafInfo = leafIterator.next();
// Check whether the information in the leaf is correct under module.
assertThat(leafInfo.getName(), is("hello"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
}
/**
* Checks inter file type linking with include list.
*/
@Test
public void processInterFileTypeLinkingWithIncludeList()
throws IOException, ParserException, MojoExecutionException {
String searchDir = "src/test/resources/interfiletypewithinclude";
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
utilManager.parseYangFileInfoSet();
YangNode refNode = null;
YangNode selfNode = null;
// Carry out linking of sub module with module.
yangLinkerManager.linkSubModulesToParentModule(utilManager.getYangFileInfoSet());
// Add reference to include list.
yangLinkerManager.addRefToYangFilesIncludeList(utilManager.getYangFileInfoSet());
// Carry out inter-file linking.
yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
YangFileInfo yangFileInfo = yangFileInfoIterator.next();
if (yangFileInfo.getRootNode().getName().equals("module1")) {
selfNode = yangFileInfo.getRootNode();
refNode = yangFileInfoIterator.next().getRootNode();
} else {
refNode = yangFileInfo.getRootNode();
selfNode = yangFileInfoIterator.next().getRootNode();
}
// Check whether the data model tree returned is of type module.
assertThat(selfNode instanceof YangModule, is(true));
// Check whether the node type is set properly to module.
assertThat(selfNode.getNodeType(), is(MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) selfNode;
assertThat(yangNode.getName(), is("module1"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("invalid-interval"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) refNode.getChild()));
assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
// Check for the effective built-in type.
assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
// Check for the restriction.
assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
}
/**
* Checks inter file uses linking with include list.
*/
@Test
public void processInterFileUsesLinkingWithInclude()
throws IOException, ParserException, MojoExecutionException {
String searchDir = "src/test/resources/interfileuseswithinclude";
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
utilManager.parseYangFileInfoSet();
YangNode refNode = null;
YangNode selfNode = null;
// Carry out linking of sub module with module.
yangLinkerManager.linkSubModulesToParentModule(utilManager.getYangFileInfoSet());
// Add reference to include list.
yangLinkerManager.addRefToYangFilesIncludeList(utilManager.getYangFileInfoSet());
// Carry out inter-file linking.
yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
YangFileInfo yangFileInfo = yangFileInfoIterator.next();
if (yangFileInfo.getRootNode().getName().equals("module1")) {
selfNode = yangFileInfo.getRootNode();
refNode = yangFileInfoIterator.next().getRootNode();
} else {
refNode = yangFileInfo.getRootNode();
selfNode = yangFileInfoIterator.next().getRootNode();
}
// Check whether the data model tree returned is of type module.
assertThat((selfNode instanceof YangModule), is(true));
// Check whether the node type is set properly to module.
assertThat(selfNode.getNodeType(), is(YangNodeType.MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) selfNode;
assertThat(yangNode.getName(), is("module1"));
ListIterator<YangLeaf> leafIterator;
YangLeaf leafInfo;
// Check whether grouping is the sibling of module's child.
assertThat((refNode.getChild() instanceof YangGrouping), is(true));
YangGrouping grouping = (YangGrouping) refNode.getChild();
leafIterator = grouping.getListOfLeaf().listIterator();
leafInfo = leafIterator.next();
// Check whether the information in the leaf is correct under grouping.
assertThat(leafInfo.getName(), is("hello"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
// Check whether uses is module's child.
assertThat((yangNode.getChild() instanceof YangUses), is(true));
YangUses uses = (YangUses) yangNode.getChild();
// Check whether uses get resolved.
assertThat(uses.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
leafIterator = yangNode.getListOfLeaf().listIterator();
leafInfo = leafIterator.next();
// Check whether the information in the leaf is correct under module.
assertThat(leafInfo.getName(), is("hello"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
}
/**
* Checks inter file type linking with revision.
*/
@Test
public void processInterFileTypeLinkingWithRevision()
throws IOException, ParserException, MojoExecutionException {
String searchDir = "src/test/resources/interfiletypewithrevision";
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
utilManager.parseYangFileInfoSet();
YangNode refNode = null;
YangNode selfNode = null;
// Add references to import list.
yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
// Carry out inter-file linking.
yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
YangFileInfo yangFileInfo = yangFileInfoIterator.next();
if (yangFileInfo.getRootNode().getName().equals("module1")) {
selfNode = yangFileInfo.getRootNode();
refNode = yangFileInfoIterator.next().getRootNode();
} else {
refNode = yangFileInfo.getRootNode();
selfNode = yangFileInfoIterator.next().getRootNode();
}
// Check whether the data model tree returned is of type module.
assertThat(selfNode instanceof YangModule, is(true));
// Check whether the node type is set properly to module.
assertThat(selfNode.getNodeType(), is(MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) selfNode;
assertThat(yangNode.getName(), is("module1"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("invalid-interval"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) refNode.getChild()));
assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
// Check for the effective built-in type.
assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
// Check for the restriction.
assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
}
/**
* Checks inter file type linking with revision in name.
*/
@Test
public void processInterFileTypeLinkingWithRevisionInName()
throws IOException, ParserException, MojoExecutionException {
String searchDir = "src/test/resources/interfiletypewithrevisioninname";
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
utilManager.parseYangFileInfoSet();
YangNode refNode = null;
YangNode selfNode = null;
// Add references to import list.
yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
// Carry out inter-file linking.
yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
YangFileInfo yangFileInfo = yangFileInfoIterator.next();
if (yangFileInfo.getRootNode().getName().equals("module1")) {
selfNode = yangFileInfo.getRootNode();
refNode = yangFileInfoIterator.next().getRootNode();
} else {
refNode = yangFileInfo.getRootNode();
selfNode = yangFileInfoIterator.next().getRootNode();
}
// Check whether the data model tree returned is of type module.
assertThat(selfNode instanceof YangModule, is(true));
// Check whether the node type is set properly to module.
assertThat(selfNode.getNodeType(), is(MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) selfNode;
assertThat(yangNode.getName(), is("module1"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("invalid-interval"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) refNode.getChild()));
assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
// Check for the effective built-in type.
assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
// Check for the restriction.
assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
}
/**
* Checks hierarchical inter file type linking.
*/
@Test
public void processHierarchicalInterFileTypeLinking()
throws IOException, ParserException, MojoExecutionException {
String searchDir = "src/test/resources/hierarchicalinterfiletype";
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
utilManager.parseYangFileInfoSet();
YangNode refNode1 = null;
YangNode refNode2 = null;
YangNode selfNode = null;
// Add references to import list.
yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
// Carry out inter-file linking.
yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
for (YangFileInfo yangFile : utilManager.getYangFileInfoSet()) {
if (yangFile.getRootNode().getName().equals("ietf-network-topology")) {
selfNode = yangFile.getRootNode();
} else if (yangFile.getRootNode().getName().equals("ietf-network")) {
refNode1 = yangFile.getRootNode();
} else {
refNode2 = yangFile.getRootNode();
}
}
// Check whether the data model tree returned is of type module.
assertThat(selfNode instanceof YangModule, is(true));
// Check whether the node type is set properly to module.
assertThat(selfNode.getNodeType(), is(MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) selfNode;
assertThat(yangNode.getName(), is("ietf-network-topology"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("source-node"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("node-id"));
assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) refNode1.getChild()));
assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
// Check for the effective built-in type.
assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
// Check for the restriction.
assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
}
/**
* Checks hierarchical intra with inter file type linking.
*/
@Test
public void processHierarchicalIntraWithInterFileTypeLinking()
throws IOException, ParserException, MojoExecutionException {
String searchDir = "src/test/resources/hierarchicalintrawithinterfiletype";
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
utilManager.parseYangFileInfoSet();
YangNode refNode1 = null;
YangNode selfNode = null;
// Add references to import list.
yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
// Carry out inter-file linking.
yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
for (YangFileInfo yangFile : utilManager.getYangFileInfoSet()) {
if (yangFile.getRootNode().getName().equals("ietf-network")) {
selfNode = yangFile.getRootNode();
} else if (yangFile.getRootNode().getName().equals("ietf-inet-types")) {
refNode1 = yangFile.getRootNode();
}
}
// Check whether the data model tree returned is of type module.
assertThat(selfNode instanceof YangModule, is(true));
// Check whether the node type is set properly to module.
assertThat(selfNode.getNodeType(), is(MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) selfNode;
assertThat(yangNode.getName(), is("ietf-network"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("node-ref"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("node-id"));
assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) selfNode.getChild()));
assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
// Check for the effective built-in type.
assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
// Check for the restriction.
assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
}
}
......@@ -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;
......@@ -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;
}
}
}