BUG-1849 Check for duplicate required sources and exclude all redundant 85/11285/1
authorMaros Marsalek <mmarsale@cisco.com>
Tue, 16 Sep 2014 14:35:13 +0000 (16:35 +0200)
committerTony Tkacik <ttkacik@cisco.com>
Wed, 17 Sep 2014 12:49:09 +0000 (12:49 +0000)
Also check for source identifier mismatch

Change-Id: I32fc76a56fd7adb564b7b9c9fc2679c5f36069cb
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaContextFactory.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaContextFactoryTest.java [new file with mode: 0644]
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaRepositoryTest.java
yang/yang-parser-impl/src/test/resources/no-revision/imported.yang
yang/yang-parser-impl/src/test/resources/no-revision/imported@2012-12-12.yang

index 3c8a45e26dca59f7f100b8f7fbc4f4289af54e73..ebb97ee8bbf632c5601f8b69ebdbe41bc0da9047 100644 (file)
@@ -10,10 +10,14 @@ package org.opendaylight.yangtools.yang.parser.repo;
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
 import com.google.common.cache.Cache;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.collect.Collections2;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.AsyncFunction;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
@@ -21,12 +25,15 @@ import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.net.URI;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Date;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Set;
 import java.util.TreeMap;
+import javax.annotation.Nullable;
 import org.antlr.v4.runtime.ParserRuleContext;
 import org.antlr.v4.runtime.tree.ParseTreeWalker;
 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
@@ -81,13 +88,13 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
             final Map<SourceIdentifier, ParserRuleContext> asts =
                     Maps.transformValues(srcs, ASTSchemaSource.GET_AST);
             final Map<String, TreeMap<Date, URI>> namespaceContext = BuilderUtils.createYangNamespaceContext(
-                    asts.values(), Optional.<SchemaContext> absent());
+                    asts.values(), Optional.<SchemaContext>absent());
 
             final ParseTreeWalker walker = new ParseTreeWalker();
             final Map<SourceIdentifier, ModuleBuilder> sourceToBuilder = new LinkedHashMap<>();
 
-            for (Entry<SourceIdentifier, ParserRuleContext> entry : asts.entrySet()) {
-                ModuleBuilder moduleBuilder = YangParserListenerImpl.create(namespaceContext, entry.getKey().getName(),
+            for (final Entry<SourceIdentifier, ParserRuleContext> entry : asts.entrySet()) {
+                final ModuleBuilder moduleBuilder = YangParserListenerImpl.create(namespaceContext, entry.getKey().getName(),
                         walker, entry.getValue()).getModuleBuilder();
 
                 moduleBuilder.setSource(srcs.get(entry.getKey()).getYangText());
@@ -106,6 +113,7 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
     // FIXME: ignored right now
     private final SchemaSourceFilter filter;
 
+    // FIXME SchemaRepository should be the type for repository parameter instead of SharedSchemaRepository (final implementation)
     public SharedSchemaContextFactory(final SharedSchemaRepository repository, final SchemaSourceFilter filter) {
         this.repository = Preconditions.checkNotNull(repository);
         this.filter = Preconditions.checkNotNull(filter);
@@ -113,14 +121,22 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
 
     @Override
     public CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(final Collection<SourceIdentifier> requiredSources) {
-        final SchemaContext existing = cache.getIfPresent(requiredSources);
+        // Make sources unique
+        final List<SourceIdentifier> uniqueSourceIdentifiers = deDuplicateSources(requiredSources);
+
+        final SchemaContext existing = cache.getIfPresent(uniqueSourceIdentifiers);
         if (existing != null) {
             LOG.debug("Returning cached context {}", existing);
             return Futures.immediateCheckedFuture(existing);
         }
 
         // Request all sources be loaded
-        final ListenableFuture<List<ASTSchemaSource>> sf = Futures.allAsList(Collections2.transform(requiredSources, requestSources));
+        ListenableFuture<List<ASTSchemaSource>> sf = Futures.allAsList(Collections2.transform(uniqueSourceIdentifiers, requestSources));
+
+        // Detect mismatch between requested Source IDs and IDs that are extracted from parsed source
+        // Also remove duplicates if present
+        // We are relying on preserved order of uniqueSourceIdentifiers as well as sf
+        sf = Futures.transform(sf, new SourceIdMismatchDetector(uniqueSourceIdentifiers));
 
         // Assemble sources into a schema context
         final ListenableFuture<SchemaContext> cf = Futures.transform(sf, assembleSources);
@@ -129,7 +145,7 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
         Futures.addCallback(cf, new FutureCallback<SchemaContext>() {
             @Override
             public void onSuccess(final SchemaContext result) {
-                cache.put(requiredSources, result);
+                cache.put(uniqueSourceIdentifiers, result);
             }
 
             @Override
@@ -140,4 +156,52 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
 
         return Futures.makeChecked(cf, MAPPER);
     }
+
+    /**
+     * @return set (preserving ordering) from the input collection
+     */
+    private List<SourceIdentifier> deDuplicateSources(final Collection<SourceIdentifier> requiredSources) {
+        final Set<SourceIdentifier> uniqueSourceIdentifiers = Collections.unmodifiableSet(Sets.newLinkedHashSet(requiredSources));
+        if(uniqueSourceIdentifiers.size() != requiredSources.size()) {
+            LOG.warn("Duplicate sources requested for schema context, removed duplicate sources: {}", Collections2.filter(uniqueSourceIdentifiers, new Predicate<SourceIdentifier>() {
+                @Override
+                public boolean apply(@Nullable final SourceIdentifier input) {
+                    return Iterables.frequency(requiredSources, input) > 1;
+                }
+            }));
+        }
+        return Lists.newArrayList(uniqueSourceIdentifiers);
+    }
+
+    private static final class SourceIdMismatchDetector implements Function<List<ASTSchemaSource>, List<ASTSchemaSource>> {
+        private final List<SourceIdentifier> sourceIdentifiers;
+
+        public SourceIdMismatchDetector(final List<SourceIdentifier> sourceIdentifiers) {
+            this.sourceIdentifiers = sourceIdentifiers;
+        }
+
+        @Override
+        public List<ASTSchemaSource> apply(final List<ASTSchemaSource> input) {
+            final Map<SourceIdentifier, ASTSchemaSource> filtered = Maps.newLinkedHashMap();
+
+            for (int i = 0; i < input.size(); i++) {
+
+                final SourceIdentifier expectedSId = sourceIdentifiers.get(i);
+                final ASTSchemaSource astSchemaSource = input.get(i);
+                final SourceIdentifier realSId = astSchemaSource.getIdentifier();
+
+                if (expectedSId.equals(realSId) == false) {
+                    LOG.warn("Source identifier mismatch for module \"{}\", requested as {} but actually is {}. Using actual id", expectedSId.getName(), expectedSId, realSId);
+                }
+
+                if (filtered.containsKey(realSId)) {
+                    LOG.warn("Duplicate source for module {} detected in reactor", realSId);
+                }
+
+                filtered.put(realSId, astSchemaSource);
+
+            }
+            return Lists.newArrayList(filtered.values());
+        }
+    }
 }
diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaContextFactoryTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaContextFactoryTest.java
new file mode 100644 (file)
index 0000000..21447aa
--- /dev/null
@@ -0,0 +1,86 @@
+package org.opendaylight.yangtools.yang.parser.repo;
+
+import static org.junit.Assert.assertNotNull;
+
+import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
+import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
+import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
+import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
+
+public class SharedSchemaContextFactoryTest {
+
+    private final SharedSchemaRepository repository = new SharedSchemaRepository("test");
+
+    @Mock
+    private SchemaSourceFilter filter;
+    private SourceIdentifier s1;
+    private SourceIdentifier s2;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        final ResourceYangSource source1 = new ResourceYangSource("/ietf/ietf-inet-types@2010-09-24.yang");
+        final ResourceYangSource source2 = new ResourceYangSource("/ietf/iana-timezones@2012-07-09.yang");
+        s1 = new SourceIdentifier("ietf-inet-types", "2010-09-24");
+        s2 = new SourceIdentifier("iana-timezones", "2012-07-09");
+
+        final TextToASTTransformer transformer = TextToASTTransformer.create(repository, repository);
+        repository.registerSchemaSourceListener(transformer);
+
+        repository.registerSchemaSource(new SchemaSourceProvider<YangTextSchemaSource>() {
+            @Override
+            public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
+                return Futures.<YangTextSchemaSource, SchemaSourceException>immediateCheckedFuture(source1);
+            }
+        }, PotentialSchemaSource.create(s1, YangTextSchemaSource.class, 1));
+
+        repository.registerSchemaSource(new SchemaSourceProvider<YangTextSchemaSource>() {
+            @Override
+            public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
+                return Futures.<YangTextSchemaSource, SchemaSourceException>immediateCheckedFuture(source2);
+            }
+        }, PotentialSchemaSource.create(s2, YangTextSchemaSource.class, 1));
+    }
+
+    @Test
+    public void testCreateSchemaContextWithDuplicateRequiredSources() throws Exception {
+        final SharedSchemaContextFactory sharedSchemaContextFactory = new SharedSchemaContextFactory(repository, filter);
+        final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContext = sharedSchemaContextFactory.createSchemaContext(Lists.newArrayList(s1, s1, s2));
+        assertNotNull(schemaContext.checkedGet());
+    }
+
+    @Test
+    public void testSourceRegisteredWithDifferentSI() throws Exception {
+        final ResourceYangSource source1 = new ResourceYangSource("/ietf/ietf-inet-types@2010-09-24.yang");
+        final ResourceYangSource source2 = new ResourceYangSource("/ietf/iana-timezones@2012-07-09.yang");
+        s1 = source1.getIdentifier();
+        s2 = source2.getIdentifier();
+
+        final SettableSchemaProvider<ASTSchemaSource> provider = SharedSchemaRepositoryTest.getImmediateYangSourceProviderFromResource("/no-revision/imported@2012-12-12.yang");
+        provider.setResult();
+        provider.register(repository);
+
+        // Register the same provider under source id without revision
+        final SourceIdentifier sIdWithoutRevision = new SourceIdentifier(provider.getId().getName());
+        repository.registerSchemaSource(provider, PotentialSchemaSource.create(
+                sIdWithoutRevision, ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
+
+        final SharedSchemaContextFactory sharedSchemaContextFactory = new SharedSchemaContextFactory(repository, filter);
+        final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContext = sharedSchemaContextFactory.createSchemaContext(Lists.newArrayList(sIdWithoutRevision, provider.getId()));
+        assertNotNull(schemaContext.checkedGet());
+    }
+}
index 9a8790a428cf4f9eb1764d8c7941d86f1c9d2970..3f351efb768e1b67520196d7700062206169df31 100644 (file)
@@ -57,6 +57,27 @@ import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
 
 public class SharedSchemaRepositoryTest {
 
+    @Test
+    public void testSourceWithAndWithoutRevision() throws Exception {
+        final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
+
+        final SourceIdentifier idNoRevision = loadAndRegisterSource(sharedSchemaRepository, "/no-revision/imported.yang");
+        final SourceIdentifier id2 = loadAndRegisterSource(sharedSchemaRepository, "/no-revision/imported@2012-12-12.yang");
+
+        CheckedFuture<ASTSchemaSource, SchemaSourceException> source = sharedSchemaRepository.getSchemaSource(idNoRevision, ASTSchemaSource.class);
+        assertEquals(idNoRevision, source.checkedGet().getIdentifier());
+        source = sharedSchemaRepository.getSchemaSource(id2, ASTSchemaSource.class);
+        assertEquals(id2, source.checkedGet().getIdentifier());
+    }
+
+    private SourceIdentifier loadAndRegisterSource(final SharedSchemaRepository sharedSchemaRepository, final String resourceName) throws Exception {
+        final SettableSchemaProvider<ASTSchemaSource> sourceProvider = getImmediateYangSourceProviderFromResource(resourceName);
+        sourceProvider.setResult();
+        final SourceIdentifier idNoRevision = sourceProvider.getId();
+        sourceProvider.register(sharedSchemaRepository);
+        return idNoRevision;
+    }
+
     @Test
     public void testSimpleSchemaContext() throws Exception {
         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
@@ -298,13 +319,13 @@ public class SharedSchemaRepositoryTest {
         assertEquals(moduleSize, schemaContext.getModules().size());
     }
 
-    private SettableSchemaProvider<ASTSchemaSource> getRemoteYangSourceProviderFromResource(final String resourceName) throws Exception {
+    static SettableSchemaProvider<ASTSchemaSource> getRemoteYangSourceProviderFromResource(final String resourceName) throws Exception {
         final ResourceYangSource yangSource = new ResourceYangSource(resourceName);
         final CheckedFuture<ASTSchemaSource, SchemaSourceException> aSTSchemaSource = TextToASTTransformer.TRANSFORMATION.apply(yangSource);
         return SettableSchemaProvider.createRemote(aSTSchemaSource.get(), ASTSchemaSource.class);
     }
 
-    private SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(final String resourceName) throws Exception {
+    static SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(final String resourceName) throws Exception {
         final ResourceYangSource yangSource = new ResourceYangSource(resourceName);
         final CheckedFuture<ASTSchemaSource, SchemaSourceException> aSTSchemaSource = TextToASTTransformer.TRANSFORMATION.apply(yangSource);
         return SettableSchemaProvider.createImmediate(aSTSchemaSource.get(), ASTSchemaSource.class);
index 54e6d3a62a251ecd6f1997df9af6f8d958c5e76d..d36c996e552586bb646e003f7c6722272400ee4f 100644 (file)
@@ -4,7 +4,7 @@ module imported {
     namespace "urn:simple.demo.test1";
     prefix "imp";
 
-    organization "opendaylight";
-    contact "WILL-BE-DEFINED-LATER";
+    //organization "opendaylight";
+    //contact "WILL-BE-DEFINED-LATER";
 
 }
index 2e775fa45f71fbf7277b473bb32cf03e993eb8be..68004c815acbeec788b6989af3dd3a21907c34f2 100644 (file)
@@ -6,7 +6,7 @@ module imported {
 
     revision 2012-12-12 {}
 
-    organization "opendaylight";
-    contact "WILL-BE-DEFINED-LATER";
+    //organization "opendaylight";
+    //contact "WILL-BE-DEFINED-LATER";
 
 }