X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fyang-model-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang%2Fparser%2Fbuilder%2Fimpl%2FModuleBuilder.java;h=67e4c6fb72cef7e1e1dd5d566a72992fb31dea6e;hp=740f39916843f8cf27697390a827bcf2bedfa7a5;hb=00ebaa790e2bffbf3b05f52e842c28c6ae6df248;hpb=699f2b3912b8cfae054c08f6f9af7e0061afce57 diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/ModuleBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/ModuleBuilder.java index 740f399168..67e4c6fb72 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/ModuleBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/ModuleBuilder.java @@ -13,6 +13,7 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -34,7 +35,7 @@ import org.opendaylight.controller.yang.model.api.TypeDefinition; import org.opendaylight.controller.yang.model.api.UsesNode; 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.DataNodeContainerBuilder; 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; @@ -61,16 +62,11 @@ public class ModuleBuilder implements Builder { private final Set imports = new HashSet(); - /** - * All nodes, that can contain other nodes - */ - private final Map, Builder> moduleNodes = new HashMap, Builder>(); - /** * Holds all child (DataSchemaNode) nodes: anyxml, choice, case, container, * list, leaf, leaf-list. */ - private final Map, DataSchemaNodeBuilder> addedChilds = new HashMap, DataSchemaNodeBuilder>(); + private final Map, DataSchemaNodeBuilder> childNodes = new HashMap, DataSchemaNodeBuilder>(); private final Map, GroupingBuilder> addedGroupings = new HashMap, GroupingBuilder>(); private final List addedAugments = new ArrayList(); @@ -79,13 +75,16 @@ public class ModuleBuilder implements Builder { private final Set addedNotifications = new HashSet(); private final Set addedIdentities = new HashSet(); private final Map, FeatureBuilder> addedFeatures = new HashMap, FeatureBuilder>(); - private final Map addedDeviations = new HashMap(); + private final Map, DeviationBuilder> addedDeviations = new HashMap, DeviationBuilder>(); private final Map, TypeDefinitionBuilder> addedTypedefs = new HashMap, TypeDefinitionBuilder>(); + private final Map, UnionTypeBuilder> addedUnionTypes = new HashMap, UnionTypeBuilder>(); private final List addedExtensions = new ArrayList(); private final Set addedUnknownNodes = new HashSet(); private final Map, TypeAwareBuilder> dirtyNodes = new HashMap, TypeAwareBuilder>(); + private final LinkedList actualPath = new LinkedList(); + public ModuleBuilder(final String name) { this.name = name; instance = new ModuleImpl(name); @@ -106,8 +105,8 @@ public class ModuleBuilder implements Builder { instance.setTypeDefinitions(typedefs); // CHILD NODES - final Map childNodes = buildModuleChildNodes(addedChilds); - instance.setChildNodes(childNodes); + final Map children = buildModuleChildNodes(childNodes); + instance.setChildNodes(children); // GROUPINGS final Set groupings = buildModuleGroupings(addedGroupings); @@ -141,7 +140,7 @@ public class ModuleBuilder implements Builder { // DEVIATIONS final Set deviations = new HashSet(); - for (Map.Entry entry : addedDeviations + for (Map.Entry, DeviationBuilder> entry : addedDeviations .entrySet()) { deviations.add(entry.getValue().build()); } @@ -169,45 +168,69 @@ public class ModuleBuilder implements Builder { return 0; } - public Builder getNode(final List path) { - return moduleNodes.get(path); + public void enterNode(final Builder node) { + actualPath.push(node); + } + + public void exitNode() { + actualPath.pop(); + } + + public Builder getActualNode() { + if (actualPath.isEmpty()) { + return null; + } else { + return actualPath.get(0); + } + } + + public Builder getModuleNode(final List path) { + return childNodes.get(path); + } + + public GroupingBuilder getGrouping(final List path) { + return addedGroupings.get(path); + } + + public Builder getModuleTypedef(final List path) { + return addedTypedefs.get(path); } public Set getChildNodes() { - final Set childNodes = new HashSet(); - for (Map.Entry, DataSchemaNodeBuilder> entry : addedChilds + final Set children = new HashSet(); + for (Map.Entry, DataSchemaNodeBuilder> entry : childNodes .entrySet()) { - List path = entry.getKey(); - DataSchemaNodeBuilder child = entry.getValue(); + final List path = entry.getKey(); + final DataSchemaNodeBuilder child = entry.getValue(); if (path.size() == 2) { - childNodes.add(child); + children.add(child); } } - return childNodes; + return children; } public Map, TypeAwareBuilder> getDirtyNodes() { return dirtyNodes; } - public List getAddedAugments() { + public List getAugments() { return addedAugments; } - public Set getAddedIdentities() { + public Set getIdentities() { return addedIdentities; } - public Map, UsesNodeBuilder> getAddedUsesNodes() { + public Map, UsesNodeBuilder> getUsesNodes() { return addedUsesNodes; } - public Set getAddedUnknownNodes() { + public Set getUnknownNodes() { return addedUnknownNodes; } public Set getModuleTypedefs() { - Set typedefs = new HashSet(); + final Set typedefs = new HashSet(); for (Map.Entry, TypeDefinitionBuilder> entry : addedTypedefs .entrySet()) { if (entry.getKey().size() == 2) { @@ -247,8 +270,8 @@ public class ModuleBuilder implements Builder { public void addDirtyNode(final List path) { final List dirtyNodePath = new ArrayList(path); - final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) moduleNodes - .get(dirtyNodePath); + final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) actualPath + .getFirst(); dirtyNodes.put(dirtyNodePath, nodeBuilder); } @@ -303,18 +326,10 @@ public class ModuleBuilder implements Builder { final List pathToNode = new ArrayList(parentPath); final ContainerSchemaNodeBuilder containerBuilder = new ContainerSchemaNodeBuilder( containerName, line); - final ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes - .get(pathToNode); - if (parent != null) { - if (parent instanceof AugmentationSchemaBuilder) { - containerBuilder.setAugmenting(true); - } - parent.addChildNode(containerBuilder); - } + updateParent(containerBuilder, line, "container"); pathToNode.add(containerName.getLocalName()); - moduleNodes.put(pathToNode, containerBuilder); - addedChilds.put(pathToNode, containerBuilder); + childNodes.put(pathToNode, containerBuilder); return containerBuilder; } @@ -324,18 +339,10 @@ public class ModuleBuilder implements Builder { final List pathToNode = new ArrayList(parentPath); final ListSchemaNodeBuilder listBuilder = new ListSchemaNodeBuilder( listName, line); - final ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes - .get(pathToNode); - if (parent != null) { - if (parent instanceof AugmentationSchemaBuilder) { - listBuilder.setAugmenting(true); - } - parent.addChildNode(listBuilder); - } + updateParent(listBuilder, line, "list"); pathToNode.add(listName.getLocalName()); - moduleNodes.put(pathToNode, listBuilder); - addedChilds.put(pathToNode, listBuilder); + childNodes.put(pathToNode, listBuilder); return listBuilder; } @@ -345,39 +352,23 @@ public class ModuleBuilder implements Builder { final List pathToNode = new ArrayList(parentPath); final LeafSchemaNodeBuilder leafBuilder = new LeafSchemaNodeBuilder( leafName, line); - final ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes - .get(pathToNode); - if (parent != null) { - if (parent instanceof AugmentationSchemaBuilder) { - leafBuilder.setAugmenting(true); - } - parent.addChildNode(leafBuilder); - } + updateParent(leafBuilder, line, "leaf"); pathToNode.add(leafName.getLocalName()); - addedChilds.put(pathToNode, leafBuilder); - moduleNodes.put(pathToNode, leafBuilder); + childNodes.put(pathToNode, leafBuilder); return leafBuilder; } - public LeafListSchemaNodeBuilder addLeafListNode(final QName leafListName, + public LeafListSchemaNodeBuilder addLeafListNode(final QName qname, final List parentPath, final int line) { final List pathToNode = new ArrayList(parentPath); final LeafListSchemaNodeBuilder leafListBuilder = new LeafListSchemaNodeBuilder( - leafListName, line); - final ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes - .get(pathToNode); - if (parent != null) { - if (parent instanceof AugmentationSchemaBuilder) { - leafListBuilder.setAugmenting(true); - } - parent.addChildNode(leafListBuilder); - } + qname, line); + updateParent(leafListBuilder, line, "leaf-list"); - pathToNode.add(leafListName.getLocalName()); - addedChilds.put(pathToNode, leafListBuilder); - moduleNodes.put(pathToNode, leafListBuilder); + pathToNode.add(qname.getLocalName()); + childNodes.put(pathToNode, leafListBuilder); return leafListBuilder; } @@ -386,15 +377,18 @@ public class ModuleBuilder implements Builder { final List parentPath, final int line) { final List pathToGroup = new ArrayList(parentPath); final GroupingBuilder builder = new GroupingBuilderImpl(qname, line); - final ChildNodeBuilder parentNodeBuilder = (ChildNodeBuilder) moduleNodes - .get(pathToGroup); - if (parentNodeBuilder != null) { - parentNodeBuilder.addGrouping(builder); + + if (!(actualPath.isEmpty())) { + final Builder parent = actualPath.getFirst(); + if (parent instanceof DataNodeContainerBuilder) { + ((DataNodeContainerBuilder) parent).addGrouping(builder); + } else { + throw new YangParseException(name, line, + "Unresolved parent of grouping " + qname.getLocalName()); + } } - pathToGroup.add("grouping"); pathToGroup.add(qname.getLocalName()); - moduleNodes.put(pathToGroup, builder); addedGroupings.put(pathToGroup, builder); return builder; @@ -407,13 +401,17 @@ public class ModuleBuilder implements Builder { name, line); // augment can only be in 'module' or 'uses' statement - final UsesNodeBuilder parent = addedUsesNodes.get(pathToAugment); - if (parent != null) { - parent.addAugment(builder); + if (!(actualPath.isEmpty())) { + final Builder parent = actualPath.getFirst(); + if (parent instanceof UsesNodeBuilder) { + ((UsesNodeBuilder) parent).addAugment(builder); + } else { + throw new YangParseException(this.name, line, + "Unresolved parent of augment " + name); + } } pathToAugment.add(name); - moduleNodes.put(pathToAugment, builder); addedAugments.add(builder); return builder; @@ -424,34 +422,53 @@ public class ModuleBuilder implements Builder { final List pathToUses = new ArrayList(parentPath); final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl( groupingPathStr, line); - final ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes - .get(pathToUses); - if (parent != null) { - parent.addUsesNode(usesBuilder); + + if (!(actualPath.isEmpty())) { + final Builder parent = actualPath.getFirst(); + if (parent instanceof DataNodeContainerBuilder) { + if (parent instanceof AugmentationSchemaBuilder) { + usesBuilder.setAugmenting(true); + } + ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder); + } else { + throw new YangParseException(name, line, + "Unresolved parent of uses " + groupingPathStr); + } } pathToUses.add(groupingPathStr); addedUsesNodes.put(pathToUses, usesBuilder); - moduleNodes.put(pathToUses, usesBuilder); return usesBuilder; } public void addRefine(final RefineHolder refine, final List parentPath) { final List path = new ArrayList(parentPath); - final Builder parent = moduleNodes.get(path); - if (!(parent instanceof UsesNodeBuilder)) { - throw new YangParseException("Failed to parse refine " - + refine.getName()); + + if (actualPath.isEmpty()) { + throw new YangParseException(name, refine.getLine(), + "refine can be defined only in uses statement"); + } else { + final Builder parent = actualPath.getFirst(); + if (parent instanceof UsesNodeBuilder) { + ((UsesNodeBuilder) parent).addRefine(refine); + } else { + throw new YangParseException(name, refine.getLine(), + "refine can be defined only in uses statement"); + } } - UsesNodeBuilder usesBuilder = (UsesNodeBuilder) parent; - usesBuilder.addRefine(refine); + path.add(refine.getName()); - moduleNodes.put(path, refine); } public RpcDefinitionBuilder addRpc(final QName qname, final List parentPath, final int line) { + + if (!(actualPath.isEmpty())) { + throw new YangParseException(name, line, + "rpc can be defined only in module or submodule"); + } + final List pathToRpc = new ArrayList(parentPath); final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(qname, line); @@ -459,37 +476,51 @@ public class ModuleBuilder implements Builder { pathToRpc.add(qname.getLocalName()); addedRpcs.put(pathToRpc, rpcBuilder); - final QName inputQName = new QName(qname.getNamespace(), - qname.getRevision(), qname.getPrefix(), "input"); + return rpcBuilder; + } + + public ContainerSchemaNodeBuilder addRpcInput(final QName inputQName, + final int line) { + final Builder parent = actualPath.getFirst(); + if (!(parent instanceof RpcDefinitionBuilder)) { + throw new YangParseException(name, line, + "input can be defined only in rpc statement"); + } + final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent; + final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder( inputQName, line); - final List pathToInput = new ArrayList(pathToRpc); - pathToInput.add("input"); - moduleNodes.put(pathToInput, inputBuilder); - rpcBuilder.setInput(inputBuilder); + rpc.setInput(inputBuilder); + return inputBuilder; + } + + public ContainerSchemaNodeBuilder addRpcOutput(final QName outputQName, + final int line) { + final Builder parent = actualPath.getFirst(); + if (!(parent instanceof RpcDefinitionBuilder)) { + throw new YangParseException(name, line, + "output can be defined only in rpc statement"); + } + final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent; - final QName outputQName = new QName(qname.getNamespace(), - qname.getRevision(), qname.getPrefix(), "output"); final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder( outputQName, line); - final List pathToOutput = new ArrayList(pathToRpc); - pathToOutput.add("output"); - moduleNodes.put(pathToOutput, outputBuilder); - rpcBuilder.setOutput(outputBuilder); - - return rpcBuilder; + rpc.setOutput(outputBuilder); + return outputBuilder; } public NotificationBuilder addNotification(final QName notificationName, final List parentPath, final int line) { - final List pathToNotification = new ArrayList( - parentPath); + if (!(actualPath.isEmpty())) { + throw new YangParseException(name, line, + "notification can be defined only in module or submodule"); + } - NotificationBuilder builder = new NotificationBuilder(notificationName, - line); + final NotificationBuilder builder = new NotificationBuilder( + notificationName, line); - pathToNotification.add(notificationName.getLocalName()); - moduleNodes.put(pathToNotification, builder); + final List notificationPath = new ArrayList(parentPath); + notificationPath.add(notificationName.getLocalName()); addedNotifications.add(builder); return builder; @@ -497,179 +528,247 @@ public class ModuleBuilder implements Builder { public FeatureBuilder addFeature(final QName featureName, final List parentPath, final int line) { - List pathToFeature = new ArrayList(parentPath); + if (!(actualPath.isEmpty())) { + throw new YangParseException(name, line, + "feature can be defined only in module or submodule"); + } + + final List pathToFeature = new ArrayList(parentPath); pathToFeature.add(featureName.getLocalName()); - FeatureBuilder builder = new FeatureBuilder(featureName, line); + final FeatureBuilder builder = new FeatureBuilder(featureName, line); addedFeatures.put(pathToFeature, builder); return builder; } public ChoiceBuilder addChoice(final QName choiceName, final List parentPath, final int line) { - List pathToChoice = new ArrayList(parentPath); - ChoiceBuilder builder = new ChoiceBuilder(choiceName, line); - - final ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes - .get(pathToChoice); - if (parent != null) { - if (parent instanceof AugmentationSchemaBuilder) { - builder.setAugmenting(true); + final List pathToChoice = new ArrayList(parentPath); + final ChoiceBuilder builder = new ChoiceBuilder(choiceName, line); + + if (!(actualPath.isEmpty())) { + Builder parent = actualPath.getFirst(); + if (parent instanceof DataNodeContainerBuilder) { + if (parent instanceof AugmentationSchemaBuilder) { + builder.setAugmenting(true); + } + ((DataNodeContainerBuilder) parent).addChildNode(builder); + } else { + throw new YangParseException(name, line, + "Unresolved parent of choice " + + choiceName.getLocalName()); } - parent.addChildNode(builder); } pathToChoice.add(choiceName.getLocalName()); - addedChilds.put(pathToChoice, builder); - moduleNodes.put(pathToChoice, builder); + childNodes.put(pathToChoice, builder); return builder; } public ChoiceCaseBuilder addCase(final QName caseName, final List parentPath, final int line) { - List pathToCase = new ArrayList(parentPath); - ChoiceCaseBuilder builder = new ChoiceCaseBuilder(caseName, line); - - final ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes - .get(pathToCase); - if (parent != null) { - if (parent instanceof AugmentationSchemaBuilder) { + final List pathToCase = new ArrayList(parentPath); + final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(caseName, line); + + if (actualPath.isEmpty()) { + throw new YangParseException(name, line, "'case' parent not found"); + } else { + final Builder parent = actualPath.getFirst(); + if (parent instanceof ChoiceBuilder) { + ((ChoiceBuilder) parent).addChildNode(builder); + } else if (parent instanceof AugmentationSchemaBuilder) { builder.setAugmenting(true); + ((AugmentationSchemaBuilder) parent).addChildNode(builder); + } else { + throw new YangParseException(name, line, + "Unresolved parent of 'case' " + + caseName.getLocalName()); } - parent.addChildNode(builder); } pathToCase.add(caseName.getLocalName()); - moduleNodes.put(pathToCase, builder); + childNodes.put(pathToCase, builder); return builder; } public AnyXmlBuilder addAnyXml(final QName anyXmlName, final List parentPath, final int line) { - List pathToAnyXml = new ArrayList(parentPath); - AnyXmlBuilder builder = new AnyXmlBuilder(anyXmlName, line); - - final ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes - .get(pathToAnyXml); - if (parent != null) { - if (parent instanceof AugmentationSchemaBuilder) { - throw new YangParseException( - "An anyxml node cannot be augmented."); - } - parent.addChildNode(builder); - } + final List pathToAnyXml = new ArrayList(parentPath); + final AnyXmlBuilder builder = new AnyXmlBuilder(anyXmlName, line); + updateParent(builder, line, "anyxml"); pathToAnyXml.add(anyXmlName.getLocalName()); - addedChilds.put(pathToAnyXml, builder); - moduleNodes.put(pathToAnyXml, builder); + childNodes.put(pathToAnyXml, builder); return builder; } - public TypedefBuilder addTypedef(final QName typeDefName, + public TypeDefinitionBuilderImpl addTypedef(final QName typeDefName, final List parentPath, final int line) { - List pathToType = new ArrayList(parentPath); - TypedefBuilder builder = new TypedefBuilder(typeDefName, line); - TypeDefinitionAwareBuilder parent = (TypeDefinitionAwareBuilder) moduleNodes - .get(pathToType); - if (parent != null) { - parent.addTypedef(builder); + final List pathToType = new ArrayList(parentPath); + final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl( + typeDefName, line); + + if (!(actualPath.isEmpty())) { + final Builder parent = actualPath.getFirst(); + if (parent instanceof TypeDefinitionAwareBuilder) { + ((TypeDefinitionAwareBuilder) parent).addTypedef(builder); + } else { + throw new YangParseException(name, line, + "Unresolved parent of typedef " + + typeDefName.getLocalName()); + } } + pathToType.add(typeDefName.getLocalName()); addedTypedefs.put(pathToType, builder); - moduleNodes.put(pathToType, builder); return builder; } - public void setType(TypeDefinition type, List parentPath) { - TypeAwareBuilder parent = (TypeAwareBuilder) moduleNodes - .get(parentPath); - if (parent == null) { - throw new YangParseException("Failed to set type '" - + type.getQName().getLocalName() - + "'. Parent node not found."); + public void setType(final TypeDefinition type, + final List parentPath) { + + if (!(actualPath.isEmpty())) { + final Builder parent = actualPath.getFirst(); + if (parent instanceof TypeAwareBuilder) { + ((TypeAwareBuilder) parent).setType(type); + } else { + throw new YangParseException("Failed to set type '" + + type.getQName().getLocalName() + + "'. Unknown parent node: " + parent); + } } - parent.setType(type); } - public void addUnionType(final List actualPath, + public UnionTypeBuilder addUnionType(final List currentPath, final URI namespace, final Date revision, final int line) { - List pathToUnion = new ArrayList(actualPath); - TypeAwareBuilder parent = (TypeAwareBuilder) moduleNodes - .get(pathToUnion); - UnionTypeBuilder union = new UnionTypeBuilder(pathToUnion, namespace, - revision, line); - parent.setType(union); + final List pathToUnion = new ArrayList(currentPath); + final UnionTypeBuilder union = new UnionTypeBuilder(line); + + if (actualPath.isEmpty()) { + throw new YangParseException(line, "union error"); + } else { + final Builder parent = actualPath.getFirst(); + if (parent instanceof TypeAwareBuilder) { + + ((TypeAwareBuilder) parent).setTypedef(union); - List path = new ArrayList(pathToUnion); - path.add("union"); + final List path = new ArrayList(pathToUnion); + path.add("union"); - moduleNodes.put(path, union); + addedUnionTypes.put(path, union); + return union; + } else { + throw new YangParseException(name, line, + "Unresolved parent of union type."); + } + } } public void addIdentityrefType(final String baseString, final List parentPath, final SchemaPath schemaPath, final int line) { - List pathToIdentityref = new ArrayList(parentPath); - TypeAwareBuilder parent = (TypeAwareBuilder) moduleNodes - .get(pathToIdentityref); - IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder( + final List pathToIdentityref = new ArrayList(parentPath); + final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder( baseString, schemaPath, line); - parent.setType(identityref); - dirtyNodes.put(pathToIdentityref, parent); + + if (actualPath.isEmpty()) { + throw new YangParseException(line, "identityref error"); + } else { + final Builder parent = actualPath.getFirst(); + if (parent instanceof TypeAwareBuilder) { + final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent; + typeParent.setTypedef(identityref); + dirtyNodes.put(pathToIdentityref, typeParent); + } else { + throw new YangParseException(name, line, + "Unresolved parent of identityref type."); + } + } } public DeviationBuilder addDeviation(final String targetPath, final List parentPath, final int line) { + if (!(actualPath.isEmpty())) { + throw new YangParseException(name, line, + "deviation can be defined only in module or submodule"); + } + final List pathToDeviation = new ArrayList(parentPath); pathToDeviation.add(targetPath); - DeviationBuilder builder = new DeviationBuilder(targetPath, line); - addedDeviations.put(targetPath, builder); - moduleNodes.put(pathToDeviation, builder); + final DeviationBuilder builder = new DeviationBuilder(targetPath, line); + addedDeviations.put(pathToDeviation, builder); return builder; } public IdentitySchemaNodeBuilder addIdentity(final QName qname, final List parentPath, final int line) { + if (!(actualPath.isEmpty())) { + throw new YangParseException(name, line, + "identity can be defined only in module or submodule"); + } + final List pathToIdentity = new ArrayList(parentPath); final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder( qname, line); pathToIdentity.add(qname.getLocalName()); - moduleNodes.put(pathToIdentity, builder); addedIdentities.add(builder); return builder; } - public void addConfiguration(boolean configuration, List parentPath) { - Builder builder = moduleNodes.get(parentPath); - // current api did not support adding config to deviate - if (!(builder instanceof DeviationBuilder)) { - if(builder instanceof RefineHolder) { - ((RefineHolder)builder).setConfig(configuration); + public void addConfiguration(final boolean configuration, + final List parentPath, final int line) { + if (actualPath.isEmpty()) { + throw new YangParseException(name, line, + "Parent node of config statement not found."); + } else { + final Builder parent = actualPath.getFirst(); + if (parent instanceof DataSchemaNodeBuilder) { + ((DataSchemaNodeBuilder) parent) + .setConfiguration(configuration); + } else if (parent instanceof RefineHolder) { + ((RefineHolder) parent).setConfig(configuration); + } else if (parent instanceof DeviationBuilder) { + // skip: set config to deviation (deviate stmt) not supported by + // current api + return; } else { - ((DataSchemaNodeBuilder)builder).setConfiguration(configuration); + throw new YangParseException(name, line, + "Unresolved parent of config statement."); } } } public UnknownSchemaNodeBuilder addUnknownSchemaNode(final QName qname, final List parentPath, final int line) { - final List pathToUnknown = new ArrayList(parentPath); final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder( qname, line); - final Builder parent = moduleNodes.get(pathToUnknown); - if (parent instanceof RefineHolder) { - ((RefineHolder) parent).addUnknownSchemaNode(builder); - } else if (parent instanceof SchemaNodeBuilder) { - ((SchemaNodeBuilder) parent).addUnknownSchemaNode(builder); + + if (!(actualPath.isEmpty())) { + final Builder parent = actualPath.getFirst(); + if (parent instanceof SchemaNodeBuilder) { + ((SchemaNodeBuilder) parent).addUnknownSchemaNode(builder); + } else if (parent instanceof RefineHolder) { + ((RefineHolder) parent).addUnknownSchemaNode(builder); + } else { + throw new YangParseException(name, line, + "Unresolved parent of unknown node '" + + qname.getLocalName() + "'"); + } } + addedUnknownNodes.add(builder); return builder; } - private class ModuleImpl implements Module { + @Override + public String toString() { + return ModuleBuilder.class.getSimpleName() + "[" + name + "]"; + } + + private final class ModuleImpl implements Module { private URI namespace; private final String name; private Date revision; @@ -1006,6 +1105,25 @@ public class ModuleBuilder implements Builder { } } + private void updateParent(DataSchemaNodeBuilder nodeBuilder, int line, + String nodeTypeName) { + if (!(actualPath.isEmpty())) { + final Builder parent = actualPath.getFirst(); + if (parent instanceof DataNodeContainerBuilder) { + if (parent instanceof AugmentationSchemaBuilder) { + nodeBuilder.setAugmenting(true); + } + ((DataNodeContainerBuilder) parent).addChildNode(nodeBuilder); + } else if (parent instanceof ChoiceBuilder) { + ((ChoiceBuilder) parent).addChildNode(nodeBuilder); + } else { + throw new YangParseException(name, line, + "Unresolved parent of " + nodeTypeName + " " + + nodeBuilder.getQName().getLocalName()); + } + } + } + private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) { ModuleImport moduleImport = new ModuleImport() {