Java Code Examples for org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier#getPathArguments()

The following examples show how to use org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier#getPathArguments() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NormalizedNodeContext.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
Optional<NormalizedNodeContext> findDescendant(final YangInstanceIdentifier path) {
    if (path.isEmpty()) {
        return Optional.of(this);
    }

    NormalizedNodeContext ctxWalk = this;
    NormalizedNode<?, ?> dataWalk = node;
    for (PathArgument arg : path.getPathArguments()) {
        checkArgument(dataWalk instanceof DataContainerNode, "Path %s refers beyond node %s", path, dataWalk);

        final Optional<DataContainerChild<? extends @Nullable PathArgument, ?>> optChild =
                ((DataContainerNode)dataWalk).getChild(arg);
        if (!optChild.isPresent()) {
            return Optional.empty();
        }

        dataWalk = optChild.get();
        ctxWalk = createChild(dataWalk);
    }

    return Optional.of(ctxWalk.createChild(dataWalk));
}
 
Example 2
Source File: StoreTreeNodes.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
    T current = tree;

    int depth = 1;
    for (PathArgument pathArg : path.getPathArguments()) {
        Optional<? extends T> potential = current.getChild(pathArg);
        if (potential.isEmpty()) {
            throw new IllegalArgumentException(String.format("Child %s is not present in tree.",
                    path.getAncestor(depth)));
        }
        current = potential.get();
        ++depth;
    }
    return current;
}
 
Example 3
Source File: AbstractMagnesiumDataOutput.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private void writeValue(final YangInstanceIdentifier value) throws IOException {
    final List<PathArgument> args = value.getPathArguments();
    final int size = args.size();
    if (size > 31) {
        output.writeByte(MagnesiumValue.YIID);
        output.writeInt(size);
    } else {
        output.writeByte(MagnesiumValue.YIID_0 + size);
    }
    for (PathArgument arg : args) {
        writePathArgumentInternal(arg);
    }
}
 
Example 4
Source File: AbstractLithiumDataOutput.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
final void writeYangInstanceIdentifierInternal(final YangInstanceIdentifier identifier) throws IOException {
    List<PathArgument> pathArguments = identifier.getPathArguments();
    output.writeInt(pathArguments.size());

    for (PathArgument pathArgument : pathArguments) {
        writePathArgumentInternal(pathArgument);
    }
}
 
Example 5
Source File: InMemoryDataTreeModification.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private OperationWithModification resolveModificationFor(final YangInstanceIdentifier path) {
    upgradeIfPossible();

    /*
     * Walk the strategy and modification trees in-sync, creating modification nodes as needed.
     *
     * If the user has provided wrong input, we may end up with a bunch of TOUCH nodes present
     * ending with an empty one, as we will throw the exception below. This fact could end up
     * being a problem, as we'd have bunch of phantom operations.
     *
     * That is fine, as we will prune any empty TOUCH nodes in the last phase of the ready
     * process.
     */
    ModificationApplyOperation operation = getStrategy();
    ModifiedNode modification = rootNode;

    int depth = 1;
    for (final PathArgument pathArg : path.getPathArguments()) {
        final Optional<ModificationApplyOperation> potential = operation.getChild(pathArg);
        if (!potential.isPresent()) {
            throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.",
                    path.getAncestor(depth)));
        }
        operation = potential.get();
        ++depth;

        modification = modification.modifyChild(pathArg, operation, version);
    }

    return OperationWithModification.from(operation, modification);
}
 
Example 6
Source File: YangFunctionContext.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private static NormalizedNode<?, ?> getNodeReferencedByInstanceIdentifier(final YangInstanceIdentifier path,
        final NormalizedNodeContext currentNodeContext) {
    final NormalizedNodeNavigator navigator = (NormalizedNodeNavigator) currentNodeContext.getNavigator();
    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());
        final Optional<NormalizedNode<?, ?>> possibleNode = NormalizedNodes.findNode(rootNode, relPath);
        if (possibleNode.isPresent()) {
            return possibleNode.get();
        }
    }

    return null;
}
 
Example 7
Source File: NormalizedNodeContextSupport.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
NormalizedNodeContext createContext(final YangInstanceIdentifier path) {
    NormalizedNodeContext result = root;
    for (PathArgument arg : path.getPathArguments()) {
        final Optional<NormalizedNode<?, ?>> node = NormalizedNodes.getDirectChild(result.getNode(), arg);
        checkArgument(node.isPresent(), "Node %s has no child %s", result.getNode(), arg);
        result = result.createChild(node.get());
    }
    return result;
}
 
Example 8
Source File: DataSchemaContextNode.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Find a child node as identified by a {@link YangInstanceIdentifier} relative to this node.
 *
 * @param path Path towards the child node
 * @return Child node if present, or empty when corresponding child is not found.
 * @throws NullPointerException if {@code path} is null
 */
public final @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(
        final @NonNull YangInstanceIdentifier path) {
    DataSchemaContextNode<?> currentOp = this;
    for (PathArgument arg : path.getPathArguments()) {
        currentOp = currentOp.getChild(arg);
        if (currentOp == null) {
            return Optional.empty();
        }
    }
    return Optional.of(currentOp);
}
 
Example 9
Source File: AbstractStringInstanceIdentifierCodec.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected final String serializeImpl(final YangInstanceIdentifier data) {
    final StringBuilder sb = new StringBuilder();
    DataSchemaContextNode<?> current = getDataContextTree().getRoot();
    QNameModule lastModule = null;
    for (PathArgument arg : data.getPathArguments()) {
        current = current.getChild(arg);
        checkArgument(current != null, "Invalid input %s: schema for argument %s (after %s) not found", data, arg,
                sb);

        if (current.isMixin()) {
            /*
             * XML/YANG instance identifier does not have concept
             * of augmentation identifier, or list as whole which
             * identifies a mixin (same as the parent element),
             * so we can safely ignore it if it is part of path
             * (since child node) is identified in same fashion.
             */
            continue;
        }

        final QName qname = arg.getNodeType();
        sb.append('/');
        appendQName(sb, qname, lastModule);
        lastModule = qname.getModule();

        if (arg instanceof NodeIdentifierWithPredicates) {
            for (Entry<QName, Object> entry : ((NodeIdentifierWithPredicates) arg).entrySet()) {
                appendQName(sb.append('['), entry.getKey(), lastModule);
                sb.append("='").append(String.valueOf(entry.getValue())).append("']");
            }
        } else if (arg instanceof NodeWithValue) {
            sb.append("[.='").append(((NodeWithValue<?>) arg).getValue()).append("']");
        }
    }
    return sb.toString();
}
 
Example 10
Source File: DataSchemaContextTree.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get a child node as identified by an absolute {@link YangInstanceIdentifier}.
 *
 * @param path Path towards the child node
 * @return Child node if present, or null when corresponding child is not found.
 * @throws NullPointerException if {@code path} is null
 *
 * @deprecated Use {@link #findChild(YangInstanceIdentifier)} instead.
 */
@Deprecated(forRemoval = true)
public @Nullable DataSchemaContextNode<?> getChild(final YangInstanceIdentifier path) {
    DataSchemaContextNode<?> currentOp = root;
    for (PathArgument arg : path.getPathArguments()) {
        currentOp = currentOp.getChild(arg);
        if (currentOp == null) {
            return null;
        }
    }
    return currentOp;
}