Refactor deviation statement implementations
[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 com.google.common.collect.ImmutableList;
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.DeclaredStatement;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
18 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
24 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
25
26 public final class DeviationStatementSupport
27         extends BaseStatementSupport<SchemaNodeIdentifier, DeviationStatement, DeviationEffectiveStatement> {
28     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
29         .DEVIATION)
30         .addOptional(YangStmtMapping.DESCRIPTION)
31         .addAny(YangStmtMapping.DEVIATE)
32         .addOptional(YangStmtMapping.REFERENCE)
33         .build();
34     private static final DeviationStatementSupport INSTANCE = new DeviationStatementSupport();
35
36     private DeviationStatementSupport() {
37         super(YangStmtMapping.DEVIATION);
38     }
39
40     public static DeviationStatementSupport getInstance() {
41         return INSTANCE;
42     }
43
44     @Override
45     public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
46         return ArgumentUtils.nodeIdentifierFromPath(ctx, value);
47     }
48
49     @Override
50     public void onFullDefinitionDeclared(
51             final Mutable<SchemaNodeIdentifier, DeviationStatement, DeviationEffectiveStatement> ctx) {
52         final QNameModule currentModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class,
53                 ctx.getRoot());
54         final QNameModule targetModule = ctx.coerceStatementArgument().getLastComponent().getModule();
55
56         if (currentModule.equals(targetModule)) {
57             throw new InferenceException(ctx.getStatementSourceReference(),
58                     "Deviation must not target the same module as the one it is defined in: %s", currentModule);
59         }
60     }
61
62     @Override
63     protected SubstatementValidator getSubstatementValidator() {
64         return SUBSTATEMENT_VALIDATOR;
65     }
66
67     @Override
68     protected DeviationStatement createDeclared(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx,
69             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
70         return new DeviationStatementImpl(ctx, substatements);
71     }
72
73     @Override
74     protected DeviationStatement createEmptyDeclared(
75             final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
76         return new DeviationStatementImpl(ctx, ImmutableList.of());
77     }
78
79     @Override
80     protected DeviationEffectiveStatement createEffective(
81             final StmtContext<SchemaNodeIdentifier, DeviationStatement, DeviationEffectiveStatement> ctx,
82             final DeviationStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
83         return new DeviationEffectiveStatementImpl(declared, substatements);
84     }
85
86     @Override
87     protected DeviationEffectiveStatement createEmptyEffective(
88             final StmtContext<SchemaNodeIdentifier, DeviationStatement, DeviationEffectiveStatement> ctx,
89             final DeviationStatement declared) {
90         return new DeviationEffectiveStatementImpl(declared, ImmutableList.of());
91     }
92 }