Sonar issues clean-up
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / ImportStatementDefinition.java
index df2ee7aa0f9ff9147f227c981da2f625a4e91210..6863486d68914fe7473c25552ff5d4884043e58a 100644 (file)
@@ -7,10 +7,13 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
-
 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_LINKAGE;
 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
 
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
+
+import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
+import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
 import com.google.common.base.Optional;
 import java.net.URI;
 import java.text.ParseException;
@@ -53,31 +56,29 @@ public class ImportStatementDefinition extends
     @Override
     public EffectiveStatement<String, ImportStatement> createEffective(
             StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
-        throw new UnsupportedOperationException();
+        return new ImportEffectiveStatementImpl(ctx);
     }
 
     @Override
-    public void onLinkageDeclared(final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt)
-            throws InferenceException, SourceException {
+    public void onLinkageDeclared(
+            final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt)
+            throws SourceException {
         final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
         ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
         final Prerequisite<StmtContext<?, ?, ?>> imported;
         final Prerequisite<Mutable<?, ?, ?>> linkageTarget;
         imported = importAction.requiresCtx(stmt, ModuleNamespace.class, impIdentifier, SOURCE_LINKAGE);
-        linkageTarget = importAction.mutatesCtx(stmt.getRoot(),SOURCE_LINKAGE);
+        linkageTarget = importAction.mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
+
+        String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
+        stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, impIdentifier);
 
         importAction.apply(new InferenceAction() {
 
             @Override
             public void apply() throws InferenceException {
                 StmtContext<?, ?, ?> importedModule = imported.get();
-                // URI importedNs =
-                // importedModule.firstSubstatement(NamespaceStatement.class).argument();
-                // String prefix =
-                // stmt.firstSubstatement(PrefixStatement.class).argument();
                 linkageTarget.get().addToNs(ImportedModuleContext.class, impIdentifier, importedModule);
-                // prefixTarget.get().addToNs(PrefixToModule.class, prefix,
-                // QNameModule.create(importedNs, null));
             }
 
             @Override
@@ -89,7 +90,9 @@ public class ImportStatementDefinition extends
         });
     }
 
-    private static ModuleIdentifier getImportedModuleIdentifier(Mutable<String, ImportStatement, ?> stmt) throws SourceException {
+    private static ModuleIdentifier getImportedModuleIdentifier(Mutable<String, ImportStatement, ?> stmt)
+            throws SourceException {
+
         String moduleName = stmt.getStatementArgument();
         String revisionArg = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
         final Optional<Date> revision;
@@ -97,12 +100,11 @@ public class ImportStatementDefinition extends
             try {
                 revision = Optional.of(SimpleDateFormatUtil.getRevisionFormat().parse(revisionArg));
             } catch (ParseException e) {
-                throw new SourceException(
-                        String.format("Revision value %s is not in required format yyyy-MM-dd", revisionArg),
-                        stmt.getStatementSourceReference(), e);
+                throw new SourceException(String.format("Revision value %s is not in required format yyyy-MM-dd",
+                        revisionArg), stmt.getStatementSourceReference(), e);
             }
         } else {
-            revision = Optional.absent();
+            revision = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP);
         }
         return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(), revision);
     }