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/eplv10.html
8 package org.opendaylight.yangtools.yang.parser.impl.util;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableMap;
12 import java.io.InputStream;
14 import java.util.Map.Entry;
15 import javax.annotation.concurrent.NotThreadSafe;
16 import org.opendaylight.yangtools.yang.model.util.repo.AdvancedSchemaSourceProvider;
17 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
18 import org.opendaylight.yangtools.yang.model.util.repo.SourceIdentifier;
21 * Resolver for YANG Schema Source which is based on DependencyInfo
23 * This resolver does not use {@link SchemaSourceProvider} but supplied map
24 * of source identifiers and {@link YangModelDependencyInfo} to construct
25 * {@link YangSourceContext}.
29 public final class YangSourceFromDependencyInfoResolver extends YangSourceContextResolver {
31 private final Map<SourceIdentifier, YangModelDependencyInfo> dependencyInfo;
33 public YangSourceFromDependencyInfoResolver(final Map<SourceIdentifier, YangModelDependencyInfo> moduleDependencies,
34 AdvancedSchemaSourceProvider<InputStream> sourceProvider) {
35 super(sourceProvider);
36 dependencyInfo = ImmutableMap.copyOf(moduleDependencies);
40 public Optional<YangModelDependencyInfo> getDependencyInfo(final SourceIdentifier identifier) {
41 if (identifier.getRevision() != null) {
42 return Optional.fromNullable(dependencyInfo.get(identifier));
44 YangModelDependencyInfo potential = dependencyInfo.get(identifier);
45 if (potential == null) {
46 for (Entry<SourceIdentifier, YangModelDependencyInfo> newPotential : dependencyInfo.entrySet()) {
47 String newPotentialName = newPotential.getKey().getName();
49 if (newPotentialName.equals(identifier.getName())) {
50 String newPotentialRevision = newPotential.getKey().getRevision();
51 if (potential == null || 1 == newPotentialRevision.compareTo(potential.getFormattedRevision())) {
52 potential = newPotential.getValue();
57 return Optional.fromNullable(potential);
61 public YangSourceContext resolveContext() {
62 for (SourceIdentifier source : dependencyInfo.keySet()) {
63 resolveSource(source);
65 return createSourceContext();