2 * Copyright (c) 2017 Pantheon Technologies, 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.parser.rfc7950.stmt.unique;
10 import com.google.common.base.Splitter;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.HashSet;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Relative;
19 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueStatement;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 public final class UniqueStatementSupport
27 extends AbstractStatementSupport<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement,
28 EffectiveStatement<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement>> {
29 private static final Splitter SPACE_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
30 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
31 YangStmtMapping.UNIQUE)
34 public UniqueStatementSupport() {
35 super(YangStmtMapping.UNIQUE);
39 public Collection<SchemaNodeIdentifier.Relative> parseArgumentValue(final StmtContext<?, ?, ?> ctx,
41 final Collection<Relative> uniqueConstraints = parseUniqueConstraintArgument(ctx, value);
42 SourceException.throwIf(uniqueConstraints.isEmpty(), ctx.getStatementSourceReference(),
43 "Invalid argument value '%s' of unique statement. The value must contains at least "
44 + "one descendant schema node identifier.", value);
45 return uniqueConstraints;
49 public UniqueStatement createDeclared(final StmtContext<Collection<Relative>, UniqueStatement, ?> ctx) {
50 return new UniqueStatementImpl(ctx);
54 public EffectiveStatement<Collection<Relative>, UniqueStatement> createEffective(
55 final StmtContext<Collection<Relative>, UniqueStatement,
56 EffectiveStatement<Collection<Relative>, UniqueStatement>> ctx) {
57 return new UniqueEffectiveStatementImpl(ctx);
61 protected SubstatementValidator getSubstatementValidator() {
62 return SUBSTATEMENT_VALIDATOR;
65 private static Collection<SchemaNodeIdentifier.Relative> parseUniqueConstraintArgument(
66 final StmtContext<?, ?, ?> ctx, final String argumentValue) {
67 final Set<SchemaNodeIdentifier.Relative> uniqueConstraintNodes = new HashSet<>();
68 for (final String uniqueArgToken : SPACE_SPLITTER.split(argumentValue)) {
69 final SchemaNodeIdentifier nodeIdentifier = ArgumentUtils.nodeIdentifierFromPath(ctx, uniqueArgToken);
70 SourceException.throwIf(nodeIdentifier.isAbsolute(), ctx.getStatementSourceReference(),
71 "Unique statement argument '%s' contains schema node identifier '%s' "
72 + "which is not in the descendant node identifier form.", argumentValue, uniqueArgToken);
73 uniqueConstraintNodes.add((SchemaNodeIdentifier.Relative) nodeIdentifier);
75 return ImmutableSet.copyOf(uniqueConstraintNodes);