Sonar: remove unused modifiers
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / repo / FilesystemSchemaCachingProvider.java
index 0fdf05a227dd96f9a354f17bb29788218ac62b9c..deb4fe038cd91e67594cd428020f8bcfdd93de4f 100644 (file)
@@ -1,5 +1,17 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/eplv10.html
+ */
 package org.opendaylight.yangtools.yang.model.util.repo;
 
+import com.google.common.base.Charsets;
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -7,60 +19,124 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStreamWriter;
-import java.io.StringBufferInputStream;
-
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
+import java.util.regex.Pattern;
+import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-public class FilesystemSchemaCachingProvider<I> extends AbstractCachingSchemaSourceProvider<I, InputStream> {
+/**
+ * Filesystem-based schema caching source provider
+ *
+ * This schema source provider caches all YANG modules loaded from backing
+ * schema source providers (registered via
+ * {@link #createInstanceFor(SchemaSourceProvider)} to supplied folder.
+ *
+ * @param <I>
+ *            Input format in which schema source is represented.
+ *
+ * @deprecated Replaced with {@link org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache}
+ *
+ */
+@Deprecated
+public final class FilesystemSchemaCachingProvider<I> extends AbstractCachingSchemaSourceProvider<I, InputStream> {
+    private static final Logger LOG = LoggerFactory.getLogger(FilesystemSchemaCachingProvider.class);
+    public static final Pattern REVISION_PATTERN = Pattern.compile("\\d\\d\\d\\d-\\d\\d-\\d\\d");
 
     private final File storageDirectory;
-    private final Function<I, String> transformationFunction;
+    private final SchemaSourceTransformation<I, String> transformationFunction;
 
-    public FilesystemSchemaCachingProvider(SchemaSourceProvider<I> delegate, File directory,
-            Function<I, String> transformationFunction) {
+    /**
+     *
+     * Construct filesystem caching schema source provider.
+     *
+     *
+     * @param delegate
+     *            Default delegate to lookup for missed entries in cache.
+     * @param directory
+     *            Directory where YANG files should be cached.
+     * @param transformationFunction
+     *            Transformation function which translates from input in format
+     *            <code>I</code> to InputStream.
+     * @throws IllegalArgumentException
+     *             If supplied directory does not exists or is not directory.
+     */
+    public FilesystemSchemaCachingProvider(final AdvancedSchemaSourceProvider<I> delegate, final File directory,
+            final SchemaSourceTransformation<I, String> transformationFunction) {
         super(delegate);
+        Preconditions.checkNotNull(directory, "directory must not be null.");
+        Preconditions.checkArgument(directory.exists(), "directory must be directory.");
+        Preconditions.checkArgument(directory.isDirectory(), "directory must be directory.");
         this.storageDirectory = directory;
-        this.transformationFunction = transformationFunction;
+        this.transformationFunction = Preconditions.checkNotNull(transformationFunction,
+                "transformationFunction must not be null.");
+    }
+
+    /**
+     *
+     * Construct filesystem caching schema source provider.
+     *
+     *
+     * @param delegate
+     *            Default delegate to lookup for missed entries in cache.
+     * @param directory
+     *            Directory where YANG files should be cached.
+     * @param transformationFunction
+     *            Transformation function which translates from input in format
+     *            <code>I</code> to InputStream.
+     * @throws IllegalArgumentException
+     *             If supplied directory does not exists or is not directory.
+     * @deprecated Use
+     *             {@link #FilesystemSchemaCachingProvider(AdvancedSchemaSourceProvider, File, SchemaSourceTransformation)}
+     *             with
+     *             {@link SchemaSourceProviders#schemaSourceTransformationFrom(Function)}
+     *             instead.
+     */
+    @Deprecated
+    public FilesystemSchemaCachingProvider(final AdvancedSchemaSourceProvider<I> delegate, final File directory,
+            final Function<I, String> transformationFunction) {
+        super(delegate);
+        Preconditions.checkNotNull(directory, "directory must not be null.");
+        Preconditions.checkArgument(directory.exists(), "directory must be directory.");
+        Preconditions.checkArgument(directory.isDirectory(), "directory must be directory.");
+        this.storageDirectory = directory;
+        this.transformationFunction = SchemaSourceProviders.schemaSourceTransformationFrom(transformationFunction);
     }
 
     @Override
-    protected synchronized Optional<InputStream> cacheSchemaSource(String moduleName, Optional<String> revision, Optional<I> source) {
-        File schemaFile = toFile(moduleName, revision);
+    protected synchronized Optional<InputStream> cacheSchemaSource(final SourceIdentifier identifier,
+            final Optional<I> source) {
+        File schemaFile = toFile(identifier);
         try {
-            if(source.isPresent() && schemaFile.createNewFile()) {
-                try (
-                        FileOutputStream outStream = new FileOutputStream(schemaFile);
-                        OutputStreamWriter writer = new OutputStreamWriter(outStream);
-                ) {
-                writer.write(transformToString(source.get()));
-                writer.flush();
+            if (source.isPresent() && schemaFile.createNewFile()) {
+                try (FileOutputStream outStream = new FileOutputStream(schemaFile);
+                        OutputStreamWriter writer = new OutputStreamWriter(outStream);) {
+                    writer.write(transformToString(source.get()));
+                    writer.flush();
                 } catch (IOException e) {
-                    
+                    LOG.warn("Could not chache source for {}. Source: ",identifier,source.get(),e);
                 }
             }
-        } catch (IOException e){
-            
+        } catch (IOException e) {
+            LOG.warn("Could not create cache file for {}. File: ",identifier,schemaFile,e);
         }
         return transformToStream(source);
     }
 
-    @SuppressWarnings("deprecation")
-    private Optional<InputStream> transformToStream(Optional<I> source) {
-        if(source.isPresent()) {
-            return Optional.<InputStream>of(new StringBufferInputStream(transformToString(source.get())));
+    private Optional<InputStream> transformToStream(final Optional<I> source) {
+        if (source.isPresent()) {
+            return Optional.<InputStream> of(new ByteArrayInputStream(transformToString(source.get()).getBytes(
+                    Charsets.UTF_8)));
         }
         return Optional.absent();
     }
 
-    private String transformToString(I input) {
-        return transformationFunction.apply(input);
+    private String transformToString(final I input) {
+        return transformationFunction.transform(input);
     }
 
     @Override
-    protected Optional<InputStream> getCachedSchemaSource(String moduleName, Optional<String> revision) {
-        File inputFile = toFile(moduleName, revision);
+    protected Optional<InputStream> getCachedSchemaSource(final SourceIdentifier identifier) {
+        File inputFile = toFile(identifier);
         try {
             if (inputFile.exists() && inputFile.canRead()) {
                 InputStream stream = new FileInputStream(inputFile);
@@ -72,32 +148,24 @@ public class FilesystemSchemaCachingProvider<I> extends AbstractCachingSchemaSou
         return Optional.absent();
     }
 
-    private File toFile(String moduleName, Optional<String> revision) {
-        return new File(storageDirectory, toYangFileName(moduleName, revision));
+    private File toFile(final SourceIdentifier identifier) {
+        return sourceIdToFile(identifier, storageDirectory);
     }
 
-    public static final String toYangFileName(String moduleName, Optional<String> revision) {
-        StringBuilder filename = new StringBuilder(moduleName);
-        if (revision.isPresent()) {
-            filename.append("@");
-            filename.append(revision.get());
-        }
-        filename.append(".yang");
-        return filename.toString();
+    public static File sourceIdToFile(final SourceIdentifier identifier, final File storageDirectory) {
+        org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier newIdentifier =
+                org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier.create(identifier.getName(), Optional.fromNullable(identifier.getRevision()));
+        return FilesystemSchemaSourceCache.sourceIdToFile(newIdentifier, storageDirectory);
     }
 
-    private static final Function<String, String> NOOP_TRANSFORMATION = new Function<String, String>() {
-        @Override
-        public String apply(String input) {
-            return input;
-        }
-    };
-
     public static FilesystemSchemaCachingProvider<String> createFromStringSourceProvider(
-            SchemaSourceProvider<String> liveProvider, File directory) {
+            final SchemaSourceProvider<String> liveProvider, final File directory) {
         Preconditions.checkNotNull(liveProvider);
         Preconditions.checkNotNull(directory);
         directory.mkdirs();
-        return new FilesystemSchemaCachingProvider<String>(liveProvider, directory,NOOP_TRANSFORMATION);
+        return new FilesystemSchemaCachingProvider<String>(
+                SchemaSourceProviders.toAdvancedSchemaSourceProvider(liveProvider),//
+                directory, //
+                SchemaSourceProviders.<String>identityTransformation());
     }
 }