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