X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fyang-model-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang%2Fparser%2Futil%2FYangModelBuilderUtil.java;h=ada2d2cfd8c427cf4072ac9c2d6227359bbab4ae;hb=154b5dde1af41aff2ae0cc6e08400153162a4a3c;hp=ae405c06a88805d0c744c1fb2a5b97f2c9dda9ec;hpb=d067191495a9515e157eed95b02cd32e63e74cf3;p=controller.git diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/YangModelBuilderUtil.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/YangModelBuilderUtil.java index ae405c06a8..ada2d2cfd8 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/YangModelBuilderUtil.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/YangModelBuilderUtil.java @@ -15,7 +15,7 @@ import java.util.List; import java.util.Stack; import org.antlr.v4.runtime.tree.ParseTree; -import org.opendaylight.controller.antlrv4.code.gen.YangParser; +import org.opendaylight.controller.antlrv4.code.gen.*; import org.opendaylight.controller.antlrv4.code.gen.YangParser.Argument_stmtContext; import org.opendaylight.controller.antlrv4.code.gen.YangParser.Base_stmtContext; import org.opendaylight.controller.antlrv4.code.gen.YangParser.Bit_stmtContext; @@ -108,16 +108,17 @@ import org.opendaylight.controller.yang.model.util.Uint64; import org.opendaylight.controller.yang.model.util.Uint8; import org.opendaylight.controller.yang.model.util.UnknownType; import org.opendaylight.controller.yang.parser.builder.api.Builder; +import org.opendaylight.controller.yang.parser.builder.api.ConfigNode; import org.opendaylight.controller.yang.parser.builder.api.SchemaNodeBuilder; import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder; +import org.opendaylight.controller.yang.parser.builder.impl.ChoiceBuilder; +import org.opendaylight.controller.yang.parser.builder.impl.ChoiceCaseBuilder; import org.opendaylight.controller.yang.parser.builder.impl.ConstraintsBuilder; -import org.opendaylight.controller.yang.parser.builder.impl.ModuleBuilder; import org.opendaylight.controller.yang.parser.builder.impl.UnionTypeBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public final class YangModelBuilderUtil { - private static final Logger logger = LoggerFactory.getLogger(YangModelBuilderUtil.class); private YangModelBuilderUtil() { @@ -216,18 +217,18 @@ public final class YangModelBuilderUtil { } /** - * Create SchemaPath object from given path list with namespace, revision - * and prefix based on given values. + * Create SchemaPath from actualPath and names. * * @param actualPath * current position in model * @param namespace * @param revision * @param prefix + * @param names * @return SchemaPath object. */ public static SchemaPath createActualSchemaPath(final List actualPath, final URI namespace, - final Date revision, final String prefix) { + final Date revision, final String prefix, final String... names) { final List path = new ArrayList(); QName qname; // start from index 1 - module name omited @@ -235,6 +236,10 @@ public final class YangModelBuilderUtil { qname = new QName(namespace, revision, prefix, actualPath.get(i)); path.add(qname); } + for (String name : names) { + qname = new QName(namespace, revision, prefix, name); + path.add(qname); + } return new SchemaPath(path, true); } @@ -672,16 +677,13 @@ public final class YangModelBuilderUtil { private static List getPatternConstraint(final Type_body_stmtsContext ctx) { List patterns = new ArrayList(); - outer: for (int j = 0; j < ctx.getChildCount(); j++) { + for (int j = 0; j < ctx.getChildCount(); j++) { ParseTree stringRestrChild = ctx.getChild(j); if (stringRestrChild instanceof String_restrictionsContext) { for (int k = 0; k < stringRestrChild.getChildCount(); k++) { ParseTree lengthChild = stringRestrChild.getChild(k); if (lengthChild instanceof Pattern_stmtContext) { patterns.add(parsePatternConstraint((Pattern_stmtContext) lengthChild)); - if (k == lengthChild.getChildCount() - 1) { - break outer; - } } } } @@ -870,11 +872,15 @@ public final class YangModelBuilderUtil { } /** - * Parse orderedby statement. + * Parse 'ordered-by' statement. + * + * The 'ordered-by' statement defines whether the order of entries within a + * list are determined by the user or the system. The argument is one of the + * strings "system" or "user". If not present, order defaults to "system". * * @param childNode * Ordered_by_stmtContext - * @return true, if orderedby contains value 'user' or false otherwise + * @return true, if ordered-by contains value 'user', false otherwise */ public static boolean parseUserOrdered(Ordered_by_stmtContext childNode) { boolean result = false; @@ -894,16 +900,66 @@ public final class YangModelBuilderUtil { return result; } + public static Boolean getConfig(final ParseTree ctx, final Builder parent, final String moduleName, final int line) { + Boolean result = null; + // parse configuration statement + Boolean configuration = null; + for (int i = 0; i < ctx.getChildCount(); i++) { + ParseTree child = ctx.getChild(i); + if (child instanceof Config_stmtContext) { + configuration = parseConfig((Config_stmtContext) child); + break; + } + } + + // If 'config' is not specified, the default is the same as the parent + // schema node's 'config' value + if (configuration == null) { + if (parent instanceof ConfigNode) { + Boolean parentConfig = ((ConfigNode) parent).isConfiguration(); + // If the parent node is a rpc input or output, it can has + // config set to null + result = parentConfig == null ? true : parentConfig; + } else if (parent instanceof ChoiceCaseBuilder) { + // If the parent node is a 'case' node, the value is the same as + // the 'case' node's parent 'choice' node + ChoiceCaseBuilder choiceCase = (ChoiceCaseBuilder) parent; + Builder choice = choiceCase.getParent(); + Boolean parentConfig = null; + if(choice instanceof ChoiceBuilder) { + parentConfig = ((ChoiceBuilder)choice).isConfiguration(); + } else { + parentConfig = true; + } + result = parentConfig; + } else { + result = true; + } + } else { + // Check first: if a node has 'config' set to 'false', no node + // underneath it can have 'config' set to 'true' + if (parent instanceof ConfigNode) { + Boolean parentConfig = ((ConfigNode) parent).isConfiguration(); + if (parentConfig == false && configuration == true) { + throw new YangParseException(moduleName, line, + "Can not set 'config' to 'true' if parent node has 'config' set to 'false'"); + } + } + result = configuration; + } + + return result; + } + /** - * Parse given config context and return true if it contains string 'true', - * false otherwise. + * Parse config statement. * * @param ctx * config context to parse. * @return true if given context contains string 'true', false otherwise */ - public static boolean parseConfig(final Config_stmtContext ctx) { - boolean result = false; + private static Boolean parseConfig(final Config_stmtContext ctx) { + Boolean result = null; if (ctx != null) { for (int i = 0; i < ctx.getChildCount(); ++i) { final ParseTree configContext = ctx.getChild(i); @@ -912,6 +968,12 @@ public final class YangModelBuilderUtil { if ("true".equals(value)) { result = true; break; + } else if ("false".equals(value)) { + result = false; + break; + } else { + throw new YangParseException(ctx.getStart().getLine(), + "Failed to parse 'config' statement value: '" + value + "'."); } } } @@ -920,17 +982,22 @@ public final class YangModelBuilderUtil { } /** - * Parse given type body and creates UnknownType definition. + * Parse type body and create UnknownType definition. * * @param typedefQName * qname of current type * @param ctx * type body + * @param actualPath + * @param namespace + * @param revision + * @param prefix + * @param parent * @return UnknownType object with constraints from parsed type body */ - public static TypeDefinition parseUnknownTypeBody(QName typedefQName, Type_body_stmtsContext ctx, - final List actualPath, final URI namespace, final Date revision, final String prefix, - Builder parent, ModuleBuilder moduleBuilder) { + public static TypeDefinition parseUnknownTypeWithBody(final QName typedefQName, + final Type_body_stmtsContext ctx, final List actualPath, final URI namespace, final Date revision, + final String prefix, final Builder parent) { String typeName = typedefQName.getLocalName(); UnknownType.Builder unknownType = new UnknownType.Builder(typedefQName); @@ -975,10 +1042,12 @@ public final class YangModelBuilderUtil { /** * Create TypeDefinition object based on given type name and type body. * + * @param moduleName + * current module name * @param typeName * name of type * @param typeBody - * type body + * type body context * @param actualPath * current path in schema * @param namespace @@ -987,19 +1056,19 @@ public final class YangModelBuilderUtil { * current revision * @param prefix * current prefix + * @param parent + * parent builder * @return TypeDefinition object based on parsed values. */ - public static TypeDefinition parseTypeBody(final String moduleName, final String typeName, + public static TypeDefinition parseTypeWithBody(final String moduleName, final String typeName, final Type_body_stmtsContext typeBody, final List actualPath, final URI namespace, - final Date revision, final String prefix, Builder parent) { + final Date revision, final String prefix, final Builder parent) { TypeDefinition baseType = null; - List rangeStatements = getRangeConstraints(typeBody); Integer fractionDigits = getFractionDigits(typeBody); List lengthStatements = getLengthConstraints(typeBody); List patternStatements = getPatternConstraint(typeBody); - List enumConstants = getEnumConstants(typeBody, actualPath, namespace, revision, - prefix); + List rangeStatements = getRangeConstraints(typeBody); TypeConstraints constraints = new TypeConstraints(moduleName, typeBody.getStart().getLine()); constraints.addFractionDigits(fractionDigits); @@ -1045,6 +1114,8 @@ public final class YangModelBuilderUtil { constraints.addRanges(uintType.getRangeStatements()); baseType = uintType; } else if ("enumeration".equals(typeName)) { + List enumConstants = getEnumConstants(typeBody, actualPath, namespace, + revision, prefix); return new EnumerationType(baseTypePathFinal, enumConstants); } else if ("string".equals(typeName)) { StringTypeDefinition stringType = new StringType(baseTypePath); @@ -1157,10 +1228,10 @@ public final class YangModelBuilderUtil { } /** - * Parse given context and find require-instance value. + * Parse type body statement and find require-instance value. * * @param ctx - * type body + * type body context * @return require-instance value */ private static boolean isRequireInstance(Type_body_stmtsContext ctx) { @@ -1179,10 +1250,10 @@ public final class YangModelBuilderUtil { } /** - * Parse given context and find leafref path. + * Parse type body statement and find leafref path. * * @param ctx - * type body + * type body context * @return leafref path as String */ private static String parseLeafrefPath(Type_body_stmtsContext ctx) { @@ -1201,7 +1272,7 @@ public final class YangModelBuilderUtil { } /** - * Internal helper method for parsing Must_stmtContext. + * Internal helper method for parsing must statement. * * @param ctx * Must_stmtContext @@ -1250,10 +1321,10 @@ public final class YangModelBuilderUtil { } /** - * Parse given tree and set constraints to given builder. + * Parse given context and set constraints to constraints builder. * * @param ctx - * context to search + * context to parse * @param constraints * ConstraintsBuilder to fill */ @@ -1389,7 +1460,7 @@ public final class YangModelBuilderUtil { */ public static RefineHolder parseRefine(Refine_stmtContext refineCtx) { final String refineTarget = stringFromNode(refineCtx); - final RefineHolder refine = new RefineHolder(refineTarget, refineCtx.getStart().getLine()); + final RefineHolder refine = new RefineHolder(refineCtx.getStart().getLine(), refineTarget); for (int j = 0; j < refineCtx.getChildCount(); j++) { ParseTree refinePom = refineCtx.getChild(j); if (refinePom instanceof Refine_pomContext) { @@ -1426,8 +1497,8 @@ public final class YangModelBuilderUtil { String reference = stringFromNode(refineArg); refine.setReference(reference); } else if (refineArg instanceof Config_stmtContext) { - boolean config = parseConfig((Config_stmtContext) refineArg); - refine.setConfig(config); + Boolean config = parseConfig((Config_stmtContext) refineArg); + refine.setConfiguration(config); } } }