2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.impl.util;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableMap.Builder;
15 import com.google.common.io.ByteSource;
17 import java.io.IOException;
18 import java.io.InputStream;
20 import java.util.Collection;
21 import java.util.Map.Entry;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
25 import javax.annotation.concurrent.GuardedBy;
26 import javax.annotation.concurrent.ThreadSafe;
28 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
29 import org.opendaylight.yangtools.concepts.Identifiable;
30 import org.opendaylight.yangtools.concepts.ObjectRegistration;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.util.repo.AdvancedSchemaSourceProvider;
33 import org.opendaylight.yangtools.yang.model.util.repo.SourceIdentifier;
34 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * @deprecated Use {@link org.opendaylight.yangtools.yang.parser.repo.URLSchemaContextResolver}
44 public class URLSchemaContextResolver implements AdvancedSchemaSourceProvider<InputStream> {
46 private static final Logger LOG = LoggerFactory.getLogger(URLSchemaContextResolver.class);
49 private final ConcurrentMap<SourceIdentifier, SourceContext> availableSources = new ConcurrentHashMap<>();
51 private YangSourceContext currentSourceContext;
53 private Optional<SchemaContext> currentSchemaContext = Optional.absent();
56 * Register new yang schema when it appears.
57 * @param source URL of a yang file
58 * @return new instance of SourceContext if the source is not null
60 public synchronized ObjectRegistration<URL> registerSource(final URL source) {
61 checkArgument(source != null, "Supplied source must not be null");
62 InputStream yangStream = getInputStream(source);
63 YangModelDependencyInfo modelInfo = YangModelDependencyInfo.fromInputStream(yangStream);
64 SourceIdentifier identifier = SourceIdentifier.create(modelInfo.getName(),
65 Optional.of(modelInfo.getFormattedRevision()));
66 SourceContext sourceContext = new SourceContext(source, identifier, modelInfo);
67 availableSources.putIfAbsent(identifier, sourceContext);
71 public synchronized Optional<SchemaContext> getSchemaContext() {
72 return currentSchemaContext;
76 public synchronized Optional<InputStream> getSchemaSource(final SourceIdentifier key) {
77 SourceContext ctx = availableSources.get(key);
79 InputStream stream = getInputStream(ctx.getInstance());
80 return Optional.fromNullable(stream);
82 return Optional.absent();
86 public Optional<InputStream> getSchemaSource(final String name, final Optional<String> version) {
87 return getSchemaSource(SourceIdentifier.create(name, version));
90 private static InputStream getInputStream(final URL source) {
93 stream = source.openStream();
94 } catch (IOException e) {
95 throw new IllegalArgumentException("Supplied stream: " + source + " is not available", e);
100 private final class SourceContext extends AbstractObjectRegistration<URL>
101 implements Identifiable<SourceIdentifier> {
103 final SourceIdentifier identifier;
104 final YangModelDependencyInfo dependencyInfo;
106 public SourceContext(final URL instance, final SourceIdentifier identifier, final YangModelDependencyInfo modelInfo) {
108 this.identifier = identifier;
109 this.dependencyInfo = modelInfo;
113 public SourceIdentifier getIdentifier() {
118 protected void removeRegistration() {
122 public YangModelDependencyInfo getDependencyInfo() {
123 return dependencyInfo;
127 private synchronized void removeSource(final SourceContext sourceContext) {
128 boolean removed = availableSources.remove(sourceContext.getIdentifier(), sourceContext);
130 tryToUpdateSchemaContext();
135 * Try to parse all currently available yang files and build new schema context.
136 * @return new schema context iif there is at least 1 yang file registered and new schema context was successfully built.
138 public synchronized Optional<SchemaContext> tryToUpdateSchemaContext() {
139 if (availableSources.isEmpty()) {
140 return Optional.absent();
142 ImmutableMap<SourceIdentifier, SourceContext> actualSources = ImmutableMap.copyOf(availableSources);
143 Builder<SourceIdentifier, YangModelDependencyInfo> builder = ImmutableMap.<SourceIdentifier, YangModelDependencyInfo>builder();
144 for (Entry<SourceIdentifier, SourceContext> entry : actualSources.entrySet()) {
145 builder.put(entry.getKey(), entry.getValue().getDependencyInfo());
147 ImmutableMap<SourceIdentifier, YangModelDependencyInfo> sourcesMap = builder.build();
148 YangSourceContext yangSourceContext = YangSourceContext.createFrom(sourcesMap, this);
149 LOG.debug("Trying to create schema context from {}", sourcesMap.keySet());
151 if (!yangSourceContext.getMissingDependencies().isEmpty()) {
152 LOG.debug("Omitting {} because of unresolved dependencies", yangSourceContext.getMissingDependencies().keySet());
153 LOG.debug("Missing model sources for {}", yangSourceContext.getMissingSources());
155 if (currentSourceContext == null || !yangSourceContext.getValidSources().equals(currentSourceContext.getValidSources())) {
157 Collection<ByteSource> byteSources = yangSourceContext.getValidByteSources();
158 YangParserImpl parser = YangParserImpl.getInstance();
159 SchemaContext schemaContext = parser.parseSources(byteSources);
160 currentSchemaContext = Optional.of(schemaContext);
161 currentSourceContext = yangSourceContext;
162 return Optional.of(schemaContext);
163 } catch (Exception e) {
164 LOG.error("Could not create schema context for {} ", yangSourceContext.getValidSources(), e);
165 return Optional.absent();
168 currentSourceContext = yangSourceContext;
169 return Optional.absent();