Refactor InferenceAction
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / IncludeStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
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
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.InferenceContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
38 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
39 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
40 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
41
42 public class IncludeStatementImpl extends AbstractDeclaredStatement<String> implements IncludeStatement {
43     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
44         YangStmtMapping.INCLUDE).addOptional(YangStmtMapping.REVISION_DATE).build();
45
46     protected IncludeStatementImpl(final StmtContext<String, IncludeStatement, ?> context) {
47         super(context);
48     }
49
50     public static class Definition extends
51             AbstractStatementSupport<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> {
52
53         public Definition() {
54             super(YangStmtMapping.INCLUDE);
55         }
56
57         @Override
58         public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
59             return value;
60         }
61
62         @Override
63         public IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
64             return new IncludeStatementImpl(ctx);
65         }
66
67         @Override
68         public EffectiveStatement<String, IncludeStatement> createEffective(
69                 final StmtContext<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> ctx) {
70             return new IncludeEffectiveStatementImpl(ctx);
71         }
72
73         @Override
74         public void onPreLinkageDeclared(
75                 final StmtContext.Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) {
76             stmt.addRequiredModule(getIncludeSubmoduleIdentifier(stmt));
77         }
78
79         @Override
80         public void onLinkageDeclared(
81                 final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) {
82             final ModuleIdentifier includeSubmoduleIdentifier = getIncludeSubmoduleIdentifier(stmt);
83
84             final ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
85             final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite = includeAction.requiresCtx(stmt,
86                     SubmoduleNamespace.class, includeSubmoduleIdentifier, SOURCE_LINKAGE);
87
88             includeAction.apply(new InferenceAction() {
89                 @Override
90                 public void apply(final InferenceContext ctx) {
91                     final StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.resolve(ctx);
92
93                     stmt.addToNs(IncludedModuleContext.class, includeSubmoduleIdentifier,
94                             includedSubModuleContext);
95                     stmt.addToNs(IncludedSubmoduleNameToIdentifier.class,
96                             stmt.getStatementArgument(), includeSubmoduleIdentifier);
97                 }
98
99                 @Override
100                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
101                     InferenceException.throwIf(failed.contains(requiresCtxPrerequisite),
102                         stmt.getStatementSourceReference(),
103                         "Included submodule '%s' was not found: ", stmt.getStatementArgument());
104                 }
105             });
106         }
107
108         private static ModuleIdentifier getIncludeSubmoduleIdentifier(final StmtContext<String, IncludeStatement, ?> stmt) {
109
110             final String subModuleName = stmt.getStatementArgument();
111
112             Date revisionDate = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
113             if (revisionDate == null) {
114                 revisionDate = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
115             }
116
117             return ModuleIdentifierImpl.create(subModuleName, Optional.empty(), Optional.of(revisionDate));
118         }
119
120         @Override
121         protected SubstatementValidator getSubstatementValidator() {
122             return SUBSTATEMENT_VALIDATOR;
123         }
124     }
125
126     @Nonnull
127     @Override
128     public String getModule() {
129         return argument();
130     }
131
132     @Nonnull
133     @Override
134     public PrefixStatement getPrefix() {
135         return firstDeclared(PrefixStatement.class);
136     }
137
138     @Override
139     public RevisionDateStatement getRevisionDate() {
140         return firstDeclared(RevisionDateStatement.class);
141     }
142
143     @Override
144     public DescriptionStatement getDescription() {
145         return firstDeclared(DescriptionStatement.class);
146     }
147
148     @Override
149     public ReferenceStatement getReference() {
150         return firstDeclared(ReferenceStatement.class);
151     }
152
153 }