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.Splitter;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.ImmutableSet.Builder;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 public final class KeyStatementSupport
33 extends BaseStatementSupport<Set<QName>, KeyStatement, KeyEffectiveStatement> {
34 private static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
35 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
38 private static final KeyStatementSupport INSTANCE = new KeyStatementSupport();
40 private KeyStatementSupport() {
41 super(YangStmtMapping.KEY);
44 public static KeyStatementSupport getInstance() {
49 public ImmutableSet<QName> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
50 final Builder<QName> builder = ImmutableSet.builder();
52 for (String keyToken : LIST_KEY_SPLITTER.split(value)) {
53 builder.add(StmtContextUtils.parseNodeIdentifier(ctx, keyToken));
57 // Throws NPE on nulls, retains first inserted value, cannot be modified
58 final ImmutableSet<QName> ret = builder.build();
59 SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
60 "Key argument '%s' contains duplicates", value);
65 public Set<QName> adaptArgumentValue(final StmtContext<Set<QName>, KeyStatement, KeyEffectiveStatement> ctx,
66 final QNameModule targetModule) {
67 final Builder<QName> builder = ImmutableSet.builder();
68 boolean replaced = false;
69 for (final QName qname : ctx.coerceStatementArgument()) {
70 if (!targetModule.equals(qname.getModule())) {
71 final QName newQname = qname.bindTo(targetModule).intern();
72 builder.add(newQname);
79 // This makes sure we reuse the collection when a grouping is
80 // instantiated in the same module
81 return replaced ? builder.build() : ctx.getStatementArgument();
85 protected SubstatementValidator getSubstatementValidator() {
86 return SUBSTATEMENT_VALIDATOR;
90 protected KeyStatement createDeclared(final StmtContext<Set<QName>, KeyStatement, ?> ctx,
91 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
92 return new RegularKeyStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(), substatements);
96 protected KeyStatement createEmptyDeclared(final StmtContext<Set<QName>, KeyStatement, ?> ctx) {
97 return new EmptyKeyStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument());
101 protected KeyEffectiveStatement createEffective(final Current<Set<QName>, KeyStatement> stmt,
102 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
103 final Set<QName> arg = stmt.getArgument();
104 final KeyStatement declared = stmt.declared();
105 if (substatements.isEmpty()) {
106 return arg.equals(declared.argument()) ? new EmptyLocalKeyEffectiveStatement(declared)
107 : new EmptyForeignKeyEffectiveStatement(declared, arg);
110 return arg.equals(declared.argument()) ? new RegularLocalKeyEffectiveStatement(declared, substatements)
111 : new RegularForeignKeyEffectiveStatement(declared, arg, substatements);
114 static @NonNull Object maskSet(final @NonNull Set<QName> set) {
115 return set.size() == 1 ? set.iterator().next() : set;
118 @SuppressWarnings("unchecked")
119 static @NonNull Set<QName> unmaskSet(final @NonNull Object masked) {
120 if (masked instanceof Set) {
121 return (Set<QName>) masked;
123 verify(masked instanceof QName, "Unexpected argument %s", masked);
124 return ImmutableSet.of((QName) masked);