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.EffectiveStmtCtx.Current;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
33 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
34 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleCtx;
35 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
37 public final class BelongsToStatementSupport
38 extends BaseStringStatementSupport<BelongsToStatement, BelongsToEffectiveStatement> {
39 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
40 SubstatementValidator.builder(YangStmtMapping.BELONGS_TO).addMandatory(YangStmtMapping.PREFIX).build();
41 private static final BelongsToStatementSupport INSTANCE = new BelongsToStatementSupport();
43 private BelongsToStatementSupport() {
44 super(YangStmtMapping.BELONGS_TO);
47 public static BelongsToStatementSupport getInstance() {
52 public void onPreLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> ctx) {
53 ctx.addRequiredSource(getSourceIdentifier(ctx));
57 public void onLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> belongsToCtx) {
58 ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
60 final SourceIdentifier belongsToSourceIdentifier = getSourceIdentifier(belongsToCtx);
61 final Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(belongsToCtx,
62 ModuleNamespaceForBelongsTo.class, belongsToCtx.coerceStatementArgument(),
63 ModelProcessingPhase.SOURCE_LINKAGE);
65 belongsToAction.apply(new InferenceAction() {
67 public void apply(final InferenceContext ctx) {
68 StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.resolve(ctx);
70 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToSourceIdentifier, belongsToModuleCtx);
71 belongsToCtx.addToNs(BelongsToPrefixToModuleCtx.class,
72 findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class).coerceStatementArgument(),
77 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
78 if (failed.contains(belongsToPrereq)) {
79 throw new InferenceException(belongsToCtx.getStatementSourceReference(),
80 "Module '%s' from belongs-to was not found", belongsToCtx.getStatementArgument());
87 protected BelongsToStatement createDeclared(final StmtContext<String, BelongsToStatement, ?> ctx,
88 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
89 return new RegularBelongsToStatement(ctx.coerceRawStatementArgument(), substatements);
93 protected BelongsToStatement createEmptyDeclared(final StmtContext<String, BelongsToStatement, ?> ctx) {
94 return new EmptyBelongsToStatement(ctx.coerceRawStatementArgument());
98 protected BelongsToEffectiveStatement createEffective(final Current<String, BelongsToStatement> stmt,
99 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
100 return substatements.isEmpty() ? new EmptyBelongsToEffectiveStatement(stmt.declared())
101 : new RegularBelongsToEffectiveStatement(stmt.declared(), substatements);
104 private static SourceIdentifier getSourceIdentifier(final StmtContext<String, BelongsToStatement,
105 BelongsToEffectiveStatement> belongsToCtx) {
106 return RevisionSourceIdentifier.create(belongsToCtx.coerceStatementArgument());
110 protected SubstatementValidator getSubstatementValidator() {
111 return SUBSTATEMENT_VALIDATOR;