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.ImmutableSet;
12 import com.google.common.collect.ImmutableSet.Builder;
13 import java.util.Collection;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
25 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 public final class KeyStatementSupport
28 extends AbstractStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement,
29 EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> {
30 private static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
31 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
35 public KeyStatementSupport() {
36 super(YangStmtMapping.KEY);
40 public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
41 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
43 for (String keyToken : LIST_KEY_SPLITTER.split(value)) {
44 builder.add(SchemaNodeIdentifier.SAME.createChild(StmtContextUtils.qnameFromArgument(ctx, keyToken)));
48 // Throws NPE on nulls, retains first inserted value, cannot be modified
49 final Collection<SchemaNodeIdentifier> ret = builder.build();
50 SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
51 "Key argument '%s' contains duplicates", value);
56 public Collection<SchemaNodeIdentifier> adaptArgumentValue(
57 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
58 EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx,
59 final QNameModule targetModule) {
60 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
61 boolean replaced = false;
62 for (final SchemaNodeIdentifier arg : ctx.getStatementArgument()) {
63 final QName qname = arg.getLastComponent();
64 if (!targetModule.equals(qname.getModule())) {
65 final QName newQname = ctx.getFromNamespace(QNameCacheNamespace.class,
66 QName.create(targetModule, qname.getLocalName()));
67 builder.add(SchemaNodeIdentifier.SAME.createChild(newQname));
74 // This makes sure we reuse the collection when a grouping is
75 // instantiated in the same module
76 return replaced ? builder.build() : ctx.getStatementArgument();
80 public KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
81 return new KeyStatementImpl(ctx);
85 public EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement> createEffective(
86 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
87 EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
88 return new KeyEffectiveStatementImpl(ctx);
92 protected SubstatementValidator getSubstatementValidator() {
93 return SUBSTATEMENT_VALIDATOR;