d33fdd8555ba605b01fbfb47146a21e3066e6d46
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : SLI
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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  *
19  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.sli.adaptors.ansible.impl;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.Properties;
33 import org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdaptorPropertiesProvider;
34 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
35 import org.onap.ccsdk.sli.core.utils.JREFileResolver;
36 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
37 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
38 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
39 import org.onap.ccsdk.sli.core.utils.common.EnvProperties;
40 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Responsible for determining the properties file to use and instantiating the
46  * <code>SqlResource</code> Service. The priority for properties file
47  * resolution is as follows:
48  *
49  * <ol>
50  * <li>A directory identified by the system environment variable
51  * <code>SDNC_CONFIG_DIR</code></li>
52  * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
53  * <li>A directory identified by the JRE argument
54  * <code>sql-resource.properties</code></li>
55  * <li>A <code>sql-resource.properties</code> file located in the karaf root
56  * directory</li>
57  * </ol>
58  */
59 public class AnsibleAdaptorPropertiesProviderImpl implements AnsibleAdaptorPropertiesProvider {
60
61     private static final Logger LOG = LoggerFactory.getLogger(AnsibleAdaptorPropertiesProviderImpl.class);
62
63     /**
64      * The name of the properties file for database configuration
65      */
66     private static final String ANSIBLE_Adaptor_PROPERTIES = "ansible-adaptor.properties";
67
68     /**
69      * A prioritized list of strategies for resolving sql-resource properties files.
70      */
71     private final List<PropertiesFileResolver> ansibleAdaptorPropertiesFileResolvers = new ArrayList<>();
72
73     /**
74      * The configuration properties for the db connection.
75      */
76     private Properties properties;
77
78     /**
79      * Set up the prioritized list of strategies for resolving dblib properties
80      * files.
81      */
82     public AnsibleAdaptorPropertiesProviderImpl() {
83         ansibleAdaptorPropertiesFileResolvers
84                 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
85         ansibleAdaptorPropertiesFileResolvers
86                 .add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
87         ansibleAdaptorPropertiesFileResolvers
88                 .add(new JREFileResolver("Using property file (3) from JRE argument", AnsibleAdaptorPropertiesProviderImpl.class));
89         ansibleAdaptorPropertiesFileResolvers
90                 .add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
91
92         // determines properties file as according to the priority described in the
93         // class header comment
94         final File propertiesFile = determinePropertiesFile();
95         if (propertiesFile != null) {
96             try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
97                 properties = new EnvProperties();
98                 properties.load(fileInputStream);
99             } catch (final IOException e) {
100                 LOG.error("Failed to load properties for file: {}", propertiesFile,
101                         new ConfigurationException("Failed to load properties for file: " + propertiesFile, e));
102             }
103         } else {
104             // Try to read properties as resource
105             InputStream propStr = getClass().getResourceAsStream("/" + ANSIBLE_Adaptor_PROPERTIES);
106             if (propStr != null) {
107                 properties = new EnvProperties();
108                 try {
109                     properties.load(propStr);
110                     propStr.close();
111                 } catch (IOException e) {
112                     properties = null;
113                 }
114             }
115         }
116         if (properties == null) {
117             reportFailure(new ConfigurationException(
118                     "Missing configuration properties resource(3): " + ANSIBLE_Adaptor_PROPERTIES));
119             LOG.info("Defaulting org.onap.appc.adaptor.ansible.clientType to TRUST_ALL");
120             properties = new Properties();
121             properties.setProperty("org.onap.appc.adaptor.ansible.clientType", "TRUST_ALL");
122         }
123
124     }
125
126     /**
127      * Instantiates a new Ansible adaptor properties provider.
128      *
129      * @param configFilePath the config file path
130      */
131     public AnsibleAdaptorPropertiesProviderImpl(String configFilePath) {
132         properties = new EnvProperties();
133         try {
134             properties.load(new FileInputStream(configFilePath));
135         } catch (IOException e) {
136             properties = null;
137         }
138         if (properties == null) {
139             reportFailure(new ConfigurationException(
140                     "Missing configuration properties resource(3): " + ANSIBLE_Adaptor_PROPERTIES));
141             LOG.info("Defaulting org.onap.appc.adaptor.ansible.clientType to TRUST_ALL");
142             properties = new Properties();
143             properties.setProperty("org.onap.appc.adaptor.ansible.clientType", "TRUST_ALL");
144         }
145
146     }
147
148     /**
149      * Reports the method chosen for properties resolution to the
150      * <code>Logger</code>.
151      *
152      * @param message      Some user friendly message
153      * @param fileOptional The file location of the chosen properties file
154      *
155      * @return the file location of the chosen properties file
156      */
157     private static File reportSuccess(final String message, final Optional<File> fileOptional) {
158         if (fileOptional.isPresent()) {
159             final File file = fileOptional.get();
160             LOG.info("{} {}", message, file.getPath());
161             return file;
162         }
163         return null;
164     }
165
166     /**
167      * Reports fatal errors. This is the case in which no properties file could be
168      * found.
169      *
170      * @param configurationException An exception describing what went wrong during resolution
171      */
172     private static void reportFailure(final ConfigurationException configurationException) {
173         LOG.error("{}", "Missing configuration properties resource(3)", configurationException);
174     }
175
176     /**
177      * Extract svclogic config properties.
178      *
179      * @return the svclogic config properties
180      */
181     public Properties getProperties() {
182         return properties;
183     }
184
185     /**
186      * Determines the sql-resource properties file to use based on the following priority:
187      * <ol>
188      * <li>A directory identified by the system environment variable
189      * <code>SDNC_CONFIG_DIR</code></li>
190      * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
191      * <li>A directory identified by the JRE argument
192      * <code>sql-resource.properties</code></li>
193      * <li>A <code>sql-resource.properties</code> file located in the karaf root
194      * directory</li>
195      * </ol>
196      */
197     File determinePropertiesFile() {
198         for (final PropertiesFileResolver propertiesFileResolver : ansibleAdaptorPropertiesFileResolvers) {
199             final Optional<File> fileOptional = propertiesFileResolver.resolveFile(ANSIBLE_Adaptor_PROPERTIES);
200             if (fileOptional.isPresent()) {
201                 return reportSuccess(propertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);
202             }
203         }
204
205         return null;
206     }
207
208 }