Migrate rawStatementArgument()/getStatementSourceReference() callers
[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 com.google.common.collect.ImmutableList;
13 import java.util.Collection;
14 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
20 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
21 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStringStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
33 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
34 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleCtx;
35 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
36
37 public final class BelongsToStatementSupport
38         extends BaseStringStatementSupport<BelongsToStatement, BelongsToEffectiveStatement> {
39     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
40             SubstatementValidator.builder(YangStmtMapping.BELONGS_TO).addMandatory(YangStmtMapping.PREFIX).build();
41     private static final BelongsToStatementSupport INSTANCE = new BelongsToStatementSupport();
42
43     private BelongsToStatementSupport() {
44         super(YangStmtMapping.BELONGS_TO);
45     }
46
47     public static BelongsToStatementSupport getInstance() {
48         return INSTANCE;
49     }
50
51     @Override
52     public void onPreLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> ctx) {
53         ctx.addRequiredSource(getSourceIdentifier(ctx));
54     }
55
56     @Override
57     public void onLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> belongsToCtx) {
58         ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
59
60         final SourceIdentifier belongsToSourceIdentifier = getSourceIdentifier(belongsToCtx);
61         final Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(belongsToCtx,
62             ModuleNamespaceForBelongsTo.class, belongsToCtx.coerceStatementArgument(),
63             ModelProcessingPhase.SOURCE_LINKAGE);
64
65         belongsToAction.apply(new InferenceAction() {
66             @Override
67             public void apply(final InferenceContext ctx) {
68                 StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.resolve(ctx);
69
70                 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToSourceIdentifier, belongsToModuleCtx);
71                 belongsToCtx.addToNs(BelongsToPrefixToModuleCtx.class,
72                     findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class).coerceStatementArgument(),
73                     belongsToModuleCtx);
74             }
75
76             @Override
77             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
78                 if (failed.contains(belongsToPrereq)) {
79                     throw new InferenceException(belongsToCtx.sourceReference(),
80                         "Module '%s' from belongs-to was not found", belongsToCtx.argument());
81                 }
82             }
83         });
84     }
85
86     @Override
87     protected BelongsToStatement createDeclared(final StmtContext<String, BelongsToStatement, ?> ctx,
88             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
89         return new RegularBelongsToStatement(ctx.coerceRawStatementArgument(), substatements);
90     }
91
92     @Override
93     protected BelongsToStatement createEmptyDeclared(final StmtContext<String, BelongsToStatement, ?> ctx) {
94         return new EmptyBelongsToStatement(ctx.coerceRawStatementArgument());
95     }
96
97     @Override
98     protected BelongsToEffectiveStatement createEffective(final Current<String, BelongsToStatement> stmt,
99             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
100         return substatements.isEmpty() ? new EmptyBelongsToEffectiveStatement(stmt.declared())
101             : new RegularBelongsToEffectiveStatement(stmt.declared(), substatements);
102     }
103
104     private static SourceIdentifier getSourceIdentifier(final StmtContext<String, BelongsToStatement,
105             BelongsToEffectiveStatement> belongsToCtx) {
106         return RevisionSourceIdentifier.create(belongsToCtx.coerceStatementArgument());
107     }
108
109     @Override
110     protected SubstatementValidator getSubstatementValidator() {
111         return SUBSTATEMENT_VALIDATOR;
112     }
113 }