5d09777914f98967f5222a7b57ca8fae3bfe8d7a
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / util / YangSourceFromCapabilitiesResolver.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/eplv10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.impl.util;
9
10 import com.google.common.base.Optional;
11 import java.io.InputStream;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
14 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProviders;
15 import org.opendaylight.yangtools.yang.model.util.repo.SourceIdentifier;
16
17 /**
18  *
19  * Source code resolver which resolves Yang Source Context against
20  * {@link SchemaSourceProvider} and set of QName which represent capabilities.
21  *
22  * This source code resolver is useful for components which deals with
23  * capability exchange similar to YANG/Netconf specification
24  * and there is {@link SchemaSourceProvider} able to retrieve YANG models.
25  *
26  */
27 @Deprecated
28 public final class YangSourceFromCapabilitiesResolver extends YangSourceContextResolver {
29
30     private final Iterable<QName> capabilities;
31
32     /**
33      * Construct new {@link YangSourceFromCapabilitiesResolver}.
34      *
35      * @param capabilities Set of QName representing module capabilities, {@link QName#getLocalName()} represents
36      * source name and {@link QName#getRevision()} represents revision of source.
37      *
38      * @param schemaSourceProvider - {@link SchemaSourceProvider} which should be used to resolve sources.
39      */
40     public YangSourceFromCapabilitiesResolver(final Iterable<QName> capabilities,
41             final SchemaSourceProvider<InputStream> schemaSourceProvider) {
42         super(SchemaSourceProviders.toAdvancedSchemaSourceProvider(schemaSourceProvider));
43         this.capabilities = capabilities;
44     }
45
46     @Override
47     public YangSourceContext resolveContext() {
48         for (QName capability : capabilities) {
49             resolveCapability(capability);
50         }
51         return createSourceContext();
52     }
53
54     private void resolveCapability(final QName capability) {
55         super.resolveSource(capability.getLocalName(), Optional.fromNullable(capability.getFormattedRevision()));
56     }
57
58     @Override
59     public Optional<YangModelDependencyInfo> getDependencyInfo(final SourceIdentifier identifier) {
60         Optional<InputStream> source = getSourceProvider().getSchemaSource(identifier);
61         if (source.isPresent()) {
62             return Optional.of(YangModelDependencyInfo.fromInputStream(source.get()));
63         }
64         return Optional.absent();
65     }
66
67 }