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.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 = qname.withModule(targetModule).intern();
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 protected SubstatementValidator getSubstatementValidator() {
85 return SUBSTATEMENT_VALIDATOR;
89 protected KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx,
90 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
91 return new RegularKeyStatement(ctx, substatements);
95 protected KeyStatement createEmptyDeclared(
96 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
97 return new EmptyKeyStatement(ctx);
101 protected KeyEffectiveStatement createEffective(
102 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
103 final KeyStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
104 final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
105 return arg.equals(declared.argument()) ? new RegularLocalKeyEffectiveStatement(declared, substatements)
106 : new RegularForeignKeyEffectiveStatement(declared, arg, substatements);
110 protected KeyEffectiveStatement createEmptyEffective(
111 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
112 final KeyStatement declared) {
113 final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
114 return arg.equals(declared.argument()) ? new EmptyLocalKeyEffectiveStatement(declared)
115 : new EmptyForeignKeyEffectiveStatement(declared, arg);