Bug 2366 - Effective statments impl merge, retest & bugfix
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / ModuleStatementSupport.java
index fadc8fccf855db7ca1144d5dda24ea719904337a..2dd4cb0d4035d84c9052743f41e136c641431b26 100644 (file)
@@ -9,54 +9,120 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
 
+import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
+
 import com.google.common.base.Optional;
 import java.net.URI;
 import java.util.Date;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.NamespaceToModule;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
-import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
+import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.source.ModuleIdentifierToModuleQName;
+import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
+import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
+import org.opendaylight.yangtools.yang.parser.spi.source.ModuleQNameToModuleName;
+import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ModuleEffectiveStatementImpl;
+
+public class ModuleStatementSupport extends
+        AbstractStatementSupport<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> {
 
-public class ModuleStatementSupport extends AbstractStatementSupport<String, ModuleStatement, EffectiveStatement<String,ModuleStatement>> {
+    private QNameModule qNameModule;
 
     public ModuleStatementSupport() {
         super(Rfc6020Mapping.MODULE);
     }
 
     @Override
-    public String parseArgumentValue(StmtContext<?,?,?> ctx, String value) {
+    public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
         return value;
     }
 
     @Override
-    public ModuleStatement createDeclared(StmtContext<String, ModuleStatement,?> ctx) {
+    public ModuleStatement createDeclared(StmtContext<String, ModuleStatement, ?> ctx) {
         return new ModuleStatementImpl(ctx);
     }
 
     @Override
-    public EffectiveStatement<String,ModuleStatement> createEffective(StmtContext<String, ModuleStatement,EffectiveStatement<String,ModuleStatement>> ctx) {
-        throw new UnsupportedOperationException();
+    public EffectiveStatement<String, ModuleStatement> createEffective(
+            StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
+        return new ModuleEffectiveStatementImpl(ctx);
     }
 
     @Override
     public void onLinkageDeclared(Mutable<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> stmt)
-            throws InferenceException, SourceException {
-        URI moduleNs = firstAttributeOf(stmt.declaredSubstatements(),NamespaceStatement.class);
+            throws SourceException {
+
+        Optional<URI> moduleNs = Optional.fromNullable(firstAttributeOf(stmt.declaredSubstatements(),
+                NamespaceStatement.class));
+        if (!moduleNs.isPresent()) {
+            throw new IllegalArgumentException("Namespace of the module [" + stmt.getStatementArgument()
+                    + "] is missing.");
+        }
+
+        Optional<Date> revisionDate = Optional.fromNullable(getLatestRevision(stmt.declaredSubstatements()));
+        if (!revisionDate.isPresent()) {
+            revisionDate = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_REV);
+        }
+
+        qNameModule = QNameModule.cachedReference(QNameModule.create(moduleNs.get(), revisionDate.orNull()));
+        ModuleIdentifier moduleIdentifier = new ModuleIdentifierImpl(stmt.getStatementArgument(),
+                Optional.<URI> absent(), revisionDate);
+
+        stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
+        stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
+        stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
+
+        String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
+        if (modulePrefix == null) {
+            throw new IllegalArgumentException("Prefix of the module [" + stmt.getStatementArgument() + "] is missing.");
+        }
 
-        QNameModule qnameNamespace = QNameModule.create(moduleNs, null);
-        ModuleIdentifierImpl moduleIdentifier = new ModuleIdentifierImpl(stmt.getStatementArgument(), Optional.<URI>absent(), Optional.<Date>absent());
+        stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule);
+        stmt.addToNs(ModuleNameToModuleQName.class, stmt.getStatementArgument(), qNameModule);
+        stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
+        stmt.addToNs(ModuleQNameToModuleName.class, qNameModule, stmt.getStatementArgument());
+        stmt.addToNs(ModuleIdentifierToModuleQName.class, moduleIdentifier, qNameModule);
+
+        stmt.addToNs(ImpPrefixToModuleIdentifier.class, modulePrefix, moduleIdentifier);
+    }
+
+    @Override
+    public void onFullDefinitionDeclared(
+            final Mutable<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> stmt)
+            throws SourceException {
+
+        stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
+    }
 
-        stmt.addContext(ModuleNamespace.class,moduleIdentifier ,stmt);
-        stmt.addContext(NamespaceToModule.class, qnameNamespace, stmt);
+    private static Date getLatestRevision(Iterable<? extends StmtContext<?, ?, ?>> subStmts) {
+        Date revision = null;
+        for (StmtContext<?, ?, ?> subStmt : subStmts) {
+            if (subStmt.getPublicDefinition().getDeclaredRepresentationClass().isAssignableFrom(RevisionStatement
+                    .class)) {
+                if (revision == null && subStmt.getStatementArgument() != null) {
+                    revision = (Date) subStmt.getStatementArgument();
+                } else if (subStmt.getStatementArgument() != null && ((Date) subStmt.getStatementArgument()).compareTo
+                        (revision) > 0) {
+                    revision = (Date) subStmt.getStatementArgument();
+                }
+            }
+        }
+        return revision;
     }
 
 }
\ No newline at end of file