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.ModelProcessingPhase;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
30 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
31 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleIdentifier;
32 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BelongsEffectiveToStatementImpl;
35 public class BelongsToStatementImpl extends AbstractDeclaredStatement<String>
36 implements BelongsToStatement {
37 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
38 SubstatementValidator.builder(YangStmtMapping.BELONGS_TO).addMandatory(YangStmtMapping.PREFIX).build();
40 protected BelongsToStatementImpl(final StmtContext<String, BelongsToStatement, ?> context) {
44 public static class Definition
46 AbstractStatementSupport<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> {
49 super(YangStmtMapping.BELONGS_TO);
53 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
58 public BelongsToStatement createDeclared(
59 final StmtContext<String, BelongsToStatement, ?> ctx) {
60 return new BelongsToStatementImpl(ctx);
64 public EffectiveStatement<String, BelongsToStatement> createEffective(
65 final StmtContext<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> ctx) {
66 return new BelongsEffectiveToStatementImpl(ctx);
70 public void onLinkageDeclared(
71 final StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
72 ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
74 final ModuleIdentifier belongsToModuleIdentifier = getModuleIdentifier(belongsToCtx);
75 final ModelActionBuilder.Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(
76 belongsToCtx, ModuleNamespaceForBelongsTo.class, belongsToModuleIdentifier.getName(),
77 ModelProcessingPhase.SOURCE_LINKAGE);
79 belongsToAction.apply(new InferenceAction() {
81 public void apply(final InferenceContext ctx) {
82 StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.resolve(ctx);
84 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToModuleIdentifier, belongsToModuleCtx);
85 belongsToCtx.addToNs(BelongsToPrefixToModuleIdentifier.class,
86 StmtContextUtils.findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class)
87 .getStatementArgument(), belongsToModuleIdentifier);
91 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
92 if (failed.contains(belongsToPrereq)) {
93 throw new InferenceException(belongsToCtx.getStatementSourceReference(),
94 "Module '%s' from belongs-to was not found", belongsToCtx.getStatementArgument());
100 private static ModuleIdentifier getModuleIdentifier(
101 final StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
102 String moduleName = belongsToCtx.getStatementArgument();
103 return ModuleIdentifierImpl.create(moduleName, Optional.empty(),
104 Optional.of(SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE));
108 protected SubstatementValidator getSubstatementValidator() {
109 return SUBSTATEMENT_VALIDATOR;
115 public String getModule() {
121 public PrefixStatement getPrefix() {
122 return firstDeclared(PrefixStatement.class);