7013a9c5d35722b7a5a00868c6abbe1fe682c2e9
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / main / java / org / onap / ccsdk / sli / core / slipluginutils / Dme2Factory.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP : CCSDK\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights\r
6  *                                              reserved.\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 \r
22 package org.onap.ccsdk.sli.core.slipluginutils;\r
23 \r
24 import java.io.File;\r
25 import java.io.FileNotFoundException;\r
26 import java.io.FileReader;\r
27 import java.io.IOException;\r
28 import java.util.Optional;\r
29 import java.util.Properties;\r
30 import java.util.Vector;\r
31 import org.onap.ccsdk.sli.core.utils.JREFileResolver;\r
32 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;\r
33 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;\r
34 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;\r
35 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;\r
36 import org.slf4j.Logger;\r
37 import org.slf4j.LoggerFactory;\r
38 \r
39 /**\r
40  * Immutable properties container for dme2 properties. Since the initial design decision was made to\r
41  * utilize <code>Properties</code> instead of an OSGi <code>ManagedService</code>, it was decided to\r
42  * make these properties immutable.\r
43  */\r
44 public final class Dme2Factory {\r
45 \r
46     private static final Logger LOG = LoggerFactory.getLogger(Dme2Factory.class);\r
47     private static final String DME2_PROPERTIES_FILE_NAME = "dme2.properties";\r
48 \r
49     static Properties properties;\r
50     private Vector<PropertiesFileResolver> dme2PropertiesFileResolvers = new Vector<>();\r
51 \r
52     public Dme2Factory() {\r
53         dme2PropertiesFileResolvers\r
54                 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));\r
55         dme2PropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));\r
56         dme2PropertiesFileResolvers\r
57                 .add(new JREFileResolver("Using property file (3) from JRE argument", Dme2Factory.class));\r
58         dme2PropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));\r
59         File dme2File = getDme2File(DME2_PROPERTIES_FILE_NAME);\r
60         init(dme2File);\r
61     }\r
62 \r
63     private void init(final File dme2propertiesFile) {\r
64         try {\r
65             properties = getProperties(dme2propertiesFile);\r
66         } catch (final FileNotFoundException e) {\r
67             LOG.error("dme2.properties file could not be found at path: {}", dme2propertiesFile, e);\r
68         } catch (final IOException e) {\r
69             LOG.error("fatal error reading dme2.properties at path: {}", dme2propertiesFile, e);\r
70         }\r
71     }\r
72 \r
73     /**\r
74      * Reports the method chosen for properties resolution to the <code>Logger</code>.\r
75      *\r
76      * @param message Some user friendly message\r
77      * @param fileOptional The file location of the chosen properties file\r
78      * @return the file location of the chosen properties file\r
79      */\r
80     private static File reportSuccess(final String message, final Optional<File> fileOptional) {\r
81         if (fileOptional.isPresent()) {\r
82             final File file = fileOptional.get();\r
83             LOG.info("{} {}", message, file.getPath());\r
84             return file;\r
85         }\r
86         return null;\r
87     }\r
88 \r
89     private File getDme2File(final String dme2Filename) {\r
90         for (final PropertiesFileResolver dblibPropertiesFileResolver : dme2PropertiesFileResolvers) {\r
91             final Optional<File> fileOptional = dblibPropertiesFileResolver.resolveFile(dme2Filename);\r
92             if (fileOptional.isPresent()) {\r
93                 return reportSuccess(dblibPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);\r
94             }\r
95         }\r
96         return (new File(dme2Filename));\r
97     }\r
98 \r
99     private static Properties getProperties(final File dme2File) throws IOException {\r
100         final Properties properties = new Properties();\r
101         properties.load(new FileReader(dme2File));\r
102         return properties;\r
103     }\r
104        \r
105     public DME2 createDme2() {\r
106         return new DME2(properties);\r
107     }\r
108 \r
109 }\r