[ONOS-4753] Identity/identityref implementation and UT
Change-Id: I40148fa228465555be3bdf410cc294ffc0f34c18
diff --git a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangEntityToResolveInfoImpl.java b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangEntityToResolveInfoImpl.java
index 17285f6..d33dfd7 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangEntityToResolveInfoImpl.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangEntityToResolveInfoImpl.java
@@ -19,6 +19,8 @@
import org.onosproject.yangutils.datamodel.YangEntityToResolveInfo;
import org.onosproject.yangutils.datamodel.YangIfFeature;
+import org.onosproject.yangutils.datamodel.YangBase;
+import org.onosproject.yangutils.datamodel.YangIdentityRef;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangUses;
@@ -79,6 +81,10 @@
prefix = ((YangUses) entityToBeResolved).getPrefix();
} else if (entityToBeResolved instanceof YangIfFeature) {
prefix = ((YangIfFeature) entityToBeResolved).getPrefix();
+ } else if (entityToBeResolved instanceof YangBase) {
+ prefix = ((YangBase) entityToBeResolved).getBaseIdentifier().getPrefix();
+ } else if (entityToBeResolved instanceof YangIdentityRef) {
+ prefix = ((YangIdentityRef) entityToBeResolved).getBaseIdentity().getPrefix();
} else {
throw new LinkerException("Linker Exception: Entity to resolved is other than type/uses");
}
diff --git a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerManager.java b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerManager.java
index 914b5d5..3847d3d 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerManager.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerManager.java
@@ -158,11 +158,16 @@
try {
((YangReferenceResolver) yangNode)
.resolveInterFileLinking(ResolvableType.YANG_IF_FEATURE);
- ((YangReferenceResolver) yangNode).resolveInterFileLinking(ResolvableType.YANG_USES);
+ ((YangReferenceResolver) yangNode)
+ .resolveInterFileLinking(ResolvableType.YANG_USES);
((YangReferenceResolver) yangNode)
.resolveInterFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
((YangReferenceResolver) yangNode)
.resolveInterFileLinking(ResolvableType.YANG_LEAFREF);
+ ((YangReferenceResolver) yangNode)
+ .resolveInterFileLinking(ResolvableType.YANG_BASE);
+ ((YangReferenceResolver) yangNode)
+ .resolveInterFileLinking(ResolvableType.YANG_IDENTITYREF);
} catch (DataModelException e) {
String errorInfo = "Error in file: " + yangNode.getName() + " at line: "
+ e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE + e.getMessage();
diff --git a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangResolutionInfoImpl.java b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangResolutionInfoImpl.java
index b70d44c..eb42233 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangResolutionInfoImpl.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangResolutionInfoImpl.java
@@ -24,12 +24,15 @@
import org.onosproject.yangutils.datamodel.Resolvable;
import org.onosproject.yangutils.datamodel.ResolvableType;
import org.onosproject.yangutils.datamodel.YangAtomicPath;
+import org.onosproject.yangutils.datamodel.YangBase;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangEntityToResolveInfo;
import org.onosproject.yangutils.datamodel.YangFeature;
import org.onosproject.yangutils.datamodel.YangFeatureHolder;
import org.onosproject.yangutils.datamodel.YangGrouping;
import org.onosproject.yangutils.datamodel.YangIfFeature;
+import org.onosproject.yangutils.datamodel.YangIdentity;
+import org.onosproject.yangutils.datamodel.YangIdentityRef;
import org.onosproject.yangutils.datamodel.YangImport;
import org.onosproject.yangutils.datamodel.YangInclude;
import org.onosproject.yangutils.datamodel.YangInput;
@@ -69,9 +72,12 @@
import static org.onosproject.yangutils.utils.UtilConstants.GROUPING_LINKER_ERROR;
import static org.onosproject.yangutils.utils.UtilConstants.INPUT;
import static org.onosproject.yangutils.utils.UtilConstants.LEAFREF;
+import static org.onosproject.yangutils.utils.UtilConstants.IDENTITYREF;
import static org.onosproject.yangutils.utils.UtilConstants.LEAFREF_LINKER_ERROR;
import static org.onosproject.yangutils.utils.UtilConstants.OUTPUT;
import static org.onosproject.yangutils.utils.UtilConstants.TYPEDEF_LINKER_ERROR;
+import static org.onosproject.yangutils.utils.UtilConstants.IDENTITYREF_LINKER_ERROR;
+import static org.onosproject.yangutils.utils.UtilConstants.BASE_LINKER_ERROR;
/**
* Represents implementation of resolution object which will be resolved by
@@ -143,7 +149,10 @@
setCurReferenceResolver(dataModelRootNode);
- // Current node to resolve, it can be a YANG type, YANG uses or YANG if-feature or YANG leafref.
+ /**
+ * Current node to resolve, it can be a YANG type, YANG uses or YANG if-feature or
+ * YANG leafref or YANG base or YANG identityref.
+ */
T entityToResolve = getEntityToResolveInfo().getEntityToResolve();
// Check if linking is already done
@@ -157,7 +166,7 @@
}
} else {
throw new DataModelException("Data Model Exception: Entity to resolved is other than " +
- "type/uses/if-feature/leafref");
+ "type/uses/if-feature/leafref/base/identityref");
}
// Push the initial entity to resolve in stack.
@@ -178,7 +187,10 @@
while (getPartialResolvedStack().size() != 0) {
- // Current node to resolve, it can be a YANG type or YANG uses.
+ /**
+ * Current node to resolve, it can be a YANG type or YANG uses or
+ * YANG if-feature or YANG leafref or YANG base or YANG identityref.
+ */
T entityToResolve = getCurrentEntityToResolveFromStack();
// Check if linking is already done
if (entityToResolve instanceof Resolvable) {
@@ -227,6 +239,10 @@
errorInfo = GROUPING_LINKER_ERROR;
} else if (resolvable instanceof YangIfFeature) {
errorInfo = FEATURE_LINKER_ERROR;
+ } else if (resolvable instanceof YangBase) {
+ errorInfo = BASE_LINKER_ERROR;
+ } else if (resolvable instanceof YangIdentityRef) {
+ errorInfo = IDENTITYREF_LINKER_ERROR;
} else {
errorInfo = LEAFREF_LINKER_ERROR;
}
@@ -246,14 +262,16 @@
} else {
throw new DataModelException(
- "Data Model Exception: Entity to resolved is other than type/uses/if-feature/leafref");
+ "Data Model Exception: Entity to resolved is other than type/uses/if-feature" +
+ "/leafref/base/identityref");
}
}
}
/**
- * Adds the leafref type to the type, which has derived type referring to typedef with leafref type.
+ * Adds the leafref/identityref type to the type, which has derived type referring to
+ * typedef with leafref/identityref type.
*/
private void addDerivedRefTypeToRefTypeResolutionList() throws DataModelException {
@@ -279,9 +297,10 @@
YangDerivedInfo derivedInfo = (YangDerivedInfo) yangType.getDataTypeExtendedInfo();
/*
- * If the derived types referred type is not leaf ref return
+ * If the derived types referred type is not leafref/identityref return
*/
- if (derivedInfo.getEffectiveBuiltInType() != YangDataTypes.LEAFREF) {
+ if ((derivedInfo.getEffectiveBuiltInType() != YangDataTypes.LEAFREF) &&
+ (derivedInfo.getEffectiveBuiltInType() != YangDataTypes.IDENTITYREF)) {
return;
}
@@ -292,28 +311,49 @@
extendedInfo = (T) derivedInfoFromTypedef.getReferredTypeDef().getTypeDefBaseType()
.getDataTypeExtendedInfo();
}
+
/*
- * Backup the derived types leaf ref info, delete all the info in
- * current type, but for resolution status as resolved. Copy the backed
- * up leaf ref to types extended info, create a leaf ref resolution info
- * using the current resolution info and add to leaf ref resolution
- * list.
+ * Backup the derived types leafref/identityref info, delete all the info in current type,
+ * but for resolution status as resolved. Copy the backed up leafref/identityref to types extended info,
+ * create a leafref/identityref resolution info using the current resolution info and
+ * add to leafref/identityref resolution list.
*/
- YangLeafRef leafRefInTypeDef = (YangLeafRef) extendedInfo;
- yangType.resetYangType();
+ if (derivedInfo.getEffectiveBuiltInType() == YangDataTypes.LEAFREF) {
+ YangLeafRef leafRefInTypeDef = (YangLeafRef) extendedInfo;
+ yangType.resetYangType();
- yangType.setResolvableStatus(RESOLVED);
- yangType.setDataType(YangDataTypes.LEAFREF);
- yangType.setDataTypeName(LEAFREF);
- yangType.setDataTypeExtendedInfo(leafRefInTypeDef);
- leafRefInTypeDef.setResolvableStatus(UNRESOLVED);
+ yangType.setResolvableStatus(RESOLVED);
+ yangType.setDataType(YangDataTypes.LEAFREF);
+ yangType.setDataTypeName(LEAFREF);
+ yangType.setDataTypeExtendedInfo(leafRefInTypeDef);
+ leafRefInTypeDef.setResolvableStatus(UNRESOLVED);
- // Add resolution information to the list.
- YangResolutionInfoImpl resolutionInfoImpl = new YangResolutionInfoImpl<>(leafRefInTypeDef,
- potentialAncestorWithReferredNode, getLineNumber(), getCharPosition());
- getCurReferenceResolver().addToResolutionList(resolutionInfoImpl,
- ResolvableType.YANG_LEAFREF);
- getCurReferenceResolver().resolveSelfFileLinking(ResolvableType.YANG_LEAFREF);
+ // Add resolution information to the list.
+ YangResolutionInfoImpl resolutionInfoImpl = new YangResolutionInfoImpl<>(leafRefInTypeDef,
+ potentialAncestorWithReferredNode,
+ getLineNumber(), getCharPosition());
+ getCurReferenceResolver().addToResolutionList(resolutionInfoImpl,
+ ResolvableType.YANG_LEAFREF);
+ getCurReferenceResolver().resolveSelfFileLinking(ResolvableType.YANG_LEAFREF);
+
+ } else if (derivedInfo.getEffectiveBuiltInType() == YangDataTypes.IDENTITYREF) {
+
+ YangIdentityRef identityRefInTypeDef = (YangIdentityRef) extendedInfo;
+ yangType.resetYangType();
+
+ yangType.setResolvableStatus(RESOLVED);
+ yangType.setDataType(YangDataTypes.IDENTITYREF);
+ yangType.setDataTypeName(IDENTITYREF);
+ yangType.setDataTypeExtendedInfo(identityRefInTypeDef);
+ identityRefInTypeDef.setResolvableStatus(UNRESOLVED);
+
+ // Add resolution information to the list.
+ YangResolutionInfoImpl resolutionInfoImpl = new YangResolutionInfoImpl<>(identityRefInTypeDef,
+ potentialAncestorWithReferredNode, getLineNumber(), getCharPosition());
+ getCurReferenceResolver().addToResolutionList(resolutionInfoImpl,
+ ResolvableType.YANG_IDENTITYREF);
+ getCurReferenceResolver().resolveSelfFileLinking(ResolvableType.YANG_IDENTITYREF);
+ }
}
/**
@@ -360,6 +400,10 @@
} else if (getCurrentEntityToResolveFromStack() instanceof YangLeafRef) {
resolveSelfFileLinkingForLeafref(potentialAncestorWithReferredNode);
return;
+ } else if ((getCurrentEntityToResolveFromStack() instanceof YangIdentityRef) ||
+ (getCurrentEntityToResolveFromStack() instanceof YangBase)) {
+ resolveSelfFileLinkingForBaseAndIdentityref();
+ return;
} else {
/**
@@ -453,6 +497,47 @@
}
/**
+ * Resolves self file linking for base/identityref.
+ *
+ * @throws DataModelException a violation of data model rules
+ */
+ private void resolveSelfFileLinkingForBaseAndIdentityref()
+ throws DataModelException {
+
+ boolean referredIdentityFound = false;
+ String nodeName = null;
+
+ if (getCurrentEntityToResolveFromStack() instanceof YangIdentityRef) {
+ nodeName = ((YangIdentityRef) getCurrentEntityToResolveFromStack()).getName();
+ }
+
+ if (getCurrentEntityToResolveFromStack() instanceof YangBase) {
+ nodeName = ((YangBase) getCurrentEntityToResolveFromStack()).getBaseIdentifier().getName();
+ }
+
+ if (getCurReferenceResolver() instanceof YangModule) {
+ YangModule rootNode = (YangModule) getCurReferenceResolver();
+ // Sends list of nodes for finding the target identity.
+ referredIdentityFound = isIdentityReferenceFound(nodeName, rootNode);
+ } else if (getCurReferenceResolver() instanceof YangSubModule) {
+ YangSubModule rootNode = (YangSubModule) getCurReferenceResolver();
+ // Sends list of nodes for finding the target identity.
+ referredIdentityFound = isIdentityReferenceFound(nodeName, rootNode);
+ }
+
+ if (referredIdentityFound) {
+ return;
+ }
+
+ /*
+ * 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);
+ }
+ }
+
+ /**
* Returns the root parent with respect to the ancestor count from leafref.
*
* @param ancestorCount count of node where parent node can be reached
@@ -561,6 +646,41 @@
}
/**
+ * Returns the status of the referred identity found for base/identityref.
+ *
+ * @param nodeName the name of the base nodeidentifier/identityref nodeidentifier
+ * @param ancestorWithTheReferredNode the parent node of base/identityref
+ * @return status of referred base/identityref
+ * @throws DataModelException a violation of data model rules
+ */
+ private boolean isIdentityReferenceFound(String nodeName, YangNode ancestorWithTheReferredNode)
+ throws DataModelException {
+
+ // When child is not present return.
+ if (ancestorWithTheReferredNode.getChild() == null) {
+ return false;
+ }
+
+ ancestorWithTheReferredNode = ancestorWithTheReferredNode.getChild();
+
+ // Checks all the siblings under the node and returns the matched node.
+ YangNode nodeFound = isReferredNodeInSiblingProcessedForIdentity(ancestorWithTheReferredNode, nodeName);
+
+ if (nodeFound != null) {
+ // Adds reference link of entity to the node under resolution.
+ addReferredEntityLink(nodeFound, LINKED);
+
+ /**
+ * resolve the reference and update the partial resolution stack with any further recursive references
+ */
+ addUnresolvedRecursiveReferenceToStack(nodeFound);
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
* Fills the referred leaf or leaf-list inside the path predicates.
*
* @param ancestorWithTheReferredNode the actual node where YANG list will be present
@@ -856,6 +976,28 @@
}
/**
+ * Checks for the referred parent node for the base/identity.
+ *
+ * @param potentialReferredNode potential referred node
+ * @return the reffered parent node of base/identity.
+ * @throws DataModelException data model errors
+ */
+ private YangNode isReferredNodeInSiblingProcessedForIdentity(YangNode potentialReferredNode,
+ String referredNodeName) throws DataModelException {
+
+ while (potentialReferredNode != null) {
+ if (potentialReferredNode instanceof YangIdentity) {
+ // Check if the potential referred node is the actual referred node
+ if (isReferredNodeForIdentity(potentialReferredNode, referredNodeName)) {
+ return potentialReferredNode;
+ }
+ }
+ potentialReferredNode = potentialReferredNode.getNextSibling();
+ }
+ return null;
+ }
+
+ /**
* Checks if the current reference node name and the name in the path are equal.
*
* @param currentReferredNode the node where the reference is pointed
@@ -878,6 +1020,28 @@
}
/**
+ * Checks if the current reference node name and the name in the base/identityref base are equal.
+ *
+ * @param currentReferredNode the node where the reference is pointed
+ * @param nameOfIdentityRefBase name of the base in the base/identityref base
+ * @return status of the match between the name
+ * @throws DataModelException a violation of data model rules
+ */
+ private boolean isReferredNodeForIdentity(YangNode currentReferredNode, String nameOfIdentityRefBase)
+ throws DataModelException {
+
+ if ((getCurrentEntityToResolveFromStack() instanceof YangIdentityRef) ||
+ (getCurrentEntityToResolveFromStack() instanceof YangBase)) {
+ /*
+ * Check if name of node name matches with the current reference node.
+ */
+ return currentReferredNode.getName().contentEquals(nameOfIdentityRefBase);
+ } else {
+ throw new DataModelException("Data Model Exception: Entity to resolved is other than identityref");
+ }
+ }
+
+ /**
* Checks for the referred node defined in a ancestor scope.
*
* @param potentialReferredNode potential referred node
@@ -946,8 +1110,18 @@
*/
return isNodeNameSameAsResolutionInfoName(potentialReferredNode);
}
+ } else if ((getCurrentEntityToResolveFromStack() instanceof YangBase) ||
+ (getCurrentEntityToResolveFromStack() instanceof YangIdentityRef)) {
+ if (potentialReferredNode instanceof YangIdentity) {
+ /*
+ * Check if name of node name matches with the entity being
+ * resolved
+ */
+ return isNodeNameSameAsResolutionInfoName(potentialReferredNode);
+ }
} else {
- throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses");
+ throw new DataModelException("Data Model Exception: Entity to resolved is other than type/" +
+ "uses/base/identityref");
}
return false;
}
@@ -977,6 +1151,16 @@
}
} else if (getCurrentEntityToResolveFromStack() instanceof YangIfFeature) {
return isFeatureDefinedInNode(node);
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangBase) {
+ if (node.getName().contentEquals(
+ ((YangBase) getCurrentEntityToResolveFromStack()).getBaseIdentifier().getName())) {
+ return true;
+ }
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangIdentityRef) {
+ if (node.getName().contentEquals(
+ ((YangIdentityRef) getCurrentEntityToResolveFromStack()).getName())) {
+ return true;
+ }
} else {
throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses");
}
@@ -1020,8 +1204,13 @@
// do nothing , referred node is already set
} else if (getCurrentEntityToResolveFromStack() instanceof YangLeafRef) {
// do nothing , referred node is already set
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangBase) {
+ ((YangBase) getCurrentEntityToResolveFromStack()).setReferredIdentity((YangIdentity) referredNode);
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangIdentityRef) {
+ ((YangIdentityRef) getCurrentEntityToResolveFromStack()).setReferredIdentity((YangIdentity) referredNode);
} else {
- throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses");
+ throw new DataModelException("Data Model Exception: Entity to resolved is other than type" +
+ "/uses/base/identityref");
}
// Sets the resolution status in inside the type/uses.
@@ -1062,6 +1251,15 @@
} else if (getCurrentEntityToResolveFromStack() instanceof YangLeafRef) {
// do nothing , referred node is already set
throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses");
+ } else if ((getCurrentEntityToResolveFromStack() instanceof YangBase) ||
+ (getCurrentEntityToResolveFromStack() instanceof YangIdentityRef)) {
+ /*
+ * Search if the identity has any un resolved base, if so return true, else return false.
+ */
+ addUnResolvedBaseToStack(referredNode);
+ } else {
+ throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses/" +
+ "base/identityref");
}
}
@@ -1109,6 +1307,26 @@
}
/**
+ * Returns if there is any unresolved base in identity.
+ *
+ * @param node module/submodule node
+ */
+ private void addUnResolvedBaseToStack(YangNode node) {
+
+ YangIdentity curNode = (YangIdentity) node;
+ if (curNode.getBaseNode() != null) {
+ if (curNode.getBaseNode().getResolvableStatus() != RESOLVED) {
+ YangEntityToResolveInfoImpl<YangBase> unResolvedEntityInfo = new YangEntityToResolveInfoImpl<>();
+ unResolvedEntityInfo.setEntityToResolve(curNode.getBaseNode());
+ unResolvedEntityInfo.setHolderOfEntityToResolve(node);
+ addInPartialResolvedStack((YangEntityToResolveInfoImpl<T>) unResolvedEntityInfo);
+
+ }
+ }
+ }
+
+
+ /**
* Returns stack of YANG type with partially resolved YANG construct
* hierarchy.
*
@@ -1247,8 +1465,13 @@
refPrefix = ((YangIfFeature) getCurrentEntityToResolveFromStack()).getPrefix();
} else if (getCurrentEntityToResolveFromStack() instanceof YangLeafRef) {
refPrefix = refPrefixForLeafRef();
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangBase) {
+ refPrefix = ((YangBase) getCurrentEntityToResolveFromStack()).getBaseIdentifier().getPrefix();
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangIdentityRef) {
+ refPrefix = ((YangIdentityRef) getCurrentEntityToResolveFromStack()).getPrefix();
} else {
- throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses");
+ throw new DataModelException("Data Model Exception: Entity to resolved is other than " +
+ "type/uses/base/identityref");
}
return refPrefix;
}
@@ -1392,6 +1615,10 @@
errorInfo = GROUPING_LINKER_ERROR;
} else if (getCurrentEntityToResolveFromStack() instanceof YangIfFeature) {
errorInfo = FEATURE_LINKER_ERROR;
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangBase) {
+ errorInfo = BASE_LINKER_ERROR;
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangIdentityRef) {
+ errorInfo = IDENTITYREF_LINKER_ERROR;
} else {
errorInfo = LEAFREF_LINKER_ERROR;
}
@@ -1449,7 +1676,12 @@
setCurReferenceResolver((YangReferenceResolver) yangInclude.getIncludedNode());
return referredNode;
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangBase) {
+ linkedNode = findRefIdentity(yangInclude.getIncludedNode());
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangIdentityRef) {
+ linkedNode = findRefIdentityRef(yangInclude.getIncludedNode());
}
+
if (linkedNode != null) {
// Add the link to external entity.
addReferredEntityLink(linkedNode, INTER_FILE_LINKED);
@@ -1502,6 +1734,10 @@
setCurReferenceResolver((YangReferenceResolver) yangImport.getImportedNode());
return referredNode;
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangBase) {
+ linkedNode = findRefIdentity(yangImport.getImportedNode());
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangIdentityRef) {
+ linkedNode = findRefIdentityRef(yangImport.getImportedNode());
}
if (linkedNode != null) {
// Add the link to external entity.
@@ -1584,8 +1820,13 @@
return (T) ((YangIfFeature) getCurrentEntityToResolveFromStack()).getReferredFeatureHolder();
} else if (getCurrentEntityToResolveFromStack() instanceof YangLeafRef) {
return (T) ((YangLeafRef) getCurrentEntityToResolveFromStack()).getReferredLeafOrLeafList();
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangBase) {
+ return (T) ((YangBase) getCurrentEntityToResolveFromStack()).getReferredIdentity();
+ } else if (getCurrentEntityToResolveFromStack() instanceof YangIdentityRef) {
+ return (T) ((YangIdentityRef) getCurrentEntityToResolveFromStack()).getReferredIdentity();
} else {
- throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses");
+ throw new DataModelException("Data Model Exception: Entity to resolved is other than type" +
+ "/uses/base/identityref");
}
}
@@ -1651,4 +1892,45 @@
}
return null;
}
+
+ /**
+ * Finds the referred identity node at the root level of imported/included node.
+ *
+ * @param refNode module/sub-module node
+ * @return referred identity
+ */
+ private YangNode findRefIdentity(YangNode refNode) {
+ YangNode tmpNode = refNode.getChild();
+ while (tmpNode != null) {
+ if (tmpNode instanceof YangIdentity) {
+ if (tmpNode.getName()
+ .equals(((YangBase) getCurrentEntityToResolveFromStack()).getBaseIdentifier().getName())) {
+ return tmpNode;
+ }
+ }
+ tmpNode = tmpNode.getNextSibling();
+ }
+ return null;
+ }
+
+ /**
+ * Finds the referred identity node at the root level of imported/included node.
+ *
+ * @param refNode module/sub-module node
+ * @return referred identity
+ */
+ private YangNode findRefIdentityRef(YangNode refNode) {
+ YangNode tmpNode = refNode.getChild();
+ while (tmpNode != null) {
+ if (tmpNode instanceof YangIdentity) {
+ if (tmpNode.getName()
+ .equals(((YangIdentityRef) getCurrentEntityToResolveFromStack())
+ .getBaseIdentity().getName())) {
+ return tmpNode;
+ }
+ }
+ tmpNode = tmpNode.getNextSibling();
+ }
+ return null;
+ }
}
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/TreeWalkListener.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/TreeWalkListener.java
index 443c071..a1daf0f 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/TreeWalkListener.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/TreeWalkListener.java
@@ -28,6 +28,7 @@
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.impl.listeners.AugmentListener;
import org.onosproject.yangutils.parser.impl.listeners.BaseFileListener;
+import org.onosproject.yangutils.parser.impl.listeners.BaseListener;
import org.onosproject.yangutils.parser.impl.listeners.BelongsToListener;
import org.onosproject.yangutils.parser.impl.listeners.BitListener;
import org.onosproject.yangutils.parser.impl.listeners.BitsListener;
@@ -42,7 +43,9 @@
import org.onosproject.yangutils.parser.impl.listeners.EnumerationListener;
import org.onosproject.yangutils.parser.impl.listeners.FeatureListener;
import org.onosproject.yangutils.parser.impl.listeners.GroupingListener;
+import org.onosproject.yangutils.parser.impl.listeners.IdentityrefListener;
import org.onosproject.yangutils.parser.impl.listeners.IfFeatureListener;
+import org.onosproject.yangutils.parser.impl.listeners.IdentityListener;
import org.onosproject.yangutils.parser.impl.listeners.ImportListener;
import org.onosproject.yangutils.parser.impl.listeners.IncludeListener;
import org.onosproject.yangutils.parser.impl.listeners.InputListener;
@@ -450,12 +453,12 @@
@Override
public void enterIdentityStatement(GeneratedYangParser.IdentityStatementContext ctx) {
- handleUnsupportedYangConstruct(YangConstructType.IDENTITY_DATA, ctx, CURRENTLY_UNSUPPORTED);
+ IdentityListener.processIdentityEntry(this, ctx);
}
@Override
public void exitIdentityStatement(GeneratedYangParser.IdentityStatementContext ctx) {
- // do nothing.
+ IdentityListener.processIdentityExit(this, ctx);
}
@Override
@@ -470,7 +473,7 @@
@Override
public void enterBaseStatement(GeneratedYangParser.BaseStatementContext ctx) {
- handleUnsupportedYangConstruct(YangConstructType.BASE_DATA, ctx, CURRENTLY_UNSUPPORTED);
+ BaseListener.processBaseEntry(this, ctx);
}
@Override
@@ -710,12 +713,12 @@
@Override
public void enterIdentityrefSpecification(GeneratedYangParser.IdentityrefSpecificationContext ctx) {
- // do nothing.
+ IdentityrefListener.processIdentityrefEntry(this, ctx);
}
@Override
public void exitIdentityrefSpecification(GeneratedYangParser.IdentityrefSpecificationContext ctx) {
- // do nothing.
+ IdentityrefListener.processIdentityrefExit(this, ctx);
}
@Override
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/BaseListener.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/BaseListener.java
new file mode 100644
index 0000000..1925375
--- /dev/null
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/BaseListener.java
@@ -0,0 +1,116 @@
+/*
+ * 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.parser.impl.listeners;
+
+import org.onosproject.yangutils.datamodel.YangBase;
+import org.onosproject.yangutils.datamodel.YangIdentity;
+import org.onosproject.yangutils.datamodel.YangIdentityRef;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl;
+import org.onosproject.yangutils.datamodel.utils.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.utils.DataModelUtils.addResolutionInfo;
+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.ListenerErrorType.*;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+import static org.onosproject.yangutils.datamodel.utils.YangConstructType.BASE_DATA;
+
+/**
+ * base-stmt = base-keyword sep identifier-ref-arg-str
+ * optsep stmtend*
+ * identifier-ref-arg = [prefix ":"] identifier
+ */
+
+/**
+ * Represents listener based call back function corresponding to the "base"
+ * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
+ */
+public final class BaseListener {
+
+ //Creates a new base listener.
+ private BaseListener() {
+ }
+
+ /**
+ * Performs validation and updates the data model tree when parser receives an
+ * input matching the grammar rule (base).
+ *
+ * @param listener listener's object
+ * @param ctx context object of the grammar rule
+ */
+ public static void processBaseEntry(TreeWalkListener listener,
+ GeneratedYangParser.BaseStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, BASE_DATA, ctx.string().getText(), ENTRY);
+
+ YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), BASE_DATA, ctx);
+
+ Parsable tmpData = listener.getParsedDataStack().peek();
+
+ /**
+ * For identityref base node identifier is copied in identity listener itself, so no need to process
+ * base statement for indentityref
+ */
+ if (tmpData instanceof YangIdentityRef) {
+ return;
+ }
+
+ if (!(tmpData instanceof YangIdentity)) {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BASE_DATA,
+ ctx.string().getText(), ENTRY));
+ }
+
+ YangBase yangBase = new YangBase();
+ yangBase.setBaseIdentifier(nodeIdentifier);
+ ((YangIdentity) tmpData).setBaseNode(yangBase);
+
+ int errorLine = ctx.getStart().getLine();
+ int errorPosition = ctx.getStart().getCharPositionInLine();
+
+ // Add resolution information to the list
+ YangResolutionInfoImpl resolutionInfo =
+ new YangResolutionInfoImpl<YangBase>(yangBase, (YangNode) tmpData, errorLine, errorPosition);
+ addToResolutionList(resolutionInfo, ctx);
+ }
+
+ /**
+ * Add to resolution list.
+ *
+ * @param resolutionInfo resolution information
+ * @param ctx context object of the grammar rule
+ */
+ private static void addToResolutionList(YangResolutionInfoImpl<YangBase> resolutionInfo,
+ GeneratedYangParser.BaseStatementContext ctx) {
+
+ try {
+ addResolutionInfo(resolutionInfo);
+ } catch (DataModelException e) {
+ throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
+ BASE_DATA, ctx.string().getText(), EXIT, e.getMessage()));
+ }
+ }
+
+}
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IdentityListener.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IdentityListener.java
new file mode 100644
index 0000000..d8dec2c
--- /dev/null
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IdentityListener.java
@@ -0,0 +1,123 @@
+/*
+ * 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.parser.impl.listeners;
+
+import org.onosproject.yangutils.datamodel.YangIdentity;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangSubModule;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.datamodel.utils.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.utils.GeneratedLanguage.JAVA_GENERATION;
+import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangIdentityNode;
+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.ListenerErrorType.MISSING_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+import static org.onosproject.yangutils.datamodel.utils.YangConstructType.IDENTITY_DATA;
+
+/**
+ * Reference: RFC6020 and YANG ANTLR Grammar.
+ *
+ * ABNF grammar as per RFC6020
+ * identity-stmt = identity-keyword sep identifier-arg-str optsep
+ * (";" /
+ * "{" stmtsep
+ * ;; these stmts can appear in any order
+ * [base-stmt stmtsep]
+ * [status-stmt stmtsep]
+ * [description-stmt stmtsep]
+ * [reference-stmt stmtsep]
+ * "}")
+ */
+
+/**
+ * Represents listener based call back function corresponding to the "identity"
+ * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
+ */
+public final class IdentityListener {
+
+ //Creates a identity listener.
+ private IdentityListener() {
+ }
+
+ /**
+ * Performs validations and update the data model tree when parser receives an input
+ * matching the grammar rule (identity).
+ *
+ * @param listener Listener's object
+ * @param ctx context object of the grammar rule
+ */
+ public static void processIdentityEntry(TreeWalkListener listener,
+ GeneratedYangParser.IdentityStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, IDENTITY_DATA, ctx.identifier().getText(), ENTRY);
+
+ String identifier = getValidIdentifier(ctx.identifier().getText(), IDENTITY_DATA, ctx);
+
+ YangIdentity identity = getYangIdentityNode(JAVA_GENERATION);
+ identity.setName(identifier);
+
+ Parsable curData = listener.getParsedDataStack().peek();
+ if (curData instanceof YangModule || curData instanceof YangSubModule) {
+ YangNode curNode = (YangNode) curData;
+ try {
+ curNode.addChild(identity);
+ } catch (DataModelException e) {
+ throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
+ IDENTITY_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
+ }
+ // Push identity node to the stack.
+ listener.getParsedDataStack().push(identity);
+ } else {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITY_DATA,
+ ctx.identifier().getText(), ENTRY));
+ }
+
+ }
+
+ /**
+ * Performs validations and update the data model tree when parser exits from grammar
+ * rule (identity).
+ *
+ * @param listener Listener's object
+ * @param ctx context object of the grammar rule
+ */
+ public static void processIdentityExit(TreeWalkListener listener,
+ GeneratedYangParser.IdentityStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, IDENTITY_DATA, ctx.identifier().getText(), EXIT);
+
+ Parsable parsableType = listener.getParsedDataStack().pop();
+ if (!(parsableType instanceof YangIdentity)) {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITY_DATA,
+ ctx.identifier().getText(), EXIT));
+ }
+ }
+
+}
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IdentityrefListener.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IdentityrefListener.java
new file mode 100644
index 0000000..49037e2
--- /dev/null
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IdentityrefListener.java
@@ -0,0 +1,214 @@
+/*
+ * 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.parser.impl.listeners;
+
+import org.onosproject.yangutils.datamodel.YangIdentityRef;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
+import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.datamodel.utils.Parsable;
+import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
+import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl;
+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.utils.DataModelUtils.addResolutionInfo;
+import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.UNRESOLVED;
+import static org.onosproject.yangutils.datamodel.utils.YangConstructType.BASE_DATA;
+import static org.onosproject.yangutils.datamodel.utils.YangConstructType.IDENTITYREF_DATA;
+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.ListenerErrorType.MISSING_HOLDER;
+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.UNHANDLED_PARSED_DATA;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+
+/**
+ * Reference: RFC6020 and YANG ANTLR Grammar
+ *
+ * ABNF grammar as per RFC6020
+ * identityref-specification =
+ * base-stmt stmtsep
+ * base-stmt = base-keyword sep identifier-ref-arg-str
+ * optsep stmtend*
+ * identifier-ref-arg = [prefix ":"] identifier
+ */
+
+/**
+ * Represents listener based call back function corresponding to the "identityref"
+ * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
+ */
+public final class IdentityrefListener {
+
+ //Creates a new type listener.
+ private IdentityrefListener() {
+ }
+
+ /**
+ * Performs validation and updates the data model tree when parser receives an input
+ * matching the grammar rule (identityref).
+ *
+ * @param listener listener's object
+ * @param ctx context object of the grammar rule
+ */
+ public static void processIdentityrefEntry(TreeWalkListener listener,
+ GeneratedYangParser.IdentityrefSpecificationContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, IDENTITYREF_DATA, "", ENTRY);
+
+ if (listener.getParsedDataStack().peek() instanceof YangType) {
+
+ YangIdentityRef identityRef = new YangIdentityRef();
+ Parsable typeData = listener.getParsedDataStack().pop();
+ YangDataTypes yangDataTypes = ((YangType) typeData).getDataType();
+ YangResolutionInfoImpl resolutionInfo;
+
+ // Validate node identifier.
+ YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.baseStatement().string().getText(),
+ BASE_DATA, ctx);
+ identityRef.setBaseIdentity(nodeIdentifier);
+ ((YangType) typeData).setDataTypeExtendedInfo(identityRef);
+
+ int errorLine = ctx.getStart().getLine();
+ int errorPosition = ctx.getStart().getCharPositionInLine();
+
+ Parsable tmpData = listener.getParsedDataStack().peek();
+ switch (tmpData.getYangConstructType()) {
+ case LEAF_DATA:
+
+ // Pop the stack entry to obtain the parent YANG node.
+ Parsable leaf = listener.getParsedDataStack().pop();
+ Parsable parentNodeOfLeaf = listener.getParsedDataStack().peek();
+
+ // Push the popped entry back to the stack.
+ listener.getParsedDataStack().push(leaf);
+
+ // Verify parent node of leaf
+ if (!(parentNodeOfLeaf instanceof YangNode)) {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
+ IDENTITYREF_DATA, ctx.getText(), EXIT));
+ }
+
+ identityRef.setResolvableStatus(UNRESOLVED);
+
+ // Add resolution information to the list
+ resolutionInfo = new YangResolutionInfoImpl<YangIdentityRef>(identityRef,
+ (YangNode) parentNodeOfLeaf, errorLine, errorPosition);
+ addToResolutionList(resolutionInfo, ctx);
+
+ break;
+ case LEAF_LIST_DATA:
+
+ // Pop the stack entry to obtain the parent YANG node.
+ Parsable leafList = listener.getParsedDataStack().pop();
+ Parsable parentNodeOfLeafList = listener.getParsedDataStack().peek();
+
+ // Push the popped entry back to the stack.
+ listener.getParsedDataStack().push(leafList);
+
+ // Verify parent node of leaf
+ if (!(parentNodeOfLeafList instanceof YangNode)) {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
+ IDENTITYREF_DATA, ctx.getText(), EXIT));
+ }
+
+ identityRef.setResolvableStatus(UNRESOLVED);
+
+ // Add resolution information to the list
+ resolutionInfo = new YangResolutionInfoImpl<YangIdentityRef>(identityRef,
+ (YangNode) parentNodeOfLeafList, errorLine, errorPosition);
+ addToResolutionList(resolutionInfo, ctx);
+ break;
+ case UNION_DATA:
+
+ Parsable parentNodeOfUnionNode = listener.getParsedDataStack().peek();
+
+ // Verify parent node of leaf
+ if (!(parentNodeOfUnionNode instanceof YangNode)) {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
+ IDENTITYREF_DATA, ctx.getText(), EXIT));
+ }
+
+ identityRef.setResolvableStatus(UNRESOLVED);
+
+ // Add resolution information to the list
+ resolutionInfo = new YangResolutionInfoImpl<YangIdentityRef>(identityRef,
+ (YangNode) parentNodeOfUnionNode, errorLine, errorPosition);
+ addToResolutionList(resolutionInfo, ctx);
+
+ break;
+ case TYPEDEF_DATA:
+ /**
+ * Do not add the identity ref to resolution list. It needs to be
+ * added to resolution list, when leaf/leaf list references to
+ * this typedef. At this time that leaf/leaf-list becomes the
+ * parent for the identityref.
+ */
+ break;
+ default:
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITYREF_DATA,
+ ctx.getText(), EXIT));
+ }
+ listener.getParsedDataStack().push(typeData);
+ listener.getParsedDataStack().push(identityRef);
+ } else {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITYREF_DATA, "", ENTRY));
+ }
+ }
+
+ /**
+ * Performs validations and update the data model tree when parser exits from grammar
+ * rule (identityref).
+ *
+ * @param listener Listener's object
+ * @param ctx context object of the grammar rule
+ */
+ public static void processIdentityrefExit(TreeWalkListener listener,
+ GeneratedYangParser.IdentityrefSpecificationContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, IDENTITYREF_DATA, ctx.getText(), EXIT);
+
+ Parsable parsableType = listener.getParsedDataStack().pop();
+ if (!(parsableType instanceof YangIdentityRef)) {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITYREF_DATA,
+ ctx.getText(), EXIT));
+ }
+ }
+
+ /**
+ * Adds to resolution list.
+ *
+ * @param resolutionInfo resolution information
+ * @param ctx context object of the grammar rule
+ */
+ private static void addToResolutionList(YangResolutionInfoImpl<YangIdentityRef> resolutionInfo,
+ GeneratedYangParser.IdentityrefSpecificationContext ctx) {
+ try {
+ addResolutionInfo(resolutionInfo);
+ } catch (DataModelException e) {
+ throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
+ IDENTITYREF_DATA, ctx.getText(), ENTRY, e.getMessage()));
+ }
+ }
+}
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java
index ebf9d97..5b50ff9 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java
@@ -130,6 +130,10 @@
.peek()).resolveSelfFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
((YangReferenceResolver) listener.getParsedDataStack()
.peek()).resolveSelfFileLinking(ResolvableType.YANG_LEAFREF);
+ ((YangReferenceResolver) listener.getParsedDataStack()
+ .peek()).resolveSelfFileLinking(ResolvableType.YANG_BASE);
+ ((YangReferenceResolver) listener.getParsedDataStack()
+ .peek()).resolveSelfFileLinking(ResolvableType.YANG_IDENTITYREF);
} catch (DataModelException e) {
LinkerException linkerException = new LinkerException(e.getMessage());
linkerException.setLine(e.getLineNumber());
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/SubModuleListener.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/SubModuleListener.java
index 2132324..d2c4f48 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/SubModuleListener.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/SubModuleListener.java
@@ -135,6 +135,10 @@
.resolveSelfFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
((YangReferenceResolver) listener.getParsedDataStack().peek())
.resolveSelfFileLinking(ResolvableType.YANG_LEAFREF);
+ ((YangReferenceResolver) listener.getParsedDataStack().peek())
+ .resolveSelfFileLinking(ResolvableType.YANG_BASE);
+ ((YangReferenceResolver) listener.getParsedDataStack().peek())
+ .resolveSelfFileLinking(ResolvableType.YANG_IDENTITYREF);
} catch (DataModelException e) {
LinkerException linkerException = new LinkerException(e.getMessage());
linkerException.setLine(e.getLineNumber());
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java
index 32955d5..e108d14 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java
@@ -310,7 +310,11 @@
parserException = new ParserException("YANG file error : a type leafref" +
" must have one path statement.");
break;
- // TODO : decimal64, identity ref
+ case IDENTITYREF:
+ parserException = new ParserException("YANG file error : a type identityref" +
+ " must have base statement.");
+ break;
+ // TODO : decimal64,
default:
return;
}
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/parserutils/ListenerUtil.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/parserutils/ListenerUtil.java
index 2aaa563..48ac0de 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/parserutils/ListenerUtil.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/parserutils/ListenerUtil.java
@@ -48,10 +48,8 @@
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
import static org.onosproject.yangutils.utils.UtilConstants.COLON;
import static org.onosproject.yangutils.utils.UtilConstants.CURRENT;
-import static org.onosproject.yangutils.utils.UtilConstants.CURRENTLY_UNSUPPORTED;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.FALSE;
-import static org.onosproject.yangutils.utils.UtilConstants.IDENTITYREF;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_SQUARE_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
@@ -341,7 +339,6 @@
String[] tmpData = tmpIdentifierString.split(Pattern.quote(COLON));
if (tmpData.length == 1) {
YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier();
- checkForUnsupportedTypes(tmpData[0], yangConstruct, ctx);
nodeIdentifier.setName(getValidIdentifier(tmpData[0], yangConstruct, ctx));
return nodeIdentifier;
} else if (tmpData.length == 2) {
@@ -375,7 +372,6 @@
String[] tmpData = tmpIdentifierString.split(Pattern.quote(COLON));
if (tmpData.length == 1) {
YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier();
- checkForUnsupportedTypes(tmpData[0], yangConstruct, ctx);
nodeIdentifier.setName(getValidIdentifierForLeafref(tmpData[0], yangConstruct, ctx, yangLeafRef));
return nodeIdentifier;
} else if (tmpData.length == 2) {
@@ -706,24 +702,6 @@
}
/**
- * Checks whether the type is an unsupported type.
- *
- * @param typeName name of the type
- * @param yangConstruct yang construct to check if it is type
- * @param ctx yang construct's context to get the line number and character position
- */
- private static void checkForUnsupportedTypes(String typeName,
- YangConstructType yangConstruct, ParserRuleContext ctx) {
-
- if (yangConstruct == YangConstructType.TYPE_DATA) {
- if (typeName.equalsIgnoreCase(IDENTITYREF)) {
- handleUnsupportedYangConstruct(YangConstructType.IDENTITYREF_DATA,
- ctx, CURRENTLY_UNSUPPORTED);
- }
- }
- }
-
- /**
* Checks and return valid absolute schema node id.
*
* @param argumentString string from yang file
diff --git a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedJavaFileType.java b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedJavaFileType.java
index d4666c0..1a2b9b8 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedJavaFileType.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedJavaFileType.java
@@ -89,6 +89,11 @@
public static final int GENERATE_EVENT_SUBJECT_CLASS = 1024;
/**
+ * Identity listener class.
+ */
+ public static final int GENERATE_IDENTITY_CLASS = 2048;
+
+ /**
* Creates an instance of generate java file type.
*/
private GeneratedJavaFileType() {
diff --git a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/YangDataModelFactory.java b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/YangDataModelFactory.java
index 4d68fe2..9ba4068 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/YangDataModelFactory.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/YangDataModelFactory.java
@@ -20,6 +20,7 @@
import org.onosproject.yangutils.datamodel.YangChoice;
import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.YangGrouping;
+import org.onosproject.yangutils.datamodel.YangIdentity;
import org.onosproject.yangutils.datamodel.YangInput;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
@@ -41,6 +42,7 @@
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaContainer;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGrouping;
+import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaIdentity;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaInput;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaLeaf;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaLeafList;
@@ -163,6 +165,24 @@
* generated
* @return the corresponding inherited node based on the target language
*/
+ public static YangIdentity getYangIdentityNode(GeneratedLanguage targetLanguage) {
+ switch (targetLanguage) {
+ case JAVA_GENERATION: {
+ return new YangJavaIdentity();
+ }
+ default: {
+ throw new TranslatorException("Only YANG to Java is supported.");
+ }
+ }
+ }
+
+ /**
+ * Returns based on the target language generate the inherited data model node.
+ *
+ * @param targetLanguage target language in which YANG mapping needs to be
+ * generated
+ * @return the corresponding inherited node based on the target language
+ */
public static YangGrouping getYangGroupingNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
diff --git a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java
index 6df66af..52595c7 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java
@@ -20,7 +20,9 @@
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangEnumeration;
+import org.onosproject.yangutils.datamodel.YangIdentity;
import org.onosproject.yangutils.datamodel.YangLeafRef;
+import org.onosproject.yangutils.datamodel.YangIdentityRef;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
@@ -161,8 +163,10 @@
YangType<?> referredType = getReferredTypeFromLeafref(yangType);
return getJavaImportClass(referredType, isListAttr, pluginConfig);
case IDENTITYREF:
- //TODO:IDENTITYREF
- break;
+ YangIdentityRef identityRef = (YangIdentityRef) yangType.getDataTypeExtendedInfo();
+ YangIdentity identity = identityRef.getReferredIdentity();
+ return getCapitalCase(getCamelCase(((YangJavaIdentity) identity).
+ getName(), pluginConfig));
case EMPTY:
return BOOLEAN_WRAPPER;
case UNION:
@@ -196,8 +200,9 @@
YangType<?> referredType = getReferredTypeFromLeafref(yangType);
return getJavaImportClass(referredType, isListAttr, pluginConfig);
case IDENTITYREF:
- //TODO:IDENTITYREF
- break;
+ YangIdentityRef identityRef = (YangIdentityRef) yangType.getDataTypeExtendedInfo();
+ YangIdentity identity = identityRef.getReferredIdentity();
+ return getCapitalCase(getCamelCase(((YangJavaIdentity) identity).getName(), pluginConfig));
case EMPTY:
return BOOLEAN_DATA_TYPE;
case UNION:
@@ -212,7 +217,6 @@
return null;
}
}
- return null;
}
/**
@@ -253,8 +257,7 @@
YangType<?> referredType = getReferredTypeFromLeafref(yangType);
return getJavaImportPackage(referredType, isListAttr, conflictResolver);
case IDENTITYREF:
- //TODO:IDENTITYREF
- break;
+ return getIdentityRefPackage(yangType, conflictResolver);
case UNION:
return getUnionPackage(yangType, conflictResolver);
case INSTANCE_IDENTIFIER:
@@ -280,8 +283,7 @@
YangType<?> referredType = getReferredTypeFromLeafref(yangType);
return getJavaImportPackage(referredType, isListAttr, conflictResolver);
case IDENTITYREF:
- //TODO:IDENTITYREF
- break;
+ return getIdentityRefPackage(yangType, conflictResolver);
case EMPTY:
return JAVA_LANG;
case UNION:
@@ -294,7 +296,6 @@
return null;
}
}
- return null;
}
/**
@@ -361,6 +362,25 @@
}
/**
+ * Returns YANG identity's java package.
+ *
+ * @param type YANG type
+ * @param conflictResolver object of YANG to java naming conflict util
+ * @return YANG identity's java package
+ */
+ private static String getIdentityRefPackage(YangType<?> type, YangToJavaNamingConflictUtil conflictResolver) {
+
+ if (!(type.getDataTypeExtendedInfo() instanceof YangIdentityRef)) {
+ throw new TranslatorException("type should have been identityref.");
+ }
+ YangIdentityRef identityRef = (YangIdentityRef) type.getDataTypeExtendedInfo();
+ YangJavaIdentity identity = (YangJavaIdentity) (identityRef.getReferredIdentity());
+ if (identity.getJavaFileInfo().getPackage() == null) {
+ return getPackageFromParent(identity.getParent(), conflictResolver);
+ }
+ return identity.getJavaFileInfo().getPackage();
+ }
+ /**
* Returns package from parent node.
*
* @param parent parent YANG node
diff --git a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaIdentity.java b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaIdentity.java
new file mode 100644
index 0000000..ec0e637
--- /dev/null
+++ b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaIdentity.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.yangutils.translator.tojava.javamodel;
+
+import org.onosproject.yangutils.datamodel.YangIdentity;
+import org.onosproject.yangutils.translator.exception.TranslatorException;
+import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
+import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
+import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.JavaImportData;
+import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
+import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
+import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_IDENTITY_CLASS;
+import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.updatePackageInfo;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.initiateJavaFileGeneration;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
+import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
+
+/**
+ * Represents input information extended to support java code generation.
+ */
+public class YangJavaIdentity extends YangIdentity
+ implements JavaCodeGeneratorInfo, JavaCodeGenerator {
+
+ //File type extension for java classes.
+ private static final String JAVA_FILE_EXTENSION = ".java";
+
+
+ //Contains the information of the java file being generated.
+ private JavaFileInfo javaFileInfo;
+
+ //Contains the information of the importd.
+ private transient JavaImportData importData;
+
+ /**
+ * File handle to maintain temporary java code fragments as per the code
+ * snippet types.
+ */
+ private TempJavaCodeFragmentFiles tempFileHandle;
+
+ /**
+ * Creates YANG java container object.
+ */
+ public YangJavaIdentity() {
+ setJavaFileInfo(new JavaFileInfo());
+ getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
+ importData = new JavaImportData();
+ }
+
+ /**
+ * Returns the generated java file information.
+ *
+ * @return generated java file information
+ */
+ @Override
+ public JavaFileInfo getJavaFileInfo() {
+ if (javaFileInfo == null) {
+ throw new TranslatorException("Missing java info in java datamodel node");
+ }
+ return javaFileInfo;
+ }
+
+ /**
+ * Sets the java file info object.
+ *
+ * @param javaInfo java file info object
+ */
+ @Override
+ public void setJavaFileInfo(JavaFileInfo javaInfo) {
+ javaFileInfo = javaInfo;
+ }
+
+ /**
+ * Returns the temporary file handle.
+ *
+ * @return temporary file handle
+ */
+ @Override
+ public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
+ return tempFileHandle;
+ }
+
+ /**
+ * Sets temporary file handle.
+ *
+ * @param fileHandle temporary file handle
+ */
+ @Override
+ public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
+ tempFileHandle = fileHandle;
+ }
+
+ /**
+ * Prepare the information for java code generation corresponding to YANG
+ * container info.
+ *
+ * @param yangPlugin YANG plugin config
+ * @throws TranslatorException translator operation fail
+ */
+ @Override
+ public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
+ try {
+ updatePackageInfo(this, yangPlugin);
+ JavaQualifiedTypeInfo basePkgInfo = new JavaQualifiedTypeInfo();
+ String className = getCapitalCase(getJavaFileInfo().getJavaName());
+ String path = getJavaFileInfo().getPackageFilePath();
+ createPackage(this);
+ List<String> imports = null;
+ boolean isQualified = false;
+
+ if (getBaseNode() != null && getBaseNode().getReferredIdentity() != null) {
+ if (!(getBaseNode().getReferredIdentity() instanceof YangJavaIdentity)) {
+ throw new TranslatorException("Failed to prepare generate code entry for base node");
+ }
+ YangJavaIdentity baseIdentity = (YangJavaIdentity) getBaseNode().getReferredIdentity();
+ String baseClassName = getCapitalCase(baseIdentity.getJavaFileInfo().getJavaName());
+ String basePkg = baseIdentity.getJavaFileInfo().getPackage();
+ basePkgInfo.setClassInfo(baseClassName);
+ basePkgInfo.setPkgInfo(basePkg);
+ isQualified = importData.addImportInfo(basePkgInfo, className, getJavaFileInfo().getPackage());
+ if (!isQualified) {
+ imports = importData.getImports();
+ }
+ }
+
+ File file = getFileObject(path, className, JAVA_FILE_EXTENSION, getJavaFileInfo());
+
+ initiateJavaFileGeneration(file, GENERATE_IDENTITY_CLASS, imports, this, className);
+ closeFile(file, false);
+ } catch (IOException e) {
+ throw new TranslatorException(
+ "Failed to prepare generate code entry for identity node " + this.getName());
+ }
+ }
+
+ /**
+ * Create a java file using the YANG container info.
+ *
+ * @throws TranslatorException translator operation fail
+ */
+ @Override
+ public void generateCodeExit() throws TranslatorException {
+ /* Do nothing, file is already generated in entry*/
+ }
+}
+
diff --git a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
index 69f6d3e..ab8134f 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
@@ -16,10 +16,13 @@
package org.onosproject.yangutils.translator.tojava.utils;
+import org.onosproject.yangutils.datamodel.YangIdentity;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNotification;
+import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
+import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaIdentity;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
@@ -27,6 +30,7 @@
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_IDENTITY_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
@@ -57,6 +61,8 @@
import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.SUBJECT;
+import static org.onosproject.yangutils.utils.UtilConstants.ABSTRACT;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
/**
@@ -128,6 +134,8 @@
return getEventListenerDefinition(yangName);
case GENERATE_EVENT_SUBJECT_CLASS:
return getClassDefinition(yangName);
+ case GENERATE_IDENTITY_CLASS:
+ return getIdentityClassDefinition(yangName, curNode);
default:
return null;
}
@@ -214,6 +222,32 @@
}
/**
+ * Returns implementation file identity class definition.
+ *
+ * @param yangName file name
+ * @return identity class definition
+ */
+ private static String getIdentityClassDefinition(String yangName, YangNode curNode) {
+ if (!(curNode instanceof YangJavaIdentity)) {
+ throw new TranslatorException("Expected java identity instance node");
+ }
+ YangJavaIdentity identity = (YangJavaIdentity) curNode;
+ if (identity.getBaseNode() != null) {
+ YangIdentity baseIdentity = identity.getBaseNode().getReferredIdentity();
+ if (!(baseIdentity instanceof YangJavaIdentity)) {
+ throw new TranslatorException("Expected java identity instance node");
+ }
+
+ YangJavaIdentity baseJavaIdentity = (YangJavaIdentity) baseIdentity;
+ return PUBLIC + SPACE + ABSTRACT + SPACE + CLASS + SPACE + yangName + SPACE + EXTEND + SPACE
+ + getCapitalCase(baseJavaIdentity.getJavaFileInfo().getJavaName()) + SPACE +
+ OPEN_CURLY_BRACKET + NEW_LINE;
+ }
+
+ return PUBLIC + SPACE + ABSTRACT + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
+ }
+
+ /**
* Returns type file class definition.
*
* @param yangName file name
diff --git a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
index fc24ff9..4a2b6f5 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
@@ -42,6 +42,7 @@
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_IDENTITY_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
@@ -68,6 +69,7 @@
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getJavaPackageFromPackagePath;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getSmallCase;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
+import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.COMPONENT_ANNOTATION;
import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
@@ -294,6 +296,7 @@
}
file.createNewFile();
+
appendContents(file, genType, imports, curNode, className);
} catch (IOException e) {
throw new IOException("Failed to create " + file.getName() + " class file.");
@@ -348,6 +351,11 @@
appendHeaderContents(file, pkgString, importsList);
write(file, genType, EVENT_SUBJECT_CLASS, curNode, className);
break;
+ case GENERATE_IDENTITY_CLASS:
+ appendHeaderContents(file, pkgString, importsList);
+ write(file, genType, EVENT_SUBJECT_CLASS, curNode, className);
+ insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET);
+ break;
default:
break;
}
diff --git a/plugin/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java b/plugin/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
index 032493c..d56a2ed 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
@@ -647,6 +647,11 @@
public static final String PUBLIC = "public";
/**
+ * Static attribute for abstract modifier.
+ */
+ public static final String ABSTRACT = "abstract";
+
+ /**
* Static attribute for protected modifier.
*/
public static final String PROTECTED = "protected";
@@ -1196,6 +1201,18 @@
+ "leaf/leaf-list for given leafref";
/**
+ * Static attribute for base linker error information.
+ */
+ public static final String BASE_LINKER_ERROR = "YANG file error: Unable to find base "
+ + "identity for given base";
+
+ /**
+ * Static attribute for identityref linker error information.
+ */
+ public static final String IDENTITYREF_LINKER_ERROR = "YANG file error: Unable to find base "
+ + "identity for given base";
+
+ /**
* Static attribute for reference.
*/
public static final String REFERENCE = "Reference";