YANGTOOLS-706: Split out yang-parser-rfc7950
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / belongs_to / BelongsToStatementSupport.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.belongs_to;
9
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.findFirstDeclaredSubstatement;
11
12 import java.util.Collection;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
17 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
18 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
29 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
30 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleCtx;
31 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
32
33 public class BelongsToStatementSupport
34         extends
35         AbstractStatementSupport<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> {
36     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
37             SubstatementValidator.builder(YangStmtMapping.BELONGS_TO).addMandatory(YangStmtMapping.PREFIX).build();
38
39     public BelongsToStatementSupport() {
40         super(YangStmtMapping.BELONGS_TO);
41     }
42
43     @Override
44     public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
45         return value;
46     }
47
48     @Override
49     public BelongsToStatement createDeclared(
50             final StmtContext<String, BelongsToStatement, ?> ctx) {
51         return new BelongsToStatementImpl(ctx);
52     }
53
54     @Override
55     public EffectiveStatement<String, BelongsToStatement> createEffective(
56             final StmtContext<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> ctx) {
57         return new BelongsToEffectiveStatementImpl(ctx);
58     }
59
60     @Override
61     public void onPreLinkageDeclared(final StmtContext.Mutable<String, BelongsToStatement,
62             EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
63         belongsToCtx.addRequiredSource(getSourceIdentifier(belongsToCtx));
64     }
65
66     @Override
67     public void onLinkageDeclared(final Mutable<String, BelongsToStatement,
68             EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
69         ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
70
71         final SourceIdentifier belongsToSourceIdentifier = getSourceIdentifier(belongsToCtx);
72         final Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(belongsToCtx,
73             ModuleNamespaceForBelongsTo.class, belongsToCtx.getStatementArgument(),
74             ModelProcessingPhase.SOURCE_LINKAGE);
75
76         belongsToAction.apply(new InferenceAction() {
77             @Override
78             public void apply(final InferenceContext ctx) {
79                 StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.resolve(ctx);
80
81                 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToSourceIdentifier, belongsToModuleCtx);
82                 belongsToCtx.addToNs(BelongsToPrefixToModuleCtx.class,
83                     findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class).getStatementArgument(),
84                     belongsToModuleCtx);
85             }
86
87             @Override
88             public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
89                 if (failed.contains(belongsToPrereq)) {
90                     throw new InferenceException(belongsToCtx.getStatementSourceReference(),
91                         "Module '%s' from belongs-to was not found", belongsToCtx.getStatementArgument());
92                 }
93             }
94         });
95     }
96
97     private static SourceIdentifier getSourceIdentifier(final StmtContext<String, BelongsToStatement,
98             EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
99         return RevisionSourceIdentifier.create(belongsToCtx.getStatementArgument());
100     }
101
102     @Override
103     protected SubstatementValidator getSubstatementValidator() {
104         return SUBSTATEMENT_VALIDATOR;
105     }
106 }