Shankara-Huawei
Committed by Gerrit Code Review

[ONOS-4753] Identity/identityref implementation and UT

Change-Id: I40148fa228465555be3bdf410cc294ffc0f34c18
Showing 56 changed files with 1613 additions and 76 deletions
......@@ -39,5 +39,15 @@ public enum ResolvableType {
/**
* Identifies the leafref.
*/
YANG_LEAFREF
YANG_LEAFREF,
/**
* Identifies the base.
*/
YANG_BASE,
/**
* Identifies the identityref.
*/
YANG_IDENTITYREF
}
......
/*
* 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;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
import java.io.Serializable;
/**
* Reference RFC 6020.
*
* Represents data model node to maintain information defined in YANG base.
* The "base" statement, which is optional, takes as an argument a
* string that is the name of an existing identity, from which the new
* identity is derived. If no "base" statement is present, the identity
* is defined from scratch.
*
* If a prefix is present on the base name, it refers to an identity
* defined in the module that was imported with that prefix, or the
* local module if the prefix matches the local module's prefix.
* Otherwise, an identity with the matching name MUST be defined in the
* current module or an included submodule.
*/
/**
* Represents data model node to maintain information defined in YANG base.
*/
public class YangBase implements Resolvable, Serializable {
private static final long serialVersionUID = 806201693L;
// YANG node identifier.
private YangNodeIdentifier baseIdentifier;
// Referred identity parent information.
private YangIdentity referredIdentity;
/**
* Status of resolution. If completely resolved enum value is "RESOLVED",
* if not enum value is "UNRESOLVED", in case reference of grouping/typedef/base/identityref
* is added to uses/type/base/identityref but it's not resolved value of enum should be
* "INTRA_FILE_RESOLVED".
*/
private ResolvableStatus resolvableStatus;
// Creates a base type of node.
public YangBase() {
resolvableStatus = ResolvableStatus.UNRESOLVED;
}
/**
* Returns the YANG node identifier.
*
* @return the YANG node identifier
*/
public YangNodeIdentifier getBaseIdentifier() {
return baseIdentifier;
}
/**
* Sets the YANG node identifier.
*
* @param baseIdentifier the YANG node identifier to set
*/
public void setBaseIdentifier(YangNodeIdentifier baseIdentifier) {
this.baseIdentifier = baseIdentifier;
}
/**
* Returns the parent identity node.
*
* @return the parent identity node
*/
public YangIdentity getReferredIdentity() {
return referredIdentity;
}
/**
* Sets the parent identity node.
*
* @param referredIdentity the parent identity node to set
*/
public void setReferredIdentity(YangIdentity referredIdentity) {
this.referredIdentity = referredIdentity;
}
@Override
public ResolvableStatus getResolvableStatus() {
return resolvableStatus;
}
@Override
public void setResolvableStatus(ResolvableStatus resolvableStatus) {
this.resolvableStatus = resolvableStatus;
}
@Override
public void resolve() throws DataModelException {
}
}
......@@ -334,7 +334,7 @@ public class YangDerivedInfo<T>
return RESOLVED;
}
}
} else if (baseType.getDataType() == LEAFREF) {
} else if ((baseType.getDataType() == LEAFREF) || (baseType.getDataType() == IDENTITYREF)) {
setEffectiveBuiltInType(baseType.getDataType());
return RESOLVED;
} else {
......
/*
* 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;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.utils.Parsable;
import org.onosproject.yangutils.datamodel.utils.YangConstructType;
import java.io.Serializable;
/*-
* Reference RFC 6020.
*
* The "identity" statement is used to define a new globally unique,
* abstract, and untyped identity. Its only purpose is to denote its
* name, semantics, and existence. An identity can either be defined
* from scratch or derived from a base identity. The identity's
* argument is an identifier that is the name of the identity. It is
* followed by a block of substatements that holds detailed identity
* information.
*
* The identity's Substatements
*
* +--------------+---------+-------------+-----------------------+
* | substatement | section | cardinality | data model mapping |
* +--------------+---------+-------------+-----------------------+
* | base | 7.16.2 | 0..1 | -YangNodeIdentifier |
* | description | 7.19.3 | 0..1 | -string |
* | reference | 7.19.4 | 0..1 | -string |
* | status | 7.19.2 | 0..1 | -YangStatus |
* +--------------+---------+-------------+-----------------------+
*/
/**
* Represents data model node to maintain information defined in YANG identity.
*/
public class YangIdentity extends YangNode implements YangCommonInfo, Parsable, Serializable {
private static final long serialVersionUID = 806201691L;
//Name of the identity.
private String name;
//Base node of identity.
private YangBase baseNode;
//Status of YANG identity.
private YangStatusType status;
//Description of YANG identity.
private String description;
//YANG reference of the identity.
private String reference;
//Creates a identity type of node.
public YangIdentity() {
super(YangNodeType.IDENTITY_NODE);
}
/**
* Returns the name of identity.
*
* @return the identity name
*/
public String getName() {
return name;
}
/**
* Sets the name of identity.
*
* @param name the identity name to set
*/
public void setName(String name) {
this.name = name;
}
@Override
public YangStatusType getStatus() {
return status;
}
@Override
public void setStatus(YangStatusType status) {
this.status = status;
}
@Override
public String getDescription() {
return description;
}
@Override
public void setDescription(String description) {
this.description = description;
}
@Override
public String getReference() {
return reference;
}
@Override
public void setReference(String reference) {
this.reference = reference;
}
@Override
public YangConstructType getYangConstructType() {
return YangConstructType.IDENTITY_DATA;
}
@Override
public void validateDataOnEntry() throws DataModelException {
}
@Override
public void validateDataOnExit() throws DataModelException {
}
/**
* Returns base node of identity.
*
* @return the base node of identity
*/
public YangBase getBaseNode() {
return baseNode;
}
/**
* Sets the base node.
*
* @param baseNode the base node to set
*/
public void setBaseNode(YangBase baseNode) {
this.baseNode = baseNode;
}
}
/*
* 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;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.utils.Parsable;
import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
import org.onosproject.yangutils.datamodel.utils.YangConstructType;
import java.io.Serializable;
/*-
* Reference RFC 6020.
*
* The identityref type is used to reference an existing identity.
*
* The identityref's base Statement :
* The "base" statement, which is a substatement to the "type"
* statement, MUST be present if the type is "identityref". The
* argument is the name of an identity, as defined by an "identity"
* statement. If a prefix is present on the identity name, it refers to
* an identity defined in the module that was imported with that prefix.
* Otherwise, an identity with the matching name MUST be defined in the
* current module or an included submodule.
* Valid values for an identityref are any identities derived from the
* identityref's base identity. On a particular server, the valid
* values are further restricted to the set of identities defined in the
* modules supported by the server.
*/
/**
* Represents data model node to maintain information defined in YANG identityref.
*/
public class YangIdentityRef extends YangNode implements Parsable, Resolvable, Serializable {
private static final long serialVersionUID = 806201692L;
// Get referred identity parent information.
private YangIdentity referredIdentity;
// YANG node identifier.
private YangNodeIdentifier baseIdentity;
/**
* Status of resolution. If completely resolved enum value is "RESOLVED",
* if not enum value is "UNRESOLVED", in case reference of grouping/typedef/identityref/base
* is added to uses/type/identityref/base but it's not resolved value of enum should be
* "INTRA_FILE_RESOLVED".
*/
private ResolvableStatus resolvableStatus;
// Creates a specific identityref of node.
public YangIdentityRef() {
super(YangNodeType.IDENTITYREF_NODE);
baseIdentity = new YangNodeIdentifier();
resolvableStatus = ResolvableStatus.UNRESOLVED;
}
@Override
public ResolvableStatus getResolvableStatus() {
return resolvableStatus;
}
@Override
public void setResolvableStatus(ResolvableStatus resolvableStatus) {
this.resolvableStatus = resolvableStatus;
}
@Override
public void resolve() throws DataModelException {
// Check if the derived info is present.
YangIdentity identity = getReferredIdentity();
if (identity == null) {
throw new DataModelException("Linker Error: Identity information is missing.");
}
while (identity.getBaseNode() != null) {
if (identity.getBaseNode().getResolvableStatus() != ResolvableStatus.RESOLVED) {
setResolvableStatus(ResolvableStatus.INTRA_FILE_RESOLVED);
return;
}
identity = identity.getBaseNode().getReferredIdentity();
}
}
/**
* Returns the YANG base node identifier.
*
* @return the YANG base node identifier
*/
public YangNodeIdentifier getBaseIdentity() {
return baseIdentity;
}
/**
* Sets the YANG node identifier.
*
* @param baseIdentity the YANG node identifier to set
*/
public void setBaseIdentity(YangNodeIdentifier baseIdentity) {
this.baseIdentity = baseIdentity;
}
/**
* Returns the name of identity.
*
* @return the identity name
*/
@Override
public String getName() {
return baseIdentity.getName();
}
/**
* Sets the name of identity.
*
* @param name the identity name to set
*/
@Override
public void setName(String name) {
baseIdentity.setName(name);
}
/**
* Sets node identifier.
*
* @param nodeIdentifier the node identifier
*/
public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
this.baseIdentity = nodeIdentifier;
}
/**
* Returns prefix associated with base.
*
* @return prefix associated with base
*/
public String getPrefix() {
return baseIdentity.getPrefix();
}
/**
* Sets prefix associated with base.
*
* @param prefix prefix associated with base
*/
public void setPrefix(String prefix) {
baseIdentity.setPrefix(prefix);
}
@Override
public YangConstructType getYangConstructType() {
return YangConstructType.IDENTITYREF_DATA;
}
@Override
public void validateDataOnEntry() throws DataModelException {
}
@Override
public void validateDataOnExit() throws DataModelException {
}
/**
* Returns the parent identity node.
*
* @return the parent identity node
*/
public YangIdentity getReferredIdentity() {
return referredIdentity;
}
/**
* Sets the parent identity node.
*
* @param referredIdentity the parent identity node to set
*/
public void setReferredIdentity(YangIdentity referredIdentity) {
this.referredIdentity = referredIdentity;
}
}
......@@ -216,6 +216,17 @@ public class YangModule
private List<YangResolutionInfo> leafrefResolutionList;
/**
* base resolution list.
*/
private List<YangResolutionInfo> baseResolutionList;
/**
* identityref resolution list.
*/
private List<YangResolutionInfo> identityrefResolutionList;
/**
* Creates a YANG node of module type.
*/
public YangModule() {
......@@ -225,6 +236,8 @@ public class YangModule
usesResolutionList = new LinkedList<>();
ifFeatureResolutionList = new LinkedList<>();
leafrefResolutionList = new LinkedList<>();
baseResolutionList = new LinkedList<>();
identityrefResolutionList = new LinkedList<>();
importList = new LinkedList<YangImport>();
includeList = new LinkedList<YangInclude>();
listOfLeaf = new LinkedList<YangLeaf>();
......@@ -597,8 +610,12 @@ public class YangModule
return usesResolutionList;
} else if (type == ResolvableType.YANG_IF_FEATURE) {
return ifFeatureResolutionList;
} else {
} else if (type == ResolvableType.YANG_LEAFREF) {
return leafrefResolutionList;
} else if (type == ResolvableType.YANG_BASE) {
return baseResolutionList;
} else {
return identityrefResolutionList;
}
}
......@@ -611,8 +628,12 @@ public class YangModule
usesResolutionList.add(resolutionInfo);
} else if (type == ResolvableType.YANG_IF_FEATURE) {
ifFeatureResolutionList.add(resolutionInfo);
} else {
} else if (type == ResolvableType.YANG_LEAFREF) {
leafrefResolutionList.add(resolutionInfo);
} else if (type == ResolvableType.YANG_BASE) {
baseResolutionList.add(resolutionInfo);
} else if (type == ResolvableType.YANG_IDENTITYREF) {
identityrefResolutionList.add(resolutionInfo);
}
}
......@@ -627,6 +648,10 @@ public class YangModule
ifFeatureResolutionList.add((YangResolutionInfo) resolutionList);
} else if (type == ResolvableType.YANG_LEAFREF) {
leafrefResolutionList = resolutionList;
} else if (type == ResolvableType.YANG_BASE) {
baseResolutionList = resolutionList;
} else if (type == ResolvableType.YANG_IDENTITYREF) {
identityrefResolutionList = resolutionList;
}
}
......@@ -650,11 +675,8 @@ public class YangModule
while (includeInfoIterator.hasNext()) {
YangInclude yangInclude = includeInfoIterator.next();
YangSubModule subModule = null;
try {
subModule = yangInclude.addReferenceToInclude(yangNodeSet);
} catch (DataModelException e) {
throw e;
}
subModule = yangInclude.addReferenceToInclude(yangNodeSet);
// Check if the referred sub-modules parent is self
if (!(subModule.getBelongsTo().getModuleNode() == this)) {
yangInclude.reportIncludeError();
......
......@@ -102,5 +102,15 @@ public enum YangNodeType {
/**
* Node contains "YANG's list" information.
*/
LIST_NODE
LIST_NODE,
/**
* Identity node.
*/
IDENTITY_NODE,
/**
* Identityref node.
*/
IDENTITYREF_NODE
}
......
......@@ -214,6 +214,16 @@ public class YangSubModule
private List<YangResolutionInfo> leafrefResolutionList;
/**
* base resolution list.
*/
private List<YangResolutionInfo> baseResolutionList;
/**
* identityref resolution list.
*/
private List<YangResolutionInfo> identityrefResolutionList;
/**
* Creates a sub module node.
*/
public YangSubModule() {
......@@ -222,6 +232,8 @@ public class YangSubModule
usesResolutionList = new LinkedList<>();
ifFeatureResolutionList = new LinkedList<>();
leafrefResolutionList = new LinkedList<>();
baseResolutionList = new LinkedList<>();
identityrefResolutionList = new LinkedList<>();
importList = new LinkedList<YangImport>();
includeList = new LinkedList<YangInclude>();
listOfLeaf = new LinkedList<YangLeaf>();
......@@ -559,8 +571,12 @@ public class YangSubModule
return usesResolutionList;
} else if (type == ResolvableType.YANG_IF_FEATURE) {
return ifFeatureResolutionList;
} else {
} else if (type == ResolvableType.YANG_LEAFREF) {
return leafrefResolutionList;
} else if (type == ResolvableType.YANG_BASE) {
return baseResolutionList;
} else {
return identityrefResolutionList;
}
}
......@@ -573,8 +589,12 @@ public class YangSubModule
usesResolutionList.add(resolutionInfo);
} else if (type == ResolvableType.YANG_IF_FEATURE) {
ifFeatureResolutionList.add(resolutionInfo);
} else {
} else if (type == ResolvableType.YANG_LEAFREF) {
leafrefResolutionList.add(resolutionInfo);
} else if (type == ResolvableType.YANG_BASE) {
baseResolutionList.add(resolutionInfo);
} else if (type == ResolvableType.YANG_IDENTITYREF) {
identityrefResolutionList.add(resolutionInfo);
}
}
......@@ -589,6 +609,10 @@ public class YangSubModule
ifFeatureResolutionList.add((YangResolutionInfo) resolutionList);
} else if (type == ResolvableType.YANG_LEAFREF) {
leafrefResolutionList = resolutionList;
} else if (type == ResolvableType.YANG_BASE) {
baseResolutionList = resolutionList;
} else if (type == ResolvableType.YANG_IDENTITYREF) {
identityrefResolutionList = resolutionList;
}
}
......
......@@ -22,6 +22,8 @@ import java.util.Set;
import org.onosproject.yangutils.datamodel.CollisionDetector;
import org.onosproject.yangutils.datamodel.ResolvableType;
import org.onosproject.yangutils.datamodel.YangIfFeature;
import org.onosproject.yangutils.datamodel.YangBase;
import org.onosproject.yangutils.datamodel.YangIdentityRef;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangLeafRef;
......@@ -176,6 +178,10 @@ public final class DataModelUtils {
.getEntityToResolve() instanceof YangLeafRef) {
resolutionNode.addToResolutionList(resolutionInfo,
ResolvableType.YANG_LEAFREF);
} else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangBase) {
resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_BASE);
} else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangIdentityRef) {
resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_IDENTITYREF);
}
}
......
......@@ -19,6 +19,8 @@ import java.io.Serializable;
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 @@ public class YangEntityToResolveInfoImpl<T> implements YangEntityToResolveInfo<T
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");
}
......
......@@ -158,11 +158,16 @@ public class YangLinkerManager
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();
......
......@@ -28,6 +28,7 @@ import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangListener;
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.EnumListener;
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 @@ public class TreeWalkListener implements GeneratedYangListener {
@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 @@ public class TreeWalkListener implements GeneratedYangListener {
@Override
public void enterBaseStatement(GeneratedYangParser.BaseStatementContext ctx) {
handleUnsupportedYangConstruct(YangConstructType.BASE_DATA, ctx, CURRENTLY_UNSUPPORTED);
BaseListener.processBaseEntry(this, ctx);
}
@Override
......@@ -710,12 +713,12 @@ public class TreeWalkListener implements GeneratedYangListener {
@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
......
/*
* 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()));
}
}
}
/*
* 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));
}
}
}
......@@ -130,6 +130,10 @@ public final class ModuleListener {
.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());
......
......@@ -135,6 +135,10 @@ public final class SubModuleListener {
.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());
......
......@@ -310,7 +310,11 @@ public final class TypeListener {
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;
}
......
......@@ -48,10 +48,8 @@ import static org.onosproject.yangutils.utils.UtilConstants.CHAR_OF_SLASH;
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 @@ public final class ListenerUtil {
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 @@ public final class ListenerUtil {
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 @@ public final class ListenerUtil {
}
/**
* 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
......
......@@ -89,6 +89,11 @@ public final class GeneratedJavaFileType {
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() {
......
......@@ -20,6 +20,7 @@ import org.onosproject.yangutils.datamodel.YangCase;
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.YangJavaChoice;
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 @@ public final class YangDataModelFactory {
* 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: {
......
......@@ -20,7 +20,9 @@ import java.util.Stack;
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 @@ public final class AttributesJavaDataType {
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 @@ public final class AttributesJavaDataType {
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 @@ public final class AttributesJavaDataType {
return null;
}
}
return null;
}
/**
......@@ -253,8 +257,7 @@ public final class AttributesJavaDataType {
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 @@ public final class AttributesJavaDataType {
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 @@ public final class AttributesJavaDataType {
return null;
}
}
return null;
}
/**
......@@ -361,6 +362,25 @@ public final class AttributesJavaDataType {
}
/**
* 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
......
/*
* 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*/
}
}
......@@ -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.
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.REGEX_FOR_ANY_STRING
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 @@ public final class ClassDefinitionGenerator {
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 @@ public final class ClassDefinitionGenerator {
}
/**
* 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
......
......@@ -42,6 +42,7 @@ import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.
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.translator.tojava.utils.ClassDefinitionG
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 @@ public final class JavaFileGeneratorUtils {
}
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 @@ public final class JavaFileGeneratorUtils {
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;
}
......
......@@ -647,6 +647,11 @@ public final class UtilConstants {
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 @@ public final class UtilConstants {
+ "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";
......
......@@ -122,20 +122,6 @@ public class TypeListenerTest {
}
/**
* Checks for unsupported type identityref.
*/
@Test
public void processIdentityrefType() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : \"identityref\" is not supported in current version,"
+ " please check wiki for YANG utils road map.");
YangNode node = manager
.getDataModel("src/test/resources/IdentityrefInvalidIdentifier.yang");
}
/**
* Checks for type instance-identifier.
*/
@Test
......
module IdentityInModule{
yang-version 1;
namespace http://huawei.com;
prefix IdentityInModule;
identity tunnel-type {
description
"Base identity from which specific tunnel types are derived.";
}
identity ref-address-family {
reference "http://www.iana.org/assignments/address-family-numbers/address-family-numbers.xhtml#address-family-numbers-2";
}
}
\ No newline at end of file
module IdentityIntraFile {
yang-version 1;
namespace http://huawei.com;
prefix IdentityIntraFile;
import "IdentityInModule" {
prefix "IdentityInModule";
}
identity ipv4-address-family {
base IdentityInModule:ref-address-family;
}
identity ipv6-address-family {
base IdentityInModule:ref-address-family;
}
leaf tunnel {
type identityref {
base IdentityInModule:ref-address-family;
}
}
}
\ No newline at end of file
module IdentityListener{
yang-version 1;
namespace http://huawei.com;
prefix IdentityListener;
identity tunnel {
description
"Base identity from which specific tunnel types are derived.";
}
identity tunnel-type {
description
"Base identity from which specific tunnel types are derived.";
}
identity ref-address-family {
reference "http://www.iana.org/assignments/address-family-numbers/address-family-numbers.xhtml#address-family-numbers-2";
}
identity ipv4-address-family {
base ref-address-family;
}
identity ipv6-address-family {
base ref-address-family;
}
leaf tunnel {
type identityref {
base ref-address-family;
}
}
leaf-list network-ref {
type identityref {
base ref-address-family;
}
}
}
\ No newline at end of file
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
identity tunnel {
description
"Base identity from which specific tunnel types are derived.";
}
leaf tunnel-value {
type type15;
}
typedef type15 {
type identityref {
base tunnel;
}
}
}
......@@ -2,10 +2,15 @@ module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
grouping currentcheck {
leaf invalid-interval {
type identityref {
}
}
identity tunnel {
description
"Base identity from which specific tunnel types are derived.";
}
typedef type15 {
type identityref {
base tunnel;
}
}
}
......
module IdentityTest{
yang-version 1;
namespace http://huawei.com;
prefix IdentityTest;
identity ref-address-family {
description "ref-address-family";
}
identity ipv4-address-family {
base ref-address-family;
}
leaf tunnel {
type identityref {
base ref-address-family;
}
}
}
\ No newline at end of file
module IdentityInModule{
yang-version 1;
namespace http://huawei.com;
prefix IdentityInModule;
identity tunnel-type {
description
"Base identity from which specific tunnel types are derived.";
}
identity ref-address-family {
reference "http://www.iana.org/assignments/address-family-numbers/address-family-numbers.xhtml#address-family-numbers-2";
}
}
\ No newline at end of file
module IdentityIntraFile {
yang-version 1;
namespace http://huawei.com;
prefix IdentityIntraFile;
import "IdentityInModule" {
prefix "IdentityInModule";
}
identity ipv4-address-family {
base IdentityInModule:ref-address-family;
}
identity ipv6-address-family {
base IdentityInModule:ref-address-family;
}
leaf tunnel {
type identityref {
base IdentityInModule:ref-address-family;
}
}
leaf-list network-ref {
type identityref {
base IdentityInModule:ref-address-family;
}
}
}
\ No newline at end of file
module syslog1 {
yang-version 1;
namespace "http://huawei1.com";
prefix "sys1";
import "syslog2" {
prefix "sys2";
}
identity ipv4-address-family {
base sys2:ref-address-family;
}
identity ipv6-address-family {
base sys2:ref-address-family;
}
leaf tunnel {
type identityref {
base sys2:ref-address-family;
}
}
leaf-list network-ref {
type identityref {
base sys2:ref-address-family;
}
}
}
module syslog2 {
yang-version 1;
namespace "http://huawei2.com";
prefix "sys2";
import "syslog3" {
prefix "sys3";
}
identity tunnel-type {
base sys3:final-address-family;
}
identity ref-address-family {
base sys3:final-address-family;
}
}
module syslog3 {
yang-version 1;
namespace "http://huawei3.com";
prefix "sys3";
identity final-address-family {
description
"Base identity from which specific tunnel types are derived.";
}
}
module syslog1 {
yang-version 1;
namespace "http://huawei1.com";
prefix "sys1";
import "syslog2" {
prefix "sys2";
}
identity ipv4-address-family {
base sys2:ref-address-family;
}
identity ipv6-address-family {
base sys2:ref-address-family;
}
leaf tunnel {
type identityref {
base sys2:ref-address-family;
}
}
leaf-list network-ref {
type identityref {
base sys2:ref-address-family;
}
}
}
module syslog2 {
yang-version 1;
namespace "http://huawei2.com";
prefix "sys2";
import "syslog3" {
prefix "sys3";
}
identity tunnel-type {
base sys3:final-address-family;
}
identity ref-address-family {
base sys3:final-address-family;
}
}
module syslog3 {
yang-version 1;
namespace "http://huawei3.com";
prefix "sys3";
}
module syslog1 {
yang-version 1;
namespace "http://huawei3.com";
prefix "sys1";
include "syslog2";
identity ipv4-address-family {
base ref-address-family;
}
identity ipv6-address-family {
base ref-address-family;
}
leaf tunnel {
type identityref {
base ref-address-family;
}
}
leaf-list network-ref {
type identityref {
base ref-address-family;
}
}
}
submodule syslog2 {
yang-version 1;
belongs-to "syslog1" {
prefix "sys1";
}
import "syslog3" {
prefix "sys3";
}
identity tunnel-type {
base sys3:final-address-family;
}
identity ref-address-family {
base sys3:final-address-family;
}
}
module syslog3 {
yang-version 1;
namespace "http://huawei3.com";
prefix "sys3";
identity final-address-family {
description
"Base identity from which specific tunnel types are derived.";
}
}
module syslog1 {
yang-version 1;
namespace "http://huawei3.com";
prefix "sys1";
include "syslog2";
identity ipv4-address-family {
base sys2:ref-address-family;
}
identity ipv6-address-family {
base sys2:ref-address-family;
}
leaf tunnel {
type identityref {
base sys2:ref-address-family;
}
}
leaf-list network-ref {
type identityref {
base sys2:ref-address-family;
}
}
}
submodule syslog2 {
yang-version 1;
belongs-to "syslog1" {
prefix "sys1";
}
import "syslog3" {
prefix "sys3";
}
identity tunnel-type {
base sys3:final-address-family;
}
identity ref-address-family {
base sys3:final-address-family;
}
}
module syslog3 {
yang-version 1;
namespace "http://huawei3.com";
prefix "sys3";
}
module syslog3 {
yang-version 1;
namespace "http://huawei3.com";
prefix "sys3";
include "syslog4";
identity ipv4-address-family {
base ref-address-family;
}
identity ipv6-address-family {
base ref-address-family;
}
leaf tunnel {
type identityref {
base ref-address-family;
}
}
leaf-list network-ref {
type identityref {
base ref-address-family;
}
}
}
submodule syslog4 {
yang-version 1;
belongs-to "syslog3" {
prefix "sys3";
}
identity tunnel-type {
description
"Base identity from which specific tunnel types are derived.";
}
identity ref-address-family {
reference "http://www.iana.org/assignments/address-family-numbers/address-family-numbers.xhtml#address-family-numbers-2";
}
}
\ No newline at end of file
module IdentityInModule{
yang-version 1;
namespace http://huawei.com;
prefix IdentityInModule;
identity tunnel-type {
description
"Base identity from which specific tunnel types are derived.";
}
identity ref-address-family {
reference "http://www.iana.org/assignments/address-family-numbers/address-family-numbers.xhtml#address-family-numbers-2";
}
}
\ No newline at end of file
module IdentityIntraFile {
yang-version 1;
namespace http://huawei.com;
prefix IdentityIntraFile;
import "IdentityInModule" {
prefix "IdentityInModule";
}
identity ipv4-address-family {
base IdentityInModule:ref-address-family;
}
identity ipv6-address-family {
base IdentityInModule:ref-address-family;
}
leaf tunnel {
type identityref {
base IdentityInModule:ref-address-family;
}
}
leaf-list network-ref {
type identityref {
base IdentityInModule:ref-address-family;
}
}
typedef type15 {
type identityref {
base IdentityInModule:ref-address-family;
}
}
}
\ No newline at end of file
module IdentityTypedef {
yang-version 1;
namespace http://huawei.com;
prefix IdentityTypedef;
import "IdentityInModule" {
prefix "IdentityInModule";
}
identity ipv4-address-family {
base IdentityInModule:ref-address-family;
}
identity ipv6-address-family {
base IdentityInModule:ref-address-family;
}
leaf tunnel {
type type15;
}
leaf-list network-ref {
type type15;
}
typedef type15 {
type identityref {
base IdentityInModule:ref-address-family;
}
}
}
\ No newline at end of file
......@@ -56,7 +56,7 @@
reference "RFC3209";
}
/*identity tunnel-type {
identity tunnel-type {
description
"Base identity from which specific tunnel types are
derived.";
......@@ -254,7 +254,7 @@
base lsp-encoding-types;
description
"Line (e.g., 8B/10B) LSP encoding";
}*/
}
/* TE basic features */
feature p2mp-te {
......@@ -452,7 +452,7 @@
}
}
/*identity route-usage-type {
identity route-usage-type {
description
"Base identity for route usage";
}
......@@ -576,7 +576,7 @@
description
"The set of attribute filters associated with a
tunnel any of which renders a link unacceptable";
}*/
}
typedef admin-group {
type binary {
......@@ -605,7 +605,7 @@
description "SRLG type";
}
/*identity path-computation-srlg-type {
identity path-computation-srlg-type {
description
"Base identity for SRLG path computation";
}
......@@ -632,7 +632,7 @@
base path-computation-srlg-type;
description
"Include weighted SRLG check in path computation";
}*/
}
typedef te-metric {
type uint32;
......