2 * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
10 import com.google.common.collect.ImmutableSet;
11 import com.google.common.collect.ImmutableSet.Builder;
12 import java.util.Collection;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
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;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.KeyEffectiveStatementImpl;
28 public class KeyStatementImpl extends AbstractDeclaredStatement<Collection<SchemaNodeIdentifier>> implements
30 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
34 protected KeyStatementImpl(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> context) {
38 public static class Definition
40 AbstractStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement,
41 EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> {
44 super(YangStmtMapping.KEY);
48 public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
49 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
51 for (String keyToken : StmtContextUtils.LIST_KEY_SPLITTER.split(value)) {
52 builder.add(SchemaNodeIdentifier.create(false, StmtContextUtils.qnameFromArgument(ctx, keyToken)));
56 // Throws NPE on nulls, retains first inserted value, cannot be modified
57 final Collection<SchemaNodeIdentifier> ret = builder.build();
58 SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
59 "Key argument '%s' contains duplicates", value);
64 public Collection<SchemaNodeIdentifier> adaptArgumentValue(
65 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
66 EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx,
67 final QNameModule targetModule) {
68 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
69 boolean replaced = false;
70 for (final SchemaNodeIdentifier arg : ctx.getStatementArgument()) {
71 final QName qname = arg.getLastComponent();
72 if (!targetModule.equals(qname)) {
73 final QName newQname = ctx.getFromNamespace(QNameCacheNamespace.class,
74 QName.create(targetModule, qname.getLocalName()));
75 builder.add(SchemaNodeIdentifier.create(false, 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 public KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
89 return new KeyStatementImpl(ctx);
93 public EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement> createEffective(
94 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
95 EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
96 return new KeyEffectiveStatementImpl(ctx);
100 protected SubstatementValidator getSubstatementValidator() {
101 return SUBSTATEMENT_VALIDATOR;