2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.belongs_to;
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.findFirstDeclaredSubstatement;
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;
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();
42 private BelongsToStatementSupport() {
43 super(YangStmtMapping.BELONGS_TO);
46 public static BelongsToStatementSupport getInstance() {
51 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
56 public void onPreLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> ctx) {
57 ctx.addRequiredSource(getSourceIdentifier(ctx));
61 public void onLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> belongsToCtx) {
62 ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
64 final SourceIdentifier belongsToSourceIdentifier = getSourceIdentifier(belongsToCtx);
65 final Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(belongsToCtx,
66 ModuleNamespaceForBelongsTo.class, belongsToCtx.coerceStatementArgument(),
67 ModelProcessingPhase.SOURCE_LINKAGE);
69 belongsToAction.apply(new InferenceAction() {
71 public void apply(final InferenceContext ctx) {
72 StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.resolve(ctx);
74 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToSourceIdentifier, belongsToModuleCtx);
75 belongsToCtx.addToNs(BelongsToPrefixToModuleCtx.class,
76 findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class).coerceStatementArgument(),
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());
91 protected BelongsToStatement createDeclared(final StmtContext<String, BelongsToStatement, ?> ctx,
92 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
93 return new RegularBelongsToStatement(ctx, substatements);
97 protected BelongsToStatement createEmptyDeclared(final StmtContext<String, BelongsToStatement, ?> ctx) {
98 return new EmptyBelongsToStatement(ctx);
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);
109 protected BelongsToEffectiveStatement createEmptyEffective(
110 final StmtContext<String, BelongsToStatement, BelongsToEffectiveStatement> ctx,
111 final BelongsToStatement declared) {
112 return new EmptyBelongsToEffectiveStatement(declared);
115 private static SourceIdentifier getSourceIdentifier(final StmtContext<String, BelongsToStatement,
116 BelongsToEffectiveStatement> belongsToCtx) {
117 return RevisionSourceIdentifier.create(belongsToCtx.coerceStatementArgument());
121 protected SubstatementValidator getSubstatementValidator() {
122 return SUBSTATEMENT_VALIDATOR;