4b425c33e161b5c5653e9d53280ac4024bbc8adc
[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.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
14
15 import java.net.URI;
16 import java.text.ParseException;
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 InferenceException, 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             final Prerequisite<Mutable<?, ?, ?>> mutatesCtxPrerequisite = includeAction.mutatesCtx(stmt.getRoot(),
78                     SOURCE_LINKAGE);
79
80             includeAction.apply(new InferenceAction() {
81
82                 @Override
83                 public void apply() throws InferenceException {
84                     StmtContext<?, ?, ?> includedSubmoduleStmt = requiresCtxPrerequisite.get();
85
86                     mutatesCtxPrerequisite.get().addToNs(IncludedModuleContext.class, includeSubmoduleIdentifier,
87                             includedSubmoduleStmt);
88                 }
89
90                 @Override
91                 public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
92                     System.out.println("");
93                     if (failed.contains(requiresCtxPrerequisite)) {
94                         throw new InferenceException("Included submodule was not found.", stmt
95                                 .getStatementSourceReference());
96                     }
97                 }
98             });
99         }
100
101         private static ModuleIdentifier getIncludeSubmoduleIdentifier(Mutable<String, IncludeStatement, ?> stmt) {
102
103             String subModuleName = stmt.getStatementArgument();
104             String revisionArg = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
105             final Optional<Date> revision;
106             if (revisionArg != null) {
107                 try {
108                     revision = Optional.of(SimpleDateFormatUtil.getRevisionFormat().parse(revisionArg));
109                 } catch (ParseException e) {
110                     throw new IllegalArgumentException(e);
111                 }
112             } else {
113                 revision = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP);
114             }
115
116             return new ModuleIdentifierImpl(subModuleName, Optional.<URI> absent(), revision);
117         }
118     }
119
120     @Override
121     public String getModule() {
122         return argument();
123     }
124
125     @Override
126     public PrefixStatement getPrefix() {
127         return firstDeclared(PrefixStatement.class);
128     }
129
130     @Override
131     public RevisionDateStatement getRevisionDate() {
132         return firstDeclared(RevisionDateStatement.class);
133     }
134
135 }