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 com.google.common.base.Splitter;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.ImmutableSet.Builder;
14 import java.util.Collection;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
23 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
30 public final class KeyStatementSupport
31 extends BaseStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> {
32 private static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
33 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
36 private static final KeyStatementSupport INSTANCE = new KeyStatementSupport();
38 private KeyStatementSupport() {
39 super(YangStmtMapping.KEY);
42 public static KeyStatementSupport getInstance() {
47 public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
48 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
50 for (String keyToken : LIST_KEY_SPLITTER.split(value)) {
51 builder.add(SchemaNodeIdentifier.SAME.createChild(StmtContextUtils.parseNodeIdentifier(ctx, keyToken)));
55 // Throws NPE on nulls, retains first inserted value, cannot be modified
56 final Collection<SchemaNodeIdentifier> ret = builder.build();
57 SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
58 "Key argument '%s' contains duplicates", value);
63 public Collection<SchemaNodeIdentifier> adaptArgumentValue(
64 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
65 final QNameModule targetModule) {
66 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
67 boolean replaced = false;
68 for (final SchemaNodeIdentifier arg : ctx.coerceStatementArgument()) {
69 final QName qname = arg.getLastComponent();
70 if (!targetModule.equals(qname.getModule())) {
71 final QName newQname = ctx.getFromNamespace(QNameCacheNamespace.class, qname.withModule(targetModule));
72 builder.add(SchemaNodeIdentifier.SAME.createChild(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<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx,
91 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
92 return new RegularKeyStatement(ctx, substatements);
96 protected KeyStatement createEmptyDeclared(
97 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
98 return new EmptyKeyStatement(ctx);
102 protected KeyEffectiveStatement createEffective(
103 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
104 final KeyStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
105 final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
106 return arg.equals(declared.argument()) ? new RegularLocalKeyEffectiveStatement(declared, substatements)
107 : new RegularForeignKeyEffectiveStatement(declared, arg, substatements);
111 protected KeyEffectiveStatement createEmptyEffective(
112 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
113 final KeyStatement declared) {
114 final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
115 return arg.equals(declared.argument()) ? new EmptyLocalKeyEffectiveStatement(declared)
116 : new EmptyForeignKeyEffectiveStatement(declared, arg);