Bug 4540: Yang parser exceptions should follow consistent path
[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.base.Preconditions;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.collect.ImmutableSet.Builder;
13 import java.util.Collection;
14 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
18 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
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.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.KeyEffectiveStatementImpl;
25
26 public class KeyStatementImpl extends AbstractDeclaredStatement<Collection<SchemaNodeIdentifier>> implements
27         KeyStatement {
28     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
29             .KEY)
30             .build();
31
32     protected KeyStatementImpl(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> context) {
33         super(context);
34     }
35
36     public static class Definition
37             extends
38             AbstractStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement,
39                     EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> {
40
41         public Definition() {
42             super(Rfc6020Mapping.KEY);
43         }
44
45         @Override
46         public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
47             final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
48             int tokens = 0;
49             for (String keyToken : StmtContextUtils.LIST_KEY_SPLITTER.split(value)) {
50                 builder.add(SchemaNodeIdentifier.create(false, Utils.qNameFromArgument(ctx, keyToken)));
51                 tokens++;
52             }
53
54             // Throws NPE on nulls, retains first inserted value, cannot be modified
55             final Collection<SchemaNodeIdentifier> ret = builder.build();
56
57             Preconditions.checkArgument(ret.size() == tokens, "Key argument '%s' contains duplicates. At %s", value,
58                     ctx.getStatementSourceReference());
59             return ret;
60         }
61
62         @Override
63         public KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
64             return new KeyStatementImpl(ctx);
65         }
66
67         @Override
68         public EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement> createEffective(
69                 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
70                         EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
71             return new KeyEffectiveStatementImpl(ctx);
72         }
73
74         @Override
75         public void onFullDefinitionDeclared(StmtContext.Mutable<Collection<SchemaNodeIdentifier>, KeyStatement,
76                 EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> stmt) throws SourceException {
77             super.onFullDefinitionDeclared(stmt);
78             SUBSTATEMENT_VALIDATOR.validate(stmt);
79         }
80     }
81 }