Sonar issues clean-up
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaContextFactory.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 java.net.URI;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.LinkedHashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.NavigableMap;
19 import java.util.Set;
20
21 import javax.annotation.Nullable;
22
23 import org.antlr.v4.runtime.ParserRuleContext;
24 import org.antlr.v4.runtime.tree.ParseTreeWalker;
25 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
26 import org.opendaylight.yangtools.util.concurrent.ReflectiveExceptionMapper;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
30 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
31 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
32 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
33 import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
34 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
35 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
36 import org.opendaylight.yangtools.yang.parser.impl.YangParserListenerImpl;
37 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
38 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.google.common.base.Function;
43 import com.google.common.base.Optional;
44 import com.google.common.base.Preconditions;
45 import com.google.common.base.Predicate;
46 import com.google.common.cache.Cache;
47 import com.google.common.cache.CacheBuilder;
48 import com.google.common.collect.Collections2;
49 import com.google.common.collect.Iterables;
50 import com.google.common.collect.Lists;
51 import com.google.common.collect.Maps;
52 import com.google.common.collect.Sets;
53 import com.google.common.util.concurrent.AsyncFunction;
54 import com.google.common.util.concurrent.CheckedFuture;
55 import com.google.common.util.concurrent.FutureCallback;
56 import com.google.common.util.concurrent.Futures;
57 import com.google.common.util.concurrent.ListenableFuture;
58
59 final class SharedSchemaContextFactory implements SchemaContextFactory {
60     private static final ExceptionMapper<SchemaResolutionException> MAPPER = ReflectiveExceptionMapper.create("resolve sources", SchemaResolutionException.class);
61     private static final Logger LOG = LoggerFactory.getLogger(SharedSchemaContextFactory.class);
62
63     private final Function<SourceIdentifier, ListenableFuture<ASTSchemaSource>> requestSources = new Function<SourceIdentifier, ListenableFuture<ASTSchemaSource>>() {
64         @Override
65         public ListenableFuture<ASTSchemaSource> apply(final SourceIdentifier input) {
66             return repository.getSchemaSource(input, ASTSchemaSource.class);
67         }
68     };
69     private final Cache<Collection<SourceIdentifier>, SchemaContext> cache = CacheBuilder.newBuilder().weakValues().build();
70
71     private final AsyncFunction<List<ASTSchemaSource>, SchemaContext> assembleSources = new AsyncFunction<List<ASTSchemaSource>, SchemaContext>() {
72         @Override
73         public ListenableFuture<SchemaContext> apply(final List<ASTSchemaSource> sources) throws SchemaResolutionException {
74             final Map<SourceIdentifier, ASTSchemaSource> srcs =
75                     Maps.uniqueIndex(sources, ASTSchemaSource.GET_IDENTIFIER);
76             final Map<SourceIdentifier, YangModelDependencyInfo> deps =
77                     Maps.transformValues(srcs, ASTSchemaSource.GET_DEPINFO);
78
79             LOG.debug("Resolving dependency reactor {}", deps);
80
81             final DependencyResolver res = DependencyResolver.create(deps);
82             if (!res.getUnresolvedSources().isEmpty()) {
83                 LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(), res.getUnsatisfiedImports());
84
85                 // FIXME: push into DependencyResolver
86
87                 throw new SchemaResolutionException("Failed to resolve required models",
88                         res.getResolvedSources(), res.getUnsatisfiedImports());
89             }
90
91             final Map<SourceIdentifier, ParserRuleContext> asts =
92                     Maps.transformValues(srcs, ASTSchemaSource.GET_AST);
93             final Map<String, NavigableMap<Date, URI>> namespaceContext = BuilderUtils.createYangNamespaceContext(
94                     asts.values(), Optional.<SchemaContext>absent());
95
96             final ParseTreeWalker walker = new ParseTreeWalker();
97             final Map<SourceIdentifier, ModuleBuilder> sourceToBuilder = new LinkedHashMap<>();
98
99             for (final Entry<SourceIdentifier, ParserRuleContext> entry : asts.entrySet()) {
100                 final ModuleBuilder moduleBuilder = YangParserListenerImpl.create(namespaceContext, entry.getKey().getName(),
101                         walker, entry.getValue()).getModuleBuilder();
102
103                 moduleBuilder.setSource(srcs.get(entry.getKey()).getYangText());
104                 sourceToBuilder.put(entry.getKey(), moduleBuilder);
105             }
106             LOG.debug("Modules ready for integration");
107
108             final YangParserImpl parser = YangParserImpl.getInstance();
109             final Collection<Module> modules = parser.buildModules(sourceToBuilder.values());
110             LOG.debug("Integrated cross-references modules");
111             return Futures.immediateCheckedFuture(parser.assembleContext(modules));
112         }
113     };
114
115     private final SharedSchemaRepository repository;
116     // FIXME: ignored right now
117     private final SchemaSourceFilter filter;
118
119     // FIXME SchemaRepository should be the type for repository parameter instead of SharedSchemaRepository (final implementation)
120     public SharedSchemaContextFactory(final SharedSchemaRepository repository, final SchemaSourceFilter filter) {
121         this.repository = Preconditions.checkNotNull(repository);
122         this.filter = Preconditions.checkNotNull(filter);
123     }
124
125     @Override
126     public CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(final Collection<SourceIdentifier> requiredSources) {
127         // Make sources unique
128         final List<SourceIdentifier> uniqueSourceIdentifiers = deDuplicateSources(requiredSources);
129
130         final SchemaContext existing = cache.getIfPresent(uniqueSourceIdentifiers);
131         if (existing != null) {
132             LOG.debug("Returning cached context {}", existing);
133             return Futures.immediateCheckedFuture(existing);
134         }
135
136         // Request all sources be loaded
137         ListenableFuture<List<ASTSchemaSource>> sf = Futures.allAsList(Collections2.transform(uniqueSourceIdentifiers, requestSources));
138
139         // Detect mismatch between requested Source IDs and IDs that are extracted from parsed source
140         // Also remove duplicates if present
141         // We are relying on preserved order of uniqueSourceIdentifiers as well as sf
142         sf = Futures.transform(sf, new SourceIdMismatchDetector(uniqueSourceIdentifiers));
143
144         // Assemble sources into a schema context
145         final ListenableFuture<SchemaContext> cf = Futures.transform(sf, assembleSources);
146
147         // Populate cache when successful
148         Futures.addCallback(cf, new FutureCallback<SchemaContext>() {
149             @Override
150             public void onSuccess(final SchemaContext result) {
151                 cache.put(uniqueSourceIdentifiers, result);
152             }
153
154             @Override
155             public void onFailure(final Throwable t) {
156                 LOG.debug("Failed to assemble sources", t);
157             }
158         });
159
160         return Futures.makeChecked(cf, MAPPER);
161     }
162
163     /**
164      * @return set (preserving ordering) from the input collection
165      */
166     private List<SourceIdentifier> deDuplicateSources(final Collection<SourceIdentifier> requiredSources) {
167         final Set<SourceIdentifier> uniqueSourceIdentifiers = Collections.unmodifiableSet(Sets.newLinkedHashSet(requiredSources));
168         if(uniqueSourceIdentifiers.size() != requiredSources.size()) {
169             LOG.warn("Duplicate sources requested for schema context, removed duplicate sources: {}", Collections2.filter(uniqueSourceIdentifiers, new Predicate<SourceIdentifier>() {
170                 @Override
171                 public boolean apply(@Nullable final SourceIdentifier input) {
172                     return Iterables.frequency(requiredSources, input) > 1;
173                 }
174             }));
175         }
176         return Lists.newArrayList(uniqueSourceIdentifiers);
177     }
178
179     private static final class SourceIdMismatchDetector implements Function<List<ASTSchemaSource>, List<ASTSchemaSource>> {
180         private final List<SourceIdentifier> sourceIdentifiers;
181
182         public SourceIdMismatchDetector(final List<SourceIdentifier> sourceIdentifiers) {
183             this.sourceIdentifiers = sourceIdentifiers;
184         }
185
186         @Override
187         public List<ASTSchemaSource> apply(final List<ASTSchemaSource> input) {
188             final Map<SourceIdentifier, ASTSchemaSource> filtered = Maps.newLinkedHashMap();
189
190             for (int i = 0; i < input.size(); i++) {
191
192                 final SourceIdentifier expectedSId = sourceIdentifiers.get(i);
193                 final ASTSchemaSource astSchemaSource = input.get(i);
194                 final SourceIdentifier realSId = astSchemaSource.getIdentifier();
195
196                 if (!expectedSId.equals(realSId)) {
197                     LOG.warn("Source identifier mismatch for module \"{}\", requested as {} but actually is {}. Using actual id", expectedSId.getName(), expectedSId, realSId);
198                 }
199
200                 if (filtered.containsKey(realSId)) {
201                     LOG.warn("Duplicate source for module {} detected in reactor", realSId);
202                 }
203
204                 filtered.put(realSId, astSchemaSource);
205
206             }
207             return Lists.newArrayList(filtered.values());
208         }
209     }
210 }