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%2Fimpl%2FYangParserImpl.java;h=5850abeb8129caace7573f5a6ede985674dc3995;hb=refs%2Fchanges%2F79%2F579%2F2;hp=6adc29b2271ce956fed15035084b1a3f9f7f77b0;hpb=6ceede511bb60f9ec6aa883c60df8c5242813b61;p=controller.git diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserImpl.java index 6adc29b227..5850abeb81 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserImpl.java @@ -39,6 +39,7 @@ import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.controller.yang.model.api.ChoiceNode; import org.opendaylight.controller.yang.model.api.ContainerSchemaNode; +import org.opendaylight.controller.yang.model.api.DataNodeContainer; import org.opendaylight.controller.yang.model.api.DataSchemaNode; import org.opendaylight.controller.yang.model.api.GroupingDefinition; import org.opendaylight.controller.yang.model.api.IdentitySchemaNode; @@ -47,6 +48,7 @@ import org.opendaylight.controller.yang.model.api.LeafSchemaNode; import org.opendaylight.controller.yang.model.api.ListSchemaNode; import org.opendaylight.controller.yang.model.api.Module; import org.opendaylight.controller.yang.model.api.SchemaContext; +import org.opendaylight.controller.yang.model.api.SchemaNode; import org.opendaylight.controller.yang.model.api.SchemaPath; import org.opendaylight.controller.yang.model.api.TypeDefinition; import org.opendaylight.controller.yang.model.api.UnknownSchemaNode; @@ -68,6 +70,7 @@ import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder; import org.opendaylight.controller.yang.parser.builder.impl.AnyXmlBuilder; import org.opendaylight.controller.yang.parser.builder.impl.ChoiceBuilder; import org.opendaylight.controller.yang.parser.builder.impl.ContainerSchemaNodeBuilder; +import org.opendaylight.controller.yang.parser.builder.impl.DeviationBuilder; import org.opendaylight.controller.yang.parser.builder.impl.GroupingBuilderImpl; import org.opendaylight.controller.yang.parser.builder.impl.IdentitySchemaNodeBuilder; import org.opendaylight.controller.yang.parser.builder.impl.IdentityrefTypeBuilder; @@ -305,6 +308,7 @@ public final class YangParserImpl implements YangModelParser { } } resolveAugments(modules); + resolveDeviations(modules); // build // LinkedHashMap MUST be used otherwise the values will not maintain @@ -333,6 +337,7 @@ public final class YangParserImpl implements YangModelParser { } } resolveAugmentsWithContext(modules, context); + resolveDeviationsWithContext(modules, context); // build // LinkedHashMap MUST be used otherwise the values will not maintain @@ -376,11 +381,9 @@ public final class YangParserImpl implements YangModelParser { * current module */ private void resolveDirtyNodes(final Map> modules, final ModuleBuilder module) { - final Map, TypeAwareBuilder> dirtyNodes = module.getDirtyNodes(); + final Set dirtyNodes = module.getDirtyNodes(); if (!dirtyNodes.isEmpty()) { - for (Map.Entry, TypeAwareBuilder> entry : dirtyNodes.entrySet()) { - final TypeAwareBuilder nodeToResolve = entry.getValue(); - + for (TypeAwareBuilder nodeToResolve : dirtyNodes) { if (nodeToResolve instanceof UnionTypeBuilder) { // special handling for union types resolveTypeUnion((UnionTypeBuilder) nodeToResolve, modules, module); @@ -397,11 +400,9 @@ public final class YangParserImpl implements YangModelParser { private void resolveDirtyNodesWithContext(final Map> modules, final ModuleBuilder module, SchemaContext context) { - final Map, TypeAwareBuilder> dirtyNodes = module.getDirtyNodes(); + final Set dirtyNodes = module.getDirtyNodes(); if (!dirtyNodes.isEmpty()) { - for (Map.Entry, TypeAwareBuilder> entry : dirtyNodes.entrySet()) { - final TypeAwareBuilder nodeToResolve = entry.getValue(); - + for (TypeAwareBuilder nodeToResolve : dirtyNodes) { if (nodeToResolve instanceof UnionTypeBuilder) { // special handling for union types resolveTypeUnionWithContext((UnionTypeBuilder) nodeToResolve, modules, module, context); @@ -1062,34 +1063,42 @@ public final class YangParserImpl implements YangModelParser { DataNodeContainerBuilder parent = usesNode.getParent(); SchemaPath parentPath = parent.getPath(); for (DataSchemaNodeBuilder child : targetGrouping.getChildNodeBuilders()) { - // if node is refined, take it from refined nodes and continue - SchemaNodeBuilder refined = getRefined(child.getQName(), refineNodes); - if (refined != null) { - refined.setPath(createSchemaPath(parentPath, refined.getQName().getLocalName())); - parent.addChildNode((DataSchemaNodeBuilder) refined); - continue; - } + if (child != null) { + // if node is refined, take it from refined nodes and continue + SchemaNodeBuilder refined = getRefined(child.getQName(), refineNodes); + if (refined != null) { + refined.setPath(createSchemaPath(parentPath, refined.getQName().getLocalName())); + parent.addChildNode((DataSchemaNodeBuilder) refined); + continue; + } - DataSchemaNodeBuilder newChild = null; - if (child instanceof AnyXmlBuilder) { - newChild = new AnyXmlBuilder((AnyXmlBuilder) child); - } else if (child instanceof ChoiceBuilder) { - newChild = new ChoiceBuilder((ChoiceBuilder) child); - } else if (child instanceof ContainerSchemaNodeBuilder) { - newChild = new ContainerSchemaNodeBuilder((ContainerSchemaNodeBuilder) child); - } else if (child instanceof LeafListSchemaNodeBuilder) { - newChild = new LeafListSchemaNodeBuilder((LeafListSchemaNodeBuilder) child); - } else if (child instanceof LeafSchemaNodeBuilder) { - newChild = new LeafSchemaNodeBuilder((LeafSchemaNodeBuilder) child); - } else if (child instanceof ListSchemaNodeBuilder) { - newChild = new ListSchemaNodeBuilder((ListSchemaNodeBuilder) child); - } + DataSchemaNodeBuilder newChild = null; + if (child instanceof AnyXmlBuilder) { + newChild = new AnyXmlBuilder((AnyXmlBuilder) child); + } else if (child instanceof ChoiceBuilder) { + newChild = new ChoiceBuilder((ChoiceBuilder) child); + } else if (child instanceof ContainerSchemaNodeBuilder) { + newChild = new ContainerSchemaNodeBuilder((ContainerSchemaNodeBuilder) child); + } else if (child instanceof LeafListSchemaNodeBuilder) { + newChild = new LeafListSchemaNodeBuilder((LeafListSchemaNodeBuilder) child); + } else if (child instanceof LeafSchemaNodeBuilder) { + newChild = new LeafSchemaNodeBuilder((LeafSchemaNodeBuilder) child); + } else if (child instanceof ListSchemaNodeBuilder) { + newChild = new ListSchemaNodeBuilder((ListSchemaNodeBuilder) child); + } + + if (newChild == null) { + throw new YangParseException(usesNode.getLine(), + "Unknown member of target grouping while resolving uses node."); + } - if (newChild instanceof GroupingMember) { - ((GroupingMember) newChild).setAddedByUses(true); + if (newChild instanceof GroupingMember) { + ((GroupingMember) newChild).setAddedByUses(true); + } + + newChild.setPath(createSchemaPath(parentPath, newChild.getQName().getLocalName())); + parent.addChildNode(newChild); } - newChild.setPath(createSchemaPath(parentPath, newChild.getQName().getLocalName())); - parent.addChildNode(newChild); } for (GroupingBuilder g : targetGrouping.getGroupingBuilders()) { GroupingBuilder newGrouping = new GroupingBuilderImpl(g); @@ -1123,34 +1132,41 @@ public final class YangParserImpl implements YangModelParser { DataNodeContainerBuilder parent = usesNode.getParent(); SchemaPath parentPath = parent.getPath(); for (DataSchemaNode child : targetGrouping.getChildNodes()) { - // if node is refined, take it from refined nodes and continue - SchemaNodeBuilder refined = getRefined(child.getQName(), refineNodes); - if (refined != null) { - refined.setPath(createSchemaPath(parentPath, refined.getQName().getLocalName())); - parent.addChildNode((DataSchemaNodeBuilder) refined); - continue; - } + if (child != null) { + // if node is refined, take it from refined nodes and continue + SchemaNodeBuilder refined = getRefined(child.getQName(), refineNodes); + if (refined != null) { + refined.setPath(createSchemaPath(parentPath, refined.getQName().getLocalName())); + parent.addChildNode((DataSchemaNodeBuilder) refined); + continue; + } - DataSchemaNodeBuilder newChild = null; - if (child instanceof AnyXmlSchemaNode) { - newChild = createAnyXml((AnyXmlSchemaNode) child, line); - } else if (child instanceof ChoiceNode) { - newChild = createChoice((ChoiceNode) child, line); - } else if (child instanceof ContainerSchemaNode) { - newChild = createContainer((ContainerSchemaNode) child, line); - } else if (child instanceof LeafListSchemaNode) { - newChild = createLeafList((LeafListSchemaNode) child, line); - } else if (child instanceof LeafSchemaNode) { - newChild = createLeafBuilder((LeafSchemaNode) child, line); - } else if (child instanceof ListSchemaNode) { - newChild = createList((ListSchemaNode) child, line); - } + DataSchemaNodeBuilder newChild = null; + if (child instanceof AnyXmlSchemaNode) { + newChild = createAnyXml((AnyXmlSchemaNode) child, line); + } else if (child instanceof ChoiceNode) { + newChild = createChoice((ChoiceNode) child, line); + } else if (child instanceof ContainerSchemaNode) { + newChild = createContainer((ContainerSchemaNode) child, line); + } else if (child instanceof LeafListSchemaNode) { + newChild = createLeafList((LeafListSchemaNode) child, line); + } else if (child instanceof LeafSchemaNode) { + newChild = createLeafBuilder((LeafSchemaNode) child, line); + } else if (child instanceof ListSchemaNode) { + newChild = createList((ListSchemaNode) child, line); + } + + if (newChild == null) { + throw new YangParseException(usesNode.getLine(), + "Unknown member of target grouping while resolving uses node."); + } - if (newChild instanceof GroupingMember) { - ((GroupingMember) newChild).setAddedByUses(true); + if (newChild instanceof GroupingMember) { + ((GroupingMember) newChild).setAddedByUses(true); + } + newChild.setPath(createSchemaPath(parentPath, newChild.getQName().getLocalName())); + parent.addChildNode(newChild); } - newChild.setPath(createSchemaPath(parentPath, newChild.getQName().getLocalName())); - parent.addChildNode(newChild); } for (GroupingDefinition g : targetGrouping.getGroupings()) { GroupingBuilder newGrouping = createGrouping(g, line); @@ -1201,7 +1217,7 @@ public final class YangParserImpl implements YangModelParser { } private void resolveUnknownNodes(final Map> modules, final ModuleBuilder module) { - for (UnknownSchemaNodeBuilder usnb : module.getUnknownNodes()) { + for (UnknownSchemaNodeBuilder usnb : module.getAllUnknownNodes()) { QName nodeType = usnb.getNodeType(); if (nodeType.getNamespace() == null || nodeType.getRevision() == null) { try { @@ -1218,8 +1234,8 @@ public final class YangParserImpl implements YangModelParser { } private void resolveUnknownNodesWithContext(final Map> modules, - final ModuleBuilder module, SchemaContext context) { - for (UnknownSchemaNodeBuilder unknownNodeBuilder : module.getUnknownNodes()) { + final ModuleBuilder module, final SchemaContext context) { + for (UnknownSchemaNodeBuilder unknownNodeBuilder : module.getAllUnknownNodes()) { QName nodeType = unknownNodeBuilder.getNodeType(); if (nodeType.getNamespace() == null || nodeType.getRevision() == null) { try { @@ -1246,4 +1262,115 @@ public final class YangParserImpl implements YangModelParser { } } + private void resolveDeviations(final Map> modules) { + for (Map.Entry> entry : modules.entrySet()) { + for (Map.Entry inner : entry.getValue().entrySet()) { + ModuleBuilder b = inner.getValue(); + resolveDeviation(modules, b); + } + } + } + + private void resolveDeviation(final Map> modules, final ModuleBuilder module) { + for (DeviationBuilder dev : module.getDeviations()) { + int line = dev.getLine(); + SchemaPath targetPath = dev.getTargetPath(); + List path = targetPath.getPath(); + QName q0 = path.get(0); + String prefix = q0.getPrefix(); + if (prefix == null) { + prefix = module.getPrefix(); + } + + ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module, prefix, line); + processDeviation(dev, dependentModuleBuilder, path, module); + } + } + + private void resolveDeviationsWithContext(final Map> modules, + final SchemaContext context) { + for (Map.Entry> entry : modules.entrySet()) { + for (Map.Entry inner : entry.getValue().entrySet()) { + ModuleBuilder b = inner.getValue(); + resolveDeviationWithContext(modules, b, context); + } + } + } + + private void resolveDeviationWithContext(final Map> modules, + final ModuleBuilder module, final SchemaContext context) { + for (DeviationBuilder dev : module.getDeviations()) { + int line = dev.getLine(); + SchemaPath targetPath = dev.getTargetPath(); + List path = targetPath.getPath(); + QName q0 = path.get(0); + String prefix = q0.getPrefix(); + if (prefix == null) { + prefix = module.getPrefix(); + } + String name = null; + + ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module, prefix, line); + if (dependentModuleBuilder == null) { + Module dependentModule = findModuleFromContext(context, module, prefix, line); + Object currentParent = dependentModule; + + for (int i = 0; i < path.size(); i++) { + if (currentParent == null) { + throw new YangParseException(module.getName(), line, "Failed to find deviation target."); + } + QName q = path.get(i); + name = q.getLocalName(); + if (currentParent instanceof DataNodeContainer) { + currentParent = ((DataNodeContainer) currentParent).getDataChildByName(name); + } + } + + if (currentParent == null) { + throw new YangParseException(module.getName(), line, "Failed to find deviation target."); + } + if (currentParent instanceof SchemaNode) { + dev.setTargetPath(((SchemaNode) currentParent).getPath()); + } + + } else { + processDeviation(dev, dependentModuleBuilder, path, module); + } + } + } + + /** + * Correct deviation target path in deviation builder. + * + * @param dev + * deviation + * @param dependentModuleBuilder + * module containing deviation target + * @param path + * current deviation target path + * @param module + * current module + */ + private void processDeviation(final DeviationBuilder dev, final ModuleBuilder dependentModuleBuilder, + final List path, final ModuleBuilder module) { + final int line = dev.getLine(); + Builder currentParent = dependentModuleBuilder; + + for (int i = 0; i < path.size(); i++) { + if (currentParent == null) { + throw new YangParseException(module.getName(), line, "Failed to find deviation target."); + } + QName q = path.get(i); + String name = q.getLocalName(); + if (currentParent instanceof DataNodeContainerBuilder) { + currentParent = ((DataNodeContainerBuilder) currentParent).getDataChildByName(name); + } + } + + if (currentParent == null || !(currentParent instanceof SchemaNodeBuilder)) { + throw new YangParseException(module.getName(), line, "Failed to find deviation target."); + } + dev.setTargetPath(((SchemaNodeBuilder) currentParent).getPath()); + } + }