Add QName.withModule(QNameModule) method
[yangtools.git] / yang / yang-parser-rfc7950 / 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.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;
26
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(
32         YangStmtMapping.KEY)
33         .build();
34     private static final KeyStatementSupport INSTANCE = new KeyStatementSupport();
35
36     private KeyStatementSupport() {
37         super(YangStmtMapping.KEY);
38     }
39
40     public static KeyStatementSupport getInstance() {
41         return INSTANCE;
42     }
43
44     @Override
45     public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
46         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
47         int tokens = 0;
48         for (String keyToken : LIST_KEY_SPLITTER.split(value)) {
49             builder.add(SchemaNodeIdentifier.SAME.createChild(StmtContextUtils.qnameFromArgument(ctx, keyToken)));
50             tokens++;
51         }
52
53         // Throws NPE on nulls, retains first inserted value, cannot be modified
54         final Collection<SchemaNodeIdentifier> ret = builder.build();
55         SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
56                 "Key argument '%s' contains duplicates", value);
57         return ret;
58     }
59
60     @Override
61     public Collection<SchemaNodeIdentifier> adaptArgumentValue(
62             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
63                 EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx,
64             final QNameModule targetModule) {
65         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
66         boolean replaced = false;
67         for (final SchemaNodeIdentifier arg : ctx.getStatementArgument()) {
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));
72                 replaced = true;
73             } else {
74                 builder.add(arg);
75             }
76         }
77
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();
81     }
82
83     @Override
84     public KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
85         return new KeyStatementImpl(ctx);
86     }
87
88     @Override
89     public EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement> createEffective(
90             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
91                     EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
92         return new KeyEffectiveStatementImpl(ctx);
93     }
94
95     @Override
96     protected SubstatementValidator getSubstatementValidator() {
97         return SUBSTATEMENT_VALIDATOR;
98     }
99 }