From 31e75bcd6033959305075cbfba28a305a144826d Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 16 May 2014 15:00:46 +0200 Subject: [PATCH] BUG-1024: reorganize code for clarity This reorganizes the code quite a bit and stops executing as soon as a result is known. Change-Id: I9537227e0e6230ee66253aa83ab0c5c2963725e4 Signed-off-by: Robert Varga --- .../yang/model/util/SchemaContextUtil.java | 104 +++++++++++------- 1 file changed, 62 insertions(+), 42 deletions(-) diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java index 64d7328004..f05c20614d 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java @@ -7,6 +7,7 @@ */ package org.opendaylight.yangtools.yang.model.util; +import com.google.common.base.Preconditions; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; @@ -16,7 +17,6 @@ import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Set; - import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; import org.opendaylight.yangtools.yang.model.api.AugmentationTarget; @@ -39,8 +39,6 @@ import org.opendaylight.yangtools.yang.model.api.UsesNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Preconditions; - /** * The Schema Context Util contains support methods for searching through Schema * Context modules for specified schema nodes via Schema Path or Revision Aware @@ -491,69 +489,91 @@ public final class SchemaContextUtil { return result; } - private static DataSchemaNode findCorrectTargetFromGrouping(final DataSchemaNode node, final SchemaContext ctx) { - if (node.getPath().getPath().size() == 1) { - // uses is under module statement - Module m = findParentModule(ctx, node); - DataSchemaNode result = null; - for (UsesNode u : m.getUses()) { - SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPath()); - if (!(targetGrouping instanceof GroupingDefinition)) { - throw new IllegalArgumentException(String.format("Failed to generate code for augment in %s", u)); - } - GroupingDefinition gr = (GroupingDefinition) targetGrouping; - result = gr.getDataChildByName(node.getQName().getLocalName()); - } - if (result == null) { - throw new IllegalArgumentException("Failed to generate code for augment"); + private static DataSchemaNode findCorrectImmediateTargetFromGrouping(final DataSchemaNode node, final SchemaContext ctx) { + // uses is under module statement + final Module m = findParentModule(ctx, node); + Preconditions.checkArgument(m != null, "Failed to find module for node {} in context {}", node, ctx); + + for (final UsesNode u : m.getUses()) { + final SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPath()); + Preconditions.checkArgument(targetGrouping instanceof GroupingDefinition, + "Failed to generate code for augment in %s", u); + + LOG.trace("Checking grouping {} for node {}", targetGrouping, node); + final GroupingDefinition gr = (GroupingDefinition) targetGrouping; + final DataSchemaNode result = gr.getDataChildByName(node.getQName().getLocalName()); + if (result != null) { + return result; } - return result; - } else { - DataSchemaNode result = null; + + LOG.debug("Skipped grouping {}, no matching node found", gr); + } + + throw new IllegalArgumentException( + String.format("Failed to find uses node matching {} in context {}", node, ctx)); + } + + private static DataSchemaNode findCorrectTargetFromGrouping(final DataSchemaNode node, final SchemaContext ctx) { + if (node.getPath().getPath().size() != 1) { QName currentName = node.getQName(); + // tmpPath is used to track level of nesting List tmpPath = new ArrayList<>(); Object parent = null; + // create schema path of parent node SchemaPath sp = node.getPath(); - List names = sp.getPath(); - List newNames = new ArrayList<>(names); + List newNames = new ArrayList<>(sp.getPath()); + // parentPath = nodePath - lastQName newNames.remove(newNames.size() - 1); SchemaPath newSp = SchemaPath.create(newNames, sp.isAbsolute()); + // find parent node by its schema path parent = findDataSchemaNode(ctx, newSp); do { tmpPath.add(currentName); + + DataSchemaNode result = null; + // search parent node's used groupings for presence of wanted + // node if (parent instanceof DataNodeContainer) { DataNodeContainer dataNodeParent = (DataNodeContainer) parent; for (UsesNode u : dataNodeParent.getUses()) { - if (result == null) { - result = getResultFromUses(u, currentName.getLocalName(), ctx); + result = getResultFromUses(u, currentName.getLocalName(), ctx); + if (result != null) { + break; } } } + + // if node is not found in any of current parent's used + // groupings => parent is added by grouping too, so repeat same + // process for parent if (result == null) { + // set current name to name of parent node currentName = ((SchemaNode) parent).getQName(); - if (parent instanceof SchemaNode) { - SchemaPath nodeSp = ((SchemaNode) parent).getPath(); - List nodeNames = nodeSp.getPath(); - List nodeNewNames = new ArrayList<>(nodeNames); - nodeNewNames.remove(nodeNewNames.size() - 1); - if (nodeNewNames.isEmpty()) { - parent = getParentModule((SchemaNode) parent, ctx); - } else { - SchemaPath nodeNewSp = SchemaPath.create(nodeNewNames, nodeSp.isAbsolute()); - parent = findDataSchemaNode(ctx, nodeNewSp); - } + Preconditions.checkArgument(parent instanceof SchemaNode, + "Failed to generate code for augmend node {} at parent {}", node, parent); + // create schema path for parent of current parent + SchemaPath nodeSp = ((SchemaNode) parent).getPath(); + List nodeNewNames = new ArrayList<>(nodeSp.getPath()); + nodeNewNames.remove(nodeNewNames.size() - 1); + // set new parent based on path + if (nodeNewNames.isEmpty()) { + parent = getParentModule((SchemaNode) parent, ctx); } else { - throw new IllegalArgumentException("Failed to generate code for augment"); + SchemaPath nodeNewSp = new SchemaPath(nodeNewNames, nodeSp.isAbsolute()); + parent = findDataSchemaNode(ctx, nodeNewSp); } + } else { + // if wanted node was found in grouping, traverse this node + // based on level of nesting + return getTargetNode(tmpPath, result, ctx); } - } while (result == null && !(parent instanceof Module)); + } while (!(parent instanceof Module)); - if (result != null) { - result = getTargetNode(tmpPath, result, ctx); - } - return result; + return null; + } else { + return findCorrectImmediateTargetFromGrouping(node, ctx); } } -- 2.36.6