Bug 3670 (part 3/5): Use of new statement parser in yang-maven-plugin
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / URLSchemaContextResolver.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.repo;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ArrayListMultimap;
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.Multimap;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.Futures;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URL;
23 import java.util.Collection;
24 import java.util.concurrent.ConcurrentLinkedDeque;
25 import java.util.concurrent.atomic.AtomicReference;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
28 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
29 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
30 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
31 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
32 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
33 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
34 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
35 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
36 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
37 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
38 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
39 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
40 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
41 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
42 import org.opendaylight.yangtools.yang.model.repo.util.InMemorySchemaSourceCache;
43 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
44 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 @Beta
49 public class URLSchemaContextResolver implements AutoCloseable, SchemaSourceProvider<YangTextSchemaSource> {
50     private static final Logger LOG = LoggerFactory.getLogger(URLSchemaContextResolver.class);
51
52     private final Collection<SourceIdentifier> requiredSources = new ConcurrentLinkedDeque<>();
53     private final Multimap<SourceIdentifier, YangTextSchemaSource> texts = ArrayListMultimap.create();
54     private final AtomicReference<Optional<SchemaContext>> currentSchemaContext =
55             new AtomicReference<>(Optional.<SchemaContext>absent());
56     private final InMemorySchemaSourceCache<ASTSchemaSource> cache;
57     private final SchemaListenerRegistration transReg;
58     private final SchemaSourceRegistry registry;
59     private final SchemaRepository repository;
60     private volatile Object version = new Object();
61     private volatile Object contextVersion = version;
62
63
64     private URLSchemaContextResolver(final SchemaRepository repository, final SchemaSourceRegistry registry) {
65         this.repository = Preconditions.checkNotNull(repository);
66         this.registry = Preconditions.checkNotNull(registry);
67
68         final TextToASTTransformer t = TextToASTTransformer.create(repository, registry);
69         transReg = registry.registerSchemaSourceListener(t);
70
71         cache = InMemorySchemaSourceCache.createSoftCache(registry, ASTSchemaSource.class);
72     }
73
74     public static URLSchemaContextResolver create(final String name) {
75         final SharedSchemaRepository sharedRepo = new SharedSchemaRepository(name);
76         return new URLSchemaContextResolver(sharedRepo, sharedRepo);
77     }
78
79     /**
80      * Register a URL hosting a YANG Text file.
81      *
82      * @param url URL
83      * @throws YangSyntaxErrorException When the YANG file is syntactically invalid
84      * @throws IOException when the URL is not readable
85      * @throws SchemaSourceException When parsing encounters general error
86      * @return new instance of AbstractURLRegistration if the URL is not null
87      */
88     public URLRegistration registerSource(final URL url) throws SchemaSourceException, IOException, YangSyntaxErrorException {
89         checkArgument(url != null, "Supplied URL must not be null");
90
91         final SourceIdentifier guessedId = new SourceIdentifier(url.getFile(), Optional.<String>absent());
92         final YangTextSchemaSource text = new YangTextSchemaSource(guessedId) {
93             @Override
94             public InputStream openStream() throws IOException {
95                 return url.openStream();
96             }
97
98             @Override
99             protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
100                 return toStringHelper.add("url", url);
101             }
102         };
103
104         final ASTSchemaSource ast = TextToASTTransformer.TRANSFORMATION.apply(text).checkedGet();
105         LOG.trace("Resolved URL {} to source {}", url, ast);
106
107         final SourceIdentifier resolvedId = ast.getIdentifier();
108
109         synchronized (this) {
110             texts.put(resolvedId, text);
111             LOG.debug("Populated {} with text", resolvedId);
112
113             final SchemaSourceRegistration<YangTextSchemaSource> reg = registry.registerSchemaSource(this,
114                 PotentialSchemaSource.create(resolvedId, YangTextSchemaSource.class, Costs.IMMEDIATE.getValue()));
115             requiredSources.add(resolvedId);
116             cache.schemaSourceEncountered(ast);
117             LOG.debug("Added source {} to schema context requirements", resolvedId);
118             version = new Object();
119
120             return new AbstractURLRegistration(text) {
121                 @Override
122                 protected void removeRegistration() {
123                     synchronized (URLSchemaContextResolver.this) {
124                         requiredSources.remove(resolvedId);
125                         LOG.trace("Removed source {} from schema context requirements", resolvedId);
126                         version = new Object();
127                         reg.close();
128                         texts.remove(resolvedId, text);
129                     }
130                 }
131             };
132         }
133     }
134
135     /**
136      * Try to parse all currently available yang files and build new schema context.
137      * @return new schema context iif there is at least 1 yang file registered and
138      *         new schema context was successfully built.
139      */
140     public Optional<SchemaContext> getSchemaContext() {
141         final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
142         Optional<SchemaContext> sc;
143         Object v;
144         do {
145             // Spin get stable context version
146             Object cv;
147             do {
148                 cv = contextVersion;
149                 sc = currentSchemaContext.get();
150                 if (version == cv) {
151                     return sc;
152                 }
153             } while (cv != contextVersion);
154
155             // Version has been updated
156             Collection<SourceIdentifier> sources;
157             do {
158                 v = version;
159                 sources = ImmutableSet.copyOf(requiredSources);
160             } while (v != version);
161
162             while (true) {
163                 final CheckedFuture<SchemaContext, SchemaResolutionException> f = factory.createSchemaContext(sources);
164                 try {
165                     sc = Optional.of(f.checkedGet());
166                     break;
167                 } catch (SchemaResolutionException e) {
168                     LOG.debug("Failed to fully assemble schema context for {}", sources, e);
169                     sources = e.getResolvedSources();
170                 }
171             }
172
173             LOG.debug("Resolved schema context for {}", sources);
174
175             synchronized (this) {
176                 if (contextVersion == cv) {
177                     currentSchemaContext.set(sc);
178                     contextVersion = v;
179                 }
180             }
181         } while (version == v);
182
183         return sc;
184     }
185
186     @Override
187     public synchronized CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
188         final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
189
190         LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
191         if (ret.isEmpty()) {
192             return Futures.<YangTextSchemaSource, SchemaSourceException>immediateFailedCheckedFuture(
193                     new MissingSchemaSourceException("URL for " + sourceIdentifier + " not registered", sourceIdentifier));
194         }
195
196         return Futures.immediateCheckedFuture(ret.iterator().next());
197     }
198
199     @Override
200     public void close() {
201         transReg.close();
202     }
203 }