9813dca4daa22550f2c35bfe30842862cc3e7579
[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 void onPreLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> ctx) {
52         ctx.addRequiredSource(getSourceIdentifier(ctx));
53     }
54
55     @Override
56     public void onLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> belongsToCtx) {
57         ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
58
59         final SourceIdentifier belongsToSourceIdentifier = getSourceIdentifier(belongsToCtx);
60         final Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(belongsToCtx,
61             ModuleNamespaceForBelongsTo.class, belongsToCtx.coerceStatementArgument(),
62             ModelProcessingPhase.SOURCE_LINKAGE);
63
64         belongsToAction.apply(new InferenceAction() {
65             @Override
66             public void apply(final InferenceContext ctx) {
67                 StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.resolve(ctx);
68
69                 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToSourceIdentifier, belongsToModuleCtx);
70                 belongsToCtx.addToNs(BelongsToPrefixToModuleCtx.class,
71                     findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class).coerceStatementArgument(),
72                     belongsToModuleCtx);
73             }
74
75             @Override
76             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
77                 if (failed.contains(belongsToPrereq)) {
78                     throw new InferenceException(belongsToCtx.getStatementSourceReference(),
79                         "Module '%s' from belongs-to was not found", belongsToCtx.getStatementArgument());
80                 }
81             }
82         });
83     }
84
85     @Override
86     protected BelongsToStatement createDeclared(final StmtContext<String, BelongsToStatement, ?> ctx,
87             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
88         return new RegularBelongsToStatement(ctx.coerceRawStatementArgument(), substatements);
89     }
90
91     @Override
92     protected BelongsToStatement createEmptyDeclared(final StmtContext<String, BelongsToStatement, ?> ctx) {
93         return new EmptyBelongsToStatement(ctx.coerceRawStatementArgument());
94     }
95
96     @Override
97     protected BelongsToEffectiveStatement createEffective(
98             final StmtContext<String, BelongsToStatement, BelongsToEffectiveStatement> ctx,
99             final BelongsToStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
100         return new RegularBelongsToEffectiveStatement(declared, substatements);
101     }
102
103     @Override
104     protected BelongsToEffectiveStatement createEmptyEffective(
105             final StmtContext<String, BelongsToStatement, BelongsToEffectiveStatement> ctx,
106             final BelongsToStatement declared) {
107         return new EmptyBelongsToEffectiveStatement(declared);
108     }
109
110     private static SourceIdentifier getSourceIdentifier(final StmtContext<String, BelongsToStatement,
111             BelongsToEffectiveStatement> belongsToCtx) {
112         return RevisionSourceIdentifier.create(belongsToCtx.coerceStatementArgument());
113     }
114
115     @Override
116     protected SubstatementValidator getSubstatementValidator() {
117         return SUBSTATEMENT_VALIDATOR;
118     }
119 }