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