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 java.util.Collection;
14 import java.util.Date;
15 import java.util.Optional;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
18 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
19 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
26 import org.opendaylight.yangtools.yang.model.util.ModuleIdentifierImpl;
27 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
37 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
38 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
39 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
41 public class IncludeStatementImpl extends AbstractDeclaredStatement<String> implements IncludeStatement {
42 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
43 YangStmtMapping.INCLUDE).addOptional(YangStmtMapping.REVISION_DATE).build();
45 protected IncludeStatementImpl(final StmtContext<String, IncludeStatement, ?> context) {
49 public static class Definition extends
50 AbstractStatementSupport<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> {
53 super(YangStmtMapping.INCLUDE);
57 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
62 public IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
63 return new IncludeStatementImpl(ctx);
67 public EffectiveStatement<String, IncludeStatement> createEffective(
68 final StmtContext<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> ctx) {
69 return new IncludeEffectiveStatementImpl(ctx);
73 public void onLinkageDeclared(
74 final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) {
75 final ModuleIdentifier includeSubmoduleIdentifier = getIncludeSubmoduleIdentifier(stmt);
77 ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
78 final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite = includeAction.requiresCtx(stmt,
79 SubmoduleNamespace.class, includeSubmoduleIdentifier, SOURCE_LINKAGE);
81 includeAction.apply(new InferenceAction() {
84 StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.get();
86 stmt.addToNs(IncludedModuleContext.class, includeSubmoduleIdentifier,
87 includedSubModuleContext);
88 stmt.addToNs(IncludedSubmoduleNameToIdentifier.class,
89 stmt.getStatementArgument(), includeSubmoduleIdentifier);
93 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
94 InferenceException.throwIf(failed.contains(requiresCtxPrerequisite),
95 stmt.getStatementSourceReference(),
96 "Included submodule '%s' was not found: ", stmt.getStatementArgument());
101 private static ModuleIdentifier getIncludeSubmoduleIdentifier(final Mutable<String, IncludeStatement, ?> stmt) {
103 String subModuleName = stmt.getStatementArgument();
105 Date revisionDate = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
106 if (revisionDate == null) {
107 revisionDate = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
110 return ModuleIdentifierImpl.create(subModuleName, Optional.empty(), Optional.of(revisionDate));
114 protected SubstatementValidator getSubstatementValidator() {
115 return SUBSTATEMENT_VALIDATOR;
121 public String getModule() {
127 public PrefixStatement getPrefix() {
128 return firstDeclared(PrefixStatement.class);
132 public RevisionDateStatement getRevisionDate() {
133 return firstDeclared(RevisionDateStatement.class);
137 public DescriptionStatement getDescription() {
138 return firstDeclared(DescriptionStatement.class);
142 public ReferenceStatement getReference() {
143 return firstDeclared(ReferenceStatement.class);