6863486d68914fe7473c25552ff5d4884043e58a
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / ImportStatementDefinition.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.ImportEffectiveStatementImpl;
14
15 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
16 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
17 import com.google.common.base.Optional;
18 import java.net.URI;
19 import java.text.ParseException;
20 import java.util.Collection;
21 import java.util.Date;
22 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
23 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
28 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
29 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
37 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38
39 public class ImportStatementDefinition extends
40         AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
41
42     public ImportStatementDefinition() {
43         super(Rfc6020Mapping.IMPORT);
44     }
45
46     @Override
47     public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
48         return value;
49     }
50
51     @Override
52     public ImportStatement createDeclared(StmtContext<String, ImportStatement, ?> ctx) {
53         return new ImportStatementImpl(ctx);
54     }
55
56     @Override
57     public EffectiveStatement<String, ImportStatement> createEffective(
58             StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
59         return new ImportEffectiveStatementImpl(ctx);
60     }
61
62     @Override
63     public void onLinkageDeclared(
64             final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt)
65             throws SourceException {
66         final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
67         ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
68         final Prerequisite<StmtContext<?, ?, ?>> imported;
69         final Prerequisite<Mutable<?, ?, ?>> linkageTarget;
70         imported = importAction.requiresCtx(stmt, ModuleNamespace.class, impIdentifier, SOURCE_LINKAGE);
71         linkageTarget = importAction.mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
72
73         String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
74         stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, impIdentifier);
75
76         importAction.apply(new InferenceAction() {
77
78             @Override
79             public void apply() throws InferenceException {
80                 StmtContext<?, ?, ?> importedModule = imported.get();
81                 linkageTarget.get().addToNs(ImportedModuleContext.class, impIdentifier, importedModule);
82             }
83
84             @Override
85             public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
86                 if (failed.contains(imported)) {
87                     throw new InferenceException("Imported module was not found.", stmt.getStatementSourceReference());
88                 }
89             }
90         });
91     }
92
93     private static ModuleIdentifier getImportedModuleIdentifier(Mutable<String, ImportStatement, ?> stmt)
94             throws SourceException {
95
96         String moduleName = stmt.getStatementArgument();
97         String revisionArg = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
98         final Optional<Date> revision;
99         if (revisionArg != null) {
100             try {
101                 revision = Optional.of(SimpleDateFormatUtil.getRevisionFormat().parse(revisionArg));
102             } catch (ParseException e) {
103                 throw new SourceException(String.format("Revision value %s is not in required format yyyy-MM-dd",
104                         revisionArg), stmt.getStatementSourceReference(), e);
105             }
106         } else {
107             revision = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP);
108         }
109         return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(), revision);
110     }
111
112 }