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