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