Sonar issues clean-up
[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 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                     if (failed.contains(requiresCtxPrerequisite)) {
93                         throw new InferenceException("Included submodule was not found.", stmt
94                                 .getStatementSourceReference());
95                     }
96                 }
97             });
98         }
99
100         private static ModuleIdentifier getIncludeSubmoduleIdentifier(Mutable<String, IncludeStatement, ?> stmt) {
101
102             String subModuleName = stmt.getStatementArgument();
103             String revisionArg = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
104             final Optional<Date> revision;
105             if (revisionArg != null) {
106                 try {
107                     revision = Optional.of(SimpleDateFormatUtil.getRevisionFormat().parse(revisionArg));
108                 } catch (ParseException e) {
109                     throw new IllegalArgumentException(e);
110                 }
111             } else {
112                 revision = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP);
113             }
114
115             return new ModuleIdentifierImpl(subModuleName, Optional.<URI> absent(), revision);
116         }
117     }
118
119     @Override
120     public String getModule() {
121         return argument();
122     }
123
124     @Override
125     public PrefixStatement getPrefix() {
126         return firstDeclared(PrefixStatement.class);
127     }
128
129     @Override
130     public RevisionDateStatement getRevisionDate() {
131         return firstDeclared(RevisionDateStatement.class);
132     }
133
134 }