Merge branch 'master' of ../controller
[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     private static final DeviationStatementSupport INSTANCE = new DeviationStatementSupport();
31
32     private DeviationStatementSupport() {
33         super(YangStmtMapping.DEVIATION);
34     }
35
36     public static DeviationStatementSupport getInstance() {
37         return INSTANCE;
38     }
39
40     @Override
41     public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
42         return ArgumentUtils.nodeIdentifierFromPath(ctx, value);
43     }
44
45     @Override
46     public DeviationStatement createDeclared(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
47         return new DeviationStatementImpl(ctx);
48     }
49
50     @Override
51     public EffectiveStatement<SchemaNodeIdentifier, DeviationStatement> createEffective(
52             final StmtContext<SchemaNodeIdentifier, DeviationStatement,
53             EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> ctx) {
54         return new DeviationEffectiveStatementImpl(ctx);
55     }
56
57     @Override
58     public void onFullDefinitionDeclared(final StmtContext.Mutable<SchemaNodeIdentifier, DeviationStatement,
59             EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> ctx) {
60         final QNameModule currentModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class,
61                 ctx.getRoot());
62         final QNameModule targetModule = ctx.coerceStatementArgument().getLastComponent().getModule();
63
64         if (currentModule.equals(targetModule)) {
65             throw new InferenceException(ctx.getStatementSourceReference(),
66                     "Deviation must not target the same module as the one it is defined in: %s", currentModule);
67         }
68     }
69
70     @Override
71     protected SubstatementValidator getSubstatementValidator() {
72         return SUBSTATEMENT_VALIDATOR;
73     }
74 }