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 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}.
30 public final class YangSourceFromDependencyInfoResolver extends YangSourceContextResolver {
32 private final Map<SourceIdentifier, YangModelDependencyInfo> dependencyInfo;
34 public YangSourceFromDependencyInfoResolver(final Map<SourceIdentifier, YangModelDependencyInfo> moduleDependencies,
35 AdvancedSchemaSourceProvider<InputStream> sourceProvider) {
36 super(sourceProvider);
37 dependencyInfo = ImmutableMap.copyOf(moduleDependencies);
41 public Optional<YangModelDependencyInfo> getDependencyInfo(final SourceIdentifier identifier) {
42 if (identifier.getRevision() != null) {
43 return Optional.fromNullable(dependencyInfo.get(identifier));
45 YangModelDependencyInfo potential = dependencyInfo.get(identifier);
46 if (potential == null) {
47 for (Entry<SourceIdentifier, YangModelDependencyInfo> newPotential : dependencyInfo.entrySet()) {
48 String newPotentialName = newPotential.getKey().getName();
50 if (newPotentialName.equals(identifier.getName())) {
51 String newPotentialRevision = newPotential.getKey().getRevision();
52 if (potential == null || 1 == newPotentialRevision.compareTo(potential.getFormattedRevision())) {
53 potential = newPotential.getValue();
58 return Optional.fromNullable(potential);
62 public YangSourceContext resolveContext() {
63 for (SourceIdentifier source : dependencyInfo.keySet()) {
64 resolveSource(source);
66 return createSourceContext();