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;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SourceLinkage;
12 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
14 import com.google.common.base.Optional;
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.ImportStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
25 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
26 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
34 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
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 throw new UnsupportedOperationException();
60 public void onLinkageDeclared(final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt)
61 throws InferenceException, SourceException {
62 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
63 ModelActionBuilder importAction = stmt.newInferenceAction(SourceLinkage);
64 final Prerequisite<StmtContext<?, ?, ?>> imported;
65 final Prerequisite<Mutable<?, ?, ?>> linkageTarget;
66 imported = importAction.requiresCtx(stmt, ModuleNamespace.class, impIdentifier, SourceLinkage);
67 linkageTarget = importAction.mutatesCtx(stmt.getRoot(),SourceLinkage);
69 importAction.apply(new InferenceAction() {
72 public void apply() throws InferenceException {
73 StmtContext<?, ?, ?> importedModule = imported.get();
75 // importedModule.firstSubstatement(NamespaceStatement.class).argument();
77 // stmt.firstSubstatement(PrefixStatement.class).argument();
78 linkageTarget.get().addToNs(ImportedModuleContext.class, impIdentifier, importedModule);
79 // prefixTarget.get().addToNs(PrefixToModule.class, prefix,
80 // QNameModule.create(importedNs, null));
84 public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
85 if (failed.contains(imported)) {
86 throw new InferenceException("Imported module was not found.", stmt.getStatementSourceReference());
92 private static ModuleIdentifier getImportedModuleIdentifier(Mutable<String, ImportStatement, ?> stmt) throws SourceException {
93 String moduleName = stmt.getStatementArgument();
94 String revisionArg = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
95 final Optional<Date> revision;
96 if (revisionArg != null) {
98 revision = Optional.of(SimpleDateFormatUtil.getRevisionFormat().parse(revisionArg));
99 } catch (ParseException e) {
100 throw new SourceException(
101 String.format("Revision value %s is not in required format yyyy-MM-dd", revisionArg),
102 stmt.getStatementSourceReference(), e);
105 revision = Optional.absent();
107 return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(), revision);