17c42ec83439b4ab231ec5bf1bcddd4b1f770399
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * onap
4  * ================================================================================
5  * Copyright (C) 2016 - 2017 ONAP
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.dblib.propertiesfileresolver;
22
23 import com.google.common.base.Strings;
24 import java.io.File;
25 import java.nio.file.Paths;
26 import java.util.Optional;
27
28 /**
29  * Resolves dblib properties files relative to the directory identified by the <code>SDNC_CONFIG_DIR</code>
30  * environment variable.
31  */
32 public class DblibEnvVarFileResolver implements DblibPropertiesFileResolver {
33
34     /**
35      * Key for environment variable representing the configuration directory
36      */
37     private static final String SDNC_CONFIG_DIR_PROP_KEY = "SDNC_CONFIG_DIR";
38
39     private final String successMessage;
40
41     public DblibEnvVarFileResolver(final String successMessage) {
42         this.successMessage = successMessage;
43     }
44
45     /**
46      * Parse a properties file location based on System environment variable
47      *
48      * @return an Optional File containing the location if it exists, or an empty Optional
49      */
50     @Override
51     public Optional<File> resolveFile(final String dblibFileName) {
52         // attempt to resolve the property directory from the corresponding environment variable
53         final String propDirectoryFromEnvVariable = System.getenv(SDNC_CONFIG_DIR_PROP_KEY);
54         final File fileFromEnvVariable;
55         if (!Strings.isNullOrEmpty(propDirectoryFromEnvVariable)) {
56             fileFromEnvVariable = Paths.get(propDirectoryFromEnvVariable).resolve(dblibFileName).toFile();
57             if(fileFromEnvVariable.exists()) {
58                 return Optional.of(fileFromEnvVariable);
59             }
60         }
61         return Optional.empty();
62     }
63
64     @Override
65     public String getSuccessfulResolutionMessage() {
66         return this.successMessage;
67     }
68 }