a63b455a169ff6a4a9e3ba802cfa7155df7dea7f
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-core / src / main / java / org / onap / config / NonConfigResource.java
1 package org.onap.config;
2
3 import io.vavr.Function1;
4 import io.vavr.collection.HashSet;
5 import io.vavr.collection.Set;
6 import io.vavr.control.Option;
7
8 import java.io.File;
9 import java.net.URISyntaxException;
10 import java.net.URL;
11 import java.nio.file.Path;
12 import java.nio.file.Paths;
13
14 import static io.vavr.API.Option;
15
16 /**
17  * The type Non config resource.
18  */
19 public class NonConfigResource {
20
21     private Function1<String, String> propertyGetter;
22     private Set<URL> urls;
23     private Set<Path> files;
24
25     private NonConfigResource(Function1<String, String> propertyGetter) {
26         this.propertyGetter = propertyGetter;
27         this.files = HashSet.empty();
28         this.urls = HashSet.empty();
29     }
30
31     public static NonConfigResource create(Function1<String, String> propertyGetter) {
32         return new NonConfigResource(propertyGetter);
33     }
34
35     /**
36      * Add.
37      *
38      * @param url the url
39      */
40     public void add(URL url) {
41         urls = urls.add(url);
42     }
43
44     /**
45      * Add.
46      *
47      * @param file the file
48      */
49     public void add(File file) {
50         files = files.add(file.toPath());
51     }
52
53     /**
54      * Locate path.
55      *
56      * @param resource the resource
57      * @return the path
58      */
59     public Path locate(String resource) {
60         Path toReturn = null;
61         try {
62             if (resource != null) {
63                 toReturn = tryToLocateResource(resource).getOrNull();
64             }
65         } catch (Exception exception) {
66             exception.printStackTrace();
67         }
68         return toReturn;
69     }
70
71     private Option<Path> tryToLocateResource(String resource) throws URISyntaxException {
72         return new File(resource).exists() ? Option(Paths.get(resource)) : getPathForResourceAmongFiles(resource)
73                 .orElse(getPathForResourceBasedOnProperty(resource, "node.config.location"))
74                 .orElse(getPathForResourceBasedOnProperty(resource, "config.location"))
75                 .orElse(getPathForResourceAmongUrls(resource));
76     }
77
78     private Option<Path> getPathForResourceBasedOnProperty(String resource, String configPropertyKey) {
79         return Option(propertyGetter.apply(configPropertyKey))
80                 .flatMap(el -> locate(new File(el), resource));
81     }
82
83     private Option<Path> getPathForResourceAmongFiles(String resource) {
84         return files.map(Path::toAbsolutePath)
85                 .filter(path -> path.toFile().exists() && path.endsWith(resource))
86                 .headOption();
87     }
88
89     private Option<Path> getPathForResourceAmongUrls(String resource) throws URISyntaxException {
90         return urls.filter(url -> url.getFile().endsWith(resource))
91                 .headOption()
92                 .flatMap(url -> Option(Paths.get(url.getPath())));
93     }
94
95     private Option<Path> locate(File root, String resource) {
96         return root.exists() ? Option.ofOptional(ConfigurationUtils.getAllFiles(root, true, false)
97                 .stream()
98                 .filter(file -> !ConfigurationUtils.isConfig(file))
99                 .peek(this::add)
100                 .filter(file -> file.getAbsolutePath().endsWith(resource))
101                 .map(file -> Paths.get(file.getAbsolutePath()))
102                 .findAny()) : Option.none();
103     }
104 }