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.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 public final class KeyStatementSupport
30 extends BaseStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> {
31 private static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
32 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
35 private static final KeyStatementSupport INSTANCE = new KeyStatementSupport();
37 private KeyStatementSupport() {
38 super(YangStmtMapping.KEY);
41 public static KeyStatementSupport getInstance() {
46 public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
47 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
49 for (String keyToken : LIST_KEY_SPLITTER.split(value)) {
50 builder.add(SchemaNodeIdentifier.SAME.createChild(StmtContextUtils.parseNodeIdentifier(ctx, keyToken)));
54 // Throws NPE on nulls, retains first inserted value, cannot be modified
55 final Collection<SchemaNodeIdentifier> ret = builder.build();
56 SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
57 "Key argument '%s' contains duplicates", value);
62 public Collection<SchemaNodeIdentifier> adaptArgumentValue(
63 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
64 final QNameModule targetModule) {
65 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
66 boolean replaced = false;
67 for (final SchemaNodeIdentifier arg : ctx.coerceStatementArgument()) {
68 final QName qname = arg.getLastComponent();
69 if (!targetModule.equals(qname.getModule())) {
70 final QName newQname = ctx.getFromNamespace(QNameCacheNamespace.class, qname.withModule(targetModule));
71 builder.add(SchemaNodeIdentifier.SAME.createChild(newQname));
78 // This makes sure we reuse the collection when a grouping is
79 // instantiated in the same module
80 return replaced ? builder.build() : ctx.getStatementArgument();
84 public KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
85 return new KeyStatementImpl(ctx);
89 protected SubstatementValidator getSubstatementValidator() {
90 return SUBSTATEMENT_VALIDATOR;
94 protected KeyEffectiveStatement createEffective(
95 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
96 final KeyStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
97 final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
98 return arg.equals(declared.argument()) ? new RegularLocalKeyEffectiveStatement(declared, substatements)
99 : new RegularForeignKeyEffectiveStatement(declared, arg, substatements);
103 protected KeyEffectiveStatement createEmptyEffective(
104 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
105 final KeyStatement declared) {
106 final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
107 return arg.equals(declared.argument()) ? new EmptyLocalKeyEffectiveStatement(declared)
108 : new EmptyForeignKeyEffectiveStatement(declared, arg);