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.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 public final class UniqueStatementSupport
32 extends BaseStatementSupport<Set<Descendant>, UniqueStatement, UniqueEffectiveStatement> {
34 * Support 'sep' ABNF rule in RFC7950 section 14. CRLF pattern is used to squash line-break from CRLF to LF form
35 * and then we use SEP_SPLITTER, which can operate on single characters.
37 private static final Pattern CRLF_PATTERN = Pattern.compile("\r\n", Pattern.LITERAL);
38 private static final Splitter SEP_SPLITTER = Splitter.on(CharMatcher.anyOf(" \t\n").precomputed())
41 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
42 YangStmtMapping.UNIQUE)
44 private static final UniqueStatementSupport INSTANCE = new UniqueStatementSupport();
46 private UniqueStatementSupport() {
47 super(YangStmtMapping.UNIQUE);
50 public static UniqueStatementSupport getInstance() {
55 public ImmutableSet<Descendant> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
56 final ImmutableSet<Descendant> uniqueConstraints = parseUniqueConstraintArgument(ctx, value);
57 SourceException.throwIf(uniqueConstraints.isEmpty(), ctx.getStatementSourceReference(),
58 "Invalid argument value '%s' of unique statement. The value must contains at least "
59 + "one descendant schema node identifier.", value);
60 return uniqueConstraints;
64 protected SubstatementValidator getSubstatementValidator() {
65 return SUBSTATEMENT_VALIDATOR;
69 protected UniqueStatement createDeclared(final StmtContext<Set<Descendant>, UniqueStatement, ?> ctx,
70 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
71 return new RegularUniqueStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(),
76 protected UniqueStatement createEmptyDeclared(final StmtContext<Set<Descendant>, UniqueStatement, ?> ctx) {
77 return new EmptyUniqueStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument());
81 protected UniqueEffectiveStatement createEffective(
82 final StmtContext<Set<Descendant>, UniqueStatement, UniqueEffectiveStatement> ctx,
83 final UniqueStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
84 return new RegularUniqueEffectiveStatement(declared, substatements);
88 protected UniqueEffectiveStatement createEmptyEffective(
89 final StmtContext<Set<Descendant>, UniqueStatement, UniqueEffectiveStatement> ctx,
90 final UniqueStatement declared) {
91 return new EmptyUniqueEffectiveStatement(declared);
94 private static ImmutableSet<Descendant> parseUniqueConstraintArgument(final StmtContext<?, ?, ?> ctx,
95 final String argumentValue) {
96 // deal with 'line-break' rule, which is either "\n" or "\r\n", but not "\r"
97 final String nocrlf = CRLF_PATTERN.matcher(argumentValue).replaceAll("\n");
99 final Set<Descendant> uniqueConstraintNodes = new HashSet<>();
100 for (final String uniqueArgToken : SEP_SPLITTER.split(nocrlf)) {
101 final SchemaNodeIdentifier nodeIdentifier = ArgumentUtils.nodeIdentifierFromPath(ctx, uniqueArgToken);
102 SourceException.throwIf(nodeIdentifier instanceof Absolute, ctx.getStatementSourceReference(),
103 "Unique statement argument '%s' contains schema node identifier '%s' "
104 + "which is not in the descendant node identifier form.", argumentValue, uniqueArgToken);
105 uniqueConstraintNodes.add((Descendant) nodeIdentifier);
107 return ImmutableSet.copyOf(uniqueConstraintNodes);