Move RemoteDeviceId
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / schema / YangLibrarySchemaYangSourceProvider.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.connect.netconf.schema;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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;
18 import java.net.URL;
19 import java.util.Map;
20 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
21 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider.NetconfYangTextSchemaSource;
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;
28
29 /**
30  * Provides YANG schema sources from yang library.
31  */
32 public final class YangLibrarySchemaYangSourceProvider implements SchemaSourceProvider<YangTextSchemaSource> {
33     private static final Logger LOG = LoggerFactory.getLogger(YangLibrarySchemaYangSourceProvider.class);
34
35     private final Map<SourceIdentifier, URL> availableSources;
36     private final RemoteDeviceId id;
37
38     public YangLibrarySchemaYangSourceProvider(final RemoteDeviceId id,
39             final Map<SourceIdentifier, URL> availableSources) {
40         this.id = id;
41         this.availableSources = ImmutableMap.copyOf(availableSources);
42     }
43
44     @Override
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));
58         }
59     }
60 }