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.
27 public final class YangSourceFromCapabilitiesResolver extends YangSourceContextResolver {
29 private final Iterable<QName> capabilities;
32 * Construct new {@link YangSourceFromCapabilitiesResolver}.
34 * @param capabilities Set of QName representing module capabilities, {@link QName#getLocalName()} represents
35 * source name and {@link QName#getRevision()} represents revision of source.
37 * @param schemaSourceProvider - {@link SchemaSourceProvider} which should be used to resolve sources.
39 public YangSourceFromCapabilitiesResolver(final Iterable<QName> capabilities,
40 final SchemaSourceProvider<InputStream> schemaSourceProvider) {
41 super(SchemaSourceProviders.toAdvancedSchemaSourceProvider(schemaSourceProvider));
42 this.capabilities = capabilities;
46 public YangSourceContext resolveContext() {
47 for (QName capability : capabilities) {
48 resolveCapability(capability);
50 return createSourceContext();
53 private void resolveCapability(final QName capability) {
54 super.resolveSource(capability.getLocalName(), Optional.fromNullable(capability.getFormattedRevision()));
58 public Optional<YangModelDependencyInfo> getDependencyInfo(final SourceIdentifier identifier) {
59 Optional<InputStream> source = getSourceProvider().getSchemaSource(identifier);
60 if (source.isPresent()) {
61 return Optional.of(YangModelDependencyInfo.fromInputStream(source.get()));
63 return Optional.absent();