2 * Copyright (c) 2015 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/epl-v10.html
8 package org.opendaylight.netconf.sal.connect.netconf.schema;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.io.IOException;
17 import java.io.InputStream;
20 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider.NetconfYangTextSchemaSource;
21 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
25 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Provides YANG schema sources from yang library.
32 public final class YangLibrarySchemaYangSourceProvider implements SchemaSourceProvider<YangTextSchemaSource> {
33 private static final Logger LOG = LoggerFactory.getLogger(YangLibrarySchemaYangSourceProvider.class);
35 private final Map<SourceIdentifier, URL> availableSources;
36 private final RemoteDeviceId id;
38 public YangLibrarySchemaYangSourceProvider(final RemoteDeviceId id,
39 final Map<SourceIdentifier, URL> availableSources) {
41 this.availableSources = ImmutableMap.copyOf(availableSources);
45 public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
46 final URL url = availableSources.get(requireNonNull(sourceIdentifier));
47 checkArgument(url != null);
48 try (InputStream in = url.openStream()) {
49 final byte[] schemaContent = in.readAllBytes();
50 final NetconfYangTextSchemaSource yangSource = new NetconfYangTextSchemaSource(id, sourceIdentifier,
51 url.toString(), schemaContent);
52 LOG.debug("Source {} downloaded from a yang library's url {}", sourceIdentifier, url);
53 return Futures.immediateFuture(yangSource);
54 } catch (IOException e) {
55 LOG.warn("Unable to download source {} from a yang library's url {}", sourceIdentifier, url, e);
56 return Futures.immediateFailedFuture(new SchemaSourceException(
57 "Unable to download remote schema for " + sourceIdentifier + " from " + url, e));