3383496cfd8a65332c14bd0f46a4b9cb56272a7c
[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.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
30
31 public final class KeyStatementSupport
32         extends BaseStatementSupport<Set<QName>, KeyStatement, KeyEffectiveStatement> {
33     private static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
34     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
35         YangStmtMapping.KEY)
36         .build();
37     private static final KeyStatementSupport INSTANCE = new KeyStatementSupport();
38
39     private KeyStatementSupport() {
40         super(YangStmtMapping.KEY);
41     }
42
43     public static KeyStatementSupport getInstance() {
44         return INSTANCE;
45     }
46
47     @Override
48     public ImmutableSet<QName> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
49         final Builder<QName> builder = ImmutableSet.builder();
50         int tokens = 0;
51         for (String keyToken : LIST_KEY_SPLITTER.split(value)) {
52             builder.add(StmtContextUtils.parseNodeIdentifier(ctx, keyToken));
53             tokens++;
54         }
55
56         // Throws NPE on nulls, retains first inserted value, cannot be modified
57         final ImmutableSet<QName> ret = builder.build();
58         SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
59                 "Key argument '%s' contains duplicates", value);
60         return ret;
61     }
62
63     @Override
64     public Set<QName> adaptArgumentValue(final StmtContext<Set<QName>, KeyStatement, KeyEffectiveStatement> ctx,
65             final QNameModule targetModule) {
66         final Builder<QName> builder = ImmutableSet.builder();
67         boolean replaced = false;
68         for (final QName qname : ctx.coerceStatementArgument()) {
69             if (!targetModule.equals(qname.getModule())) {
70                 final QName newQname = qname.bindTo(targetModule).intern();
71                 builder.add(newQname);
72                 replaced = true;
73             } else {
74                 builder.add(qname);
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<Set<QName>, KeyStatement, ?> ctx,
90             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
91         return new RegularKeyStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(), substatements);
92     }
93
94     @Override
95     protected KeyStatement createEmptyDeclared(final StmtContext<Set<QName>, KeyStatement, ?> ctx) {
96         return new EmptyKeyStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument());
97     }
98
99     @Override
100     protected KeyEffectiveStatement createEffective(
101             final StmtContext<Set<QName>, KeyStatement, KeyEffectiveStatement> ctx, final KeyStatement declared,
102             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
103         final Set<QName> arg = ctx.coerceStatementArgument();
104         return arg.equals(declared.argument()) ? new RegularLocalKeyEffectiveStatement(declared, substatements)
105                 : new RegularForeignKeyEffectiveStatement(declared, arg, substatements);
106     }
107
108     @Override
109     protected KeyEffectiveStatement createEmptyEffective(
110             final StmtContext<Set<QName>, KeyStatement, KeyEffectiveStatement> ctx, final KeyStatement declared) {
111         final Set<QName> arg = ctx.coerceStatementArgument();
112         return arg.equals(declared.argument()) ? new EmptyLocalKeyEffectiveStatement(declared)
113                 : new EmptyForeignKeyEffectiveStatement(declared, arg);
114     }
115
116     static @NonNull Object maskSet(final @NonNull Set<QName> set) {
117         return set.size() == 1 ? set.iterator().next() : set;
118     }
119
120     @SuppressWarnings("unchecked")
121     static @NonNull Set<QName> unmaskSet(final @NonNull Object masked) {
122         if (masked instanceof Set) {
123             return (Set<QName>) masked;
124         }
125         verify(masked instanceof QName, "Unexpected argument %s", masked);
126         return ImmutableSet.of((QName) masked);
127     }
128 }