Remove QNameCacheNamespace
[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.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;
28
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(
33         YangStmtMapping.KEY)
34         .build();
35     private static final KeyStatementSupport INSTANCE = new KeyStatementSupport();
36
37     private KeyStatementSupport() {
38         super(YangStmtMapping.KEY);
39     }
40
41     public static KeyStatementSupport getInstance() {
42         return INSTANCE;
43     }
44
45     @Override
46     public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
47         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
48         int tokens = 0;
49         for (String keyToken : LIST_KEY_SPLITTER.split(value)) {
50             builder.add(SchemaNodeIdentifier.SAME.createChild(StmtContextUtils.parseNodeIdentifier(ctx, keyToken)));
51             tokens++;
52         }
53
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);
58         return ret;
59     }
60
61     @Override
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));
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     protected SubstatementValidator getSubstatementValidator() {
85         return SUBSTATEMENT_VALIDATOR;
86     }
87
88     @Override
89     protected KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx,
90             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
91         return new RegularKeyStatement(ctx, substatements);
92     }
93
94     @Override
95     protected KeyStatement createEmptyDeclared(
96             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
97         return new EmptyKeyStatement(ctx);
98     }
99
100     @Override
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);
107     }
108
109     @Override
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);
116     }
117 }