Propagate @Nonnull and @Nullable annotations
[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.Rfc6020Mapping;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
24 import org.opendaylight.yangtools.yang.model.util.ModuleIdentifierImpl;
25 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
35 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
36 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
37 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
38
39 public class IncludeStatementImpl extends AbstractDeclaredStatement<String> implements IncludeStatement {
40     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
41         Rfc6020Mapping.INCLUDE).addOptional(Rfc6020Mapping.REVISION_DATE).build();
42
43     protected IncludeStatementImpl(final StmtContext<String, IncludeStatement, ?> context) {
44         super(context);
45     }
46
47     public static class Definition extends
48             AbstractStatementSupport<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> {
49
50         public Definition() {
51             super(Rfc6020Mapping.INCLUDE);
52         }
53
54         @Override
55         public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
56             return value;
57         }
58
59         @Override
60         public IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
61             return new IncludeStatementImpl(ctx);
62         }
63
64         @Override
65         public EffectiveStatement<String, IncludeStatement> createEffective(
66                 final StmtContext<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> ctx) {
67             return new IncludeEffectiveStatementImpl(ctx);
68         }
69
70         @Override
71         public void onLinkageDeclared(
72                 final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) {
73             final ModuleIdentifier includeSubmoduleIdentifier = getIncludeSubmoduleIdentifier(stmt);
74
75             ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
76             final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite = includeAction.requiresCtx(stmt,
77                     SubmoduleNamespace.class, includeSubmoduleIdentifier, SOURCE_LINKAGE);
78
79             includeAction.apply(new InferenceAction() {
80                 @Override
81                 public void apply() {
82                     StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.get();
83
84                     stmt.addToNs(IncludedModuleContext.class, includeSubmoduleIdentifier,
85                             includedSubModuleContext);
86                     stmt.addToNs(IncludedSubmoduleNameToIdentifier.class,
87                             stmt.getStatementArgument(), includeSubmoduleIdentifier);
88                 }
89
90                 @Override
91                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
92                     InferenceException.throwIf(failed.contains(requiresCtxPrerequisite),
93                         stmt.getStatementSourceReference(),
94                         "Included submodule '%s' was not found: ", stmt.getStatementArgument());
95                 }
96             });
97         }
98
99         private static ModuleIdentifier getIncludeSubmoduleIdentifier(final Mutable<String, IncludeStatement, ?> stmt) {
100
101             String subModuleName = stmt.getStatementArgument();
102
103             Date revisionDate = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
104             if (revisionDate == null) {
105                 revisionDate = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
106             }
107
108             return ModuleIdentifierImpl.create(subModuleName, Optional.empty(), Optional.of(revisionDate));
109         }
110
111         @Override
112         public void onFullDefinitionDeclared(final Mutable<String, IncludeStatement,
113                 EffectiveStatement<String, IncludeStatement>> stmt) {
114             super.onFullDefinitionDeclared(stmt);
115             SUBSTATEMENT_VALIDATOR.validate(stmt);
116         }
117     }
118
119     @Nonnull
120     @Override
121     public String getModule() {
122         return argument();
123     }
124
125     @Nonnull
126     @Override
127     public PrefixStatement getPrefix() {
128         return firstDeclared(PrefixStatement.class);
129     }
130
131     @Override
132     public RevisionDateStatement getRevisionDate() {
133         return firstDeclared(RevisionDateStatement.class);
134     }
135
136 }