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