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.source.BelongsToModuleContext;
28 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BelongsEffectiveToStatementImpl;
32 public class BelongsToStatementImpl extends AbstractDeclaredStatement<String>
33 implements BelongsToStatement {
35 protected BelongsToStatementImpl(
36 StmtContext<String, BelongsToStatement, ?> context) {
40 public static class Definition
42 AbstractStatementSupport<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> {
45 super(Rfc6020Mapping.BELONGS_TO);
49 public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
54 public BelongsToStatement createDeclared(
55 StmtContext<String, BelongsToStatement, ?> ctx) {
56 return new BelongsToStatementImpl(ctx);
60 public EffectiveStatement<String, BelongsToStatement> createEffective(
61 StmtContext<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> ctx) {
62 return new BelongsEffectiveToStatementImpl(ctx);
66 public void onLinkageDeclared(final StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) throws SourceException {
67 ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
69 final ModuleIdentifier belongsToModuleIdentifier = getModuleIdentifier(belongsToCtx);
70 final ModelActionBuilder.Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(belongsToCtx, ModuleNamespaceForBelongsTo.class, belongsToModuleIdentifier.getName(), ModelProcessingPhase.SOURCE_LINKAGE);
72 belongsToAction.apply(new InferenceAction() {
75 public void apply() throws InferenceException {
76 StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.get();
78 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToModuleIdentifier,
83 public void prerequisiteFailed(Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) throws InferenceException {
84 if (failed.contains(belongsToPrereq)) {
85 throw new InferenceException("Module from belongs-to was not found: " + belongsToCtx.getStatementArgument(), belongsToCtx
86 .getStatementSourceReference());
92 private ModuleIdentifier getModuleIdentifier(StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
93 String moduleName = belongsToCtx.getStatementArgument();
94 return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(), Optional.of(SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE));
99 public String getModule() {
104 public PrefixStatement getPrefix() {
105 return firstDeclared(PrefixStatement.class);