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;
12 import java.util.Collection;
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.Rfc6020Mapping;
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.parser.builder.impl.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.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.spi.source.SourceException;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BelongsEffectiveToStatementImpl;
34 public class BelongsToStatementImpl extends AbstractDeclaredStatement<String>
35 implements BelongsToStatement {
37 protected BelongsToStatementImpl(
38 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(final StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) throws SourceException {
69 ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
71 final ModuleIdentifier belongsToModuleIdentifier = getModuleIdentifier(belongsToCtx);
72 final ModelActionBuilder.Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(belongsToCtx, ModuleNamespaceForBelongsTo.class, belongsToModuleIdentifier.getName(), ModelProcessingPhase.SOURCE_LINKAGE);
74 belongsToAction.apply(new InferenceAction() {
77 public void apply() throws InferenceException {
78 StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.get();
80 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToModuleIdentifier,
82 belongsToCtx.addToNs(BelongsToPrefixToModuleIdentifier.class, StmtContextUtils
83 .findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class).getStatementArgument
84 (), belongsToModuleIdentifier);
88 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) throws InferenceException {
89 if (failed.contains(belongsToPrereq)) {
90 throw new InferenceException("Module from belongs-to was not found: " + belongsToCtx.getStatementArgument(), belongsToCtx
91 .getStatementSourceReference());
97 private static ModuleIdentifier getModuleIdentifier(final StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
98 String moduleName = belongsToCtx.getStatementArgument();
99 return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(), Optional.of(SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE));
104 public String getModule() {
109 public PrefixStatement getPrefix() {
110 return firstDeclared(PrefixStatement.class);