Split out yang-data-spi
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / StructuralContainerModificationStrategy.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import java.util.Optional;
11 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
12 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
13 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
14 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
15 import org.opendaylight.yangtools.yang.data.spi.tree.Version;
16 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
17
18 /**
19  * Structural containers are special in that they appear when implied by child nodes and disappear whenever they are
20  * empty. We could implement this as a subclass of {@link SchemaAwareApplyOperation}, but the automatic semantic
21  * is quite different from all the other strategies. We create a {@link ContainerModificationStrategy} to tap into that
22  * logic, but wrap it so we only call out into it. We do not use {@link PresenceContainerModificationStrategy} because
23  * it enforces presence of mandatory leaves, which is not something we want here, as structural containers are not
24  * root anchors for that validation.
25  */
26 final class StructuralContainerModificationStrategy extends ContainerModificationStrategy {
27     private final ContainerNode emptyNode;
28
29     StructuralContainerModificationStrategy(final ContainerLike schema, final DataTreeConfiguration treeConfig) {
30         super(schema, treeConfig);
31         this.emptyNode = ImmutableNodes.containerNode(schema.getQName());
32     }
33
34     @Override
35     Optional<? extends TreeNode> apply(final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta,
36             final Version version) {
37         return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, storeMeta,
38             version);
39     }
40
41     @Override
42     TreeNode defaultTreeNode() {
43         return defaultTreeNode(emptyNode);
44     }
45 }