2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
15 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
19 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
20 import org.opendaylight.yangtools.yang.data.spi.tree.Version;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
23 class MapEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
24 private static final class EnforcingMandatory extends MapEntryModificationStrategy {
25 private final MandatoryLeafEnforcer enforcer;
27 EnforcingMandatory(final ListSchemaNode schemaNode, final DataTreeConfiguration treeConfig,
28 final MandatoryLeafEnforcer enforcer) {
29 super(schemaNode, treeConfig);
30 this.enforcer = requireNonNull(enforcer);
34 void mandatoryVerifyValueChildren(final NormalizedNode writtenValue) {
35 enforcer.enforceOnData(writtenValue);
39 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
40 final Version version) {
41 final TreeNode ret = super.applyMerge(modification, currentMeta, version);
42 enforcer.enforceOnTreeNode(ret);
47 protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
48 final Optional<? extends TreeNode> currentMeta, final Version version) {
49 final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version);
50 enforcer.enforceOnTreeNode(ret);
55 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
56 final Version version) {
57 final TreeNode ret = super.applyTouch(modification, currentMeta, version);
58 enforcer.enforceOnTreeNode(ret);
63 private static final NormalizedNodeContainerSupport<NodeIdentifierWithPredicates, MapEntryNode> SUPPORT =
64 new NormalizedNodeContainerSupport<>(MapEntryNode.class, ImmutableMapEntryNodeBuilder::create,
65 ImmutableMapEntryNodeBuilder::create);
67 MapEntryModificationStrategy(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
68 super(SUPPORT, schema, treeConfig);
71 static @NonNull MapEntryModificationStrategy of(final ListSchemaNode schema,
72 final DataTreeConfiguration treeConfig) {
73 final Optional<MandatoryLeafEnforcer> enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig);
74 return enforcer.isPresent() ? new EnforcingMandatory(schema, treeConfig, enforcer.get())
75 : new MapEntryModificationStrategy(schema, treeConfig);