2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.tree.impl;
10 import static com.google.common.base.Preconditions.checkState;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.base.Stopwatch;
16 import com.google.common.collect.HashMultimap;
17 import com.google.common.collect.ImmutableList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
31 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
32 import org.opendaylight.yangtools.yang.data.tree.api.UniqueConstraintException;
33 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
38 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueEffectiveStatement;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * A {@link AbstractValidation} which ensures a particular {@code list} node complies with its {@code unique}
46 final class UniqueValidation extends AbstractValidation {
47 private static final Logger LOG = LoggerFactory.getLogger(UniqueValidation.class);
49 private final @NonNull ImmutableList<UniqueValidator<?>> validators;
51 private UniqueValidation(final ModificationApplyOperation delegate,
52 final ImmutableList<UniqueValidator<?>> validators) {
54 this.validators = requireNonNull(validators);
57 static ModificationApplyOperation of(final ListSchemaNode schema, final DataTreeConfiguration treeConfig,
58 final ModificationApplyOperation delegate) {
59 final ImmutableList<UniqueValidator<?>> validators = validatorsOf(schema, treeConfig);
60 return validators.isEmpty() ? delegate : new UniqueValidation(delegate, validators);
63 static ImmutableList<UniqueValidator<?>> validatorsOf(final ListSchemaNode schema,
64 final DataTreeConfiguration treeConfig) {
65 final Collection<? extends @NonNull UniqueEffectiveStatement> uniques = schema.getUniqueConstraints();
66 if (!treeConfig.isUniqueIndexEnabled() || uniques.isEmpty()) {
67 return ImmutableList.of();
70 final Stopwatch sw = Stopwatch.createStarted();
71 final Map<Descendant, List<NodeIdentifier>> paths = new HashMap<>();
72 final ImmutableList<UniqueValidator<?>> validators = uniques.stream()
73 .map(unique -> UniqueValidator.of(unique.argument().stream()
74 .map(descendant -> paths.computeIfAbsent(descendant, key -> toDescendantPath(schema, key)))
75 .collect(ImmutableList.toImmutableList())))
76 .collect(ImmutableList.toImmutableList());
77 LOG.debug("Constructed {} validators in {}", validators.size(), sw);
83 void enforceOnData(final NormalizedNode data) {
84 enforceOnData(data, (message, values) -> new UniqueValidationFailedException(message));
88 void enforceOnData(final ModificationPath path, final NormalizedNode data) throws UniqueConstraintException {
89 enforceOnData(data, (message, values) -> new UniqueConstraintException(path.toInstanceIdentifier(), values,
93 private <T extends @NonNull Exception> void enforceOnData(final NormalizedNode data,
94 final ExceptionSupplier<T> exceptionSupplier) throws T {
95 final Stopwatch sw = Stopwatch.createStarted();
96 verify(data instanceof NormalizedNodeContainer, "Unexpected data %s", data);
97 final var children = ((NormalizedNodeContainer<?>) data).body();
98 final var collected = HashMultimap.<UniqueValidator<?>, Object>create(validators.size(), children.size());
99 for (NormalizedNode child : children) {
100 verify(child instanceof DataContainerNode, "Unexpected child %s", child);
101 final DataContainerNode cont = (DataContainerNode) child;
103 final Map<List<NodeIdentifier>, Object> valueCache = new HashMap<>();
104 for (UniqueValidator<?> validator : validators) {
105 final Object values = validator.extractValues(valueCache, cont);
106 final Object masked = BinaryValue.wrap(values);
107 if (!collected.put(validator, masked)) {
108 final Map<Descendant, @Nullable Object> index = validator.indexValues(values);
109 throw exceptionSupplier.get(cont.name()
110 + " violates unique constraint on " + masked + " of " + index.keySet(), index);
115 LOG.trace("Enforced {} validators in {}", validators.size(), sw);
119 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
120 return super.addToStringAttributes(helper.add("validators", validators));
123 private static ImmutableList<NodeIdentifier> toDescendantPath(final ListSchemaNode parent,
124 final Descendant descendant) {
125 final List<QName> qnames = descendant.getNodeIdentifiers();
126 final ImmutableList.Builder<NodeIdentifier> builder = ImmutableList.builderWithExpectedSize(qnames.size());
127 final Iterator<QName> it = descendant.getNodeIdentifiers().iterator();
128 DataNodeContainer current = parent;
130 final QName qname = it.next();
131 final DataSchemaNode next = current.findDataChildByName(qname)
132 .orElseThrow(() -> new IllegalStateException("Cannot find component " + qname + " of " + descendant));
133 builder.add(NodeIdentifier.create(qname));
135 checkState(next instanceof TypedDataSchemaNode, "Unexpected schema %s for %s", next, descendant);
136 final ImmutableList<NodeIdentifier> ret = builder.build();
137 LOG.trace("Resolved {} to {}", descendant, ret);
141 checkState(next instanceof DataNodeContainer, "Unexpected non-container %s for %s", next, descendant);
142 current = (DataNodeContainer) next;
148 interface ExceptionSupplier<T extends Exception> {
149 T get(String message, Map<Descendant, @Nullable Object> values);