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