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 / DeviationStatementImpl.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 java.util.Collection;
11 import javax.annotation.Nonnull;
12 import javax.annotation.Nullable;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.DeviateStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
26 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeviationEffectiveStatementImpl;
28
29 public class DeviationStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier>
30         implements DeviationStatement {
31     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
32             .DEVIATION)
33             .addOptional(YangStmtMapping.DESCRIPTION)
34             .addAny(YangStmtMapping.DEVIATE)
35             .addOptional(YangStmtMapping.REFERENCE)
36             .build();
37
38     protected DeviationStatementImpl(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> context) {
39         super(context);
40     }
41
42     public static class Definition extends AbstractStatementSupport<SchemaNodeIdentifier, DeviationStatement,
43             EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> {
44
45         public Definition() {
46             super(YangStmtMapping.DEVIATION);
47         }
48
49         @Override
50         public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
51             return Utils.nodeIdentifierFromPath(ctx, value);
52         }
53
54         @Override
55         public DeviationStatement createDeclared(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
56             return new DeviationStatementImpl(ctx);
57         }
58
59         @Override
60         public EffectiveStatement<SchemaNodeIdentifier, DeviationStatement> createEffective(
61                 final StmtContext<SchemaNodeIdentifier, DeviationStatement,
62                 EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> ctx) {
63             return new DeviationEffectiveStatementImpl(ctx);
64         }
65
66         @Override
67         public void onFullDefinitionDeclared(final StmtContext.Mutable<SchemaNodeIdentifier, DeviationStatement,
68                 EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> ctx) {
69             final QNameModule currentModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class,
70                     ctx.getRoot());
71             final QNameModule targetModule = ctx.getStatementArgument().getLastComponent().getModule();
72
73             if (currentModule.equals(targetModule)) {
74                 throw new InferenceException(ctx.getStatementSourceReference(),
75                         "Deviation must not target the same module as the one it is defined in: %s", currentModule);
76             }
77         }
78
79         @Override
80         protected SubstatementValidator getSubstatementValidator() {
81             return SUBSTATEMENT_VALIDATOR;
82         }
83     }
84
85     @Nonnull
86     @Override
87     public SchemaNodeIdentifier getTargetNode() {
88         return argument();
89     }
90
91     @Nullable
92     @Override
93     public DescriptionStatement getDescription() {
94         return firstDeclared(DescriptionStatement.class);
95     }
96
97     @Nullable
98     @Override
99     public ReferenceStatement getReference() {
100         return firstDeclared(ReferenceStatement.class);
101     }
102
103     @Nonnull
104     @Override
105     public Collection<? extends DeviateStatement> getDeviateStatements() {
106         return allDeclared(DeviateStatement.class);
107     }
108 }