2 * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
10 import java.util.Collection;
11 import java.util.Optional;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
14 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
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.util.ModuleIdentifierImpl;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
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.StmtContextUtils;
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.BelongsToPrefixToModuleIdentifier;
34 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BelongsToEffectiveStatementImpl;
37 public class BelongsToStatementImpl extends AbstractDeclaredStatement<String>
38 implements BelongsToStatement {
39 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
40 SubstatementValidator.builder(YangStmtMapping.BELONGS_TO).addMandatory(YangStmtMapping.PREFIX).build();
42 protected BelongsToStatementImpl(final StmtContext<String, BelongsToStatement, ?> context) {
46 public static class Definition
48 AbstractStatementSupport<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> {
51 super(YangStmtMapping.BELONGS_TO);
55 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
60 public BelongsToStatement createDeclared(
61 final StmtContext<String, BelongsToStatement, ?> ctx) {
62 return new BelongsToStatementImpl(ctx);
66 public EffectiveStatement<String, BelongsToStatement> createEffective(
67 final StmtContext<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> ctx) {
68 return new BelongsToEffectiveStatementImpl(ctx);
72 public void onPreLinkageDeclared(final StmtContext.Mutable<String, BelongsToStatement,
73 EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
74 belongsToCtx.addRequiredModule(getModuleIdentifier(belongsToCtx));
78 public void onLinkageDeclared(final Mutable<String, BelongsToStatement,
79 EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
80 ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
82 final ModuleIdentifier belongsToModuleIdentifier = getModuleIdentifier(belongsToCtx);
83 final Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(belongsToCtx,
84 ModuleNamespaceForBelongsTo.class, belongsToModuleIdentifier.getName(),
85 ModelProcessingPhase.SOURCE_LINKAGE);
87 belongsToAction.apply(new InferenceAction() {
89 public void apply(final InferenceContext ctx) {
90 StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.resolve(ctx);
92 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToModuleIdentifier, belongsToModuleCtx);
93 belongsToCtx.addToNs(BelongsToPrefixToModuleIdentifier.class,
94 StmtContextUtils.findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class)
95 .getStatementArgument(), belongsToModuleIdentifier);
99 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
100 if (failed.contains(belongsToPrereq)) {
101 throw new InferenceException(belongsToCtx.getStatementSourceReference(),
102 "Module '%s' from belongs-to was not found", belongsToCtx.getStatementArgument());
108 private static ModuleIdentifier getModuleIdentifier(final Mutable<String, BelongsToStatement,
109 EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
110 return ModuleIdentifierImpl.create(belongsToCtx.getStatementArgument(), Optional.empty(),
111 Optional.of(SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE));
115 protected SubstatementValidator getSubstatementValidator() {
116 return SUBSTATEMENT_VALIDATOR;
122 public String getModule() {
128 public PrefixStatement getPrefix() {
129 return firstDeclared(PrefixStatement.class);