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.CharMatcher;
11 import com.google.common.base.Splitter;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.HashSet;
16 import java.util.regex.Pattern;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
23 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueStatement;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 public final class UniqueStatementSupport
33 extends BaseStatementSupport<Set<Descendant>, UniqueStatement, UniqueEffectiveStatement> {
35 * Support 'sep' ABNF rule in RFC7950 section 14. CRLF pattern is used to squash line-break from CRLF to LF form
36 * and then we use SEP_SPLITTER, which can operate on single characters.
38 private static final Pattern CRLF_PATTERN = Pattern.compile("\r\n", Pattern.LITERAL);
39 private static final Splitter SEP_SPLITTER = Splitter.on(CharMatcher.anyOf(" \t\n").precomputed())
42 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
43 YangStmtMapping.UNIQUE)
45 private static final UniqueStatementSupport INSTANCE = new UniqueStatementSupport();
47 private UniqueStatementSupport() {
48 super(YangStmtMapping.UNIQUE);
51 public static UniqueStatementSupport getInstance() {
56 public ImmutableSet<Descendant> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
57 final ImmutableSet<Descendant> uniqueConstraints = parseUniqueConstraintArgument(ctx, value);
58 SourceException.throwIf(uniqueConstraints.isEmpty(), ctx.sourceReference(),
59 "Invalid argument value '%s' of unique statement. The value must contains at least one descendant schema "
60 + "node identifier.", value);
61 return uniqueConstraints;
65 protected SubstatementValidator getSubstatementValidator() {
66 return SUBSTATEMENT_VALIDATOR;
70 protected UniqueStatement createDeclared(final StmtContext<Set<Descendant>, UniqueStatement, ?> ctx,
71 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
72 return new RegularUniqueStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(),
77 protected UniqueStatement createEmptyDeclared(final StmtContext<Set<Descendant>, UniqueStatement, ?> ctx) {
78 return new EmptyUniqueStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument());
82 protected UniqueEffectiveStatement createEffective(final Current<Set<Descendant>, UniqueStatement> stmt,
83 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
84 return substatements.isEmpty() ? new EmptyUniqueEffectiveStatement(stmt.declared())
85 : new RegularUniqueEffectiveStatement(stmt.declared(), substatements);
89 private static ImmutableSet<Descendant> parseUniqueConstraintArgument(final StmtContext<?, ?, ?> ctx,
90 final String argumentValue) {
91 // deal with 'line-break' rule, which is either "\n" or "\r\n", but not "\r"
92 final String nocrlf = CRLF_PATTERN.matcher(argumentValue).replaceAll("\n");
94 final Set<Descendant> uniqueConstraintNodes = new HashSet<>();
95 for (final String uniqueArgToken : SEP_SPLITTER.split(nocrlf)) {
96 final SchemaNodeIdentifier nodeIdentifier = ArgumentUtils.nodeIdentifierFromPath(ctx, uniqueArgToken);
97 SourceException.throwIf(nodeIdentifier instanceof Absolute, ctx.sourceReference(),
98 "Unique statement argument '%s' contains schema node identifier '%s' which is not in the descendant "
99 + "node identifier form.", argumentValue, uniqueArgToken);
100 uniqueConstraintNodes.add((Descendant) nodeIdentifier);
102 return ImmutableSet.copyOf(uniqueConstraintNodes);