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