Remove SchemaContextProvider
[yangtools.git] / yang / yang-data-jaxen / src / main / java / org / opendaylight / yangtools / yang / data / jaxen / YangFunctionContext.java
index 1c7d2fec613b261c7976f390534bce8ee9f4eeba..f93d0100fb073518aadf95220800f22d608b6d9a 100644 (file)
@@ -7,13 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.data.jaxen;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.base.Verify.verify;
+
 import com.google.common.base.Splitter;
-import com.google.common.base.Verify;
 import java.util.AbstractMap.SimpleImmutableEntry;
-import java.util.ArrayDeque;
-import java.util.Deque;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.Optional;
@@ -24,26 +23,23 @@ import org.jaxen.ContextSupport;
 import org.jaxen.Function;
 import org.jaxen.FunctionCallException;
 import org.jaxen.FunctionContext;
-import org.jaxen.JaxenRuntimeException;
 import org.jaxen.UnresolvableException;
-import org.jaxen.UnsupportedAxisException;
 import org.jaxen.XPathFunctionContext;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
-import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
+import org.opendaylight.yangtools.yang.model.api.PathExpression;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
@@ -52,7 +48,6 @@ import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.RegexUtils;
-import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
 
 /**
  * A {@link FunctionContext} which contains also YANG-specific functions current(), re-match(), deref(),
@@ -115,12 +110,8 @@ final class YangFunctionContext implements FunctionContext {
 
         final String bitName = (String) args.get(0);
 
-        Verify.verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass());
-
-        final NormalizedNodeContext currentNodeContext = (NormalizedNodeContext) context;
-        final SchemaContext schemaContext = getSchemaContext(currentNodeContext);
-        final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(schemaContext,
-            currentNodeContext);
+        final NormalizedNodeContext currentNodeContext = verifyContext(context);
+        final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(currentNodeContext);
 
         final TypeDefinition<?> nodeType = correspondingSchemaNode.getType();
         if (!(nodeType instanceof BitsTypeDefinition)) {
@@ -133,8 +124,7 @@ final class YangFunctionContext implements FunctionContext {
         }
 
         final BitsTypeDefinition bitsType = (BitsTypeDefinition) nodeType;
-        Preconditions.checkState(containsBit(bitsType, bitName), "Bit %s does not belong to bits %s.", bitName,
-            bitsType);
+        checkState(containsBit(bitsType, bitName), "Bit %s does not belong to bits %s.", bitName, bitsType);
         return ((Set<?>)nodeValue).contains(bitName);
     }
 
@@ -145,8 +135,7 @@ final class YangFunctionContext implements FunctionContext {
             throw new FunctionCallException("current() takes no arguments.");
         }
 
-        Verify.verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass());
-        return (NormalizedNodeContext) context;
+        return verifyContext(context);
     }
 
     // deref(node-set nodes) function as per https://tools.ietf.org/html/rfc7950#section-10.3.1
@@ -155,11 +144,8 @@ final class YangFunctionContext implements FunctionContext {
             throw new FunctionCallException("deref() takes only one argument: node-set nodes.");
         }
 
-        Verify.verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass());
-        final NormalizedNodeContext currentNodeContext = (NormalizedNodeContext) context;
-        final SchemaContext schemaContext = getSchemaContext(currentNodeContext);
-        final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(schemaContext,
-                currentNodeContext);
+        final NormalizedNodeContext currentNodeContext = verifyContext(context);
+        final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(currentNodeContext);
 
         final Object nodeValue = currentNodeContext.getNode().getValue();
         final TypeDefinition<?> type = correspondingSchemaNode.getType();
@@ -169,16 +155,16 @@ final class YangFunctionContext implements FunctionContext {
                             : null;
         }
         if (type instanceof LeafrefTypeDefinition) {
-            final RevisionAwareXPath xpath = ((LeafrefTypeDefinition) type).getPathStatement();
-            return getNodeReferencedByLeafref(xpath, currentNodeContext, schemaContext, correspondingSchemaNode,
-                    nodeValue);
+            final PathExpression xpath = ((LeafrefTypeDefinition) type).getPathStatement();
+            return getNodeReferencedByLeafref(xpath, currentNodeContext, getSchemaContext(currentNodeContext),
+                correspondingSchemaNode, nodeValue);
         }
         return null;
     }
 
     // derived-from(node-set nodes, string identity) function as per https://tools.ietf.org/html/rfc7950#section-10.4.1
     private static boolean derivedFrom(final Context context, final List<?> args) throws FunctionCallException {
-        final Entry<IdentitySchemaNode, IdentitySchemaNode> ids =  commonDerivedFrom("derived-from", context, args);
+        final Entry<IdentitySchemaNode, IdentitySchemaNode> ids = commonDerivedFrom("derived-from", context, args);
         return ids != null && isAncestorOf(ids.getKey(), ids.getValue());
     }
 
@@ -199,13 +185,11 @@ final class YangFunctionContext implements FunctionContext {
             throw new FunctionCallException("Argument 'identity' of " + functionName
                 + "() function should be a String.");
         }
-        Verify.verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass());
 
-        final NormalizedNodeContext currentNodeContext = (NormalizedNodeContext) context;
-        final SchemaContext schemaContext = getSchemaContext(currentNodeContext);
-        final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(schemaContext,
-            currentNodeContext);
+        final NormalizedNodeContext currentNodeContext = verifyContext(context);
+        final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(currentNodeContext);
 
+        final SchemaContext schemaContext = getSchemaContext(currentNodeContext);
         return correspondingSchemaNode.getType() instanceof IdentityrefTypeDefinition
                 && currentNodeContext.getNode().getValue() instanceof QName ? new SimpleImmutableEntry<>(
                         getIdentitySchemaNodeFromString((String) args.get(0), schemaContext, correspondingSchemaNode),
@@ -219,12 +203,8 @@ final class YangFunctionContext implements FunctionContext {
             throw new FunctionCallException("enum-value() takes one argument: node-set nodes.");
         }
 
-        Verify.verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass());
-
-        final NormalizedNodeContext currentNodeContext = (NormalizedNodeContext) context;
-        final SchemaContext schemaContext = getSchemaContext(currentNodeContext);
-        final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(schemaContext,
-            currentNodeContext);
+        final NormalizedNodeContext currentNodeContext = verifyContext(context);
+        final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(currentNodeContext);
 
         final TypeDefinition<?> nodeType = correspondingSchemaNode.getType();
         if (!(nodeType instanceof EnumTypeDefinition)) {
@@ -271,7 +251,7 @@ final class YangFunctionContext implements FunctionContext {
     private static IdentitySchemaNode getIdentitySchemaNodeFromQName(final QName identityQName,
             final SchemaContext schemaContext) {
         final Optional<Module> module = schemaContext.findModule(identityQName.getModule());
-        Preconditions.checkArgument(module.isPresent(), "Module for %s not found", identityQName);
+        checkArgument(module.isPresent(), "Module for %s not found", identityQName);
         return findIdentitySchemaNodeInModule(module.get(), identityQName);
     }
 
@@ -323,7 +303,7 @@ final class YangFunctionContext implements FunctionContext {
     private static NormalizedNode<?, ?> getNodeReferencedByInstanceIdentifier(final YangInstanceIdentifier path,
             final NormalizedNodeContext currentNodeContext) {
         final NormalizedNodeNavigator navigator = (NormalizedNodeNavigator) currentNodeContext.getNavigator();
-        final NormalizedNode<?, ?> rootNode = navigator.getRootNode();
+        final NormalizedNode<?, ?> rootNode = navigator.getDocument().getRootNode();
         final List<PathArgument> pathArguments = path.getPathArguments();
         if (pathArguments.get(0).getNodeType().equals(rootNode.getNodeType())) {
             final List<PathArgument> relPath = pathArguments.subList(1, pathArguments.size());
@@ -336,7 +316,7 @@ final class YangFunctionContext implements FunctionContext {
         return null;
     }
 
-    private static NormalizedNode<?, ?> getNodeReferencedByLeafref(final RevisionAwareXPath xpath,
+    private static NormalizedNode<?, ?> getNodeReferencedByLeafref(final PathExpression xpath,
             final NormalizedNodeContext currentNodeContext, final SchemaContext schemaContext,
             final TypedDataSchemaNode correspondingSchemaNode, final Object nodeValue) {
         final NormalizedNode<?, ?> referencedNode = xpath.isAbsolute() ? getNodeReferencedByAbsoluteLeafref(xpath,
@@ -354,14 +334,14 @@ final class YangFunctionContext implements FunctionContext {
         return null;
     }
 
-    private static NormalizedNode<?, ?> getNodeReferencedByAbsoluteLeafref(final RevisionAwareXPath xpath,
+    private static NormalizedNode<?, ?> getNodeReferencedByAbsoluteLeafref(final PathExpression xpath,
             final NormalizedNodeContext currentNodeContext, final SchemaContext schemaContext,
             final TypedDataSchemaNode correspondingSchemaNode) {
         final LeafrefXPathStringParsingPathArgumentBuilder builder = new LeafrefXPathStringParsingPathArgumentBuilder(
-                xpath.toString(), schemaContext, correspondingSchemaNode, currentNodeContext);
+                xpath.getOriginalString(), schemaContext, correspondingSchemaNode, currentNodeContext);
         final List<PathArgument> pathArguments = builder.build();
         final NormalizedNodeNavigator navigator = (NormalizedNodeNavigator) currentNodeContext.getNavigator();
-        final NormalizedNode<?, ?> rootNode = navigator.getRootNode();
+        final NormalizedNode<?, ?> rootNode = navigator.getDocument().getRootNode();
         if (pathArguments.get(0).getNodeType().equals(rootNode.getNodeType())) {
             final List<PathArgument> relPath = pathArguments.subList(1, pathArguments.size());
             final Optional<NormalizedNode<?, ?>> possibleNode = NormalizedNodes.findNode(rootNode, relPath);
@@ -373,11 +353,11 @@ final class YangFunctionContext implements FunctionContext {
         return null;
     }
 
-    private static NormalizedNode<?, ?> getNodeReferencedByRelativeLeafref(final RevisionAwareXPath xpath,
+    private static NormalizedNode<?, ?> getNodeReferencedByRelativeLeafref(final PathExpression xpath,
             final NormalizedNodeContext currentNodeContext, final SchemaContext schemaContext,
             final TypedDataSchemaNode correspondingSchemaNode) {
         NormalizedNodeContext relativeNodeContext = currentNodeContext;
-        final StringBuilder xPathStringBuilder = new StringBuilder(xpath.toString());
+        final StringBuilder xPathStringBuilder = new StringBuilder(xpath.getOriginalString());
         // strip the relative path of all ../ at the beginning
         while (xPathStringBuilder.indexOf("../") == 0) {
             xPathStringBuilder.delete(0, 3);
@@ -431,38 +411,23 @@ final class YangFunctionContext implements FunctionContext {
                 enumName, enumerationType));
     }
 
-    private static SchemaContext getSchemaContext(final NormalizedNodeContext normalizedNodeContext) {
+    private static EffectiveModelContext getSchemaContext(final NormalizedNodeContext normalizedNodeContext) {
         final ContextSupport contextSupport = normalizedNodeContext.getContextSupport();
-        Verify.verify(contextSupport instanceof NormalizedNodeContextSupport, "Unhandled context support %s",
+        verify(contextSupport instanceof NormalizedNodeContextSupport, "Unhandled context support %s",
                 contextSupport.getClass());
-        return ((NormalizedNodeContextSupport) contextSupport).getSchemaContext();
+        return ((NormalizedNodeContextSupport) contextSupport).getEffectiveModelContext();
     }
 
-    private static TypedDataSchemaNode getCorrespondingTypedSchemaNode(final SchemaContext schemaContext,
-            final NormalizedNodeContext currentNodeContext) {
-        Iterator<NormalizedNodeContext> ancestorOrSelfAxisIterator;
-        try {
-            ancestorOrSelfAxisIterator = currentNodeContext.getContextSupport().getNavigator()
-                    .getAncestorOrSelfAxisIterator(currentNodeContext);
-        } catch (UnsupportedAxisException ex) {
-            throw new JaxenRuntimeException(ex);
-        }
-
-        final Deque<QName> schemaPathToCurrentNode = new ArrayDeque<>();
-        while (ancestorOrSelfAxisIterator.hasNext()) {
-            final NormalizedNode<?, ?> nextNode = ancestorOrSelfAxisIterator.next().getNode();
-            if (!(nextNode instanceof MapNode) && !(nextNode instanceof LeafSetNode)
-                    && !(nextNode instanceof AugmentationNode)) {
-                schemaPathToCurrentNode.addFirst(nextNode.getNodeType());
-            }
-        }
-
-        final SchemaNode schemaNode = SchemaContextUtil.findNodeInSchemaContext(schemaContext, schemaPathToCurrentNode);
-
-        Preconditions.checkNotNull(schemaNode, "Node %s does not have a corresponding SchemaNode in the SchemaContext.",
-                currentNodeContext.getNode());
-        Preconditions.checkState(schemaNode instanceof TypedDataSchemaNode, "Node %s must be a leaf or a leaf-list.",
+    private static TypedDataSchemaNode getCorrespondingTypedSchemaNode(final NormalizedNodeContext currentNodeContext) {
+        final DataSchemaNode schemaNode = currentNodeContext.getSchema().getDataSchemaNode();
+        checkState(schemaNode instanceof TypedDataSchemaNode, "Node %s must be a leaf or a leaf-list.",
                 currentNodeContext.getNode());
         return (TypedDataSchemaNode) schemaNode;
     }
+
+    private static NormalizedNodeContext verifyContext(final Context context) {
+        verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass());
+        return (NormalizedNodeContext) context;
+    }
+
 }