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.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;
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();
41 private BelongsToStatementSupport() {
42 super(YangStmtMapping.BELONGS_TO);
45 public static BelongsToStatementSupport getInstance() {
50 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
55 public BelongsToStatement createDeclared(final StmtContext<String, BelongsToStatement, ?> ctx) {
56 return new BelongsToStatementImpl(ctx);
60 public void onPreLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> ctx) {
61 ctx.addRequiredSource(getSourceIdentifier(ctx));
65 public void onLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> belongsToCtx) {
66 ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
68 final SourceIdentifier belongsToSourceIdentifier = getSourceIdentifier(belongsToCtx);
69 final Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(belongsToCtx,
70 ModuleNamespaceForBelongsTo.class, belongsToCtx.coerceStatementArgument(),
71 ModelProcessingPhase.SOURCE_LINKAGE);
73 belongsToAction.apply(new InferenceAction() {
75 public void apply(final InferenceContext ctx) {
76 StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.resolve(ctx);
78 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToSourceIdentifier, belongsToModuleCtx);
79 belongsToCtx.addToNs(BelongsToPrefixToModuleCtx.class,
80 findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class).coerceStatementArgument(),
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());
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);
102 protected BelongsToEffectiveStatement createEmptyEffective(
103 final StmtContext<String, BelongsToStatement, BelongsToEffectiveStatement> ctx,
104 final BelongsToStatement declared) {
105 return new EmptyBelongsToEffectiveStatement(declared);
108 private static SourceIdentifier getSourceIdentifier(final StmtContext<String, BelongsToStatement,
109 BelongsToEffectiveStatement> belongsToCtx) {
110 return RevisionSourceIdentifier.create(belongsToCtx.coerceStatementArgument());
114 protected SubstatementValidator getSubstatementValidator() {
115 return SUBSTATEMENT_VALIDATOR;