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;
11 import static com.google.common.base.Verify.verifyNotNull;
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;
17 import java.util.Collection;
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.model.api.stmt.SchemaNodeIdentifier;
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<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> {
35 private static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
36 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
39 private static final KeyStatementSupport INSTANCE = new KeyStatementSupport();
41 private KeyStatementSupport() {
42 super(YangStmtMapping.KEY);
45 public static KeyStatementSupport getInstance() {
50 public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
51 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
53 for (String keyToken : LIST_KEY_SPLITTER.split(value)) {
54 builder.add(SchemaNodeIdentifier.SAME.createChild(StmtContextUtils.parseNodeIdentifier(ctx, keyToken)));
58 // Throws NPE on nulls, retains first inserted value, cannot be modified
59 final Collection<SchemaNodeIdentifier> ret = builder.build();
60 SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
61 "Key argument '%s' contains duplicates", value);
66 public Collection<SchemaNodeIdentifier> adaptArgumentValue(
67 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
68 final QNameModule targetModule) {
69 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
70 boolean replaced = false;
71 for (final SchemaNodeIdentifier arg : ctx.coerceStatementArgument()) {
72 final QName qname = arg.getLastComponent();
73 if (!targetModule.equals(qname.getModule())) {
74 final QName newQname = qname.bindTo(targetModule).intern();
75 builder.add(SchemaNodeIdentifier.SAME.createChild(newQname));
82 // This makes sure we reuse the collection when a grouping is
83 // instantiated in the same module
84 return replaced ? builder.build() : ctx.getStatementArgument();
88 protected SubstatementValidator getSubstatementValidator() {
89 return SUBSTATEMENT_VALIDATOR;
93 protected KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx,
94 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
95 return new RegularKeyStatement(ctx, substatements);
99 protected KeyStatement createEmptyDeclared(
100 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
101 return new EmptyKeyStatement(ctx);
105 protected KeyEffectiveStatement createEffective(
106 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
107 final KeyStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
108 final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
109 return arg.equals(declared.argument()) ? new RegularLocalKeyEffectiveStatement(declared, substatements)
110 : new RegularForeignKeyEffectiveStatement(declared, arg, substatements);
114 protected KeyEffectiveStatement createEmptyEffective(
115 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
116 final KeyStatement declared) {
117 final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
118 return arg.equals(declared.argument()) ? new EmptyLocalKeyEffectiveStatement(declared)
119 : new EmptyForeignKeyEffectiveStatement(declared, arg);
122 static @NonNull Object maskCollection(final @NonNull Collection<SchemaNodeIdentifier> coll) {
123 return coll.size() == 1 ? verifyNotNull(coll.iterator().next()) : coll;
126 @SuppressWarnings("unchecked")
127 static @NonNull Collection<SchemaNodeIdentifier> unmaskCollection(final @NonNull Object masked) {
128 if (masked instanceof Collection) {
129 return (Collection<SchemaNodeIdentifier>) masked;
131 verify(masked instanceof SchemaNodeIdentifier, "Unexpected argument %s", masked);
132 return ImmutableSet.of((SchemaNodeIdentifier) masked);