Fix license header violations in yang-parser-impl
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / util / YangSourceFromDependencyInfoResolver.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 com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableMap;
12 import java.io.InputStream;
13 import java.util.Map;
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;
19
20 /**
21  * Resolver for YANG Schema Source which is based on DependencyInfo
22  *
23  * This resolver does not use {@link SchemaSourceProvider} but supplied map
24  * of source identifiers and {@link YangModelDependencyInfo} to construct
25  * {@link YangSourceContext}.
26  *
27  */
28 @Deprecated
29 @NotThreadSafe
30 public final class YangSourceFromDependencyInfoResolver extends YangSourceContextResolver {
31
32     private final Map<SourceIdentifier, YangModelDependencyInfo> dependencyInfo;
33
34     public YangSourceFromDependencyInfoResolver(final Map<SourceIdentifier, YangModelDependencyInfo> moduleDependencies,
35                                                 AdvancedSchemaSourceProvider<InputStream> sourceProvider) {
36         super(sourceProvider);
37         dependencyInfo = ImmutableMap.copyOf(moduleDependencies);
38     }
39
40     @Override
41     public Optional<YangModelDependencyInfo> getDependencyInfo(final SourceIdentifier identifier) {
42         if (identifier.getRevision() != null) {
43             return Optional.fromNullable(dependencyInfo.get(identifier));
44         }
45         YangModelDependencyInfo potential = dependencyInfo.get(identifier);
46         if (potential == null) {
47             for (Entry<SourceIdentifier, YangModelDependencyInfo> newPotential : dependencyInfo.entrySet()) {
48                 String newPotentialName = newPotential.getKey().getName();
49
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();
54                     }
55                 }
56             }
57         }
58         return Optional.fromNullable(potential);
59     }
60
61     @Override
62     public YangSourceContext resolveContext() {
63         for (SourceIdentifier source : dependencyInfo.keySet()) {
64             resolveSource(source);
65         }
66         return createSourceContext();
67     }
68 }