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.ImmutableSet;
13 import java.util.HashSet;
15 import java.util.regex.Pattern;
16 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Relative;
20 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueStatement;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
25 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 public final class UniqueStatementSupport extends AbstractStatementSupport<Set<Relative>, UniqueStatement,
28 EffectiveStatement<Set<Relative>, UniqueStatement>> {
30 * Support 'sep' ABNF rule in RFC7950 section 14. CRLF pattern is used to squash line-break from CRLF to LF form
31 * and then we use SEP_SPLITTER, which can operate on single characters.
33 private static final Pattern CRLF_PATTERN = Pattern.compile("\r\n", Pattern.LITERAL);
34 private static final Splitter SEP_SPLITTER = Splitter.on(CharMatcher.anyOf(" \t\n").precomputed())
37 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
38 YangStmtMapping.UNIQUE)
40 private static final UniqueStatementSupport INSTANCE = new UniqueStatementSupport();
42 private UniqueStatementSupport() {
43 super(YangStmtMapping.UNIQUE);
46 public static UniqueStatementSupport getInstance() {
51 public Set<Relative> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
52 final Set<Relative> uniqueConstraints = parseUniqueConstraintArgument(ctx, value);
53 SourceException.throwIf(uniqueConstraints.isEmpty(), ctx.getStatementSourceReference(),
54 "Invalid argument value '%s' of unique statement. The value must contains at least "
55 + "one descendant schema node identifier.", value);
56 return uniqueConstraints;
60 public UniqueStatement createDeclared(final StmtContext<Set<Relative>, UniqueStatement, ?> ctx) {
61 return new UniqueStatementImpl(ctx);
65 public EffectiveStatement<Set<Relative>, UniqueStatement> createEffective(
66 final StmtContext<Set<Relative>, UniqueStatement, EffectiveStatement<Set<Relative>, UniqueStatement>> ctx) {
67 return new UniqueEffectiveStatementImpl(ctx);
71 protected SubstatementValidator getSubstatementValidator() {
72 return SUBSTATEMENT_VALIDATOR;
75 private static Set<Relative> parseUniqueConstraintArgument(final StmtContext<?, ?, ?> ctx,
76 final String argumentValue) {
77 // deal with 'line-break' rule, which is either "\n" or "\r\n", but not "\r"
78 final String nocrlf = CRLF_PATTERN.matcher(argumentValue).replaceAll("\n");
80 final Set<Relative> uniqueConstraintNodes = new HashSet<>();
81 for (final String uniqueArgToken : SEP_SPLITTER.split(nocrlf)) {
82 final SchemaNodeIdentifier nodeIdentifier = ArgumentUtils.nodeIdentifierFromPath(ctx, uniqueArgToken);
83 SourceException.throwIf(nodeIdentifier.isAbsolute(), ctx.getStatementSourceReference(),
84 "Unique statement argument '%s' contains schema node identifier '%s' "
85 + "which is not in the descendant node identifier form.", argumentValue, uniqueArgToken);
86 uniqueConstraintNodes.add((Relative) nodeIdentifier);
88 return ImmutableSet.copyOf(uniqueConstraintNodes);