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=72e364cca5ef0dc2baa611e81e1968eeecb3e287;hpb=39ff6ed2000232d607877311770c74cef36cf96f;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 72e364cca5..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; @@ -36,7 +36,9 @@ import org.opendaylight.controller.antlrv4.code.gen.YangParser.Length_stmtContex import org.opendaylight.controller.antlrv4.code.gen.YangParser.Mandatory_argContext; import org.opendaylight.controller.antlrv4.code.gen.YangParser.Mandatory_stmtContext; import org.opendaylight.controller.antlrv4.code.gen.YangParser.Max_elements_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Max_value_argContext; import org.opendaylight.controller.antlrv4.code.gen.YangParser.Min_elements_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Min_value_argContext; import org.opendaylight.controller.antlrv4.code.gen.YangParser.Must_stmtContext; import org.opendaylight.controller.antlrv4.code.gen.YangParser.Numerical_restrictionsContext; import org.opendaylight.controller.antlrv4.code.gen.YangParser.Ordered_by_argContext; @@ -74,18 +76,24 @@ import org.opendaylight.controller.yang.model.api.SchemaPath; import org.opendaylight.controller.yang.model.api.Status; import org.opendaylight.controller.yang.model.api.TypeDefinition; import org.opendaylight.controller.yang.model.api.UnknownSchemaNode; +import org.opendaylight.controller.yang.model.api.type.BinaryTypeDefinition; import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition; import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit; import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition; import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair; +import org.opendaylight.controller.yang.model.api.type.IntegerTypeDefinition; import org.opendaylight.controller.yang.model.api.type.LengthConstraint; import org.opendaylight.controller.yang.model.api.type.PatternConstraint; import org.opendaylight.controller.yang.model.api.type.RangeConstraint; +import org.opendaylight.controller.yang.model.api.type.StringTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.UnsignedIntegerTypeDefinition; import org.opendaylight.controller.yang.model.util.BaseConstraints; +import org.opendaylight.controller.yang.model.util.BaseTypes; import org.opendaylight.controller.yang.model.util.BinaryType; import org.opendaylight.controller.yang.model.util.BitsType; import org.opendaylight.controller.yang.model.util.Decimal64; import org.opendaylight.controller.yang.model.util.EnumerationType; +import org.opendaylight.controller.yang.model.util.ExtendedType; import org.opendaylight.controller.yang.model.util.InstanceIdentifier; import org.opendaylight.controller.yang.model.util.Int16; import org.opendaylight.controller.yang.model.util.Int32; @@ -99,15 +107,19 @@ import org.opendaylight.controller.yang.model.util.Uint32; 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.UnionTypeBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public final class YangModelBuilderUtil { - - private static final Logger logger = LoggerFactory - .getLogger(YangModelBuilderUtil.class); + private static final Logger logger = LoggerFactory.getLogger(YangModelBuilderUtil.class); private YangModelBuilderUtil() { } @@ -123,8 +135,7 @@ public final class YangModelBuilderUtil { final String result = ""; for (int i = 0; i < treeNode.getChildCount(); ++i) { if (treeNode.getChild(i) instanceof StringContext) { - final StringContext context = (StringContext) treeNode - .getChild(i); + final StringContext context = (StringContext) treeNode.getChild(i); if (context != null) { return context.getChild(0).getText().replace("\"", ""); } @@ -142,8 +153,7 @@ public final class YangModelBuilderUtil { * @param builder * builder to fill in with parsed statements */ - public static void parseSchemaNodeArgs(final ParseTree ctx, - final SchemaNodeBuilder builder) { + public static void parseSchemaNodeArgs(final ParseTree ctx, final SchemaNodeBuilder builder) { for (int i = 0; i < ctx.getChildCount(); i++) { final ParseTree child = ctx.getChild(i); if (child instanceof Description_stmtContext) { @@ -207,19 +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) { + public static SchemaPath createActualSchemaPath(final List actualPath, final URI namespace, + final Date revision, final String prefix, final String... names) { final List path = new ArrayList(); QName qname; // start from index 1 - module name omited @@ -227,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); } @@ -248,8 +261,7 @@ public final class YangModelBuilderUtil { if (splittedElement.length == 1) { name = new QName(null, null, null, splittedElement[0]); } else { - name = new QName(null, null, splittedElement[0], - splittedElement[1]); + name = new QName(null, null, splittedElement[0], splittedElement[1]); } path.add(name); } @@ -271,8 +283,8 @@ public final class YangModelBuilderUtil { * current prefix * @return YANG list key as java.util.List of QName objects */ - public static List createListKey(final String keyDefinition, - final URI namespace, final Date revision, final String prefix) { + public static List createListKey(final String keyDefinition, final URI namespace, final Date revision, + final String prefix) { List key = new ArrayList(); String[] splittedKey = keyDefinition.split(" "); @@ -298,9 +310,8 @@ public final class YangModelBuilderUtil { * @param prefix * @return List of EnumPair object parsed from given context */ - private static List getEnumConstants( - final Type_body_stmtsContext ctx, final List path, - final URI namespace, final Date revision, final String prefix) { + private static List getEnumConstants(final Type_body_stmtsContext ctx, + final List path, final URI namespace, final Date revision, final String prefix) { List enumConstants = new ArrayList(); for (int j = 0; j < ctx.getChildCount(); j++) { @@ -310,9 +321,8 @@ public final class YangModelBuilderUtil { for (int k = 0; k < enumSpecChild.getChildCount(); k++) { ParseTree enumChild = enumSpecChild.getChild(k); if (enumChild instanceof Enum_stmtContext) { - EnumPair enumPair = createEnumPair( - (Enum_stmtContext) enumChild, highestValue, - path, namespace, revision, prefix); + EnumPair enumPair = createEnumPair((Enum_stmtContext) enumChild, highestValue, path, namespace, + revision, prefix); if (enumPair.getValue() > highestValue) { highestValue = enumPair.getValue(); } @@ -338,10 +348,8 @@ public final class YangModelBuilderUtil { * @param prefix * @return EnumPair object parsed from given context */ - private static EnumTypeDefinition.EnumPair createEnumPair( - final Enum_stmtContext ctx, final int highestValue, - final List path, final URI namespace, final Date revision, - final String prefix) { + private static EnumTypeDefinition.EnumPair createEnumPair(final Enum_stmtContext ctx, final int highestValue, + final List path, final URI namespace, final Date revision, final String prefix) { final String name = stringFromNode(ctx); final QName qname = new QName(namespace, revision, prefix, name); Integer value = null; @@ -371,17 +379,13 @@ public final class YangModelBuilderUtil { value = highestValue + 1; } if (value < -2147483648 || value > 2147483647) { - throw new YangParseException( - "Error on enum '" - + name - + "': the enum value MUST be in the range from -2147483648 to 2147483647, but was: " - + value); + throw new YangParseException(ctx.getStart().getLine(), "Error on enum '" + name + + "': the enum value MUST be in the range from -2147483648 to 2147483647, but was: " + value); } EnumPairImpl result = new EnumPairImpl(); result.qname = qname; - result.path = createActualSchemaPath(enumPairPath, namespace, revision, - prefix); + result.path = createActualSchemaPath(enumPairPath, namespace, revision, prefix); result.description = description; result.reference = reference; result.status = status; @@ -399,8 +403,7 @@ public final class YangModelBuilderUtil { private String description; private String reference; private Status status; - private List extensionSchemaNodes = Collections - .emptyList(); + private List extensionSchemaNodes = Collections.emptyList(); private String name; private Integer value; @@ -450,10 +453,7 @@ public final class YangModelBuilderUtil { int result = 1; result = prime * result + ((qname == null) ? 0 : qname.hashCode()); result = prime * result + ((path == null) ? 0 : path.hashCode()); - result = prime - * result - + ((extensionSchemaNodes == null) ? 0 - : extensionSchemaNodes.hashCode()); + result = prime * result + ((extensionSchemaNodes == null) ? 0 : extensionSchemaNodes.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((value == null) ? 0 : value.hashCode()); return result; @@ -511,8 +511,7 @@ public final class YangModelBuilderUtil { @Override public String toString() { - return EnumTypeDefinition.EnumPair.class.getSimpleName() + "[name=" - + name + ", value=" + value + "]"; + return EnumTypeDefinition.EnumPair.class.getSimpleName() + "[name=" + name + ", value=" + value + "]"; } } @@ -523,8 +522,7 @@ public final class YangModelBuilderUtil { * type body context to parse * @return List of RangeConstraint created from this context */ - private static List getRangeConstraints( - final Type_body_stmtsContext ctx) { + private static List getRangeConstraints(final Type_body_stmtsContext ctx) { List rangeConstraints = Collections.emptyList(); outer: for (int j = 0; j < ctx.getChildCount(); j++) { ParseTree numRestrChild = ctx.getChild(j); @@ -548,8 +546,7 @@ public final class YangModelBuilderUtil { * range context to parse * @return List of RangeConstraints parsed from this context */ - private static List parseRangeConstraints( - final Range_stmtContext ctx) { + private static List parseRangeConstraints(final Range_stmtContext ctx) { final int line = ctx.getStart().getLine(); List rangeConstraints = new ArrayList(); String description = null; @@ -572,14 +569,12 @@ public final class YangModelBuilderUtil { Number min; Number max; if (splittedRangeDef.length == 1) { - min = max = parseNumberConstraintValue(splittedRangeDef[0], - line); + min = max = parseNumberConstraintValue(splittedRangeDef[0], line); } else { min = parseNumberConstraintValue(splittedRangeDef[0], line); max = parseNumberConstraintValue(splittedRangeDef[1], line); } - RangeConstraint range = BaseConstraints.rangeConstraint(min, max, - description, reference); + RangeConstraint range = BaseConstraints.rangeConstraint(min, max, description, reference); rangeConstraints.add(range); } @@ -593,8 +588,7 @@ public final class YangModelBuilderUtil { * type body context to parse * @return List of LengthConstraint created from this context */ - private static List getLengthConstraints( - final Type_body_stmtsContext ctx) { + private static List getLengthConstraints(final Type_body_stmtsContext ctx) { List lengthConstraints = Collections.emptyList(); outer: for (int j = 0; j < ctx.getChildCount(); j++) { ParseTree stringRestrChild = ctx.getChild(j); @@ -618,8 +612,7 @@ public final class YangModelBuilderUtil { * length context to parse * @return List of LengthConstraints parsed from this context */ - private static List parseLengthConstraints( - final Length_stmtContext ctx) { + private static List parseLengthConstraints(final Length_stmtContext ctx) { final int line = ctx.getStart().getLine(); List lengthConstraints = new ArrayList(); String description = null; @@ -642,14 +635,12 @@ public final class YangModelBuilderUtil { Number min; Number max; if (splittedRangeDef.length == 1) { - min = max = parseNumberConstraintValue(splittedRangeDef[0], - line); + min = max = parseNumberConstraintValue(splittedRangeDef[0], line); } else { min = parseNumberConstraintValue(splittedRangeDef[0], line); max = parseNumberConstraintValue(splittedRangeDef[1], line); } - LengthConstraint range = BaseConstraints.lengthConstraint(min, max, - description, reference); + LengthConstraint range = BaseConstraints.lengthConstraint(min, max, description, reference); lengthConstraints.add(range); } @@ -662,8 +653,7 @@ public final class YangModelBuilderUtil { * @return wrapper object of primitive java type or UnknownBoundaryNumber if * type is one of special YANG values 'min' or 'max' */ - private static Number parseNumberConstraintValue(final String value, - final int line) { + private static Number parseNumberConstraintValue(final String value, final int line) { Number result = null; if ("min".equals(value) || "max".equals(value)) { result = new UnknownBoundaryNumber(value); @@ -671,27 +661,29 @@ public final class YangModelBuilderUtil { try { result = Long.valueOf(value); } catch (NumberFormatException e) { - throw new YangParseException("Error on line " + line - + ": Unable to parse range value '" + value + "'.", e); + throw new YangParseException(line, "Unable to parse range value '" + value + "'.", e); } } return result; } - private static List getPatternConstraint( - final Type_body_stmtsContext ctx) { + /** + * Parse type body and return pattern constraints. + * + * @param ctx + * type body + * @return list of pattern constraints + */ + 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; - } } } } @@ -706,8 +698,7 @@ public final class YangModelBuilderUtil { * pattern context * @return PatternConstraint object */ - private static PatternConstraint parsePatternConstraint( - final Pattern_stmtContext ctx) { + private static PatternConstraint parsePatternConstraint(final Pattern_stmtContext ctx) { String description = null; String reference = null; for (int i = 0; i < ctx.getChildCount(); i++) { @@ -719,8 +710,7 @@ public final class YangModelBuilderUtil { } } String pattern = patternStringFromNode(ctx); - return BaseConstraints.patternConstraint(pattern, description, - reference); + return BaseConstraints.patternConstraint(pattern, description, reference); } /** @@ -738,8 +728,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < child.getChildCount(); j++) { if (j % 2 == 0) { String patternToken = child.getChild(j).getText(); - result.append(patternToken.substring(1, - patternToken.length() - 1)); + result.append(patternToken.substring(1, patternToken.length() - 1)); } } } @@ -748,7 +737,7 @@ public final class YangModelBuilderUtil { } /** - * Get fraction digits value from context. + * Get fraction digits value from type body. * * @param ctx * type body context to parse @@ -766,8 +755,14 @@ public final class YangModelBuilderUtil { return result; } - private static Integer parseFractionDigits( - Decimal64_specificationContext ctx) { + /** + * Parse decimal64 fraction-digits value. + * + * @param ctx + * decimal64 context + * @return fraction-digits value as Integer + */ + private static Integer parseFractionDigits(Decimal64_specificationContext ctx) { Integer result = null; for (int k = 0; k < ctx.getChildCount(); k++) { ParseTree fdChild = ctx.getChild(k); @@ -776,9 +771,8 @@ public final class YangModelBuilderUtil { try { result = Integer.valueOf(value); } catch (NumberFormatException e) { - throw new YangParseException( - "Unable to parse fraction digits value '" + value - + "'.", e); + throw new YangParseException(ctx.getStart().getLine(), "Unable to parse fraction digits value '" + + value + "'.", e); } } } @@ -798,9 +792,8 @@ public final class YangModelBuilderUtil { * @param prefix * @return List of Bit objects created from this context */ - private static List getBits( - Type_body_stmtsContext ctx, List actualPath, URI namespace, - Date revision, String prefix) { + private static List getBits(Type_body_stmtsContext ctx, List actualPath, + URI namespace, Date revision, String prefix) { final List bits = new ArrayList(); for (int j = 0; j < ctx.getChildCount(); j++) { ParseTree bitsSpecChild = ctx.getChild(j); @@ -809,8 +802,7 @@ public final class YangModelBuilderUtil { for (int k = 0; k < bitsSpecChild.getChildCount(); k++) { ParseTree bitChild = bitsSpecChild.getChild(k); if (bitChild instanceof Bit_stmtContext) { - Bit bit = parseBit((Bit_stmtContext) bitChild, - highestPosition, actualPath, namespace, + Bit bit = parseBit((Bit_stmtContext) bitChild, highestPosition, actualPath, namespace, revision, prefix); if (bit.getPosition() > highestPosition) { highestPosition = bit.getPosition(); @@ -837,9 +829,8 @@ public final class YangModelBuilderUtil { * @param prefix * @return Bit object parsed from this context */ - private static BitsTypeDefinition.Bit parseBit(final Bit_stmtContext ctx, - long highestPosition, List actualPath, final URI namespace, - final Date revision, final String prefix) { + private static BitsTypeDefinition.Bit parseBit(final Bit_stmtContext ctx, long highestPosition, + List actualPath, final URI namespace, final Date revision, final String prefix) { String name = stringFromNode(ctx); final QName qname = new QName(namespace, revision, prefix, name); Long position = null; @@ -852,8 +843,7 @@ public final class YangModelBuilderUtil { bitPath.addAll(actualPath); bitPath.add(name); - SchemaPath schemaPath = createActualSchemaPath(bitPath, namespace, - revision, prefix); + SchemaPath schemaPath = createActualSchemaPath(bitPath, namespace, revision, prefix); for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); @@ -873,23 +863,24 @@ public final class YangModelBuilderUtil { position = highestPosition + 1; } if (position < 0 || position > 4294967295L) { - throw new YangParseException( - "Error on bit '" - + name - + "': the position value MUST be in the range 0 to 4294967295"); + throw new YangParseException(ctx.getStart().getLine(), "Error on bit '" + name + + "': the position value MUST be in the range 0 to 4294967295"); } final List unknownNodes = Collections.emptyList(); - return new BitImpl(position, qname, schemaPath, description, reference, - status, unknownNodes); + return new BitImpl(position, qname, schemaPath, description, reference, status, unknownNodes); } /** - * 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; @@ -909,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); @@ -927,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 + "'."); } } } @@ -935,16 +982,24 @@ 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) { + 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); if (ctx != null) { @@ -953,10 +1008,32 @@ public final class YangModelBuilderUtil { List patternStatements = getPatternConstraint(ctx); Integer fractionDigits = getFractionDigits(ctx); - unknownType.rangeStatements(rangeStatements); - unknownType.lengthStatements(lengthStatements); - unknownType.patterns(patternStatements); - unknownType.fractionDigits(fractionDigits); + if (parent instanceof TypeDefinitionBuilder) { + TypeDefinitionBuilder typedef = (TypeDefinitionBuilder) parent; + typedef.setRanges(rangeStatements); + typedef.setLengths(lengthStatements); + typedef.setPatterns(patternStatements); + typedef.setFractionDigits(fractionDigits); + return unknownType.build(); + } else { + TypeDefinition baseType = unknownType.build(); + TypeDefinition result = null; + QName qname = new QName(namespace, revision, prefix, typeName); + ExtendedType.Builder typeBuilder = null; + + SchemaPath schemaPath = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, false, + false); + typeBuilder = new ExtendedType.Builder(qname, baseType, "", "", schemaPath); + + typeBuilder.ranges(rangeStatements); + typeBuilder.lengths(lengthStatements); + typeBuilder.patterns(patternStatements); + typeBuilder.fractionDigits(fractionDigits); + + result = typeBuilder.build(); + + return result; + } } return unknownType.build(); @@ -965,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 @@ -977,79 +1056,160 @@ 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 typeName, - final Type_body_stmtsContext typeBody, - final List actualPath, final URI namespace, - final Date revision, final String prefix) { - TypeDefinition type = null; + 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, 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); + constraints.addLengths(lengthStatements); + constraints.addPatterns(patternStatements); + constraints.addRanges(rangeStatements); + + SchemaPath baseTypePathFinal = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, true, + true); + SchemaPath baseTypePath = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, true, false); if ("decimal64".equals(typeName)) { - type = new Decimal64(actualPath, namespace, revision, - fractionDigits); + if (rangeStatements.isEmpty()) { + return new Decimal64(baseTypePathFinal, fractionDigits); + } + Decimal64 decimalType = new Decimal64(baseTypePath, fractionDigits); + constraints.addRanges(decimalType.getRangeStatements()); + baseType = decimalType; } else if (typeName.startsWith("int")) { - if (typeName.equals("int8")) { - type = new Int8(actualPath, namespace, revision, - rangeStatements, null, null); - } else if (typeName.equals("int16")) { - type = new Int16(actualPath, namespace, revision, - rangeStatements, null, null); - } else if (typeName.equals("int32")) { - type = new Int32(actualPath, namespace, revision, - rangeStatements, null, null); - } else if (typeName.equals("int64")) { - type = new Int64(actualPath, namespace, revision, - rangeStatements, null, null); + IntegerTypeDefinition intType = null; + if ("int8".equals(typeName)) { + intType = new Int8(baseTypePath); + } else if ("int16".equals(typeName)) { + intType = new Int16(baseTypePath); + } else if ("int32".equals(typeName)) { + intType = new Int32(baseTypePath); + } else if ("int64".equals(typeName)) { + intType = new Int64(baseTypePath); } + constraints.addRanges(intType.getRangeStatements()); + baseType = intType; } else if (typeName.startsWith("uint")) { - if (typeName.equals("uint8")) { - type = new Uint8(actualPath, namespace, revision, - rangeStatements, null, null); - } else if (typeName.equals("uint16")) { - type = new Uint16(actualPath, namespace, revision, - rangeStatements, null, null); - } else if (typeName.equals("uint32")) { - type = new Uint32(actualPath, namespace, revision, - rangeStatements, null, null); - } else if (typeName.equals("uint64")) { - type = new Uint64(actualPath, namespace, revision, - rangeStatements, null, null); + UnsignedIntegerTypeDefinition uintType = null; + if ("uint8".equals(typeName)) { + uintType = new Uint8(baseTypePath); + } else if ("uint16".equals(typeName)) { + uintType = new Uint16(baseTypePath); + } else if ("uint32".equals(typeName)) { + uintType = new Uint32(baseTypePath); + } else if ("uint64".equals(typeName)) { + uintType = new Uint64(baseTypePath); } + constraints.addRanges(uintType.getRangeStatements()); + baseType = uintType; } else if ("enumeration".equals(typeName)) { - type = new EnumerationType(actualPath, namespace, revision, - enumConstants); + List enumConstants = getEnumConstants(typeBody, actualPath, namespace, + revision, prefix); + return new EnumerationType(baseTypePathFinal, enumConstants); } else if ("string".equals(typeName)) { - type = new StringType(actualPath, namespace, revision, - lengthStatements, patternStatements); + StringTypeDefinition stringType = new StringType(baseTypePath); + constraints.addLengths(stringType.getLengthStatements()); + baseType = stringType; } else if ("bits".equals(typeName)) { - type = new BitsType(actualPath, namespace, revision, getBits( - typeBody, actualPath, namespace, revision, prefix)); + return new BitsType(baseTypePathFinal, getBits(typeBody, actualPath, namespace, revision, prefix)); } else if ("leafref".equals(typeName)) { final String path = parseLeafrefPath(typeBody); final boolean absolute = path.startsWith("/"); - RevisionAwareXPath xpath = new RevisionAwareXPathImpl(path, - absolute); - type = new Leafref(actualPath, namespace, revision, xpath); + RevisionAwareXPath xpath = new RevisionAwareXPathImpl(path, absolute); + return new Leafref(baseTypePathFinal, xpath); } else if ("binary".equals(typeName)) { - List bytes = Collections.emptyList(); - type = new BinaryType(actualPath, namespace, revision, bytes, - lengthStatements, null); + BinaryTypeDefinition binaryType = new BinaryType(baseTypePath); + constraints.addLengths(binaryType.getLengthConstraints()); + baseType = binaryType; } else if ("instance-identifier".equals(typeName)) { boolean requireInstance = isRequireInstance(typeBody); - type = new InstanceIdentifier(actualPath, namespace, revision, - null, requireInstance); + baseType = new InstanceIdentifier(baseTypePath, null, requireInstance); + } + + if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) { + TypeDefinitionBuilder typedef = (TypeDefinitionBuilder) parent; + typedef.setRanges(constraints.getRange()); + typedef.setLengths(constraints.getLength()); + typedef.setPatterns(constraints.getPatterns()); + typedef.setFractionDigits(constraints.getFractionDigits()); + return baseType; + } + + TypeDefinition result = null; + QName qname = new QName(namespace, revision, prefix, typeName); + ExtendedType.Builder typeBuilder = null; + + SchemaPath schemaPath = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, false, false); + typeBuilder = new ExtendedType.Builder(qname, baseType, "", "", schemaPath); + + typeBuilder.ranges(constraints.getRange()); + typeBuilder.lengths(constraints.getLength()); + typeBuilder.patterns(constraints.getPatterns()); + typeBuilder.fractionDigits(constraints.getFractionDigits()); + + result = typeBuilder.build(); + return result; + } + + /** + * Create SchemaPath object from given path list with namespace, revision + * and prefix based on given values. + * + * @param actualPath + * current position in model + * @param namespace + * @param revision + * @param prefix + * @param typeName + * @param isBaseYangType + * if this is base yang type + * @param isBaseYangTypeFinal + * if this is base yang type without restrictions + * @return SchemaPath object. + */ + private static SchemaPath createTypeSchemaPath(final List actualPath, final URI namespace, + final Date revision, final String prefix, final String typeName, final boolean isBaseYangType, + final boolean isBaseYangTypeFinal) { + List typePath = new ArrayList(actualPath); + if (isBaseYangType && !isBaseYangTypeFinal) { + typePath.add(typeName); + } + + final List path = new ArrayList(); + QName qname; + // start from index 1 -> module name omited + for (int i = 1; i < typePath.size(); i++) { + qname = new QName(namespace, revision, prefix, typePath.get(i)); + path.add(qname); + } + QName typeQName; + if (isBaseYangType) { + typeQName = new QName(BaseTypes.BaseTypesNamespace, typeName); + } else { + typeQName = new QName(namespace, revision, prefix, typeName); } - return type; + path.add(typeQName); + return new SchemaPath(path, true); } + /** + * Parse given context and find identityref base value. + * + * @param ctx + * type body + * @return identityref base value as String + */ public static String getIdentityrefBase(Type_body_stmtsContext ctx) { String result = null; outer: for (int i = 0; i < ctx.getChildCount(); i++) { @@ -1067,6 +1227,13 @@ public final class YangModelBuilderUtil { return result; } + /** + * Parse type body statement and find require-instance value. + * + * @param ctx + * type body context + * @return require-instance value + */ private static boolean isRequireInstance(Type_body_stmtsContext ctx) { for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); @@ -1082,6 +1249,13 @@ public final class YangModelBuilderUtil { return false; } + /** + * Parse type body statement and find leafref path. + * + * @param ctx + * type body context + * @return leafref path as String + */ private static String parseLeafrefPath(Type_body_stmtsContext ctx) { for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); @@ -1098,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 @@ -1122,8 +1296,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < context.getChildCount(); j++) { String mustPart = context.getChild(j).getText(); if (j == 0) { - mustText.append(mustPart.substring(0, - mustPart.length() - 1)); + mustText.append(mustPart.substring(0, mustPart.length() - 1)); continue; } if (j % 2 == 0) { @@ -1142,28 +1315,27 @@ public final class YangModelBuilderUtil { } } - MustDefinition must = new MustDefinitionImpl(mustText.toString(), - description, reference, errorAppTag, errorMessage); + MustDefinition must = new MustDefinitionImpl(mustText.toString(), description, reference, errorAppTag, + errorMessage); return must; } /** - * 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 */ - public static void parseConstraints(final ParseTree ctx, - final ConstraintsBuilder constraints) { + public static void parseConstraints(final ParseTree ctx, final ConstraintsBuilder constraints) { for (int i = 0; i < ctx.getChildCount(); ++i) { final ParseTree childNode = ctx.getChild(i); if (childNode instanceof Max_elements_stmtContext) { - Integer max = Integer.valueOf(stringFromNode(childNode)); - constraints.setMinElements(max); + Integer max = parseMaxElements((Max_elements_stmtContext) childNode); + constraints.setMaxElements(max); } else if (childNode instanceof Min_elements_stmtContext) { - Integer min = Integer.valueOf(stringFromNode(childNode)); + Integer min = parseMinElements((Min_elements_stmtContext) childNode); constraints.setMinElements(min); } else if (childNode instanceof Must_stmtContext) { MustDefinition must = parseMust((Must_stmtContext) childNode); @@ -1172,8 +1344,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < childNode.getChildCount(); j++) { ParseTree mandatoryTree = ctx.getChild(j); if (mandatoryTree instanceof Mandatory_argContext) { - Boolean mandatory = Boolean - .valueOf(stringFromNode(mandatoryTree)); + Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree)); constraints.setMandatory(mandatory); } } @@ -1183,6 +1354,42 @@ public final class YangModelBuilderUtil { } } + private static Integer parseMinElements(Min_elements_stmtContext ctx) { + Integer result = null; + try { + for (int j = 0; j < ctx.getChildCount(); j++) { + ParseTree minArg = ctx.getChild(j); + if (minArg instanceof Min_value_argContext) { + result = Integer.valueOf(stringFromNode(minArg)); + } + } + if (result == null) { + throw new IllegalArgumentException(); + } + return result; + } catch (Exception e) { + throw new YangParseException(ctx.getStart().getLine(), "Failed to parse min-elements.", e); + } + } + + private static Integer parseMaxElements(Max_elements_stmtContext ctx) { + Integer result = null; + try { + for (int j = 0; j < ctx.getChildCount(); j++) { + ParseTree maxArg = ctx.getChild(j); + if (maxArg instanceof Max_value_argContext) { + result = Integer.valueOf(stringFromNode(maxArg)); + } + } + if (result == null) { + throw new IllegalArgumentException(); + } + return result; + } catch (Exception e) { + throw new YangParseException(ctx.getStart().getLine(), "Failed to parse max-elements.", e); + } + } + /** * Parse given context and return yin value. * @@ -1223,8 +1430,8 @@ public final class YangModelBuilderUtil { * if this is one of YANG type which MUST contain additional * informations in its body */ - public static void checkMissingBody(final String typeName, - final String moduleName, final int line) throws YangParseException { + public static void checkMissingBody(final String typeName, final String moduleName, final int line) + throws YangParseException { if ("decimal64".equals(typeName)) { throw new YangParseException(moduleName, line, "The 'fraction-digits' statement MUST be present if the type is 'decimal64'."); @@ -1234,42 +1441,45 @@ public final class YangModelBuilderUtil { } else if ("leafref".equals(typeName)) { throw new YangParseException(moduleName, line, "The 'path' statement MUST be present if the type is 'leafref'."); - } else if("bits".equals(typeName)) { - throw new YangParseException(moduleName, line, - "The 'bit' statement MUST be present if the type is 'bits'."); - } else if("enumeration".equals(typeName)) { + } else if ("bits".equals(typeName)) { + throw new YangParseException(moduleName, line, "The 'bit' statement MUST be present if the type is 'bits'."); + } else if ("enumeration".equals(typeName)) { throw new YangParseException(moduleName, line, "The 'enum' statement MUST be present if the type is 'enumeration'."); } } - public static RefineHolder parseRefine(Refine_stmtContext child) { - final String refineTarget = stringFromNode(child); - final RefineHolder refine = new RefineHolder(refineTarget, child - .getStart().getLine()); - for (int j = 0; j < child.getChildCount(); j++) { - ParseTree refinePom = child.getChild(j); + /** + * Parse refine statement. + * + * @param refineCtx + * refine statement + * @param line + * current line in yang model + * @return RefineHolder object representing this refine statement + */ + public static RefineHolder parseRefine(Refine_stmtContext refineCtx) { + final String refineTarget = stringFromNode(refineCtx); + 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) { for (int k = 0; k < refinePom.getChildCount(); k++) { ParseTree refineStmt = refinePom.getChild(k); + parseRefineDefault(refine, refineStmt); + if (refineStmt instanceof Refine_leaf_stmtsContext) { - parseRefine(refine, - (Refine_leaf_stmtsContext) refineStmt); + parseRefine(refine, (Refine_leaf_stmtsContext) refineStmt); } else if (refineStmt instanceof Refine_container_stmtsContext) { - parseRefine(refine, - (Refine_container_stmtsContext) refineStmt); + parseRefine(refine, (Refine_container_stmtsContext) refineStmt); } else if (refineStmt instanceof Refine_list_stmtsContext) { - parseRefine(refine, - (Refine_list_stmtsContext) refineStmt); + parseRefine(refine, (Refine_list_stmtsContext) refineStmt); } else if (refineStmt instanceof Refine_leaf_list_stmtsContext) { - parseRefine(refine, - (Refine_leaf_list_stmtsContext) refineStmt); + parseRefine(refine, (Refine_leaf_list_stmtsContext) refineStmt); } else if (refineStmt instanceof Refine_choice_stmtsContext) { - parseRefine(refine, - (Refine_choice_stmtsContext) refineStmt); + parseRefine(refine, (Refine_choice_stmtsContext) refineStmt); } else if (refineStmt instanceof Refine_anyxml_stmtsContext) { - parseRefine(refine, - (Refine_anyxml_stmtsContext) refineStmt); + parseRefine(refine, (Refine_anyxml_stmtsContext) refineStmt); } } } @@ -1277,8 +1487,23 @@ public final class YangModelBuilderUtil { return refine; } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_leaf_stmtsContext refineStmt) { + private static void parseRefineDefault(RefineHolder refine, ParseTree refineStmt) { + for (int i = 0; i < refineStmt.getChildCount(); i++) { + ParseTree refineArg = refineStmt.getChild(i); + if (refineArg instanceof Description_stmtContext) { + String description = stringFromNode(refineArg); + refine.setDescription(description); + } else if (refineArg instanceof Reference_stmtContext) { + String reference = stringFromNode(refineArg); + refine.setReference(reference); + } else if (refineArg instanceof Config_stmtContext) { + Boolean config = parseConfig((Config_stmtContext) refineArg); + refine.setConfiguration(config); + } + } + } + + private static RefineHolder parseRefine(RefineHolder refine, Refine_leaf_stmtsContext refineStmt) { for (int i = 0; i < refineStmt.getChildCount(); i++) { ParseTree refineArg = refineStmt.getChild(i); if (refineArg instanceof Default_stmtContext) { @@ -1288,8 +1513,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < refineArg.getChildCount(); j++) { ParseTree mandatoryTree = refineArg.getChild(j); if (mandatoryTree instanceof Mandatory_argContext) { - Boolean mandatory = Boolean - .valueOf(stringFromNode(mandatoryTree)); + Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree)); refine.setMandatory(mandatory); } } @@ -1302,8 +1526,7 @@ public final class YangModelBuilderUtil { return refine; } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_container_stmtsContext refineStmt) { + private static RefineHolder parseRefine(RefineHolder refine, Refine_container_stmtsContext refineStmt) { for (int m = 0; m < refineStmt.getChildCount(); m++) { ParseTree refineArg = refineStmt.getChild(m); if (refineArg instanceof Must_stmtContext) { @@ -1316,44 +1539,41 @@ public final class YangModelBuilderUtil { return refine; } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_list_stmtsContext refineStmt) { + private static RefineHolder parseRefine(RefineHolder refine, Refine_list_stmtsContext refineStmt) { for (int m = 0; m < refineStmt.getChildCount(); m++) { ParseTree refineArg = refineStmt.getChild(m); if (refineArg instanceof Must_stmtContext) { MustDefinition must = parseMust((Must_stmtContext) refineArg); refine.setMust(must); } else if (refineArg instanceof Max_elements_stmtContext) { - Integer max = Integer.valueOf(stringFromNode(refineArg)); - refine.setMinElements(max); + Integer max = parseMaxElements((Max_elements_stmtContext) refineArg); + refine.setMaxElements(max); } else if (refineArg instanceof Min_elements_stmtContext) { - Integer min = Integer.valueOf(stringFromNode(refineArg)); + Integer min = parseMinElements((Min_elements_stmtContext) refineArg); refine.setMinElements(min); } } return refine; } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_leaf_list_stmtsContext refineStmt) { + private static RefineHolder parseRefine(RefineHolder refine, Refine_leaf_list_stmtsContext refineStmt) { for (int m = 0; m < refineStmt.getChildCount(); m++) { ParseTree refineArg = refineStmt.getChild(m); if (refineArg instanceof Must_stmtContext) { MustDefinition must = parseMust((Must_stmtContext) refineArg); refine.setMust(must); } else if (refineArg instanceof Max_elements_stmtContext) { - Integer max = Integer.valueOf(stringFromNode(refineArg)); - refine.setMinElements(max); + Integer max = parseMaxElements((Max_elements_stmtContext) refineArg); + refine.setMaxElements(max); } else if (refineArg instanceof Min_elements_stmtContext) { - Integer min = Integer.valueOf(stringFromNode(refineArg)); + Integer min = parseMinElements((Min_elements_stmtContext) refineArg); refine.setMinElements(min); } } return refine; } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_choice_stmtsContext refineStmt) { + private static RefineHolder parseRefine(RefineHolder refine, Refine_choice_stmtsContext refineStmt) { for (int i = 0; i < refineStmt.getChildCount(); i++) { ParseTree refineArg = refineStmt.getChild(i); if (refineArg instanceof Default_stmtContext) { @@ -1363,8 +1583,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < refineArg.getChildCount(); j++) { ParseTree mandatoryTree = refineArg.getChild(j); if (mandatoryTree instanceof Mandatory_argContext) { - Boolean mandatory = Boolean - .valueOf(stringFromNode(mandatoryTree)); + Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree)); refine.setMandatory(mandatory); } } @@ -1373,8 +1592,7 @@ public final class YangModelBuilderUtil { return refine; } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_anyxml_stmtsContext refineStmt) { + private static RefineHolder parseRefine(RefineHolder refine, Refine_anyxml_stmtsContext refineStmt) { for (int i = 0; i < refineStmt.getChildCount(); i++) { ParseTree refineArg = refineStmt.getChild(i); if (refineArg instanceof Must_stmtContext) { @@ -1384,8 +1602,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < refineArg.getChildCount(); j++) { ParseTree mandatoryTree = refineArg.getChild(j); if (mandatoryTree instanceof Mandatory_argContext) { - Boolean mandatory = Boolean - .valueOf(stringFromNode(mandatoryTree)); + Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree)); refine.setMandatory(mandatory); } }