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