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%2FParserUtils.java;h=b4e250d19f9e2df482e4421f5943a29e73e350d9;hb=0f393657ea0987d02556ea6e2a367d492a8e5a04;hp=84e0b041e6f5630b6de25b36c46a51533ab254c6;hpb=699f2b3912b8cfae054c08f6f9af7e0061afce57;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/ParserUtils.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/ParserUtils.java index 84e0b041e6..b4e250d19f 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/ParserUtils.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/ParserUtils.java @@ -8,6 +8,7 @@ package org.opendaylight.controller.yang.parser.util; import java.lang.reflect.Method; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -16,11 +17,45 @@ import org.opendaylight.controller.yang.model.api.ModuleImport; import org.opendaylight.controller.yang.model.api.MustDefinition; import org.opendaylight.controller.yang.model.api.SchemaPath; import org.opendaylight.controller.yang.model.api.TypeDefinition; +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.BooleanTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.DecimalTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.EmptyTypeDefinition; +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.IdentityrefTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.InstanceIdentifierTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.IntegerTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.LeafrefTypeDefinition; +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.UnionTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.UnsignedIntegerTypeDefinition; +import org.opendaylight.controller.yang.model.util.BinaryType; +import org.opendaylight.controller.yang.model.util.BitsType; +import org.opendaylight.controller.yang.model.util.BooleanType; +import org.opendaylight.controller.yang.model.util.Decimal64; +import org.opendaylight.controller.yang.model.util.EmptyType; +import org.opendaylight.controller.yang.model.util.EnumerationType; +import org.opendaylight.controller.yang.model.util.IdentityrefType; +import org.opendaylight.controller.yang.model.util.InstanceIdentifier; +import org.opendaylight.controller.yang.model.util.Int16; +import org.opendaylight.controller.yang.model.util.Int32; +import org.opendaylight.controller.yang.model.util.Int64; +import org.opendaylight.controller.yang.model.util.Int8; +import org.opendaylight.controller.yang.model.util.Leafref; +import org.opendaylight.controller.yang.model.util.StringType; +import org.opendaylight.controller.yang.model.util.UnionType; import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder; import org.opendaylight.controller.yang.parser.builder.api.Builder; import org.opendaylight.controller.yang.parser.builder.api.ChildNodeBuilder; import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder; import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder; +import org.opendaylight.controller.yang.parser.builder.api.SchemaNodeBuilder; +import org.opendaylight.controller.yang.parser.builder.api.TypeAwareBuilder; import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder; import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder; import org.opendaylight.controller.yang.parser.builder.impl.AnyXmlBuilder; @@ -28,11 +63,14 @@ 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.ContainerSchemaNodeBuilder; +import org.opendaylight.controller.yang.parser.builder.impl.GroupingBuilderImpl; import org.opendaylight.controller.yang.parser.builder.impl.LeafListSchemaNodeBuilder; import org.opendaylight.controller.yang.parser.builder.impl.LeafSchemaNodeBuilder; import org.opendaylight.controller.yang.parser.builder.impl.ListSchemaNodeBuilder; import org.opendaylight.controller.yang.parser.builder.impl.ModuleBuilder; +import org.opendaylight.controller.yang.parser.builder.impl.TypedefBuilder; import org.opendaylight.controller.yang.parser.builder.impl.UnknownSchemaNodeBuilder; +import org.opendaylight.controller.yang.parser.builder.impl.UsesNodeBuilderImpl; public final class ParserUtils { @@ -98,29 +136,170 @@ public final class ParserUtils { final ChildNodeBuilder target) { for (DataSchemaNodeBuilder builder : augment.getChildNodes()) { builder.setAugmenting(true); - correctPath(augment, target.getPath()); + correctAugmentChildPath(augment, target.getPath()); target.addChildNode(builder); } } - private static void correctPath(final ChildNodeBuilder node, + private static void correctAugmentChildPath(final ChildNodeBuilder node, final SchemaPath parentSchemaPath) { for (DataSchemaNodeBuilder builder : node.getChildNodes()) { // add correct path - SchemaPath targetNodeSchemaPath = parentSchemaPath; List targetNodePath = new ArrayList( - targetNodeSchemaPath.getPath()); + parentSchemaPath.getPath()); targetNodePath.add(builder.getQName()); builder.setPath(new SchemaPath(targetNodePath, true)); if (builder instanceof ChildNodeBuilder) { ChildNodeBuilder cnb = (ChildNodeBuilder) builder; - correctPath(cnb, builder.getPath()); + correctAugmentChildPath(cnb, builder.getPath()); + } + + // if child can contains type, correct path for this type too + if(builder instanceof TypeAwareBuilder) { + TypeAwareBuilder nodeBuilder = (TypeAwareBuilder)builder; + QName nodeBuilderQName = nodeBuilder.getQName(); + TypeDefinition nodeBuilderType = nodeBuilder.getType(); + if(nodeBuilderType != null) { + TypeDefinition newType = createCorrectTypeDefinition(parentSchemaPath, nodeBuilderQName, nodeBuilderType); + nodeBuilder.setType(newType); + } else { + TypeDefinitionBuilder nodeBuilderTypedef = nodeBuilder.getTypedef(); + SchemaPath newSchemaPath = createNewSchemaPath(nodeBuilderTypedef.getPath(), nodeBuilderQName, nodeBuilderTypedef.getQName()); + nodeBuilderTypedef.setPath(newSchemaPath); + } } } } + private static TypeDefinition createCorrectTypeDefinition(SchemaPath parentSchemaPath, QName nodeQName, TypeDefinition nodeType) { + TypeDefinition result = null; + SchemaPath newSchemaPath = null; + if(nodeType != null) { + if(nodeType instanceof BinaryTypeDefinition) { + BinaryTypeDefinition binType = (BinaryTypeDefinition)nodeType; + newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, binType.getQName()); + List bytes = (List)binType.getDefaultValue(); + result = new BinaryType(newSchemaPath, bytes, binType.getLengthConstraints(), binType.getUnits()); + } else if(nodeType instanceof BitsTypeDefinition) { + BitsTypeDefinition bitsType = (BitsTypeDefinition)nodeType; + newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, nodeType.getQName()); + result = new BitsType(newSchemaPath, bitsType.getBits(), bitsType.getUnits()); + } else if(nodeType instanceof BooleanTypeDefinition) { + BooleanTypeDefinition booleanType = (BooleanTypeDefinition)nodeType; + newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, booleanType.getQName()); + result = new BooleanType(newSchemaPath, (Boolean)booleanType.getDefaultValue(), booleanType.getUnits()); + } else if(nodeType instanceof DecimalTypeDefinition) { + DecimalTypeDefinition decimalType = (DecimalTypeDefinition)nodeType; + newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, decimalType.getQName()); + BigDecimal defaultValue = (BigDecimal)decimalType.getDefaultValue(); + result = new Decimal64(newSchemaPath, decimalType.getUnits(), defaultValue, decimalType.getRangeStatements(), decimalType.getFractionDigits()); + } else if(nodeType instanceof EmptyTypeDefinition) { + newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, nodeType.getQName()); + result = new EmptyType(newSchemaPath); + } else if(nodeType instanceof EnumTypeDefinition) { + EnumTypeDefinition enumType = (EnumTypeDefinition)nodeType; + newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, enumType.getQName()); + result = new EnumerationType(newSchemaPath, (EnumPair)enumType.getDefaultValue(), enumType.getValues(), enumType.getUnits()); + } else if(nodeType instanceof IdentityrefTypeDefinition) { + IdentityrefTypeDefinition idrefType = (IdentityrefTypeDefinition)nodeType; + newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, idrefType.getQName()); + result = new IdentityrefType(idrefType.getIdentity(), newSchemaPath); + } else if(nodeType instanceof InstanceIdentifierTypeDefinition) { + InstanceIdentifierTypeDefinition instIdType = (InstanceIdentifierTypeDefinition)nodeType; + newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, instIdType.getQName()); + return new InstanceIdentifier(newSchemaPath, instIdType.getPathStatement(), instIdType.requireInstance()); + } else if(nodeType instanceof StringTypeDefinition) { + result = copyStringType(parentSchemaPath, nodeQName, (StringTypeDefinition)nodeType); + } else if(nodeType instanceof IntegerTypeDefinition) { + result = copyIntType(parentSchemaPath, nodeQName, (IntegerTypeDefinition)nodeType); + } else if(nodeType instanceof UnsignedIntegerTypeDefinition) { + result = copyUIntType(parentSchemaPath, nodeQName, (UnsignedIntegerTypeDefinition)nodeType); + } else if(nodeType instanceof LeafrefTypeDefinition) { + newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, nodeType.getQName()); + result = new Leafref(newSchemaPath, ((LeafrefTypeDefinition)nodeType).getPathStatement()); + } else if(nodeType instanceof UnionTypeDefinition) { + UnionTypeDefinition unionType = (UnionTypeDefinition)nodeType; + newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, unionType.getQName()); + return new UnionType(newSchemaPath, unionType.getTypes()); + } + } + return result; + } + + private static TypeDefinition copyStringType(SchemaPath schemaPath, QName nodeQName, StringTypeDefinition nodeType) { + List path = schemaPath.getPath(); + List newPath = new ArrayList(path); + newPath.add(nodeQName); + newPath.add(nodeType.getQName()); + SchemaPath newSchemaPath = new SchemaPath(newPath, schemaPath.isAbsolute()); + + String newDefault = nodeType.getDefaultValue().toString(); + String newUnits = nodeType.getUnits(); + List lengths = nodeType.getLengthStatements(); + List patterns = nodeType.getPatterns(); + + return new StringType(newSchemaPath, newDefault, lengths, patterns, newUnits); + } + + private static TypeDefinition copyIntType(SchemaPath schemaPath, QName nodeQName, IntegerTypeDefinition type) { + QName typeQName = type.getQName(); + SchemaPath newSchemaPath = createNewSchemaPath(schemaPath, nodeQName, typeQName); + + String localName = typeQName.getLocalName(); + List ranges = type.getRangeStatements(); + String units = type.getUnits(); + + if("int8".equals(localName)) { + Byte defaultValue = (Byte)type.getDefaultValue(); + return new Int8(newSchemaPath, ranges, units, defaultValue); + } else if("int16".equals(localName)) { + Short defaultValue = (Short)type.getDefaultValue(); + return new Int16(newSchemaPath, ranges, units, defaultValue); + } else if("int32".equals(localName)) { + Integer defaultValue = (Integer)type.getDefaultValue(); + return new Int32(newSchemaPath, ranges, units, defaultValue); + } else if("int64".equals(localName)) { + Long defaultValue = (Long)type.getDefaultValue(); + return new Int64(newSchemaPath, ranges, units, defaultValue); + } else { + return null; + } + } + + private static TypeDefinition copyUIntType(SchemaPath schemaPath, QName nodeQName, UnsignedIntegerTypeDefinition type) { + QName typeQName = type.getQName(); + SchemaPath newSchemaPath = createNewSchemaPath(schemaPath, nodeQName, typeQName); + + String localName = typeQName.getLocalName(); + List ranges = type.getRangeStatements(); + String units = type.getUnits(); + + if("uint8".equals(localName)) { + Byte defaultValue = (Byte)type.getDefaultValue(); + return new Int8(newSchemaPath, ranges, units, defaultValue); + } else if("uint16".equals(localName)) { + Short defaultValue = (Short)type.getDefaultValue(); + return new Int16(newSchemaPath, ranges, units, defaultValue); + } else if("uint32".equals(localName)) { + Integer defaultValue = (Integer)type.getDefaultValue(); + return new Int32(newSchemaPath, ranges, units, defaultValue); + } else if("uint64".equals(localName)) { + Long defaultValue = (Long)type.getDefaultValue(); + return new Int64(newSchemaPath, ranges, units, defaultValue); + } else { + return null; + } + } + + private static SchemaPath createNewSchemaPath(SchemaPath schemaPath, QName currentQName, QName qname) { + List newPath = new ArrayList(schemaPath.getPath()); + newPath.add(currentQName); + newPath.add(qname); + return new SchemaPath(newPath, schemaPath.isAbsolute()); + } + public static void refineLeaf(LeafSchemaNodeBuilder leaf, RefineHolder refine, int line) { String defaultStr = refine.getDefaultStr(); @@ -264,8 +443,7 @@ public final class ParserUtils { * @param line * current line in yang model */ - public static void refineDefault(Builder node, RefineHolder refine, - int line) { + public static void refineDefault(Builder node, RefineHolder refine, int line) { Class cls = node.getClass(); String description = refine.getDescription(); @@ -355,9 +533,6 @@ public final class ParserUtils { for (UsesNodeBuilder use : old.getUsesNodes()) { copy.addUsesNode(use); } - for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) { - copy.addUnknownSchemaNode(unknown); - } copy.setDescription(old.getDescription()); copy.setReference(old.getReference()); copy.setStatus(old.getStatus()); @@ -391,9 +566,6 @@ public final class ParserUtils { for (UsesNodeBuilder use : old.getUsesNodes()) { copy.addUsesNode(use); } - for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) { - copy.addUnknownSchemaNode(unknown); - } copy.setDescription(old.getDescription()); copy.setReference(old.getReference()); copy.setStatus(old.getStatus()); @@ -418,9 +590,6 @@ public final class ParserUtils { for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) { copy.addUnknownSchemaNode(unknown); } - for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) { - copy.addUnknownSchemaNode(unknown); - } copy.setDescription(old.getDescription()); copy.setReference(old.getReference()); copy.setStatus(old.getStatus()); @@ -447,9 +616,6 @@ public final class ParserUtils { for (UsesNodeBuilder use : old.getUsesNodes()) { copy.addUsesNode(use); } - for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) { - copy.addUnknownSchemaNode(unknown); - } copy.setDefaultCase(old.getDefaultCase()); copy.setDescription(old.getDescription()); copy.setReference(old.getReference()); @@ -474,6 +640,72 @@ public final class ParserUtils { return copy; } + public static GroupingBuilder copyGroupingBuilder(final GroupingBuilder old) { + final GroupingBuilder copy = new GroupingBuilderImpl(old.getQName(), + old.getLine()); + copy.setPath(old.getPath()); + for (DataSchemaNodeBuilder child : old.getChildNodes()) { + copy.addChildNode(child); + } + for (GroupingBuilder grouping : old.getGroupings()) { + copy.addGrouping(grouping); + } + for (TypeDefinitionBuilder typedef : old.getTypedefs()) { + copy.addTypedef(typedef); + } + for (UsesNodeBuilder use : old.getUses()) { + copy.addUsesNode(use); + } + for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) { + copy.addUnknownSchemaNode(unknown); + } + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + return copy; + } + + public static TypedefBuilder copyTypedefBuilder(TypedefBuilder old) { + final TypedefBuilder copy = new TypedefBuilder(old.getQName(), + old.getLine()); + copy.setPath(old.getPath()); + copy.setDefaultValue(old.getDefaultValue()); + copy.setUnits(old.getUnits()); + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + + copy.setRanges(old.getRanges()); + copy.setLengths(old.getLengths()); + copy.setPatterns(old.getPatterns()); + copy.setFractionDigits(old.getFractionDigits()); + + TypeDefinition type = old.getType(); + if(type == null) { + copy.setType(old.getTypedef()); + } else { + copy.setType(old.getType()); + } + copy.setUnits(old.getUnits()); + for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) { + copy.addUnknownSchemaNode(unknown); + } + return copy; + } + + public static UsesNodeBuilder copyUsesNodeBuilder(UsesNodeBuilder old) { + final UsesNodeBuilder copy = new UsesNodeBuilderImpl( + old.getGroupingPathString(), old.getLine()); + for (AugmentationSchemaBuilder augment : old.getAugmentations()) { + copy.addAugment(augment); + } + copy.setAugmenting(old.isAugmenting()); + for (SchemaNodeBuilder refineNode : old.getRefineNodes()) { + copy.addRefineNode(refineNode); + } + return copy; + } + private static void copyConstraints(final DataSchemaNodeBuilder oldBuilder, final DataSchemaNodeBuilder newBuilder) { final ConstraintsBuilder oldConstraints = oldBuilder.getConstraints();