+ private static @Nullable SchemaNode resolveRelativeXPath(final SchemaContext context, final Module module,
+ final String pathStr, final SchemaNode actualSchemaNode) {
+ checkState(actualSchemaNode.getPath() != null, "Schema Path reference for Leafref cannot be NULL");
+
+ return pathStr.startsWith("deref(") ? resolveDerefPath(context, module, actualSchemaNode, pathStr)
+ : findTargetNode(context, resolveRelativePath(context, module, actualSchemaNode,
+ doSplitXPath(pathStr)));
+ }
+
+ private static Iterable<QName> resolveRelativePath(final SchemaContext context, final Module module,
+ final SchemaNode actualSchemaNode, final List<String> steps) {
+ // Find out how many "parent" components there are and trim them
+ final int colCount = normalizeXPath(steps);
+ final List<String> xpaths = colCount == 0 ? steps : steps.subList(colCount, steps.size());
+
+ final List<QName> walkablePath = createWalkablePath(actualSchemaNode.getPath().getPathFromRoot(),
+ context, colCount);
+
+ if (walkablePath.size() - colCount >= 0) {
+ return Iterables.concat(Iterables.limit(walkablePath, walkablePath.size() - colCount),
+ Iterables.transform(xpaths, input -> stringPathPartToQName(context, module, input)));
+ }
+ return Iterables.concat(walkablePath,
+ Iterables.transform(xpaths, input -> stringPathPartToQName(context, module, input)));
+ }
+
+ /**
+ * Return List of qNames that are walkable using xPath. When getting a path from schema node it will return path
+ * with parents like CaseSchemaNode and ChoiceSchemaNode as well if they are parents of the node. We need to get
+ * rid of these in order to find the node that xPath is pointing to. Also we can not remove any node beyond the
+ * amount of "../" because we will not be able to find the correct schema node from schema context
+ *
+ * @param schemaNodePath list of qNames as a path to the leaf of type leafref
+ * @param context create schema context
+ * @param colCount amount of "../" in the xPath expression
+ * @return list of QNames as a path where we should be able to find referenced node
+ */
+ private static List<QName> createWalkablePath(final Iterable<QName> schemaNodePath, final SchemaContext context,
+ final int colCount) {
+ final List<Integer> indexToRemove = new ArrayList<>();
+ List<QName> schemaNodePathRet = Lists.newArrayList(schemaNodePath);
+ for (int j = 0, i = schemaNodePathRet.size() - 1; i >= 0 && j != colCount; i--, j++) {
+ final SchemaNode nodeIn = findTargetNode(context, schemaNodePathRet);
+ if (nodeIn instanceof CaseSchemaNode || nodeIn instanceof ChoiceSchemaNode) {
+ indexToRemove.add(i);
+ j--;
+ }
+ schemaNodePathRet.remove(i);
+ }
+ schemaNodePathRet = Lists.newArrayList(schemaNodePath);
+ for (int i : indexToRemove) {
+ schemaNodePathRet.remove(i);
+ }
+ return schemaNodePathRet;
+ }
+
+ private static SchemaNode resolveDerefPath(final SchemaContext context, final Module module,
+ final SchemaNode actualSchemaNode, final String xpath) {
+ final int paren = xpath.indexOf(')', 6);
+ checkArgument(paren != -1, "Cannot find matching parentheses in %s", xpath);
+
+ final String derefArg = xpath.substring(6, paren).strip();
+ // Look up the node which we need to reference
+ final SchemaNode derefTarget = findTargetNode(context, resolveRelativePath(context, module, actualSchemaNode,
+ doSplitXPath(derefArg)));
+ checkArgument(derefTarget != null, "Cannot find deref(%s) target node %s in context of %s", derefArg,
+ actualSchemaNode);
+ checkArgument(derefTarget instanceof TypedDataSchemaNode, "deref(%s) resolved to non-typed %s", derefArg,
+ derefTarget);
+
+ // We have a deref() target, decide what to do about it
+ final TypeDefinition<?> targetType = ((TypedDataSchemaNode) derefTarget).getType();
+ if (targetType instanceof InstanceIdentifierTypeDefinition) {
+ // Static inference breaks down, we cannot determine where this points to
+ // FIXME: dedicated exception, users can recover from it, derive from IAE
+ throw new UnsupportedOperationException("Cannot infer instance-identifier reference " + targetType);
+ }
+
+ // deref() is define only for instance-identifier and leafref types, handle the latter
+ checkArgument(targetType instanceof LeafrefTypeDefinition, "Illegal target type %s", targetType);
+
+ final PathExpression targetPath = ((LeafrefTypeDefinition) targetType).getPathStatement();
+ LOG.debug("Derefencing path {}", targetPath);
+
+ final SchemaNode deref = targetPath.isAbsolute()
+ ? findTargetNode(context, actualSchemaNode.getQName().getModule(),
+ ((LocationPathSteps) targetPath.getSteps()).getLocationPath())
+ : findDataSchemaNodeForRelativeXPath(context, module, actualSchemaNode, targetPath);
+ if (deref == null) {
+ LOG.debug("Path {} could not be derefenced", targetPath);
+ return null;
+ }