X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fimpl%2Futil%2FURLSchemaContextResolver.java;h=91c5c18028ffd58f9d232006868055c6b96d8ab1;hb=2832d604e4de5aa8136e65baca528a9be9154557;hp=41066939c79ac1fed046a8702a64316d01a589dd;hpb=b3e9bdc78ca793648a8cc69b68af0c7f46727cd4;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/URLSchemaContextResolver.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/URLSchemaContextResolver.java index 41066939c7..91c5c18028 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/URLSchemaContextResolver.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/URLSchemaContextResolver.java @@ -9,19 +9,25 @@ package org.opendaylight.yangtools.yang.parser.impl.util; import static com.google.common.base.Preconditions.checkArgument; +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; +import com.google.common.io.ByteSource; + import java.io.IOException; import java.io.InputStream; import java.net.URL; -import java.util.List; +import java.util.Collection; import java.util.Map.Entry; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import javax.annotation.concurrent.GuardedBy; +import javax.annotation.concurrent.ThreadSafe; + import org.opendaylight.yangtools.concepts.AbstractObjectRegistration; import org.opendaylight.yangtools.concepts.Identifiable; import org.opendaylight.yangtools.concepts.ObjectRegistration; -import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.util.repo.AdvancedSchemaSourceProvider; import org.opendaylight.yangtools.yang.model.util.repo.SourceIdentifier; @@ -29,19 +35,27 @@ import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Optional; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMap.Builder; - +/** + * @deprecated Use {@link org.opendaylight.yangtools.yang.parser.repo.URLSchemaContextResolver} + * instead. + */ +@Deprecated +@ThreadSafe public class URLSchemaContextResolver implements AdvancedSchemaSourceProvider { private static final Logger LOG = LoggerFactory.getLogger(URLSchemaContextResolver.class); - private final ConcurrentMap availableSources = new ConcurrentHashMap<>(); + @GuardedBy("this") + private final ConcurrentMap availableSources = new ConcurrentHashMap<>(); + @GuardedBy("this") private YangSourceContext currentSourceContext; + @GuardedBy("this") private Optional currentSchemaContext = Optional.absent(); - public ObjectRegistration registerSource(URL source) { + /** + * Register new yang schema when it appears. + */ + public synchronized ObjectRegistration registerSource(final URL source) { checkArgument(source != null, "Supplied source must not be null"); InputStream yangStream = getInputStream(source); YangModelDependencyInfo modelInfo = YangModelDependencyInfo.fromInputStream(yangStream); @@ -52,12 +66,12 @@ public class URLSchemaContextResolver implements AdvancedSchemaSourceProvider getSchemaContext() { + public synchronized Optional getSchemaContext() { return currentSchemaContext; } @Override - public Optional getSchemaSource(SourceIdentifier key) { + public synchronized Optional getSchemaSource(final SourceIdentifier key) { SourceContext ctx = availableSources.get(key); if (ctx != null) { InputStream stream = getInputStream(ctx.getInstance()); @@ -67,11 +81,11 @@ public class URLSchemaContextResolver implements AdvancedSchemaSourceProvider getSchemaSource(String name, Optional version) { + public Optional getSchemaSource(final String name, final Optional version) { return getSchemaSource(SourceIdentifier.create(name, version)); } - private InputStream getInputStream(URL source) { + private static InputStream getInputStream(final URL source) { InputStream stream; try { stream = source.openStream(); @@ -81,13 +95,13 @@ public class URLSchemaContextResolver implements AdvancedSchemaSourceProvider // + private final class SourceContext extends AbstractObjectRegistration implements Identifiable { final SourceIdentifier identifier; final YangModelDependencyInfo dependencyInfo; - public SourceContext(URL instance, SourceIdentifier identifier, YangModelDependencyInfo modelInfo) { + public SourceContext(final URL instance, final SourceIdentifier identifier, final YangModelDependencyInfo modelInfo) { super(instance); this.identifier = identifier; this.dependencyInfo = modelInfo; @@ -108,46 +122,49 @@ public class URLSchemaContextResolver implements AdvancedSchemaSourceProvider tryToUpdateSchemaContext() { - if(availableSources.isEmpty()) { + if (availableSources.isEmpty()) { return Optional.absent(); } ImmutableMap actualSources = ImmutableMap.copyOf(availableSources); - Builder builder = ImmutableMap. builder(); - for(Entry entry : actualSources.entrySet()) { + Builder builder = ImmutableMap.builder(); + for (Entry entry : actualSources.entrySet()) { builder.put(entry.getKey(), entry.getValue().getDependencyInfo()); } ImmutableMap sourcesMap = builder.build(); - YangSourceContext context = YangSourceContext.createFrom(sourcesMap); - LOG.debug("Trying to create schema context from {}",sourcesMap.keySet()); + YangSourceContext yangSourceContext = YangSourceContext.createFrom(sourcesMap, this); + LOG.debug("Trying to create schema context from {}", sourcesMap.keySet()); - if (context.getMissingDependencies().size() != 0) { - LOG.debug("Omitting {} because of unresolved dependencies", context.getMissingDependencies().keySet()); - LOG.debug("Missing model sources for {}", context.getMissingSources()); + if (!yangSourceContext.getMissingDependencies().isEmpty()) { + LOG.debug("Omitting {} because of unresolved dependencies", yangSourceContext.getMissingDependencies().keySet()); + LOG.debug("Missing model sources for {}", yangSourceContext.getMissingSources()); } - - try { - if(currentSourceContext == null || !context.getValidSources().equals(currentSourceContext.getValidSources())) { - List streams = YangSourceContext.getValidInputStreams(context, this); - YangParserImpl parser = new YangParserImpl(); - Set modules = parser.parseYangModelsFromStreams(streams); - SchemaContext schemaContext = parser.resolveSchemaContext(modules); + if (currentSourceContext == null || !yangSourceContext.getValidSources().equals(currentSourceContext.getValidSources())) { + try { + Collection byteSources = yangSourceContext.getValidByteSources(); + YangParserImpl parser = YangParserImpl.getInstance(); + SchemaContext schemaContext = parser.parseSources(byteSources); currentSchemaContext = Optional.of(schemaContext); - currentSourceContext = context; - return currentSchemaContext; + currentSourceContext = yangSourceContext; + return Optional.of(schemaContext); + } catch (Exception e) { + LOG.error("Could not create schema context for {} ", yangSourceContext.getValidSources(), e); + return Optional.absent(); } - currentSourceContext = context; - } catch (Exception e) { - LOG.error("Could not create schema context for {} ", context.getValidSources(), e); + } else { + currentSourceContext = yangSourceContext; + return Optional.absent(); } - return Optional.absent(); } - }