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