From: Martin Vitez Date: Mon, 14 Oct 2013 16:03:00 +0000 (+0200) Subject: Minor code refactoring. X-Git-Tag: yangtools-0.6.0~216 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=934fa2f832135385a1ef05377a5b43527f4049a1;p=yangtools.git Minor code refactoring. - fixed code formatting - removed redundant code - replaced explicitly defined generic type parameters with Java 7 diamond type '<>' - replaced too complex if/else statements with switch where possible - updated javadocs Signed-off-by: Martin Vitez --- diff --git a/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/RefineTest.java b/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/RefineTest.java index 92185784c9..ecf545eae6 100644 --- a/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/RefineTest.java +++ b/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/RefineTest.java @@ -148,7 +148,7 @@ public class RefineTest { DataSchemaNodeBuilder builderData = new AnyXmlBuilder("module", 4, qname, createSchemaPath(qname)); assertNotNull("Refine holder data wasn't initialized.", refHolderData); - RefineUtils.refineAnyxml((AnyXmlBuilder) builderData, refHolderData, 3); + RefineUtils.refineAnyxml((AnyXmlBuilder) builderData, refHolderData); // data node ConstraintsBuilder conDefData = builderData.getConstraints(); @@ -169,7 +169,7 @@ public class RefineTest { DataSchemaNodeBuilder builderChc2 = new ChoiceBuilder("module", 4, qname); assertNotNull("Refine holder chc2 wasn't initialized.", refHolderChc2); - RefineUtils.refineChoice((ChoiceBuilder) builderChc2, refHolderChc2, 3); + RefineUtils.refineChoice((ChoiceBuilder) builderChc2, refHolderChc2); // chc2 node ConstraintsBuilder conDefChc2 = builderChc2.getConstraints(); @@ -185,7 +185,7 @@ public class RefineTest { assertNotNull("Refine holder chc wasn't initialized.", refHolderChc); assertNotNull("Data schema node builder chc wasn't initialized.", builderChc); - RefineUtils.refineChoice((ChoiceBuilder) builderChc, refHolderChc, 2); + RefineUtils.refineChoice((ChoiceBuilder) builderChc, refHolderChc); ChoiceBuilder choiceBuilder = null; if (builderChc instanceof ChoiceBuilder) { @@ -207,7 +207,7 @@ public class RefineTest { assertNotNull("Refine holder " + nodeName + " wasn't initialized.", refHolderLflst); assertNotNull("Data schema node builder " + nodeName + " wasn't initialized.", builderLflst); - RefineUtils.refineLeafList((LeafListSchemaNodeBuilder) builderLflst, refHolderLflst, 1); + RefineUtils.refineLeafList((LeafListSchemaNodeBuilder) builderLflst, refHolderLflst); // lflst node ConstraintsBuilder conDefLflst = builderLflst.getConstraints(); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java index 89b7575616..38b282de69 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java @@ -47,7 +47,6 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; -import org.opendaylight.yangtools.yang.model.util.IdentityrefType; import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.Builder; import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder; @@ -162,7 +161,7 @@ public final class YangParserImpl implements YangModelParser { } } - return new LinkedHashSet(buildWithContext(modules, context).values()); + return new LinkedHashSet<>(buildWithContext(modules, context).values()); } return Collections.emptySet(); } @@ -178,7 +177,7 @@ public final class YangParserImpl implements YangModelParser { Map builderToStreamMap = Maps.newHashMap(); final Map> modules = resolveModuleBuildersWithContext( yangModelStreams, builderToStreamMap, context); - return new LinkedHashSet(buildWithContext(modules, context).values()); + return new LinkedHashSet<>(buildWithContext(modules, context).values()); } return Collections.emptySet(); } @@ -249,7 +248,7 @@ public final class YangParserImpl implements YangModelParser { final Map builders = new LinkedHashMap<>(); // validate yang - new YangModelBasicValidator(walker).validate(new ArrayList(trees.values())); + new YangModelBasicValidator(walker).validate(new ArrayList<>(trees.values())); YangParserListenerImpl yangModelParser; for(Map.Entry entry : trees.entrySet()) { @@ -275,7 +274,7 @@ public final class YangParserImpl implements YangModelParser { final SchemaContext context) { Map parsedBuilders = parseModuleBuilders(yangFileStreams, streamToBuilderMap); ModuleBuilder[] builders = new ModuleBuilder[parsedBuilders.size()]; - final ModuleBuilder[] moduleBuilders = new ArrayList<>(parsedBuilders.values()).toArray(builders); + parsedBuilders.values().toArray(builders); // module dependency graph sorted List sorted; @@ -309,7 +308,7 @@ public final class YangParserImpl implements YangModelParser { } TreeMap builderByRevision = result.get(builderName); if (builderByRevision == null) { - builderByRevision = new TreeMap(); + builderByRevision = new TreeMap<>(); } builderByRevision.put(builderRevision, builder); result.put(builderName, builderByRevision); @@ -375,7 +374,7 @@ public final class YangParserImpl implements YangModelParser { resolveDeviations(modules); // build - final Map result = new LinkedHashMap(); + final Map result = new LinkedHashMap<>(); for (Map.Entry> entry : modules.entrySet()) { for (Map.Entry childEntry : entry.getValue().entrySet()) { final ModuleBuilder moduleBuilder = childEntry.getValue(); @@ -398,7 +397,7 @@ public final class YangParserImpl implements YangModelParser { resolveDeviationsWithContext(modules, context); // build - final Map result = new LinkedHashMap(); + final Map result = new LinkedHashMap<>(); for (Map.Entry> entry : modules.entrySet()) { for (Map.Entry childEntry : entry.getValue().entrySet()) { final ModuleBuilder moduleBuilder = childEntry.getValue(); @@ -1040,18 +1039,16 @@ public final class YangParserImpl implements YangModelParser { if (prefix == null) { prefix = module.getPrefix(); } - String name = null; ModuleBuilder dependentModuleBuilder = findModuleFromBuilders(modules, module, prefix, line); if (dependentModuleBuilder == null) { - Module dependentModule = findModuleFromContext(context, module, prefix, line); - Object currentParent = dependentModule; + Object currentParent = findModuleFromContext(context, module, prefix, line); for (QName q : path) { if (currentParent == null) { throw new YangParseException(module.getName(), line, FAIL_DEVIATION_TARGET); } - name = q.getLocalName(); + String name = q.getLocalName(); if (currentParent instanceof DataNodeContainer) { currentParent = ((DataNodeContainer) currentParent).getDataChildByName(name); } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/CopyUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/CopyUtils.java index f56e7623df..d9858bc3cc 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/CopyUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/CopyUtils.java @@ -63,9 +63,13 @@ public final class CopyUtils { * true, qname of node will be corrected based on new parent. * * @param old + * builder to copy * @param newParent + * new parent * @param updateQName - * @return copy + * flag to indicate if qname should be updated based on new + * parent location + * @return copy of given builder */ public static DataSchemaNodeBuilder copy(DataSchemaNodeBuilder old, Builder newParent, boolean updateQName) { if (old instanceof AnyXmlBuilder) { @@ -357,7 +361,7 @@ public final class CopyUtils { DataBean data = getdata(old, newParent, updateQName); QName newQName = data.qname; SchemaPath newSchemaPath = data.schemaPath; - TypeDefinitionBuilder type = null; + TypeDefinitionBuilder type; if (old instanceof UnionTypeBuilder) { UnionTypeBuilder oldUnion = (UnionTypeBuilder) old; @@ -568,7 +572,9 @@ public final class CopyUtils { * Create AnyXmlBuilder from given AnyXmlSchemaNode. * * @param anyxml + * base anyxml * @param qname + * new qname * @param moduleName * current module name * @param line @@ -587,7 +593,9 @@ public final class CopyUtils { * Create GroupingBuilder from given GroupingDefinition. * * @param grouping + * base grouping * @param qname + * new qname * @param moduleName * current module name * @param line @@ -612,7 +620,9 @@ public final class CopyUtils { * Create TypeDefinitionBuilder from given ExtendedType. * * @param typedef + * base typedef * @param qname + * new qname * @param moduleName * current module name * @param line @@ -642,7 +652,9 @@ public final class CopyUtils { * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode. * * @param unknownNode + * base unknown node * @param qname + * new qname * @param moduleName * current module name * @param line @@ -669,6 +681,7 @@ public final class CopyUtils { * @param leaf * leaf from which to create builder * @param qname + * new qname * @param moduleName * current module name * @param line @@ -692,7 +705,9 @@ public final class CopyUtils { * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode. * * @param container + * base container * @param qname + * new qname * @param moduleName * current module name * @param line @@ -719,7 +734,9 @@ public final class CopyUtils { * Create ListSchemaNodeBuilder from given ListSchemaNode. * * @param list + * base list * @param qname + * new qname * @param moduleName * current module name * @param line @@ -744,7 +761,9 @@ public final class CopyUtils { * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode. * * @param leafList + * base leaf-list * @param qname + * new qname * @param moduleName * current module name * @param line @@ -767,7 +786,9 @@ public final class CopyUtils { * Create ChoiceBuilder from given ChoiceNode. * * @param choice + * base choice * @param qname + * new qname * @param moduleName * current module name * @param line diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/GroupingUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/GroupingUtils.java index 6f3bb1757a..14718e6a4b 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/GroupingUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/GroupingUtils.java @@ -81,7 +81,7 @@ public final class GroupingUtils { groupingName = groupingString; } - ModuleBuilder dependentModule = null; + ModuleBuilder dependentModule; if (groupingPrefix.equals(module.getPrefix())) { dependentModule = module; } else { @@ -92,7 +92,7 @@ public final class GroupingUtils { return null; } - GroupingBuilder result = null; + GroupingBuilder result; Set groupings = dependentModule.getGroupingBuilders(); result = findGroupingBuilder(groupings, groupingName); if (result != null) { @@ -196,6 +196,7 @@ public final class GroupingUtils { * Add nodes defined in uses target grouping to uses parent. * * @param usesNode + * yang uses node which will be instantiated in current location */ public static void updateUsesParent(UsesNodeBuilder usesNode) { DataNodeContainerBuilder parent = usesNode.getParent(); @@ -205,10 +206,8 @@ public final class GroupingUtils { String prefix = module.getPrefix(); SchemaPath parentPath = parent.getPath(); - - - if(parent instanceof AugmentationSchemaBuilder) { - parentPath = ((AugmentationSchemaBuilder)parent).getTargetNodeSchemaPath(); + if (parent instanceof AugmentationSchemaBuilder) { + parentPath = ((AugmentationSchemaBuilder) parent).getTargetNodeSchemaPath(); } // child nodes @@ -217,19 +216,25 @@ public final class GroupingUtils { ((GroupingMember) child).setAddedByUses(true); } - GroupingMember gm = (GroupingMember) child; - if (gm.isAddedByUses()) { - if(usesNode.isAugmenting()) { - child.setAugmenting(true); - } - if(usesNode.isAugmenting() && !(usesNode.getParentAugment().getParent() instanceof UsesNodeBuilder)) { - AugmentationSchemaBuilder parentAugment = usesNode.getParentAugment(); - ModuleBuilder m = ParserUtils.getParentModule(parentAugment); - correctNodePathForUsesNodes(child, parentPath, m); - } else { - child.setQName(new QName(ns, rev, prefix, child.getQName().getLocalName())); - correctNodePathForUsesNodes(child, parentPath, module); + if (child instanceof GroupingMember) { + GroupingMember gm = (GroupingMember) child; + if (gm.isAddedByUses()) { + if (usesNode.isAugmenting()) { + child.setAugmenting(true); + } + if (usesNode.isAugmenting() + && !(usesNode.getParentAugment().getParent() instanceof UsesNodeBuilder)) { + AugmentationSchemaBuilder parentAugment = usesNode.getParentAugment(); + ModuleBuilder m = ParserUtils.getParentModule(parentAugment); + correctNodePathForUsesNodes(child, parentPath, m); + } else { + child.setQName(new QName(ns, rev, prefix, child.getQName().getLocalName())); + correctNodePathForUsesNodes(child, parentPath, module); + } } + } else { + throw new YangParseException(module.getName(), usesNode.getLine(), + "Failed to process uses node: unresolved child node"); } parent.addChildNode(child); @@ -279,17 +284,17 @@ public final class GroupingUtils { * Read child nodes defined in target grouping and make a copy of them. * * @param usesNode - * @return copy of child nodes defined in uses target grouping + * uses node for which data will be collected */ private static void collectTargetChildNodes(UsesNodeBuilder usesNode) { final GroupingBuilder target = usesNode.getGroupingBuilder(); final Set collection = new HashSet<>(); - addChildNodeToCollection(usesNode, collection, target.getChildNodeBuilders()); + addChildNodeToCollection(usesNode, collection, target.getChildNodeBuilders(), usesNode.getParent()); for (UsesNodeBuilder targetUses : target.getUsesNodes()) { Set targetUsesChildNodes = collectTargetUsesChildNodes(targetUses, usesNode.getParent()); - addChildNodeToCollection(usesNode, collection, targetUsesChildNodes); + addChildNodeToCollection(usesNode, collection, targetUsesChildNodes, usesNode.getParent()); } usesNode.getTargetChildren().addAll(collection); } @@ -298,17 +303,17 @@ public final class GroupingUtils { DataNodeContainerBuilder parent) { final GroupingBuilder target = usesNode.getGroupingBuilder(); final Set collection = new HashSet<>(usesNode.getTargetChildren()); - addChildNodeToCollection(usesNode, collection, target.getChildNodeBuilders()); + addChildNodeToCollection(usesNode, collection, target.getChildNodeBuilders(), parent); for (UsesNodeBuilder targetUses : target.getUsesNodes()) { Set targetUsesChildNodes = collectTargetUsesChildNodes(targetUses, parent); - addChildNodeToCollection(usesNode, collection, targetUsesChildNodes); + addChildNodeToCollection(usesNode, collection, targetUsesChildNodes, parent); } return collection; } private static void addChildNodeToCollection(UsesNodeBuilder usesNode, Set collection, - Set allNodes) { + Set allNodes, Builder parent) { for (DataSchemaNodeBuilder childNode : allNodes) { boolean exists = false; for (DataSchemaNodeBuilder usesChildNode : usesNode.getTargetChildren()) { @@ -318,7 +323,7 @@ public final class GroupingUtils { } } if (!exists) { - DataSchemaNodeBuilder copy = CopyUtils.copy(childNode, usesNode.getParent(), true); + DataSchemaNodeBuilder copy = CopyUtils.copy(childNode, parent, true); collection.add(copy); } } @@ -328,16 +333,16 @@ public final class GroupingUtils { * Read typedefs defined in target grouping and make a copy of them. * * @param usesNode - * @return copy of typedefs defined in uses target grouping + * uses node for which data will be collected */ private static void collectTargetTypedefs(UsesNodeBuilder usesNode) { final GroupingBuilder target = usesNode.getGroupingBuilder(); Set collection = new HashSet<>(); - addTypedefToCollection(usesNode, collection, target.getTypeDefinitionBuilders()); + addTypedefToCollection(usesNode, collection, target.getTypeDefinitionBuilders(), usesNode.getParent()); for (UsesNodeBuilder targetUses : target.getUsesNodes()) { Set targetUsesTypedefs = collectTargetUsesTypedefs(targetUses, usesNode.getParent()); - addTypedefToCollection(usesNode, collection, targetUsesTypedefs); + addTypedefToCollection(usesNode, collection, targetUsesTypedefs, usesNode.getParent()); } usesNode.getTargetTypedefs().addAll(collection); } @@ -346,17 +351,17 @@ public final class GroupingUtils { DataNodeContainerBuilder parent) { final GroupingBuilder target = usesNode.getGroupingBuilder(); Set collection = new HashSet<>(usesNode.getTargetTypedefs()); - addTypedefToCollection(usesNode, collection, target.getTypeDefinitionBuilders()); + addTypedefToCollection(usesNode, collection, target.getTypeDefinitionBuilders(), parent); for (UsesNodeBuilder targetUses : target.getUsesNodes()) { Set targetUsesTypedefs = collectTargetUsesTypedefs(targetUses, parent); - addTypedefToCollection(usesNode, collection, targetUsesTypedefs); + addTypedefToCollection(usesNode, collection, targetUsesTypedefs, parent); } return collection; } private static void addTypedefToCollection(UsesNodeBuilder usesNode, Set collection, - Set allTypedefs) { + Set allTypedefs, Builder parent) { for (TypeDefinitionBuilder childNode : allTypedefs) { boolean exists = false; for (TypeDefinitionBuilder usesTypedef : usesNode.getTargetTypedefs()) { @@ -366,7 +371,7 @@ public final class GroupingUtils { } } if (!exists) { - TypeDefinitionBuilder copy = CopyUtils.copy(childNode, usesNode.getParent(), true); + TypeDefinitionBuilder copy = CopyUtils.copy(childNode, parent, true); collection.add(copy); } } @@ -376,16 +381,16 @@ public final class GroupingUtils { * Read groupings defined in target grouping and make a copy of them. * * @param usesNode - * @return copy of groupings defined in uses target grouping + * uses node for which data will be collected */ private static void collectTargetGroupings(UsesNodeBuilder usesNode) { final GroupingBuilder target = usesNode.getGroupingBuilder(); Set collection = new HashSet<>(); - addGroupingToCollection(usesNode, collection, target.getGroupingBuilders()); + addGroupingToCollection(usesNode, collection, target.getGroupingBuilders(), usesNode.getParent()); for (UsesNodeBuilder targetUses : target.getUsesNodes()) { Set targetUsesGrouping = collectTargetGroupings(targetUses, usesNode.getParent()); - addGroupingToCollection(usesNode, collection, targetUsesGrouping); + addGroupingToCollection(usesNode, collection, targetUsesGrouping, usesNode.getParent()); } usesNode.getTargetGroupings().addAll(collection); } @@ -393,17 +398,17 @@ public final class GroupingUtils { private static Set collectTargetGroupings(UsesNodeBuilder usesNode, DataNodeContainerBuilder parent) { final GroupingBuilder target = usesNode.getGroupingBuilder(); Set collection = new HashSet<>(usesNode.getTargetGroupings()); - addGroupingToCollection(usesNode, collection, target.getGroupingBuilders()); + addGroupingToCollection(usesNode, collection, target.getGroupingBuilders(), parent); for (UsesNodeBuilder targetUses : target.getUsesNodes()) { Set targetUsesGroupings = collectTargetGroupings(targetUses, parent); - addGroupingToCollection(usesNode, collection, targetUsesGroupings); + addGroupingToCollection(usesNode, collection, targetUsesGroupings, parent); } return collection; } private static void addGroupingToCollection(UsesNodeBuilder usesNode, Set collection, - Set allGroupings) { + Set allGroupings, Builder parent) { for (GroupingBuilder childNode : allGroupings) { boolean exists = false; for (GroupingBuilder usesGrouping : usesNode.getTargetGroupings()) { @@ -413,7 +418,7 @@ public final class GroupingUtils { } } if (!exists) { - GroupingBuilder copy = CopyUtils.copy(childNode, usesNode.getParent(), true); + GroupingBuilder copy = CopyUtils.copy(childNode, parent, true); collection.add(copy); } } @@ -423,17 +428,17 @@ public final class GroupingUtils { * Read unknown nodes defined in target grouping and make a copy of them. * * @param usesNode - * @return copy of unknown nodes defined in uses target grouping + * uses node for which data will be collected */ private static void collectTargetUnknownNodes(UsesNodeBuilder usesNode) { final GroupingBuilder target = usesNode.getGroupingBuilder(); final List collection = new ArrayList<>(); - addUnknownNodeToCollection(usesNode, collection, target.getUnknownNodeBuilders()); + addUnknownNodeToCollection(usesNode, collection, target.getUnknownNodeBuilders(), usesNode.getParent()); for (UsesNodeBuilder targetUses : target.getUsesNodes()) { List targetUsesUnknownNodes = collectTargetUnknownNodes(targetUses, usesNode.getParent()); - addUnknownNodeToCollection(usesNode, collection, targetUsesUnknownNodes); + addUnknownNodeToCollection(usesNode, collection, targetUsesUnknownNodes, usesNode.getParent()); } usesNode.getTargetUnknownNodes().addAll(collection); } @@ -442,17 +447,17 @@ public final class GroupingUtils { DataNodeContainerBuilder parent) { final GroupingBuilder target = usesNode.getGroupingBuilder(); List collection = new ArrayList<>(usesNode.getTargetUnknownNodes()); - addUnknownNodeToCollection(usesNode, collection, target.getUnknownNodeBuilders()); + addUnknownNodeToCollection(usesNode, collection, target.getUnknownNodeBuilders(), parent); for (UsesNodeBuilder targetUses : target.getUsesNodes()) { List targetUsesUnknownNodes = collectTargetUnknownNodes(targetUses, parent); - addUnknownNodeToCollection(usesNode, collection, targetUsesUnknownNodes); + addUnknownNodeToCollection(usesNode, collection, targetUsesUnknownNodes, parent); } return collection; } private static void addUnknownNodeToCollection(UsesNodeBuilder usesNode, List collection, - List allUnknownNodes) { + List allUnknownNodes, Builder parent) { for (UnknownSchemaNodeBuilder childNode : allUnknownNodes) { boolean exists = false; for (UnknownSchemaNodeBuilder usesUnknownNode : usesNode.getTargetUnknownNodes()) { @@ -462,7 +467,7 @@ public final class GroupingUtils { } } if (!exists) { - UnknownSchemaNodeBuilder copy = CopyUtils.copy(childNode, usesNode.getParent(), true); + UnknownSchemaNodeBuilder copy = CopyUtils.copy(childNode, parent, true); collection.add(copy); } } @@ -562,10 +567,8 @@ public final class GroupingUtils { throw new YangParseException(moduleName, lineNumber, "Unknown member of target grouping while resolving uses node."); } - if (newChild instanceof GroupingMember) { - ((GroupingMember) newChild).setAddedByUses(true); - } + ((GroupingMember) newChild).setAddedByUses(true); newChildren.add(newChild); } } @@ -586,7 +589,7 @@ public final class GroupingUtils { private static void correctNodePathForUsesNodes(final SchemaNodeBuilder node, final SchemaPath parentSchemaPath, final ModuleBuilder parentModule) { // set correct path - List targetNodePath = new ArrayList(parentSchemaPath.getPath()); + List targetNodePath = new ArrayList<>(parentSchemaPath.getPath()); targetNodePath.add(new QName(parentModule.getNamespace(), parentModule.getRevision(), parentModule.getPrefix(), node.getQName().getLocalName())); node.setPath(new SchemaPath(targetNodePath, true)); @@ -613,6 +616,7 @@ public final class GroupingUtils { * already performed. * * @param usesNode + * uses node containing refine statements */ public static void performRefine(UsesNodeBuilder usesNode) { for (RefineHolder refine : usesNode.getRefines()) { diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/MustDefinitionImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/MustDefinitionImpl.java index c1cb4a51d7..d0b232c550 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/MustDefinitionImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/MustDefinitionImpl.java @@ -17,8 +17,7 @@ final class MustDefinitionImpl implements MustDefinition { private final String errorAppTag; private final String errorMessage; - MustDefinitionImpl(String mustStr, String description, String reference, - String errorAppTag, String errorMessage) { + MustDefinitionImpl(String mustStr, String description, String reference, String errorAppTag, String errorMessage) { this.mustStr = mustStr; this.description = description; this.reference = reference; @@ -56,10 +55,8 @@ final class MustDefinitionImpl implements MustDefinition { final int prime = 31; int result = 1; result = prime * result + ((mustStr == null) ? 0 : mustStr.hashCode()); - result = prime * result - + ((description == null) ? 0 : description.hashCode()); - result = prime * result - + ((reference == null) ? 0 : reference.hashCode()); + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + ((reference == null) ? 0 : reference.hashCode()); return result; } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserListenerUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserListenerUtils.java index 505fa97dee..917a001c6b 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserListenerUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserListenerUtils.java @@ -112,7 +112,6 @@ import org.opendaylight.yangtools.yang.parser.builder.api.Builder; import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder; -import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.ConstraintsBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder; @@ -183,13 +182,17 @@ public final class ParserListenerUtils { ParseTree statusArg = ctx.getChild(i); if (statusArg instanceof Status_argContext) { String statusArgStr = stringFromNode(statusArg); - if ("current".equals(statusArgStr)) { + switch (statusArgStr) { + case "current": result = Status.CURRENT; - } else if ("deprecated".equals(statusArgStr)) { + break; + case "deprecated": result = Status.DEPRECATED; - } else if ("obsolete".equals(statusArgStr)) { + break; + case "obsolete": result = Status.OBSOLETE; - } else { + break; + default: LOG.warn("Invalid 'status' statement: " + statusArgStr); } } @@ -245,7 +248,7 @@ public final class ParserListenerUtils { * @return SchemaPath object */ public static SchemaPath createActualSchemaPath(final Stack actualPath) { - final List path = new ArrayList(actualPath); + final List path = new ArrayList<>(actualPath); return new SchemaPath(path, true); } @@ -265,10 +268,10 @@ public final class ParserListenerUtils { */ public static List createListKey(final String keyDefinition, final URI namespace, final Date revision, final String prefix) { - List key = new ArrayList(); + List key = new ArrayList<>(); String[] splittedKey = keyDefinition.split(" "); - QName qname = null; + QName qname; for (String keyElement : splittedKey) { if (keyElement.length() != 0) { qname = new QName(namespace, revision, prefix, keyElement); @@ -291,7 +294,7 @@ public final class ParserListenerUtils { */ private static List getEnumConstants(final Type_body_stmtsContext ctx, final Stack path, final String moduleName) { - List enumConstants = new ArrayList(); + List enumConstants = new ArrayList<>(); for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree enumSpecChild = ctx.getChild(i); @@ -319,7 +322,7 @@ public final class ParserListenerUtils { * enum statement context * @param highestValue * current highest value in enumeration - * @param path + * @param actualPath * actual position in YANG model * @param moduleName * current module name @@ -495,6 +498,7 @@ public final class ParserListenerUtils { * @param ctx * type body context to parse * @param moduleName + * name of current module * @return List of RangeConstraint created from this context */ private static List getRangeConstraints(final Type_body_stmtsContext ctx, final String moduleName) { @@ -520,11 +524,12 @@ public final class ParserListenerUtils { * @param ctx * range context to parse * @param moduleName + * name of current module * @return List of RangeConstraints parsed from this context */ private static List parseRangeConstraints(final Range_stmtContext ctx, final String moduleName) { final int line = ctx.getStart().getLine(); - List rangeConstraints = new ArrayList(); + List rangeConstraints = new ArrayList<>(); String description = null; String reference = null; @@ -563,6 +568,7 @@ public final class ParserListenerUtils { * @param ctx * type body context to parse * @param moduleName + * name of current module * @return List of LengthConstraint created from this context */ private static List getLengthConstraints(final Type_body_stmtsContext ctx, final String moduleName) { @@ -588,11 +594,12 @@ public final class ParserListenerUtils { * @param ctx * length context to parse * @param moduleName + * name of current module * @return List of LengthConstraints parsed from this context */ private static List parseLengthConstraints(final Length_stmtContext ctx, final String moduleName) { final int line = ctx.getStart().getLine(); - List lengthConstraints = new ArrayList(); + List lengthConstraints = new ArrayList<>(); String description = null; String reference = null; @@ -636,7 +643,7 @@ public final class ParserListenerUtils { * type is one of special YANG values 'min' or 'max' */ private static Number parseNumberConstraintValue(final String value, final String moduleName, final int line) { - Number result = null; + Number result; if ("min".equals(value) || "max".equals(value)) { result = new UnknownBoundaryNumber(value); } else { @@ -657,7 +664,7 @@ public final class ParserListenerUtils { * @return list of pattern constraints */ private static List getPatternConstraint(final Type_body_stmtsContext ctx) { - List patterns = new ArrayList(); + List patterns = new ArrayList<>(); for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree stringRestrChild = ctx.getChild(i); @@ -779,7 +786,7 @@ public final class ParserListenerUtils { */ private static List getBits(Type_body_stmtsContext ctx, Stack actualPath, String moduleName) { - final List bits = new ArrayList(); + final List bits = new ArrayList<>(); for (int j = 0; j < ctx.getChildCount(); j++) { ParseTree bitsSpecChild = ctx.getChild(j); if (bitsSpecChild instanceof Bits_specificationContext) { @@ -867,11 +874,14 @@ public final class ParserListenerUtils { ParseTree orderArg = ctx.getChild(j); if (orderArg instanceof Ordered_by_argContext) { String orderStr = stringFromNode(orderArg); - if ("system".equals(orderStr)) { + switch (orderStr) { + case "system": result = false; - } else if ("user".equals(orderStr)) { + break; + case "user": result = true; - } else { + break; + default: LOG.warn("Invalid 'ordered-by' statement."); } } @@ -894,7 +904,7 @@ public final class ParserListenerUtils { * @return config statement parsed from given context */ public static Boolean getConfig(final ParseTree ctx, final Builder parent, final String moduleName, final int line) { - Boolean result = null; + Boolean result; // parse configuration statement Boolean config = null; for (int i = 0; i < ctx.getChildCount(); i++) { @@ -913,18 +923,6 @@ public final class ParserListenerUtils { // 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; } @@ -960,13 +958,14 @@ public final class ParserListenerUtils { final ParseTree configContext = ctx.getChild(i); if (configContext instanceof Config_argContext) { final String value = stringFromNode(configContext); - if ("true".equals(value)) { + switch (value) { + case "true": result = true; break; - } else if ("false".equals(value)) { + case "false": result = false; break; - } else { + default: throw new YangParseException(moduleName, ctx.getStart().getLine(), "Failed to parse 'config' statement value: '" + value + "'."); } @@ -984,10 +983,15 @@ public final class ParserListenerUtils { * @param ctx * type body * @param actualPath + * actual path in model * @param namespace + * module namespace * @param revision + * module revision * @param prefix + * module prefix * @param parent + * current node parent * @return UnknownType object with constraints from parsed type body */ public static TypeDefinition parseUnknownTypeWithBody(final QName typedefQName, @@ -1013,7 +1017,6 @@ public final class ParserListenerUtils { return unknownType.build(); } else { TypeDefinition baseType = unknownType.build(); - TypeDefinition result = null; QName qname = new QName(namespace, revision, prefix, typeName); SchemaPath schemaPath = createTypePath(actualPath, typeName); @@ -1022,9 +1025,8 @@ public final class ParserListenerUtils { typeBuilder.lengths(lengthStatements); typeBuilder.patterns(patternStatements); typeBuilder.fractionDigits(fractionDigits); - result = typeBuilder.build(); - return result; + return typeBuilder.build(); } } @@ -1089,14 +1091,19 @@ public final class ParserListenerUtils { baseType = decimalType; } else if (typeName.startsWith("int")) { IntegerTypeDefinition intType = null; - if ("int8".equals(typeName)) { + switch (typeName) { + case "int8": intType = Int8.getInstance(); - } else if ("int16".equals(typeName)) { + break; + case "int16": intType = Int16.getInstance(); - } else if ("int32".equals(typeName)) { + break; + case "int32": intType = Int32.getInstance(); - } else if ("int64".equals(typeName)) { + break; + case "int64": intType = Int64.getInstance(); + break; } if (intType == null) { throw new YangParseException(moduleName, line, "Unknown yang type " + typeName); @@ -1105,14 +1112,19 @@ public final class ParserListenerUtils { baseType = intType; } else if (typeName.startsWith("uint")) { UnsignedIntegerTypeDefinition uintType = null; - if ("uint8".equals(typeName)) { + switch (typeName) { + case "uint8": uintType = Uint8.getInstance(); - } else if ("uint16".equals(typeName)) { + break; + case "uint16": uintType = Uint16.getInstance(); - } else if ("uint32".equals(typeName)) { + break; + case "uint32": uintType = Uint32.getInstance(); - } else if ("uint64".equals(typeName)) { + break; + case "uint64": uintType = Uint64.getInstance(); + break; } if (uintType == null) { throw new YangParseException(moduleName, line, "Unknown yang type " + typeName); @@ -1151,35 +1163,31 @@ public final class ParserListenerUtils { return baseType; } - TypeDefinition result = null; - ExtendedType.Builder typeBuilder = null; - - List path = new ArrayList(actualPath); + List path = new ArrayList<>(actualPath); path.add(new QName(namespace, revision, prefix, typeName)); SchemaPath schemaPath = new SchemaPath(path, true); QName qname = schemaPath.getPath().get(schemaPath.getPath().size() - 1); - typeBuilder = new ExtendedType.Builder(qname, baseType, "", "", schemaPath); + ExtendedType.Builder 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; + return typeBuilder.build(); } private static SchemaPath createTypePath(Stack actual, String typeName) { QName last = actual.peek(); QName typeQName = new QName(last.getNamespace(), last.getRevision(), last.getPrefix(), typeName); - List path = new ArrayList(actual); + List path = new ArrayList<>(actual); path.add(typeQName); return new SchemaPath(path, true); } private static SchemaPath createBaseTypePath(Stack actual, String typeName) { - List path = new ArrayList(actual); + List path = new ArrayList<>(actual); path.add(BaseTypes.constructQName(typeName)); return new SchemaPath(path, true); } @@ -1188,7 +1196,7 @@ public final class ParserListenerUtils { String prefix, String typeName) { QName extTypeName = new QName(namespace, revision, prefix, typeName); QName baseTypeName = BaseTypes.constructQName(typeName); - List path = new ArrayList(actual); + List path = new ArrayList<>(actual); path.add(extTypeName); path.add(baseTypeName); return new SchemaPath(path, true); @@ -1311,9 +1319,7 @@ public final class ParserListenerUtils { } } - MustDefinition must = new MustDefinitionImpl(mustText.toString(), description, reference, errorAppTag, - errorMessage); - return must; + return new MustDefinitionImpl(mustText.toString(), description, reference, errorAppTag, errorMessage); } /** @@ -1427,18 +1433,19 @@ public final class ParserListenerUtils { * informations in its body */ public static void checkMissingBody(final String typeName, final String moduleName, final int line) { - if ("decimal64".equals(typeName)) { + switch (typeName) { + case "decimal64": throw new YangParseException(moduleName, line, "The 'fraction-digits' statement MUST be present if the type is 'decimal64'."); - } else if ("identityref".equals(typeName)) { + case "identityref": throw new YangParseException(moduleName, line, "The 'base' statement MUST be present if the type is 'identityref'."); - } else if ("leafref".equals(typeName)) { + case "leafref": throw new YangParseException(moduleName, line, "The 'path' statement MUST be present if the type is 'leafref'."); - } else if ("bits".equals(typeName)) { + case "bits": throw new YangParseException(moduleName, line, "The 'bit' statement MUST be present if the type is 'bits'."); - } else if ("enumeration".equals(typeName)) { + case "enumeration": throw new YangParseException(moduleName, line, "The 'enum' statement MUST be present if the type is 'enumeration'."); } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/RefineUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/RefineUtils.java index 53e5205d9e..b499fdbce4 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/RefineUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/RefineUtils.java @@ -55,7 +55,7 @@ public final class RefineUtils { } } - public static void refineContainer(ContainerSchemaNodeBuilder container, RefineHolder refine, int line) { + public static void refineContainer(ContainerSchemaNodeBuilder container, RefineHolder refine) { Boolean presence = refine.isPresence(); MustDefinition must = refine.getMust(); List unknownNodes = refine.getUnknownNodeBuilders(); @@ -73,7 +73,7 @@ public final class RefineUtils { } } - public static void refineList(ListSchemaNodeBuilder list, RefineHolder refine, int line) { + public static void refineList(ListSchemaNodeBuilder list, RefineHolder refine) { MustDefinition must = refine.getMust(); Integer min = refine.getMinElements(); Integer max = refine.getMaxElements(); @@ -95,7 +95,7 @@ public final class RefineUtils { } } - public static void refineLeafList(LeafListSchemaNodeBuilder leafList, RefineHolder refine, int line) { + public static void refineLeafList(LeafListSchemaNodeBuilder leafList, RefineHolder refine) { MustDefinition must = refine.getMust(); Integer min = refine.getMinElements(); Integer max = refine.getMaxElements(); @@ -117,7 +117,7 @@ public final class RefineUtils { } } - public static void refineChoice(ChoiceBuilder choice, RefineHolder refine, int line) { + public static void refineChoice(ChoiceBuilder choice, RefineHolder refine) { String defaultStr = refine.getDefaultStr(); Boolean mandatory = refine.isMandatory(); List unknownNodes = refine.getUnknownNodeBuilders(); @@ -135,7 +135,7 @@ public final class RefineUtils { } } - public static void refineAnyxml(AnyXmlBuilder anyXml, RefineHolder refine, int line) { + public static void refineAnyxml(AnyXmlBuilder anyXml, RefineHolder refine) { Boolean mandatory = refine.isMandatory(); MustDefinition must = refine.getMust(); List unknownNodes = refine.getUnknownNodeBuilders(); @@ -298,21 +298,20 @@ public final class RefineUtils { * refine object containing information about refine process */ public static void performRefine(SchemaNodeBuilder nodeToRefine, RefineHolder refine) { - final int line = refine.getLine(); checkRefine(nodeToRefine, refine); refineDefault(nodeToRefine, refine); if (nodeToRefine instanceof LeafSchemaNodeBuilder) { refineLeaf((LeafSchemaNodeBuilder) nodeToRefine, refine); } else if (nodeToRefine instanceof ContainerSchemaNodeBuilder) { - refineContainer((ContainerSchemaNodeBuilder) nodeToRefine, refine, line); + refineContainer((ContainerSchemaNodeBuilder) nodeToRefine, refine); } else if (nodeToRefine instanceof ListSchemaNodeBuilder) { - refineList((ListSchemaNodeBuilder) nodeToRefine, refine, line); + refineList((ListSchemaNodeBuilder) nodeToRefine, refine); } else if (nodeToRefine instanceof LeafListSchemaNodeBuilder) { - refineLeafList((LeafListSchemaNodeBuilder) nodeToRefine, refine, line); + refineLeafList((LeafListSchemaNodeBuilder) nodeToRefine, refine); } else if (nodeToRefine instanceof ChoiceBuilder) { - refineChoice((ChoiceBuilder) nodeToRefine, refine, line); + refineChoice((ChoiceBuilder) nodeToRefine, refine); } else if (nodeToRefine instanceof AnyXmlBuilder) { - refineAnyxml((AnyXmlBuilder) nodeToRefine, refine, line); + refineAnyxml((AnyXmlBuilder) nodeToRefine, refine); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeConstraints.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeConstraints.java index e3c31966a3..8391759ec3 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeConstraints.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeConstraints.java @@ -23,20 +23,16 @@ import org.opendaylight.yangtools.yang.model.util.BaseConstraints; public final class TypeConstraints { private final String moduleName; private final int line; - private final List> ranges = new ArrayList>(); - private final List> lengths = new ArrayList>(); - private final List> patterns = new ArrayList>(); - private final List fractionDigits = new ArrayList(); + private final List> ranges = new ArrayList<>(); + private final List> lengths = new ArrayList<>(); + private final List> patterns = new ArrayList<>(); + private final List fractionDigits = new ArrayList<>(); public TypeConstraints(final String moduleName, final int line) { this.moduleName = moduleName; this.line = line; } - List> getAllRanges() { - return ranges; - } - public List getRange() { if (ranges.size() < 2) { return Collections.emptyList(); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeUtils.java index 2839779a45..d73807c9d5 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeUtils.java @@ -7,7 +7,8 @@ */ package org.opendaylight.yangtools.yang.parser.util; -import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.*; +import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.findModuleFromBuilders; +import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.findModuleFromContext; import java.util.ArrayList; import java.util.Date; @@ -20,7 +21,12 @@ import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.*; +import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.UnknownType; import org.opendaylight.yangtools.yang.parser.builder.api.Builder; @@ -121,7 +127,7 @@ public final class TypeUtils { public static void resolveTypeUnion(final UnionTypeBuilder union, final Map> modules, final ModuleBuilder builder) { final List> unionTypes = union.getTypes(); - final List> toRemove = new ArrayList>(); + final List> toRemove = new ArrayList<>(); for (TypeDefinition unionType : unionTypes) { if (unionType instanceof UnknownType) { final ModuleBuilder dependentModule = findModuleFromBuilders(modules, builder, unionType.getQName() @@ -147,7 +153,7 @@ public final class TypeUtils { /** * Resolve union type which contains one or more unresolved types. - * + * * @param union * union type builder to resolve * @param modules @@ -161,7 +167,7 @@ public final class TypeUtils { final Map> modules, final ModuleBuilder module, final SchemaContext context) { final List> unionTypes = union.getTypes(); - final List> toRemove = new ArrayList>(); + final List> toRemove = new ArrayList<>(); for (TypeDefinition unionType : unionTypes) { if (unionType instanceof UnknownType) { resolveUnionUnknownType(union, (UnknownType) unionType, modules, module, context); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/YangParseException.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/YangParseException.java index 0ec354b017..056215fa52 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/YangParseException.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/YangParseException.java @@ -18,16 +18,12 @@ public class YangParseException extends RuntimeException { super(errorMsg, exception); } - public YangParseException(final String moduleName, final int line, - final String errorMsg) { - super("Error in module '" + moduleName + "' at line " + line + ": " - + errorMsg); + public YangParseException(final String moduleName, final int line, final String errorMsg) { + super("Error in module '" + moduleName + "' at line " + line + ": " + errorMsg); } - public YangParseException(final String moduleName, final int line, - final String errorMsg, final Exception exception) { - super("Error in module '" + moduleName + "' at line " + line + ": " - + errorMsg, exception); + public YangParseException(final String moduleName, final int line, final String errorMsg, final Exception exception) { + super("Error in module '" + moduleName + "' at line " + line + ": " + errorMsg, exception); } } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java index 97454af9d2..6ee61357e5 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java @@ -14,6 +14,7 @@ import java.net.URI; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.List; @@ -34,8 +35,6 @@ import org.opendaylight.yangtools.yang.model.api.RpcDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import com.google.common.collect.Lists; - public class AugmentTest { private final URI types1NS = URI.create("urn:simple.nodes.test"); @@ -70,14 +69,21 @@ public class AugmentTest { @Test public void testAugmentParsing() { - SchemaPath expectedPath = null; - QName[] qnames = null; + SchemaPath expectedSchemaPath; + QName[] qnames = new QName[3]; + qnames[0] = q0; + qnames[1] = q1; + qnames[2] = q2; // testfile1 Module module1 = TestUtils.findModule(modules, "nodes"); Set augmentations = module1.getAugmentations(); assertEquals(1, augmentations.size()); AugmentationSchema augment = augmentations.iterator().next(); + assertNotNull(augment); + + expectedSchemaPath = new SchemaPath(Arrays.asList(qnames), true); + assertEquals(expectedSchemaPath, augment.getTargetPath()); Set augmentChildren = augment.getChildNodes(); assertEquals(5, augmentChildren.size()); @@ -104,33 +110,38 @@ public class AugmentTest { // leaf ds0ChannelNumber qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); - assertEquals(expectedPath, ds0ChannelNumber.getPath()); + expectedSchemaPath = new SchemaPath(Arrays.asList(qnames), true); + assertEquals(expectedSchemaPath, ds0ChannelNumber.getPath()); + assertFalse(ds0ChannelNumber.isAugmenting()); // type of leaf ds0ChannelNumber QName typeQName = BaseTypes.constructQName("string"); List typePath = Collections.singletonList(typeQName); - expectedPath = new SchemaPath(typePath, true); - assertEquals(expectedPath, ds0ChannelNumber.getType().getPath()); + expectedSchemaPath = new SchemaPath(typePath, true); + assertEquals(expectedSchemaPath, ds0ChannelNumber.getType().getPath()); // leaf interface-id qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); - assertEquals(expectedPath, interfaceId.getPath()); + expectedSchemaPath = new SchemaPath(Arrays.asList(qnames), true); + assertEquals(expectedSchemaPath, interfaceId.getPath()); + assertFalse(interfaceId.isAugmenting()); // leaf my-type qnames[3] = new QName(types1NS, types1Rev, t1, "my-type"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); - assertEquals(expectedPath, myType.getPath()); + expectedSchemaPath = new SchemaPath(Arrays.asList(qnames), true); + assertEquals(expectedSchemaPath, myType.getPath()); + assertFalse(myType.isAugmenting()); // container schemas qnames[3] = new QName(types1NS, types1Rev, t1, "schemas"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); - assertEquals(expectedPath, schemas.getPath()); + expectedSchemaPath = new SchemaPath(Arrays.asList(qnames), true); + assertEquals(expectedSchemaPath, schemas.getPath()); + assertFalse(schemas.isAugmenting()); // choice odl qnames[3] = new QName(types1NS, types1Rev, t1, "odl"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); - assertEquals(expectedPath, odl.getPath()); + expectedSchemaPath = new SchemaPath(Arrays.asList(qnames), true); + assertEquals(expectedSchemaPath, odl.getPath()); + assertFalse(odl.isAugmenting()); // testfile3 Module module3 = TestUtils.findModule(modules, "custom"); @@ -159,9 +170,6 @@ public class AugmentTest { @Test public void testAugmentResolving() throws ParseException { - SchemaPath expectedPath = null; - QName[] qnames = null; - Module module2 = TestUtils.findModule(modules, "types"); ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces"); ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry"); @@ -182,42 +190,40 @@ public class AugmentTest { assertNotNull(schemas); assertNotNull(odl); - qnames = new QName[4]; + SchemaPath expectedPath; + QName[] qnames = new QName[4]; qnames[0] = q0; qnames[1] = q1; qnames[2] = q2; // leaf ds0ChannelNumber qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, ds0ChannelNumber.getPath()); // leaf interface-id qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, interfaceId.getPath()); // leaf my-type qnames[3] = new QName(types1NS, types1Rev, t1, "my-type"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, myType.getPath()); // container schemas qnames[3] = new QName(types1NS, types1Rev, t1, "schemas"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, schemas.getPath()); // choice odl qnames[3] = new QName(types1NS, types1Rev, t1, "odl"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, odl.getPath()); } @Test public void testAugmentChoice() throws ParseException { - SchemaPath expectedPath = null; - QName[] qnames = null; - Module module2 = TestUtils.findModule(modules, "types"); ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces"); ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry"); @@ -253,7 +259,8 @@ public class AugmentTest { assertNotNull(node2); assertNotNull(node3); - qnames = new QName[5]; + SchemaPath expectedPath; + QName[] qnames = new QName[5]; qnames[0] = q0; qnames[1] = q1; qnames[2] = q2; @@ -261,28 +268,28 @@ public class AugmentTest { // case id qnames[4] = new QName(types1NS, types1Rev, t1, "id"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, id.getPath()); Set idChildren = id.getChildNodes(); assertEquals(1, idChildren.size()); // case node1 qnames[4] = new QName(types1NS, types1Rev, t1, "node1"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, node1.getPath()); Set node1Children = node1.getChildNodes(); assertTrue(node1Children.isEmpty()); // case node2 qnames[4] = new QName(types1NS, types1Rev, t1, "node2"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, node2.getPath()); Set node2Children = node2.getChildNodes(); assertTrue(node2Children.isEmpty()); // case node3 qnames[4] = new QName(types1NS, types1Rev, t1, "node3"); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, node3.getPath()); Set node3Children = node3.getChildNodes(); assertEquals(1, node3Children.size()); @@ -299,7 +306,7 @@ public class AugmentTest { qnames[5] = new QName(types1NS, types1Rev, t1, "id"); LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next(); assertNotNull(caseIdChild); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, caseIdChild.getPath()); // case node3 child @@ -307,7 +314,7 @@ public class AugmentTest { qnames[5] = new QName(types1NS, types1Rev, t1, "node3"); ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next(); assertNotNull(caseNode3Child); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, caseNode3Child.getPath()); } @@ -363,7 +370,7 @@ public class AugmentTest { assertTrue(create.isAugmenting()); assertTrue(destroy.isAugmenting()); - SchemaPath expectedPath = null; + SchemaPath expectedPath; QName[] qnames = new QName[4]; qnames[0] = submitQName; qnames[1] = inputQName; @@ -372,7 +379,7 @@ public class AugmentTest { // case attach qnames[3] = new QName(NS_FOO, revision, "f", "attach"); assertEquals(qnames[3], attach.getQName()); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, attach.getPath()); Set attachChildren = attach.getChildNodes(); assertEquals(1, attachChildren.size()); @@ -380,7 +387,7 @@ public class AugmentTest { // case create qnames[3] = new QName(NS_FOO, revision, "f", "create"); assertEquals(qnames[3], create.getQName()); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, create.getPath()); Set createChildren = create.getChildNodes(); assertEquals(1, createChildren.size()); @@ -388,7 +395,7 @@ public class AugmentTest { // case attach qnames[3] = new QName(NS_FOO, revision, "f", "destroy"); assertEquals(qnames[3], destroy.getQName()); - expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); assertEquals(expectedPath, destroy.getPath()); Set destroyChildren = destroy.getChildNodes(); assertEquals(1, destroyChildren.size()); diff --git a/yang/yang-parser-impl/src/test/resources/augment-test/rpc/bar.yang b/yang/yang-parser-impl/src/test/resources/augment-test/rpc/bar.yang index 99357bbfe9..0d1ffb9ea8 100644 --- a/yang/yang-parser-impl/src/test/resources/augment-test/rpc/bar.yang +++ b/yang/yang-parser-impl/src/test/resources/augment-test/rpc/bar.yang @@ -1,7 +1,7 @@ module bar { yang-version 1; namespace "urn:opendaylight:bar"; - prefix "b"; + prefix "bar"; revision "2013-10-11" { } diff --git a/yang/yang-parser-impl/src/test/resources/augment-test/rpc/foo.yang b/yang/yang-parser-impl/src/test/resources/augment-test/rpc/foo.yang index cecce24ced..7ee2c1b40a 100644 --- a/yang/yang-parser-impl/src/test/resources/augment-test/rpc/foo.yang +++ b/yang/yang-parser-impl/src/test/resources/augment-test/rpc/foo.yang @@ -1,14 +1,14 @@ module foo { yang-version 1; namespace "urn:opendaylight:foo"; - prefix "f"; + prefix "foo"; import bar { prefix b; revision-date 2013-10-11; } revision "2013-10-11" { } - augment "/b:submit/b:input/b:arguments" { + augment "/b:submit/b:input/b:arguments" { when "../type = attach-path-instruction"; case attach {