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 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;
17 import com.google.common.base.Optional;
21 * Source code resolver which resolves Yang Source Context against
22 * {@link SchemaSourceProvider} and set of QName which represent capabilities.
24 * This source code resolver is useful for components which deals with
25 * capability exchange similar to YANG/Netconf specification
26 * and there is {@link SchemaSourceProvider} able to retrieve YANG models.
29 public final class YangSourceFromCapabilitiesResolver extends YangSourceContextResolver {
31 private final Iterable<QName> capabilities;
34 * Construct new {@link YangSourceFromCapabilitiesResolver}.
36 * @param capabilities Set of QName representing module capabilities, {@link QName#getLocalName()} represents
37 * source name and {@link QName#getRevision()} represents revision of source.
39 * @param schemaSourceProvider - {@link SchemaSourceProvider} which should be used to resolve sources.
41 public YangSourceFromCapabilitiesResolver(final Iterable<QName> capabilities,
42 final SchemaSourceProvider<InputStream> schemaSourceProvider) {
43 super(SchemaSourceProviders.toAdvancedSchemaSourceProvider(schemaSourceProvider));
44 this.capabilities = capabilities;
48 public YangSourceContext resolveContext() {
49 for (QName capability : capabilities) {
50 resolveCapability(capability);
52 return createSourceContext();
55 private void resolveCapability(final QName capability) {
56 super.resolveSource(capability.getLocalName(), Optional.fromNullable(capability.getFormattedRevision()));
60 public Optional<YangModelDependencyInfo> getDependencyInfo(final SourceIdentifier identifier) {
61 Optional<InputStream> source = getSourceProvider().getSchemaSource(identifier);
62 if (source.isPresent()) {
63 return Optional.of(YangModelDependencyInfo.fromInputStream(source.get()));
65 return Optional.absent();