Reduce the use of AttrBuilders
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / schema / YangLibrarySchemaYangSourceProvider.java
index 33af8ec298be82279c80e9d13b3170eb0a16eaf1..f4251296592bf92182c773b2fe367cb4251c1214 100644 (file)
@@ -7,15 +7,20 @@
  */
 package org.opendaylight.netconf.sal.connect.netconf.schema;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.collect.ImmutableMap;
 import com.google.common.io.ByteStreams;
-import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.nio.charset.Charset;
 import java.util.Map;
+import java.util.Optional;
+import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider.NetconfYangTextSchemaSource;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
@@ -34,35 +39,27 @@ public final class YangLibrarySchemaYangSourceProvider implements SchemaSourcePr
     private final Map<SourceIdentifier, URL> availableSources;
     private final RemoteDeviceId id;
 
-    public YangLibrarySchemaYangSourceProvider(
-            final RemoteDeviceId id, final Map<SourceIdentifier, URL> availableSources) {
+    public YangLibrarySchemaYangSourceProvider(final RemoteDeviceId id,
+            final Map<SourceIdentifier, URL> availableSources) {
         this.id = id;
-        this.availableSources = Preconditions.checkNotNull(availableSources);
+        this.availableSources = ImmutableMap.copyOf(availableSources);
     }
 
     @Override
-    public CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> getSource(
-            final SourceIdentifier sourceIdentifier) {
-        Preconditions.checkNotNull(sourceIdentifier);
-        Preconditions.checkArgument(availableSources.containsKey(sourceIdentifier));
-        return download(sourceIdentifier);
-    }
-
-    private CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> download(
-            final SourceIdentifier sourceIdentifier) {
-        final URL url = availableSources.get(sourceIdentifier);
+    public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
+        final URL url = availableSources.get(requireNonNull(sourceIdentifier));
+        checkArgument(url != null);
         try (InputStream in = url.openStream()) {
-            final String schemaContent = new String(ByteStreams.toByteArray(in));
-            final NetconfRemoteSchemaYangSourceProvider.NetconfYangTextSchemaSource yangSource =
-                    new NetconfRemoteSchemaYangSourceProvider
-                            .NetconfYangTextSchemaSource(id, sourceIdentifier, Optional.of(schemaContent));
+            // FIXME: defaultCharset() seems to be wrong here
+            final String schemaContent = new String(ByteStreams.toByteArray(in), Charset.defaultCharset());
+            final NetconfYangTextSchemaSource yangSource = new NetconfYangTextSchemaSource(id, sourceIdentifier,
+                Optional.of(schemaContent));
             LOG.debug("Source {} downloaded from a yang library's url {}", sourceIdentifier, url);
-            return Futures.immediateCheckedFuture(yangSource);
+            return Futures.immediateFuture(yangSource);
         } catch (IOException e) {
             LOG.warn("Unable to download source {} from a yang library's url {}", sourceIdentifier, url, e);
-            return Futures.immediateFailedCheckedFuture(
-                    new SchemaSourceException(
-                            "Unable to download remote schema for " + sourceIdentifier + " from " + url, e));
+            return Futures.immediateFailedFuture(new SchemaSourceException(
+                "Unable to download remote schema for " + sourceIdentifier + " from " + url, e));
         }
     }
 }