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