YANGTOOLS-706: Split out yang-parser-rfc7950
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / deviation / DeviationStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.rfc7950.stmt.deviation;
9
10 import org.opendaylight.yangtools.yang.common.QNameModule;
11 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
12 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
15 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
20 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
21
22 public final class DeviationStatementSupport extends AbstractStatementSupport<SchemaNodeIdentifier, DeviationStatement,
23         EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> {
24     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
25         .DEVIATION)
26         .addOptional(YangStmtMapping.DESCRIPTION)
27         .addAny(YangStmtMapping.DEVIATE)
28         .addOptional(YangStmtMapping.REFERENCE)
29         .build();
30
31     public DeviationStatementSupport() {
32         super(YangStmtMapping.DEVIATION);
33     }
34
35     @Override
36     public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
37         return ArgumentUtils.nodeIdentifierFromPath(ctx, value);
38     }
39
40     @Override
41     public DeviationStatement createDeclared(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
42         return new DeviationStatementImpl(ctx);
43     }
44
45     @Override
46     public EffectiveStatement<SchemaNodeIdentifier, DeviationStatement> createEffective(
47             final StmtContext<SchemaNodeIdentifier, DeviationStatement,
48             EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> ctx) {
49         return new DeviationEffectiveStatementImpl(ctx);
50     }
51
52     @Override
53     public void onFullDefinitionDeclared(final StmtContext.Mutable<SchemaNodeIdentifier, DeviationStatement,
54             EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> ctx) {
55         final QNameModule currentModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class,
56                 ctx.getRoot());
57         final QNameModule targetModule = ctx.getStatementArgument().getLastComponent().getModule();
58
59         if (currentModule.equals(targetModule)) {
60             throw new InferenceException(ctx.getStatementSourceReference(),
61                     "Deviation must not target the same module as the one it is defined in: %s", currentModule);
62         }
63     }
64
65     @Override
66     protected SubstatementValidator getSubstatementValidator() {
67         return SUBSTATEMENT_VALIDATOR;
68     }
69 }