Added support for parsing submodules & added dependency utility parser
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / repo / FilesystemSchemaCachingProvider.java
1 package org.opendaylight.yangtools.yang.model.util.repo;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStreamWriter;
10 import java.io.StringBufferInputStream;
11
12 import com.google.common.base.Function;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15
16 public class FilesystemSchemaCachingProvider<I> extends AbstractCachingSchemaSourceProvider<I, InputStream> {
17
18     private final File storageDirectory;
19     private final Function<I, String> transformationFunction;
20
21     public FilesystemSchemaCachingProvider(SchemaSourceProvider<I> delegate, File directory,
22             Function<I, String> transformationFunction) {
23         super(delegate);
24         this.storageDirectory = directory;
25         this.transformationFunction = transformationFunction;
26     }
27
28     @Override
29     protected synchronized Optional<InputStream> cacheSchemaSource(SourceIdentifier identifier, Optional<I> source) {
30         File schemaFile = toFile(identifier);
31         try {
32             if(source.isPresent() && schemaFile.createNewFile()) {
33                 try (
34                         FileOutputStream outStream = new FileOutputStream(schemaFile);
35                         OutputStreamWriter writer = new OutputStreamWriter(outStream);
36                 ) {
37                 writer.write(transformToString(source.get()));
38                 writer.flush();
39                 } catch (IOException e) {
40                     
41                 }
42             }
43         } catch (IOException e){
44             
45         }
46         return transformToStream(source);
47     }
48
49     @SuppressWarnings("deprecation")
50     private Optional<InputStream> transformToStream(Optional<I> source) {
51         if(source.isPresent()) {
52             return Optional.<InputStream>of(new StringBufferInputStream(transformToString(source.get())));
53         }
54         return Optional.absent();
55     }
56
57     private String transformToString(I input) {
58         return transformationFunction.apply(input);
59     }
60
61     @Override
62     protected Optional<InputStream> getCachedSchemaSource(SourceIdentifier identifier) {
63         File inputFile = toFile(identifier);
64         try {
65             if (inputFile.exists() && inputFile.canRead()) {
66                 InputStream stream = new FileInputStream(inputFile);
67                 return Optional.of(stream);
68             }
69         } catch (FileNotFoundException e) {
70             return Optional.absent();
71         }
72         return Optional.absent();
73     }
74
75     private File toFile(SourceIdentifier identifier) {
76         return new File(storageDirectory, identifier.toYangFilename());
77     }
78
79
80
81     private static final Function<String, String> NOOP_TRANSFORMATION = new Function<String, String>() {
82         @Override
83         public String apply(String input) {
84             return input;
85         }
86     };
87
88     public static FilesystemSchemaCachingProvider<String> createFromStringSourceProvider(
89             SchemaSourceProvider<String> liveProvider, File directory) {
90         Preconditions.checkNotNull(liveProvider);
91         Preconditions.checkNotNull(directory);
92         directory.mkdirs();
93         return new FilesystemSchemaCachingProvider<String>(liveProvider, directory,NOOP_TRANSFORMATION);
94     }
95 }