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.meta;
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 com.google.common.collect.ImmutableSet.Builder;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
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.KeyEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
23 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
24 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
25 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementLexer;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 public final class KeyStatementSupport
34 extends AbstractStatementSupport<Set<QName>, KeyStatement, KeyEffectiveStatement> {
36 * This is equivalent to {@link YangStatementLexer#SEP}'s definition. Currently equivalent to the non-repeating
40 * {@code SEP: [ \n\r\t]+ -> type(SEP);}.
42 private static final CharMatcher SEP = CharMatcher.anyOf(" \n\r\t").precomputed();
45 * Splitter corresponding to {@code key-arg} ABNF as defined
46 * in <a href="https://tools.ietf.org/html/rfc6020#section-12">RFC6020, section 12</a>:
49 * {@code key-arg = node-identifier *(sep node-identifier)}
52 * We also account for {@link #SEP} not handling repetition by ignoring empty strings.
54 private static final Splitter KEY_ARG_SPLITTER = Splitter.on(SEP).omitEmptyStrings();
56 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
57 SubstatementValidator.builder(YangStmtMapping.KEY).build();
58 private static final KeyStatementSupport INSTANCE = new KeyStatementSupport();
60 private KeyStatementSupport() {
61 super(YangStmtMapping.KEY, StatementPolicy.copyDeclared(
62 // Identity comparison is sufficient because adaptArgumentValue() is careful about reuse.
63 (copy, current, substatements) -> copy.getArgument() == current.getArgument()));
66 public static KeyStatementSupport getInstance() {
71 public ImmutableSet<QName> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
72 final Builder<QName> builder = ImmutableSet.builder();
74 for (String keyToken : KEY_ARG_SPLITTER.split(value)) {
75 builder.add(StmtContextUtils.parseNodeIdentifier(ctx, keyToken));
79 // Throws NPE on nulls, retains first inserted value, cannot be modified
80 final ImmutableSet<QName> ret = builder.build();
81 SourceException.throwIf(ret.size() != tokens, ctx, "Key argument '%s' contains duplicates", value);
86 public Set<QName> adaptArgumentValue(final StmtContext<Set<QName>, KeyStatement, KeyEffectiveStatement> ctx,
87 final QNameModule targetModule) {
88 final Builder<QName> builder = ImmutableSet.builder();
89 boolean replaced = false;
90 for (final QName qname : ctx.getArgument()) {
91 if (!targetModule.equals(qname.getModule())) {
92 final QName newQname = qname.bindTo(targetModule).intern();
93 builder.add(newQname);
100 // This makes sure we reuse the collection when a grouping is instantiated in the same module.
101 return replaced ? builder.build() : ctx.argument();
105 protected SubstatementValidator getSubstatementValidator() {
106 return SUBSTATEMENT_VALIDATOR;
110 protected KeyStatement createDeclared(final StmtContext<Set<QName>, KeyStatement, ?> ctx,
111 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
112 return DeclaredStatements.createKey(ctx.getRawArgument(), ctx.getArgument(), substatements);
116 protected KeyEffectiveStatement createEffective(final Current<Set<QName>, KeyStatement> stmt,
117 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
118 return EffectiveStatements.createKey(stmt.declared(), stmt.getArgument(), substatements);