Merge branch 'master' of ../controller
[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.HashSet;
14 import java.util.Set;
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;
26
27 public final class UniqueStatementSupport extends AbstractStatementSupport<Set<Relative>, UniqueStatement,
28         EffectiveStatement<Set<Relative>, UniqueStatement>> {
29     /**
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.
32      */
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())
35             .omitEmptyStrings();
36
37     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
38         YangStmtMapping.UNIQUE)
39         .build();
40     private static final UniqueStatementSupport INSTANCE = new UniqueStatementSupport();
41
42     private UniqueStatementSupport() {
43         super(YangStmtMapping.UNIQUE);
44     }
45
46     public static UniqueStatementSupport getInstance() {
47         return INSTANCE;
48     }
49
50     @Override
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;
57     }
58
59     @Override
60     public UniqueStatement createDeclared(final StmtContext<Set<Relative>, UniqueStatement, ?> ctx) {
61         return new UniqueStatementImpl(ctx);
62     }
63
64     @Override
65     public EffectiveStatement<Set<Relative>, UniqueStatement> createEffective(
66             final StmtContext<Set<Relative>, UniqueStatement, EffectiveStatement<Set<Relative>, UniqueStatement>> ctx) {
67         return new UniqueEffectiveStatementImpl(ctx);
68     }
69
70     @Override
71     protected SubstatementValidator getSubstatementValidator() {
72         return SUBSTATEMENT_VALIDATOR;
73     }
74
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");
79
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);
87         }
88         return ImmutableSet.copyOf(uniqueConstraintNodes);
89     }
90 }