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;
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;
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());
// 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);
@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);
Futures.addCallback(cf, new FutureCallback<SchemaContext>() {
@Override
public void onSuccess(final SchemaContext result) {
- cache.put(requiredSources, result);
+ cache.put(uniqueSourceIdentifiers, result);
}
@Override
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());
+ }
+ }
}
--- /dev/null
+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());
+ }
+}
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");
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);
namespace "urn:simple.demo.test1";
prefix "imp";
- organization "opendaylight";
- contact "WILL-BE-DEFINED-LATER";
+ //organization "opendaylight";
+ //contact "WILL-BE-DEFINED-LATER";
}
revision 2012-12-12 {}
- organization "opendaylight";
- contact "WILL-BE-DEFINED-LATER";
+ //organization "opendaylight";
+ //contact "WILL-BE-DEFINED-LATER";
}