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 org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
13 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
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(
61 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt)
62 throws InferenceException, 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();
79 // importedModule.firstSubstatement(NamespaceStatement.class).argument();
81 // stmt.firstSubstatement(PrefixStatement.class).argument();
82 linkageTarget.get().addToNs(ImportedModuleContext.class, impIdentifier, importedModule);
83 // prefixTarget.get().addToNs(PrefixToModule.class, prefix,
84 // QNameModule.create(importedNs, null));
88 public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
89 if (failed.contains(imported)) {
90 throw new InferenceException("Imported module was not found.", stmt.getStatementSourceReference());
96 private static ModuleIdentifier getImportedModuleIdentifier(Mutable<String, ImportStatement, ?> stmt)
97 throws SourceException {
99 String moduleName = stmt.getStatementArgument();
100 String revisionArg = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
101 final Optional<Date> revision;
102 if (revisionArg != null) {
104 revision = Optional.of(SimpleDateFormatUtil.getRevisionFormat().parse(revisionArg));
105 } catch (ParseException e) {
106 throw new SourceException(String.format("Revision value %s is not in required format yyyy-MM-dd",
107 revisionArg), stmt.getStatementSourceReference(), e);
110 revision = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP);
112 return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(), revision);