Search in sources :

Example 1 with ModificationType

use of org.opendaylight.yangtools.yang.data.tree.api.ModificationType in project yangtools by opendaylight.

the class AutomaticLifecycleMixin method disappearResult.

private static Optional<? extends TreeNode> disappearResult(final ModifiedNode modification, final TreeNode result, final Optional<? extends TreeNode> storeMeta) {
    // Check if the result is in fact empty before pulling any tricks
    if (!isEmpty(result)) {
        return Optional.of(result);
    }
    // We are pulling the 'disappear' trick, but what we report can be three different things
    final ModificationType finalType;
    if (!storeMeta.isPresent()) {
        // ... there was nothing in the datastore, no change
        finalType = ModificationType.UNMODIFIED;
    } else if (modification.getModificationType() == ModificationType.WRITE) {
        // ... this was an empty write, possibly originally a delete
        finalType = ModificationType.DELETE;
    } else {
        // ... it really disappeared
        finalType = ModificationType.DISAPPEARED;
    }
    modification.resolveModificationType(finalType);
    return Optional.empty();
}
Also used : ModificationType(org.opendaylight.yangtools.yang.data.tree.api.ModificationType)

Example 2 with ModificationType

use of org.opendaylight.yangtools.yang.data.tree.api.ModificationType in project yangtools by opendaylight.

the class DataTreeCandidates method cleanUpTree.

// Compare data before and after in order to find modified nodes without actual changes
private static DataTreeCandidateNode cleanUpTree(final TerminalDataTreeCandidateNode finalNode, final TerminalDataTreeCandidateNode node) {
    PathArgument identifier = node.getIdentifier();
    ModificationType nodeModification = node.getModificationType();
    Collection<DataTreeCandidateNode> childNodes = node.getChildNodes();
    for (DataTreeCandidateNode childNode : childNodes) {
        cleanUpTree(finalNode, (TerminalDataTreeCandidateNode) childNode);
    }
    Optional<NormalizedNode> dataBefore = finalNode.getDataBefore(identifier);
    switch(nodeModification) {
        case UNMODIFIED:
            finalNode.deleteNode(identifier);
            return finalNode;
        case WRITE:
            return finalNode;
        case DELETE:
            if (dataBefore.isEmpty()) {
                finalNode.deleteNode(identifier);
            }
            return finalNode;
        case APPEARED:
            if (dataBefore.isPresent()) {
                illegalModification(ModificationType.APPEARED, ModificationType.WRITE);
            }
            if (childNodes.isEmpty()) {
                finalNode.deleteNode(identifier);
            }
            return finalNode;
        case DISAPPEARED:
            if (dataBefore.isEmpty() || childNodes.isEmpty()) {
                finalNode.deleteNode(identifier);
            }
            return finalNode;
        case SUBTREE_MODIFIED:
            if (dataBefore.isEmpty()) {
                illegalModification(ModificationType.SUBTREE_MODIFIED, ModificationType.DELETE);
            }
            if (childNodes.isEmpty()) {
                finalNode.deleteNode(identifier);
            }
            return finalNode;
        default:
            throw new IllegalStateException("Unsupported modification type " + nodeModification);
    }
}
Also used : ModificationType(org.opendaylight.yangtools.yang.data.tree.api.ModificationType) DataTreeCandidateNode(org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Example 3 with ModificationType

use of org.opendaylight.yangtools.yang.data.tree.api.ModificationType in project yangtools by opendaylight.

the class DataTreeCandidates method compressNode.

private static void compressNode(final TerminalDataTreeCandidateNode finalNode, final DataTreeCandidateNode node, final PathArgument parent) {
    PathArgument identifier;
    try {
        identifier = node.getIdentifier();
    } catch (IllegalStateException e) {
        identifier = null;
    }
    // Check if finalNode has stored any changes for node
    if (finalNode.getNode(identifier).isEmpty()) {
        TerminalDataTreeCandidateNode parentNode = finalNode.getNode(parent).orElseThrow(() -> new IllegalArgumentException("No node found for " + parent + " identifier"));
        TerminalDataTreeCandidateNode childNode = new TerminalDataTreeCandidateNode(identifier, node.getDataBefore().orElse(null), parentNode);
        parentNode.addChildNode(childNode);
    }
    ModificationType nodeModification = node.getModificationType();
    switch(nodeModification) {
        case UNMODIFIED:
            // If node is unmodified there is no need iterate through its child nodes
            break;
        case WRITE:
        case DELETE:
        case APPEARED:
        case DISAPPEARED:
        case SUBTREE_MODIFIED:
            finalNode.setModification(identifier, compressModifications(finalNode.getModification(identifier), nodeModification, finalNode.getDataAfter(identifier).isEmpty()));
            finalNode.setData(identifier, node.getDataAfter().orElse(null));
            for (DataTreeCandidateNode child : node.getChildNodes()) {
                compressNode(finalNode, child, identifier);
            }
            break;
        default:
            throw new IllegalStateException("Unsupported modification type " + nodeModification);
    }
}
Also used : ModificationType(org.opendaylight.yangtools.yang.data.tree.api.ModificationType) DataTreeCandidateNode(org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)

Example 4 with ModificationType

use of org.opendaylight.yangtools.yang.data.tree.api.ModificationType in project yangtools by opendaylight.

the class DataTreeCandidates method fastCompressNode.

private static DataTreeCandidateNode fastCompressNode(final DataTreeCandidateNode first, final List<DataTreeCandidateNode> input) {
    final DataTreeCandidateNode last = input.get(input.size() - 1);
    ModificationType nodeModification = last.getModificationType();
    Optional<NormalizedNode> dataBefore = first.getDataBefore();
    Optional<NormalizedNode> dataAfter = last.getDataAfter();
    switch(nodeModification) {
        case DELETE:
            ModificationType previous = first.getModificationType();
            // Check if node had data before
            if (previous == ModificationType.DELETE || previous == ModificationType.DISAPPEARED || previous == ModificationType.UNMODIFIED && dataBefore.isEmpty()) {
                illegalModification(ModificationType.DELETE, ModificationType.DELETE);
            }
            if (dataBefore.isEmpty()) {
                return new TerminalDataTreeCandidateNode(null, ModificationType.UNMODIFIED, null, null);
            }
            return new TerminalDataTreeCandidateNode(null, nodeModification, dataBefore.get(), null);
        case WRITE:
            return new TerminalDataTreeCandidateNode(null, nodeModification, dataBefore.orElse(null), dataAfter.orElseThrow());
        case APPEARED:
        case DISAPPEARED:
        case SUBTREE_MODIFIED:
        case UNMODIFIED:
            // No luck, we need to iterate
            return slowCompressNodes(first, input);
        default:
            throw new IllegalStateException("Unsupported modification type " + nodeModification);
    }
}
Also used : ModificationType(org.opendaylight.yangtools.yang.data.tree.api.ModificationType) DataTreeCandidateNode(org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Aggregations

ModificationType (org.opendaylight.yangtools.yang.data.tree.api.ModificationType)4 DataTreeCandidateNode (org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode)3 PathArgument (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)2 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)2