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