929634c14cdbdc8a77c94cc936ebe4c3dc1fe42b
[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 org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
14
15 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
16 import java.net.URI;
17 import java.util.Collection;
18 import java.util.Date;
19 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
20 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
21 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
26 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
27 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
36 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
37 import com.google.common.base.Optional;
38
39 public class IncludeStatementImpl extends AbstractDeclaredStatement<String> implements IncludeStatement {
40
41     protected IncludeStatementImpl(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(StmtContext<?, ?, ?> ctx, String value) {
54             return value;
55         }
56
57         @Override
58         public IncludeStatement createDeclared(StmtContext<String, IncludeStatement, ?> ctx) {
59             return new IncludeStatementImpl(ctx);
60         }
61
62         @Override
63         public EffectiveStatement<String, IncludeStatement> createEffective(
64                 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                 throws SourceException {
72             final ModuleIdentifier includeSubmoduleIdentifier = getIncludeSubmoduleIdentifier(stmt);
73
74             ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
75             final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite = includeAction.requiresCtx(stmt,
76                     SubmoduleNamespace.class, includeSubmoduleIdentifier, SOURCE_LINKAGE);
77
78             includeAction.apply(new InferenceAction() {
79
80                 @Override
81                 public void apply() throws InferenceException {
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(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
92                     if (failed.contains(requiresCtxPrerequisite)) {
93                         throw new InferenceException("Included submodule was not found: "+stmt.getStatementArgument(), stmt
94                                 .getStatementSourceReference());
95                     }
96                 }
97             });
98         }
99
100         private static ModuleIdentifier getIncludeSubmoduleIdentifier(Mutable<String, IncludeStatement, ?> stmt) {
101
102             String subModuleName = stmt.getStatementArgument();
103
104             Date revisionDate = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
105             if (revisionDate == null) {
106                 revisionDate = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
107             }
108
109             return new ModuleIdentifierImpl(subModuleName, Optional.<URI> absent(), Optional.<Date> of(revisionDate));
110         }
111     }
112
113     @Override
114     public String getModule() {
115         return argument();
116     }
117
118     @Override
119     public PrefixStatement getPrefix() {
120         return firstDeclared(PrefixStatement.class);
121     }
122
123     @Override
124     public RevisionDateStatement getRevisionDate() {
125         return firstDeclared(RevisionDateStatement.class);
126     }
127
128 }