Move YangLibrarySchemaYangSourceProvider
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / LibrarySchemaSourceProvider.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.client.mdsal;
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.net.URL;
18 import java.util.Map;
19 import org.opendaylight.netconf.client.mdsal.api.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;
26
27 /**
28  * Provides YANG schema sources from YANG library. The set of available sources is pre-determined when this provider
29  * is created, but each source is acquired on demand.
30  */
31 public final class LibrarySchemaSourceProvider implements SchemaSourceProvider<YangTextSchemaSource> {
32     private static final Logger LOG = LoggerFactory.getLogger(LibrarySchemaSourceProvider.class);
33
34     private final ImmutableMap<SourceIdentifier, URL> availableSources;
35     private final RemoteDeviceId id;
36
37     public LibrarySchemaSourceProvider(final RemoteDeviceId id, final Map<SourceIdentifier, URL> availableSources) {
38         this.id = requireNonNull(id);
39         this.availableSources = ImmutableMap.copyOf(availableSources);
40     }
41
42     @Override
43     public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
44         final var url = availableSources.get(requireNonNull(sourceIdentifier));
45         checkArgument(url != null);
46
47         final byte[] schemaContent;
48         try (var in = url.openStream()) {
49             schemaContent = in.readAllBytes();
50         } catch (IOException e) {
51             LOG.warn("Unable to download source {} from a yang library's url {}", sourceIdentifier, url, e);
52             return Futures.immediateFailedFuture(new SchemaSourceException(
53                 "Unable to download remote schema for " + sourceIdentifier + " from " + url, e));
54         }
55
56         final var yangSource = new CachedYangTextSchemaSource(id, sourceIdentifier, url.toString(), schemaContent);
57         LOG.debug("Source {} downloaded from a yang library's url {}", sourceIdentifier, url);
58         return Futures.immediateFuture(yangSource);
59     }
60 }