BUG-865: remove ChoiceNode
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / util / 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.impl.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URL;
20 import java.util.Collection;
21 import java.util.Map.Entry;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24
25 import javax.annotation.concurrent.GuardedBy;
26 import javax.annotation.concurrent.ThreadSafe;
27
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;
37
38 /**
39  * @deprecated Use {@link org.opendaylight.yangtools.yang.parser.repo.URLSchemaContextResolver}
40  * instead.
41  */
42 @Deprecated
43 @ThreadSafe
44 public class URLSchemaContextResolver implements AdvancedSchemaSourceProvider<InputStream> {
45
46     private static final Logger LOG = LoggerFactory.getLogger(URLSchemaContextResolver.class);
47
48     @GuardedBy("this")
49     private final ConcurrentMap<SourceIdentifier, SourceContext> availableSources = new ConcurrentHashMap<>();
50     @GuardedBy("this")
51     private YangSourceContext currentSourceContext;
52     @GuardedBy("this")
53     private Optional<SchemaContext> currentSchemaContext = Optional.absent();
54
55     /**
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
59      */
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);
68         return sourceContext;
69     }
70
71     public synchronized Optional<SchemaContext> getSchemaContext() {
72         return currentSchemaContext;
73     }
74
75     @Override
76     public synchronized Optional<InputStream> getSchemaSource(final SourceIdentifier key) {
77         SourceContext ctx = availableSources.get(key);
78         if (ctx != null) {
79             InputStream stream = getInputStream(ctx.getInstance());
80             return Optional.fromNullable(stream);
81         }
82         return Optional.absent();
83     }
84
85     @Override
86     public Optional<InputStream> getSchemaSource(final String name, final Optional<String> version) {
87         return getSchemaSource(SourceIdentifier.create(name, version));
88     }
89
90     private static InputStream getInputStream(final URL source) {
91         InputStream stream;
92         try {
93             stream = source.openStream();
94         } catch (IOException e) {
95             throw new IllegalArgumentException("Supplied stream: " + source + " is not available", e);
96         }
97         return stream;
98     }
99
100     private final class SourceContext extends AbstractObjectRegistration<URL>
101             implements Identifiable<SourceIdentifier> {
102
103         final SourceIdentifier identifier;
104         final YangModelDependencyInfo dependencyInfo;
105
106         public SourceContext(final URL instance, final SourceIdentifier identifier, final YangModelDependencyInfo modelInfo) {
107             super(instance);
108             this.identifier = identifier;
109             this.dependencyInfo = modelInfo;
110         }
111
112         @Override
113         public SourceIdentifier getIdentifier() {
114             return identifier;
115         }
116
117         @Override
118         protected void removeRegistration() {
119             removeSource(this);
120         }
121
122         public YangModelDependencyInfo getDependencyInfo() {
123             return dependencyInfo;
124         }
125     }
126
127     private synchronized void removeSource(final SourceContext sourceContext) {
128         boolean removed = availableSources.remove(sourceContext.getIdentifier(), sourceContext);
129         if (removed) {
130             tryToUpdateSchemaContext();
131         }
132     }
133
134     /**
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.
137      */
138     public synchronized Optional<SchemaContext> tryToUpdateSchemaContext() {
139         if (availableSources.isEmpty()) {
140             return Optional.absent();
141         }
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());
146         }
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());
150
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());
154         }
155         if (currentSourceContext == null || !yangSourceContext.getValidSources().equals(currentSourceContext.getValidSources())) {
156             try {
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();
166             }
167         } else {
168             currentSourceContext = yangSourceContext;
169             return Optional.absent();
170         }
171     }
172 }