2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
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;
13 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
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;
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;
39 public class ImportStatementDefinition extends
40 AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
42 public ImportStatementDefinition() {
43 super(Rfc6020Mapping.IMPORT);
47 public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
52 public ImportStatement createDeclared(StmtContext<String, ImportStatement, ?> ctx) {
53 return new ImportStatementImpl(ctx);
57 public EffectiveStatement<String, ImportStatement> createEffective(
58 StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
59 return new ImportEffectiveStatementImpl(ctx);
63 public void onLinkageDeclared(
64 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt)
65 throws InferenceException, 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);
73 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
74 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, impIdentifier);
76 importAction.apply(new InferenceAction() {
79 public void apply() throws InferenceException {
80 StmtContext<?, ?, ?> importedModule = imported.get();
82 // importedModule.firstSubstatement(NamespaceStatement.class).argument();
84 // stmt.firstSubstatement(PrefixStatement.class).argument();
85 linkageTarget.get().addToNs(ImportedModuleContext.class, impIdentifier, importedModule);
86 // prefixTarget.get().addToNs(PrefixToModule.class, prefix,
87 // QNameModule.create(importedNs, null));
91 public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
92 if (failed.contains(imported)) {
93 throw new InferenceException("Imported module was not found.", stmt.getStatementSourceReference());
99 private static ModuleIdentifier getImportedModuleIdentifier(Mutable<String, ImportStatement, ?> stmt)
100 throws SourceException {
102 String moduleName = stmt.getStatementArgument();
103 String revisionArg = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
104 final Optional<Date> revision;
105 if (revisionArg != null) {
107 revision = Optional.of(SimpleDateFormatUtil.getRevisionFormat().parse(revisionArg));
108 } catch (ParseException e) {
109 throw new SourceException(String.format("Revision value %s is not in required format yyyy-MM-dd",
110 revisionArg), stmt.getStatementSourceReference(), e);
113 revision = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP);
115 return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(), revision);