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.key;
10 import static com.google.common.base.Verify.verify;
12 import com.google.common.base.CharMatcher;
13 import com.google.common.base.Splitter;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.collect.ImmutableSet.Builder;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
26 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementLexer;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
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 BaseStatementSupport<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 = SubstatementValidator.builder(
59 private static final KeyStatementSupport INSTANCE = new KeyStatementSupport();
61 private KeyStatementSupport() {
62 super(YangStmtMapping.KEY);
65 public static KeyStatementSupport getInstance() {
70 public ImmutableSet<QName> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
71 final Builder<QName> builder = ImmutableSet.builder();
73 for (String keyToken : KEY_ARG_SPLITTER.split(value)) {
74 builder.add(StmtContextUtils.parseNodeIdentifier(ctx, keyToken));
78 // Throws NPE on nulls, retains first inserted value, cannot be modified
79 final ImmutableSet<QName> ret = builder.build();
80 SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
81 "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.coerceStatementArgument()) {
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
101 // instantiated in the same module
102 return replaced ? builder.build() : ctx.getStatementArgument();
106 protected SubstatementValidator getSubstatementValidator() {
107 return SUBSTATEMENT_VALIDATOR;
111 protected KeyStatement createDeclared(final StmtContext<Set<QName>, KeyStatement, ?> ctx,
112 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
113 return new RegularKeyStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(), substatements);
117 protected KeyStatement createEmptyDeclared(final StmtContext<Set<QName>, KeyStatement, ?> ctx) {
118 return new EmptyKeyStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument());
122 protected KeyEffectiveStatement createEffective(
123 final StmtContext<Set<QName>, KeyStatement, KeyEffectiveStatement> ctx, final KeyStatement declared,
124 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
125 final Set<QName> arg = ctx.coerceStatementArgument();
126 return arg.equals(declared.argument()) ? new RegularLocalKeyEffectiveStatement(declared, substatements)
127 : new RegularForeignKeyEffectiveStatement(declared, arg, substatements);
131 protected KeyEffectiveStatement createEmptyEffective(
132 final StmtContext<Set<QName>, KeyStatement, KeyEffectiveStatement> ctx, final KeyStatement declared) {
133 final Set<QName> arg = ctx.coerceStatementArgument();
134 return arg.equals(declared.argument()) ? new EmptyLocalKeyEffectiveStatement(declared)
135 : new EmptyForeignKeyEffectiveStatement(declared, arg);
138 static @NonNull Object maskSet(final @NonNull Set<QName> set) {
139 return set.size() == 1 ? set.iterator().next() : set;
142 @SuppressWarnings("unchecked")
143 static @NonNull Set<QName> unmaskSet(final @NonNull Object masked) {
144 if (masked instanceof Set) {
145 return (Set<QName>) masked;
147 verify(masked instanceof QName, "Unexpected argument %s", masked);
148 return ImmutableSet.of((QName) masked);