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 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;
19 * Source code resolver which resolves Yang Source Context against
20 * {@link SchemaSourceProvider} and set of QName which represent capabilities.
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.
28 public final class YangSourceFromCapabilitiesResolver extends YangSourceContextResolver {
30 private final Iterable<QName> capabilities;
33 * Construct new {@link YangSourceFromCapabilitiesResolver}.
35 * @param capabilities Set of QName representing module capabilities, {@link QName#getLocalName()} represents
36 * source name and {@link QName#getRevision()} represents revision of source.
38 * @param schemaSourceProvider - {@link SchemaSourceProvider} which should be used to resolve sources.
40 public YangSourceFromCapabilitiesResolver(final Iterable<QName> capabilities,
41 final SchemaSourceProvider<InputStream> schemaSourceProvider) {
42 super(SchemaSourceProviders.toAdvancedSchemaSourceProvider(schemaSourceProvider));
43 this.capabilities = capabilities;
47 public YangSourceContext resolveContext() {
48 for (QName capability : capabilities) {
49 resolveCapability(capability);
51 return createSourceContext();
54 private void resolveCapability(final QName capability) {
55 super.resolveSource(capability.getLocalName(), Optional.fromNullable(capability.getFormattedRevision()));
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()));
64 return Optional.absent();