YANGTOOLS-706: Split up base utility classes into rfc6020.util
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / KeyStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
9
10 import com.google.common.collect.ImmutableSet;
11 import com.google.common.collect.ImmutableSet.Builder;
12 import java.util.Collection;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
25 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.KeyEffectiveStatementImpl;
27
28 public class KeyStatementImpl extends AbstractDeclaredStatement<Collection<SchemaNodeIdentifier>> implements
29         KeyStatement {
30     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
31             .KEY)
32             .build();
33
34     protected KeyStatementImpl(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> context) {
35         super(context);
36     }
37
38     public static class Definition
39             extends
40             AbstractStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement,
41                     EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> {
42
43         public Definition() {
44             super(YangStmtMapping.KEY);
45         }
46
47         @Override
48         public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
49             final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
50             int tokens = 0;
51             for (String keyToken : StmtContextUtils.LIST_KEY_SPLITTER.split(value)) {
52                 builder.add(SchemaNodeIdentifier.create(false, StmtContextUtils.qnameFromArgument(ctx, keyToken)));
53                 tokens++;
54             }
55
56             // Throws NPE on nulls, retains first inserted value, cannot be modified
57             final Collection<SchemaNodeIdentifier> 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 Collection<SchemaNodeIdentifier> adaptArgumentValue(
65                 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
66                     EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx,
67                 final QNameModule targetModule) {
68             final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
69             boolean replaced = false;
70             for (final SchemaNodeIdentifier arg : ctx.getStatementArgument()) {
71                 final QName qname = arg.getLastComponent();
72                 if (!targetModule.equals(qname.getModule())) {
73                     final QName newQname = ctx.getFromNamespace(QNameCacheNamespace.class,
74                             QName.create(targetModule, qname.getLocalName()));
75                     builder.add(SchemaNodeIdentifier.create(false, 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         public KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
89             return new KeyStatementImpl(ctx);
90         }
91
92         @Override
93         public EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement> createEffective(
94                 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
95                         EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
96             return new KeyEffectiveStatementImpl(ctx);
97         }
98
99         @Override
100         protected SubstatementValidator getSubstatementValidator() {
101             return SUBSTATEMENT_VALIDATOR;
102         }
103     }
104 }