47a002035a0b01ba34135703549a5077bd232d4d
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / meta / 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.meta;
9
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;
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.DeclarationReference;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
24 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueStatement;
26 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
27 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
28 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
29 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
35 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
36
37 public final class UniqueStatementSupport
38         extends AbstractStatementSupport<Set<Descendant>, UniqueStatement, UniqueEffectiveStatement> {
39     /**
40      * Support 'sep' ABNF rule in RFC7950 section 14. CRLF pattern is used to squash line-break from CRLF to LF form
41      * and then we use SEP_SPLITTER, which can operate on single characters.
42      */
43     private static final Pattern CRLF_PATTERN = Pattern.compile("\r\n", Pattern.LITERAL);
44     private static final Splitter SEP_SPLITTER = Splitter.on(CharMatcher.anyOf(" \t\n").precomputed())
45             .omitEmptyStrings();
46
47     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
48         SubstatementValidator.builder(YangStmtMapping.UNIQUE).build();
49
50     public UniqueStatementSupport(final YangParserConfiguration config) {
51         // FIXME: This reflects what the current implementation does. We really want to define an adaptArgumentValue(),
52         //        but how that plays with the argument and expectations needs to be investigated.
53         super(YangStmtMapping.UNIQUE, StatementPolicy.contextIndependent(), config);
54     }
55
56     @Override
57     public ImmutableSet<Descendant> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
58         final ImmutableSet<Descendant> uniqueConstraints = parseUniqueConstraintArgument(ctx, value);
59         SourceException.throwIf(uniqueConstraints.isEmpty(), ctx,
60             "Invalid argument value '%s' of unique statement. The value must contains at least one descendant schema "
61                 + "node identifier.", value);
62         return uniqueConstraints;
63     }
64
65     @Override
66     protected SubstatementValidator getSubstatementValidator() {
67         return SUBSTATEMENT_VALIDATOR;
68     }
69
70     @Override
71     protected UniqueStatement createDeclared(final StmtContext<Set<Descendant>, UniqueStatement, ?> ctx,
72             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
73         return DeclaredStatements.createUnique(ctx.getRawArgument(), ctx.getArgument(), substatements);
74     }
75
76     @Override
77     protected UniqueStatement attachDeclarationReference(final UniqueStatement stmt,
78             final DeclarationReference reference) {
79         return DeclaredStatementDecorators.decorateUnique(stmt, reference);
80     }
81
82     @Override
83     protected UniqueEffectiveStatement createEffective(final Current<Set<Descendant>, UniqueStatement> stmt,
84             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
85         return EffectiveStatements.createUnique(stmt.declared(), substatements);
86     }
87
88     private static ImmutableSet<Descendant> parseUniqueConstraintArgument(final StmtContext<?, ?, ?> ctx,
89             final String argumentValue) {
90         // deal with 'line-break' rule, which is either "\n" or "\r\n", but not "\r"
91         final String nocrlf = CRLF_PATTERN.matcher(argumentValue).replaceAll("\n");
92
93         final Set<Descendant> uniqueConstraintNodes = new HashSet<>();
94         for (final String uniqueArgToken : SEP_SPLITTER.split(nocrlf)) {
95             final SchemaNodeIdentifier nodeIdentifier = ArgumentUtils.nodeIdentifierFromPath(ctx, uniqueArgToken);
96             SourceException.throwIf(nodeIdentifier instanceof Absolute, ctx,
97                 "Unique statement argument '%s' contains schema node identifier '%s' which is not in the descendant "
98                     + "node identifier form.", argumentValue, uniqueArgToken);
99             uniqueConstraintNodes.add((Descendant) nodeIdentifier);
100         }
101         return ImmutableSet.copyOf(uniqueConstraintNodes);
102     }
103 }