Add AbstractQName.bindTo(QNameModule)
[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 static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12
13 import com.google.common.base.Splitter;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.collect.ImmutableSet.Builder;
17 import java.util.Collection;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32
33 public final class KeyStatementSupport
34         extends BaseStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> {
35     private static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
36     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
37         YangStmtMapping.KEY)
38         .build();
39     private static final KeyStatementSupport INSTANCE = new KeyStatementSupport();
40
41     private KeyStatementSupport() {
42         super(YangStmtMapping.KEY);
43     }
44
45     public static KeyStatementSupport getInstance() {
46         return INSTANCE;
47     }
48
49     @Override
50     public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
51         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
52         int tokens = 0;
53         for (String keyToken : LIST_KEY_SPLITTER.split(value)) {
54             builder.add(SchemaNodeIdentifier.SAME.createChild(StmtContextUtils.parseNodeIdentifier(ctx, keyToken)));
55             tokens++;
56         }
57
58         // Throws NPE on nulls, retains first inserted value, cannot be modified
59         final Collection<SchemaNodeIdentifier> ret = builder.build();
60         SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
61                 "Key argument '%s' contains duplicates", value);
62         return ret;
63     }
64
65     @Override
66     public Collection<SchemaNodeIdentifier> adaptArgumentValue(
67             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
68             final QNameModule targetModule) {
69         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
70         boolean replaced = false;
71         for (final SchemaNodeIdentifier arg : ctx.coerceStatementArgument()) {
72             final QName qname = arg.getLastComponent();
73             if (!targetModule.equals(qname.getModule())) {
74                 final QName newQname = qname.bindTo(targetModule).intern();
75                 builder.add(SchemaNodeIdentifier.SAME.createChild(newQname));
76                 replaced = true;
77             } else {
78                 builder.add(arg);
79             }
80         }
81
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();
85     }
86
87     @Override
88     protected SubstatementValidator getSubstatementValidator() {
89         return SUBSTATEMENT_VALIDATOR;
90     }
91
92     @Override
93     protected KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx,
94             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
95         return new RegularKeyStatement(ctx, substatements);
96     }
97
98     @Override
99     protected KeyStatement createEmptyDeclared(
100             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
101         return new EmptyKeyStatement(ctx);
102     }
103
104     @Override
105     protected KeyEffectiveStatement createEffective(
106             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
107             final KeyStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
108         final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
109         return arg.equals(declared.argument()) ? new RegularLocalKeyEffectiveStatement(declared, substatements)
110                 : new RegularForeignKeyEffectiveStatement(declared, arg, substatements);
111     }
112
113     @Override
114     protected KeyEffectiveStatement createEmptyEffective(
115             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
116             final KeyStatement declared) {
117         final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
118         return arg.equals(declared.argument()) ? new EmptyLocalKeyEffectiveStatement(declared)
119                 : new EmptyForeignKeyEffectiveStatement(declared, arg);
120     }
121
122     static @NonNull Object maskCollection(final @NonNull Collection<SchemaNodeIdentifier> coll) {
123         return coll.size() == 1 ? verifyNotNull(coll.iterator().next()) : coll;
124     }
125
126     @SuppressWarnings("unchecked")
127     static @NonNull Collection<SchemaNodeIdentifier> unmaskCollection(final @NonNull Object masked) {
128         if (masked instanceof Collection) {
129             return (Collection<SchemaNodeIdentifier>) masked;
130         }
131         verify(masked instanceof SchemaNodeIdentifier, "Unexpected argument %s", masked);
132         return ImmutableSet.of((SchemaNodeIdentifier) masked);
133     }
134 }