Bug 6897: Adding getSubstatementValidator() method to AbstractStatementSupport
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / UniqueStatementImpl.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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
10
11 import java.util.Collection;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
16 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Relative;
17 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueStatement;
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.source.SourceException;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UniqueEffectiveStatementImpl;
24
25 public class UniqueStatementImpl extends AbstractDeclaredStatement<Collection<SchemaNodeIdentifier.Relative>> implements UniqueStatement {
26     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
27             .UNIQUE)
28             .build();
29
30     protected UniqueStatementImpl(StmtContext<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement, ?> context) {
31         super(context);
32     }
33
34     public static class Definition
35             extends
36             AbstractStatementSupport<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement,
37                     EffectiveStatement<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement>> {
38
39         public Definition() {
40             super(YangStmtMapping.UNIQUE);
41         }
42
43         @Override
44         public Collection<SchemaNodeIdentifier.Relative> parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
45             final Collection<Relative> uniqueConstraints = Utils.parseUniqueConstraintArgument(ctx, value);
46             SourceException.throwIf(uniqueConstraints.isEmpty(), ctx.getStatementSourceReference(),
47                     "Invalid argument value '%s' of unique statement. The value must contains at least "
48                             + "one descendant schema node identifier.", value);
49             return uniqueConstraints;
50         }
51
52         @Override
53         public UniqueStatement createDeclared(StmtContext<Collection<Relative>, UniqueStatement, ?> ctx) {
54             return new UniqueStatementImpl(ctx);
55         }
56
57         @Override
58         public EffectiveStatement<Collection<Relative>, UniqueStatement> createEffective
59                 (StmtContext<Collection<Relative>, UniqueStatement, EffectiveStatement<Collection<Relative>,
60                         UniqueStatement>> ctx) {
61             return new UniqueEffectiveStatementImpl(ctx);
62         }
63
64         @Override
65         public void onFullDefinitionDeclared(StmtContext.Mutable<Collection<Relative>, UniqueStatement,
66                 EffectiveStatement<Collection<Relative>, UniqueStatement>> stmt) {
67             super.onFullDefinitionDeclared(stmt);
68             getSubstatementValidator().validate(stmt);
69         }
70
71         @Override
72         protected SubstatementValidator getSubstatementValidator() {
73             return SUBSTATEMENT_VALIDATOR;
74         }
75     }
76
77     @Nonnull
78     @Override
79     public Collection<Relative> getTag() {
80         return argument();
81     }
82 }