Fix unique argument parser
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / unique / UniqueStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.unique;
9
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.Collection;
14 import java.util.HashSet;
15 import java.util.Set;
16 import java.util.regex.Pattern;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Relative;
21 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueStatement;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27
28 public final class UniqueStatementSupport extends AbstractStatementSupport<Collection<Relative>, UniqueStatement,
29         EffectiveStatement<Collection<Relative>, UniqueStatement>> {
30     /**
31      * Support 'sep' ABNF rule in RFC7950 section 14. CRLF pattern is used to squash line-break from CRLF to LF form
32      * and then we use SEP_SPLITTER, which can operate on single characters.
33      */
34     private static final Pattern CRLF_PATTERN = Pattern.compile("\r\n", Pattern.LITERAL);
35     private static final Splitter SEP_SPLITTER = Splitter.on(CharMatcher.anyOf(" \t\n").precomputed())
36             .omitEmptyStrings();
37
38     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
39         YangStmtMapping.UNIQUE)
40         .build();
41     private static final UniqueStatementSupport INSTANCE = new UniqueStatementSupport();
42
43     private UniqueStatementSupport() {
44         super(YangStmtMapping.UNIQUE);
45     }
46
47     public static UniqueStatementSupport getInstance() {
48         return INSTANCE;
49     }
50
51     @Override
52     public Collection<Relative> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
53         final Collection<Relative> uniqueConstraints = parseUniqueConstraintArgument(ctx, value);
54         SourceException.throwIf(uniqueConstraints.isEmpty(), ctx.getStatementSourceReference(),
55                 "Invalid argument value '%s' of unique statement. The value must contains at least "
56                         + "one descendant schema node identifier.", value);
57         return uniqueConstraints;
58     }
59
60     @Override
61     public UniqueStatement createDeclared(final StmtContext<Collection<Relative>, UniqueStatement, ?> ctx) {
62         return new UniqueStatementImpl(ctx);
63     }
64
65     @Override
66     public EffectiveStatement<Collection<Relative>, UniqueStatement> createEffective(
67             final StmtContext<Collection<Relative>, UniqueStatement, EffectiveStatement<Collection<Relative>,
68             UniqueStatement>> ctx) {
69         return new UniqueEffectiveStatementImpl(ctx);
70     }
71
72     @Override
73     protected SubstatementValidator getSubstatementValidator() {
74         return SUBSTATEMENT_VALIDATOR;
75     }
76
77     private static Collection<Relative> parseUniqueConstraintArgument(final StmtContext<?, ?, ?> ctx,
78             final String argumentValue) {
79         // deal with 'line-break' rule, which is either "\n" or "\r\n", but not "\r"
80         final String nocrlf = CRLF_PATTERN.matcher(argumentValue).replaceAll("\n");
81
82         final Set<Relative> uniqueConstraintNodes = new HashSet<>();
83         for (final String uniqueArgToken : SEP_SPLITTER.split(nocrlf)) {
84             final SchemaNodeIdentifier nodeIdentifier = ArgumentUtils.nodeIdentifierFromPath(ctx, uniqueArgToken);
85             SourceException.throwIf(nodeIdentifier.isAbsolute(), ctx.getStatementSourceReference(),
86                     "Unique statement argument '%s' contains schema node identifier '%s' "
87                             + "which is not in the descendant node identifier form.", argumentValue, uniqueArgToken);
88             uniqueConstraintNodes.add((Relative) nodeIdentifier);
89         }
90         return ImmutableSet.copyOf(uniqueConstraintNodes);
91     }
92 }