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 static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_LINKAGE;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
13 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
15 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
17 import java.util.Collection;
18 import java.util.Date;
19 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
20 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
21 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
26 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
27 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
36 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
37 import com.google.common.base.Optional;
39 public class IncludeStatementImpl extends AbstractDeclaredStatement<String> implements IncludeStatement {
41 protected IncludeStatementImpl(StmtContext<String, IncludeStatement, ?> context) {
45 public static class Definition extends
46 AbstractStatementSupport<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> {
49 super(Rfc6020Mapping.INCLUDE);
53 public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
58 public IncludeStatement createDeclared(StmtContext<String, IncludeStatement, ?> ctx) {
59 return new IncludeStatementImpl(ctx);
63 public EffectiveStatement<String, IncludeStatement> createEffective(
64 StmtContext<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> ctx) {
65 return new IncludeEffectiveStatementImpl(ctx);
69 public void onLinkageDeclared(
70 final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt)
71 throws SourceException {
72 final ModuleIdentifier includeSubmoduleIdentifier = getIncludeSubmoduleIdentifier(stmt);
74 ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
75 final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite = includeAction.requiresCtx(stmt,
76 SubmoduleNamespace.class, includeSubmoduleIdentifier, SOURCE_LINKAGE);
78 includeAction.apply(new InferenceAction() {
81 public void apply() throws InferenceException {
82 StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.get();
84 stmt.addToNs(IncludedModuleContext.class, includeSubmoduleIdentifier,
85 includedSubModuleContext);
86 stmt.addToNs(IncludedSubmoduleNameToIdentifier.class,
87 stmt.getStatementArgument(), includeSubmoduleIdentifier);
91 public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
92 if (failed.contains(requiresCtxPrerequisite)) {
93 throw new InferenceException("Included submodule was not found: "+stmt.getStatementArgument(), stmt
94 .getStatementSourceReference());
100 private static ModuleIdentifier getIncludeSubmoduleIdentifier(Mutable<String, IncludeStatement, ?> stmt) {
102 String subModuleName = stmt.getStatementArgument();
104 Date revisionDate = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
105 if (revisionDate == null) {
106 revisionDate = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
109 return new ModuleIdentifierImpl(subModuleName, Optional.<URI> absent(), Optional.<Date> of(revisionDate));
114 public String getModule() {
119 public PrefixStatement getPrefix() {
120 return firstDeclared(PrefixStatement.class);
124 public RevisionDateStatement getRevisionDate() {
125 return firstDeclared(RevisionDateStatement.class);