*/
package org.opendaylight.yangtools.yang.model.util;
+import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
SchemaPath actualNodePath = actualSchemaNode.getPath();
if (actualNodePath != null) {
- List<QName> qnamePath = resolveRelativeXPath(context, module, relativeXPath, actualSchemaNode);
+ Iterable<QName> qnamePath = resolveRelativeXPath(context, module, relativeXPath, actualSchemaNode);
if (qnamePath != null) {
return findNodeInSchemaContext(context, qnamePath);
return context.findModuleByNamespaceAndRevision(qname.getNamespace(), qname.getRevision());
}
- public static SchemaNode findNodeInSchemaContext(final SchemaContext context, final List<QName> path) {
- final QName current = path.get(0);
+ public static SchemaNode findNodeInSchemaContext(final SchemaContext context, final Iterable<QName> path) {
+ final QName current = path.iterator().next();
LOG.trace("Looking up module {} in context {}", current, path);
final Module module = context.findModuleByNamespaceAndRevision(current.getNamespace(), current.getRevision());
return (GroupingDefinition) currentParent;
}
- private static SchemaNode findNodeInModule(final Module module, final List<QName> path) {
- final QName current = path.get(0);
+ private static SchemaNode findNodeInModule(final Module module, final Iterable<QName> path) {
+ final QName current = path.iterator().next();
LOG.trace("Looking for data container {} in module {}", current, module);
SchemaNode parent = module.getDataChildByName(current);
return null;
}
- private static SchemaNode findNodeInGrouping(final GroupingDefinition grouping, final List<QName> path) {
- if (path.isEmpty()) {
+ private static SchemaNode findNodeInGrouping(final GroupingDefinition grouping, final Iterable<QName> path) {
+ final QName current = Iterables.getFirst(path, null);
+ if (current == null) {
LOG.debug("Found grouping {}", grouping);
return grouping;
}
LOG.trace("Looking for path {} in grouping {}", path, grouping);
- final QName current = path.get(0);
final DataSchemaNode node = grouping.getDataChildByName(current);
if (node == null) {
LOG.debug("No node matching {} found in grouping {}", current, grouping);
return findNode(node, nextLevel(path));
}
- private static SchemaNode findNodeInRpc(final RpcDefinition rpc, final List<QName> path) {
- if (path.isEmpty()) {
+ private static SchemaNode findNodeInRpc(final RpcDefinition rpc, final Iterable<QName> path) {
+ final QName current = Iterables.getFirst(path, null);
+ if (current == null) {
LOG.debug("Found RPC {}", rpc);
return rpc;
}
LOG.trace("Looking for path {} in rpc {}", path, rpc);
- final QName current = path.get(0);
switch (current.getLocalName()) {
case "input":
return findNode(rpc.getInput(), nextLevel(path));
}
}
- private static SchemaNode findNodeInNotification(final NotificationDefinition ntf, final List<QName> path) {
- if (path.isEmpty()) {
+ private static SchemaNode findNodeInNotification(final NotificationDefinition ntf, final Iterable<QName> path) {
+ final QName current = Iterables.getFirst(path, null);
+ if (current == null) {
LOG.debug("Found notification {}", ntf);
return ntf;
}
LOG.trace("Looking for path {} in notification {}", path, ntf);
- final QName current = path.get(0);
DataSchemaNode node = ntf.getDataChildByName(current);
if (node == null) {
LOG.debug("No node matching {} found in notification {}", current, ntf);
return findNode(node, nextLevel(path));
}
- private static SchemaNode findNode(final ChoiceNode parent, final List<QName> path) {
- if (path.isEmpty()) {
+ private static SchemaNode findNode(final ChoiceNode parent, final Iterable<QName> path) {
+ final QName current = Iterables.getFirst(path, null);
+ if (current == null) {
return parent;
}
- QName current = path.get(0);
ChoiceCaseNode node = parent.getCaseNodeByName(current);
if (node != null) {
return findNodeInCase(node, nextLevel(path));
return null;
}
- private static SchemaNode findNode(final ContainerSchemaNode parent, final List<QName> path) {
- if (path.isEmpty()) {
+ private static SchemaNode findNode(final ContainerSchemaNode parent, final Iterable<QName> path) {
+ final QName current = Iterables.getFirst(path, null);
+ if (current == null) {
return parent;
}
- final QName current = path.get(0);
final DataSchemaNode node = parent.getDataChildByName(current);
if (node == null) {
LOG.debug("Failed to find {} in parent {}", path, parent);
return findNode(node, nextLevel(path));
}
- private static SchemaNode findNode(final ListSchemaNode parent, final List<QName> path) {
- if (path.isEmpty()) {
+ private static SchemaNode findNode(final ListSchemaNode parent, final Iterable<QName> path) {
+ final QName current = Iterables.getFirst(path, null);
+ if (current == null) {
return parent;
}
- QName current = path.get(0);
DataSchemaNode node = parent.getDataChildByName(current);
if (node == null) {
LOG.debug("Failed to find {} in parent {}", path, parent);
return findNode(node, nextLevel(path));
}
- private static SchemaNode findNode(final DataSchemaNode parent, final List<QName> path) {
+ private static SchemaNode findNode(final DataSchemaNode parent, final Iterable<QName> path) {
final SchemaNode node;
- if (!path.isEmpty()) {
+ if (!Iterables.isEmpty(path)) {
if (parent instanceof ContainerSchemaNode) {
node = findNode((ContainerSchemaNode) parent, path);
} else if (parent instanceof ListSchemaNode) {
return node;
}
- public static SchemaNode findNodeInCase(final ChoiceCaseNode parent, final List<QName> path) {
- if (path.isEmpty()) {
+ public static SchemaNode findNodeInCase(final ChoiceCaseNode parent, final Iterable<QName> path) {
+ final QName current = Iterables.getFirst(path, null);
+ if (current == null) {
return parent;
}
- QName current = path.get(0);
DataSchemaNode node = parent.getDataChildByName(current);
if (node == null) {
LOG.debug("Failed to find {} in parent {}", path, parent);
return null;
}
- private static List<QName> nextLevel(final List<QName> path) {
- return path.subList(1, path.size());
+ private static Iterable<QName> nextLevel(final Iterable<QName> path) {
+ return Iterables.skip(path, 1);
}
public static NotificationDefinition getNotificationByName(final Module module, final QName name) {
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());
+ final SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPathFromRoot());
Preconditions.checkArgument(targetGrouping instanceof GroupingDefinition,
"Failed to generate code for augment in %s", u);
}
private static DataSchemaNode getResultFromUses(final UsesNode u, final String currentName, final SchemaContext ctx) {
- SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPath());
+ SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPathFromRoot());
Preconditions.checkArgument(targetGrouping instanceof GroupingDefinition,
"Failed to generate code for augment in %s", u);
* Schema Path for Leafref
* @return list of QName
*/
- private static List<QName> resolveRelativeXPath(final SchemaContext context, final Module module,
+ private static Iterable<QName> resolveRelativeXPath(final SchemaContext context, final Module module,
final RevisionAwareXPath relativeXPath, final SchemaNode leafrefParentNode) {
Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
++colCount;
}
- final List<QName> absolutePath = new LinkedList<QName>();
- List<QName> path = leafrefParentNode.getPath().getPath();
- if (path != null) {
- int lenght = path.size() - colCount;
- absolutePath.addAll(path.subList(0, lenght));
- for (String s : Iterables.skip(xpaths, colCount)) {
- absolutePath.add(stringPathPartToQName(context, module, s));
- }
- }
-
- return absolutePath;
+ final Iterable<QName> parent = leafrefParentNode.getPath().getPathFromRoot();
+ return Iterables.concat(Iterables.limit(parent, Iterables.size(parent) - colCount),
+ Iterables.transform(Iterables.skip(xpaths, colCount), new Function<String, QName>() {
+ @Override
+ public QName apply(final String input) {
+ return stringPathPartToQName(context, module, input);
+ }
+ }));
}
}