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 com.google.common.base.Optional;
11 import java.util.Collection;
12 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
13 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
14 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
18 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
19 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
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.ModelProcessingPhase;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
29 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleIdentifier;
30 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BelongsEffectiveToStatementImpl;
33 public class BelongsToStatementImpl extends AbstractDeclaredStatement<String>
34 implements BelongsToStatement {
35 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
36 SubstatementValidator.builder(Rfc6020Mapping.BELONGS_TO).addMandatory(Rfc6020Mapping.PREFIX).build();
38 protected BelongsToStatementImpl(final StmtContext<String, BelongsToStatement, ?> context) {
42 public static class Definition
44 AbstractStatementSupport<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> {
47 super(Rfc6020Mapping.BELONGS_TO);
51 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
56 public BelongsToStatement createDeclared(
57 final StmtContext<String, BelongsToStatement, ?> ctx) {
58 return new BelongsToStatementImpl(ctx);
62 public EffectiveStatement<String, BelongsToStatement> createEffective(
63 final StmtContext<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> ctx) {
64 return new BelongsEffectiveToStatementImpl(ctx);
68 public void onLinkageDeclared(
69 final StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
70 ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
72 final ModuleIdentifier belongsToModuleIdentifier = getModuleIdentifier(belongsToCtx);
73 final ModelActionBuilder.Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(
74 belongsToCtx, ModuleNamespaceForBelongsTo.class, belongsToModuleIdentifier.getName(),
75 ModelProcessingPhase.SOURCE_LINKAGE);
77 belongsToAction.apply(new InferenceAction() {
80 StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.get();
82 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToModuleIdentifier, belongsToModuleCtx);
83 belongsToCtx.addToNs(BelongsToPrefixToModuleIdentifier.class,
84 StmtContextUtils.findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class)
85 .getStatementArgument(), belongsToModuleIdentifier);
89 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
90 if (failed.contains(belongsToPrereq)) {
91 throw new InferenceException(belongsToCtx.getStatementSourceReference(),
92 "Module '%s' from belongs-to was not found", belongsToCtx.getStatementArgument());
98 private static ModuleIdentifier getModuleIdentifier(
99 final StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
100 String moduleName = belongsToCtx.getStatementArgument();
101 return new ModuleIdentifierImpl(moduleName, Optional.absent(),
102 Optional.of(SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE));
106 public void onFullDefinitionDeclared(final StmtContext.Mutable<String, BelongsToStatement,
107 EffectiveStatement<String, BelongsToStatement>> stmt) {
108 super.onFullDefinitionDeclared(stmt);
109 SUBSTATEMENT_VALIDATOR.validate(stmt);
114 public String getModule() {
119 public PrefixStatement getPrefix() {
120 return firstDeclared(PrefixStatement.class);