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;
12 import com.google.common.base.Optional;
14 import java.util.Collection;
15 import java.util.Date;
16 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
17 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
23 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
24 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
32 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
36 public class ImportStatementDefinition extends
37 AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
39 public ImportStatementDefinition() {
40 super(Rfc6020Mapping.IMPORT);
44 public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
49 public ImportStatement createDeclared(StmtContext<String, ImportStatement, ?> ctx) {
50 return new ImportStatementImpl(ctx);
54 public EffectiveStatement<String, ImportStatement> createEffective(
55 StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
56 return new ImportEffectiveStatementImpl(ctx);
60 public void onLinkageDeclared(
61 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt)
62 throws SourceException {
63 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
64 ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
65 final Prerequisite<StmtContext<?, ?, ?>> imported;
66 final Prerequisite<Mutable<?, ?, ?>> linkageTarget;
67 imported = importAction.requiresCtx(stmt, ModuleNamespace.class, impIdentifier, SOURCE_LINKAGE);
68 linkageTarget = importAction.mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
70 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
71 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, impIdentifier);
73 importAction.apply(new InferenceAction() {
76 public void apply() throws InferenceException {
77 StmtContext<?, ?, ?> importedModule = imported.get();
78 linkageTarget.get().addToNs(ImportedModuleContext.class, impIdentifier, importedModule);
82 public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
83 if (failed.contains(imported)) {
84 throw new InferenceException(String.format("Imported module [%s] was not found.", impIdentifier),
85 stmt.getStatementSourceReference());
91 private static ModuleIdentifier getImportedModuleIdentifier(Mutable<String, ImportStatement, ?> stmt)
92 throws SourceException {
94 String moduleName = stmt.getStatementArgument();
95 Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
96 if (revision == null) {
97 revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
100 return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(), Optional.<Date> of(revision));