YANGTOOLS-706: reorganize statement definitions
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / key / KeyStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.key;
9
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.AbstractStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25
26 public final class KeyStatementSupport
27         extends AbstractStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement,
28                 EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> {
29     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
30         YangStmtMapping.KEY)
31         .build();
32
33     public KeyStatementSupport() {
34         super(YangStmtMapping.KEY);
35     }
36
37     @Override
38     public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
39         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
40         int tokens = 0;
41         for (String keyToken : StmtContextUtils.LIST_KEY_SPLITTER.split(value)) {
42             builder.add(SchemaNodeIdentifier.create(false, StmtContextUtils.qnameFromArgument(ctx, keyToken)));
43             tokens++;
44         }
45
46         // Throws NPE on nulls, retains first inserted value, cannot be modified
47         final Collection<SchemaNodeIdentifier> ret = builder.build();
48         SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
49                 "Key argument '%s' contains duplicates", value);
50         return ret;
51     }
52
53     @Override
54     public Collection<SchemaNodeIdentifier> adaptArgumentValue(
55             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
56                 EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx,
57             final QNameModule targetModule) {
58         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
59         boolean replaced = false;
60         for (final SchemaNodeIdentifier arg : ctx.getStatementArgument()) {
61             final QName qname = arg.getLastComponent();
62             if (!targetModule.equals(qname.getModule())) {
63                 final QName newQname = ctx.getFromNamespace(QNameCacheNamespace.class,
64                         QName.create(targetModule, qname.getLocalName()));
65                 builder.add(SchemaNodeIdentifier.create(false, newQname));
66                 replaced = true;
67             } else {
68                 builder.add(arg);
69             }
70         }
71
72         // This makes sure we reuse the collection when a grouping is
73         // instantiated in the same module
74         return replaced ? builder.build() : ctx.getStatementArgument();
75     }
76
77     @Override
78     public KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
79         return new KeyStatementImpl(ctx);
80     }
81
82     @Override
83     public EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement> createEffective(
84             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
85                     EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
86         return new KeyEffectiveStatementImpl(ctx);
87     }
88
89     @Override
90     protected SubstatementValidator getSubstatementValidator() {
91         return SUBSTATEMENT_VALIDATOR;
92     }
93 }