Make empty lists and choices disappear
[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.api.schema.tree.DataValidationFailedException;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
16 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18
19 /**
20  * Structural containers are special in that they appear when implied by child nodes and disappear whenever they are
21  * empty. We could implement this as a subclass of {@link SchemaAwareApplyOperation}, but the automatic semantic
22  * is quite different from all the other strategies. We create a {@link ContainerModificationStrategy} to tap into that
23  * logic, but wrap it so we only call out into it. We do not use {@link PresenceContainerModificationStrategy} because
24  * it enforces presence of mandatory leaves, which is not something we want here, as structural containers are not
25  * root anchors for that validation.
26  */
27 final class StructuralContainerModificationStrategy extends ContainerModificationStrategy {
28     private final ContainerNode emptyNode;
29
30     StructuralContainerModificationStrategy(final ContainerSchemaNode schema, final DataTreeConfiguration treeConfig) {
31         super(schema, treeConfig);
32         this.emptyNode = ImmutableNodes.containerNode(schema.getQName());
33     }
34
35     @Override
36     Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
37             final Version version) {
38         return AutomaticLifecycleMixin.apply(super::apply, emptyNode, modification, storeMeta, version);
39     }
40
41     @Override
42     void checkApplicable(final ModificationPath path, final NodeModification modification,
43             final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
44         AutomaticLifecycleMixin.checkApplicable(super::checkApplicable, emptyNode, path, modification, current,
45             version);
46     }
47 }