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;
12 import com.google.common.base.Optional;
14 import java.util.Collection;
15 import java.util.Date;
16 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
17 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
23 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
24 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
34 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
35 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
38 public class IncludeStatementImpl extends AbstractDeclaredStatement<String> implements IncludeStatement {
39 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
40 Rfc6020Mapping.INCLUDE).add(Rfc6020Mapping.REVISION_DATE, 0, 1).build();
42 protected IncludeStatementImpl(final StmtContext<String, IncludeStatement, ?> context) {
46 public static class Definition extends
47 AbstractStatementSupport<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> {
50 super(Rfc6020Mapping.INCLUDE);
54 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
59 public IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
60 return new IncludeStatementImpl(ctx);
64 public EffectiveStatement<String, IncludeStatement> createEffective(
65 final StmtContext<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> ctx) {
66 return new IncludeEffectiveStatementImpl(ctx);
70 public void onLinkageDeclared(
71 final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) {
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 StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.get();
83 stmt.addToNs(IncludedModuleContext.class, includeSubmoduleIdentifier,
84 includedSubModuleContext);
85 stmt.addToNs(IncludedSubmoduleNameToIdentifier.class,
86 stmt.getStatementArgument(), includeSubmoduleIdentifier);
90 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
91 InferenceException.throwIf(failed.contains(requiresCtxPrerequisite),
92 stmt.getStatementSourceReference(),
93 "Included submodule '%s' was not found: ", stmt.getStatementArgument());
98 private static ModuleIdentifier getIncludeSubmoduleIdentifier(final Mutable<String, IncludeStatement, ?> stmt) {
100 String subModuleName = stmt.getStatementArgument();
102 Date revisionDate = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
103 if (revisionDate == null) {
104 revisionDate = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
107 return new ModuleIdentifierImpl(subModuleName, Optional.<URI> absent(), Optional.<Date> of(revisionDate));
111 public void onFullDefinitionDeclared(final Mutable<String, IncludeStatement,
112 EffectiveStatement<String, IncludeStatement>> stmt) throws SourceException {
113 super.onFullDefinitionDeclared(stmt);
114 SUBSTATEMENT_VALIDATOR.validate(stmt);
119 public String getModule() {
124 public PrefixStatement getPrefix() {
125 return firstDeclared(PrefixStatement.class);
129 public RevisionDateStatement getRevisionDate() {
130 return firstDeclared(RevisionDateStatement.class);