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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.io.ByteStreams;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15 import java.io.IOException;
16 import java.io.InputStream;
19 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
21 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
23 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Provides YANG schema sources from yang library
30 public final class YangLibrarySchemaYangSourceProvider implements SchemaSourceProvider<YangTextSchemaSource> {
32 private static final Logger LOG = LoggerFactory.getLogger(YangLibrarySchemaYangSourceProvider.class);
34 private final Map<SourceIdentifier, URL> availableSources;
35 private final RemoteDeviceId id;
37 public YangLibrarySchemaYangSourceProvider(
38 final RemoteDeviceId id, final Map<SourceIdentifier, URL> availableSources) {
40 this.availableSources = Preconditions.checkNotNull(availableSources);
44 public CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> getSource(
45 final SourceIdentifier sourceIdentifier) {
46 Preconditions.checkNotNull(sourceIdentifier);
47 Preconditions.checkArgument(availableSources.containsKey(sourceIdentifier));
48 return download(sourceIdentifier);
51 private CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> download(final SourceIdentifier sId) {
52 final URL url = availableSources.get(sId);
53 try(final InputStream in = url.openStream()) {
54 final String schemaContent = new String(ByteStreams.toByteArray(in));
55 final NetconfRemoteSchemaYangSourceProvider.NetconfYangTextSchemaSource yangSource =
56 new NetconfRemoteSchemaYangSourceProvider.
57 NetconfYangTextSchemaSource(id, sId, Optional.of(schemaContent));
58 LOG.debug("Source {} downloaded from a yang library's url {}", sId, url);
59 return Futures.immediateCheckedFuture(yangSource);
60 } catch (IOException e) {
61 LOG.warn("Unable to download source {} from a yang library's url {}", sId, url, e);
62 return Futures.immediateFailedCheckedFuture(
63 new SchemaSourceException("Unable to download remote schema for " + sId + " from " + url, e));